File size: 13,632 Bytes
4f5578c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bias-Variance Tradeoff Visualization</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.5em;
        }
        
        .subtitle {
            text-align: center;
            color: #7f8c8d;
            margin-bottom: 40px;
            font-size: 1.1em;
        }
        
        .scenarios-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 30px;
            margin-bottom: 40px;
        }
        
        .scenario {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            border: 3px solid transparent;
        }
        
        .scenario:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
        }
        
        .scenario.high-bias-high-variance {
            border-color: #e74c3c;
        }
        
        .scenario.high-bias-low-variance {
            border-color: #f39c12;
        }
        
        .scenario.low-bias-high-variance {
            border-color: #3498db;
        }
        
        .scenario.low-bias-low-variance {
            border-color: #27ae60;
        }
        
        .scenario h3 {
            text-align: center;
            margin-bottom: 20px;
            font-size: 1.2em;
        }
        
        .dartboard {
            width: 200px;
            height: 200px;
            margin: 0 auto 20px;
            position: relative;
            background: #f8f9fa;
            border-radius: 50%;
            border: 3px solid #34495e;
        }
        
        .target {
            width: 20px;
            height: 20px;
            background: #e74c3c;
            border-radius: 50%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 10;
        }
        
        .target::after {
            content: '×';
            color: white;
            font-size: 14px;
            font-weight: bold;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        
        .shot {
            width: 8px;
            height: 8px;
            background: #2c3e50;
            border-radius: 50%;
            position: absolute;
            transform: translate(-50%, -50%);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .rings {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        
        .ring {
            border: 1px solid rgba(52, 73, 94, 0.2);
            border-radius: 50%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        
        .description {
            text-align: center;
            color: #2c3e50;
            line-height: 1.6;
        }
        
        .controls {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .btn {
            background: linear-gradient(45deg, #3498db, #2980b9);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 16px;
            margin: 0 10px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
        }
        
        .legend {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 20px;
            margin-top: 20px;
        }
        
        .legend h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }
        
        .legend-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        
        .legend-color {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Bias-Variance Tradeoff</h1>
        <p class="subtitle">Understanding the four fundamental scenarios in machine learning</p>
        
        <div class="controls">
            <button class="btn" onclick="generateAllShots()">🎯 Generate New Shots</button>
            <button class="btn" onclick="animateShots()">🎬 Animate Shots</button>
        </div>
        
        <div class="scenarios-grid">
            <div class="scenario high-bias-high-variance">
                <h3>High Bias, High Variance</h3>
                <div class="dartboard" id="dartboard1">
                    <div class="rings">
                        <div class="ring" style="width: 60px; height: 60px;"></div>
                        <div class="ring" style="width: 120px; height: 120px;"></div>
                        <div class="ring" style="width: 180px; height: 180px;"></div>
                    </div>
                    <div class="target"></div>
                </div>
                <div class="description">
                    <strong>Worst Case:</strong> Shots are both far from target (biased) and scattered everywhere (high variance). The model is both systematically wrong and inconsistent.
                </div>
            </div>
            
            <div class="scenario high-bias-low-variance">
                <h3>High Bias, Low Variance</h3>
                <div class="dartboard" id="dartboard2">
                    <div class="rings">
                        <div class="ring" style="width: 60px; height: 60px;"></div>
                        <div class="ring" style="width: 120px; height: 120px;"></div>
                        <div class="ring" style="width: 180px; height: 180px;"></div>
                    </div>
                    <div class="target"></div>
                </div>
                <div class="description">
                    <strong>Underfitting:</strong> Shots are consistently off-target but grouped together. The model is predictable but systematically wrong.
                </div>
            </div>
            
            <div class="scenario low-bias-high-variance">
                <h3>Low Bias, High Variance</h3>
                <div class="dartboard" id="dartboard3">
                    <div class="rings">
                        <div class="ring" style="width: 60px; height: 60px;"></div>
                        <div class="ring" style="width: 120px; height: 120px;"></div>
                        <div class="ring" style="width: 180px; height: 180px;"></div>
                    </div>
                    <div class="target"></div>
                </div>
                <div class="description">
                    <strong>Overfitting:</strong> Shots are centered around the target but widely scattered. The model can be accurate on average but inconsistent.
                </div>
            </div>
            
            <div class="scenario low-bias-low-variance">
                <h3>Low Bias, Low Variance</h3>
                <div class="dartboard" id="dartboard4">
                    <div class="rings">
                        <div class="ring" style="width: 60px; height: 60px;"></div>
                        <div class="ring" style="width: 120px; height: 120px;"></div>
                        <div class="ring" style="width: 180px; height: 180px;"></div>
                    </div>
                    <div class="target"></div>
                </div>
                <div class="description">
                    <strong>Ideal Case:</strong> Shots are tightly grouped around the target. The model is both accurate and consistent - perfect generalization!
                </div>
            </div>
        </div>
        
        <div class="legend">
            <h3>Key Concepts:</h3>
            <div class="legend-item">
                <div class="legend-color" style="background: #e74c3c;"></div>
                <span><strong>Target (×):</strong> True value we want to predict</span>
            </div>
            <div class="legend-item">
                <div class="legend-color" style="background: #2c3e50;"></div>
                <span><strong>Shots (●):</strong> Model predictions from different training sets</span>
            </div>
            <div style="margin-top: 15px;">
                <p><strong>Bias:</strong> How far the average prediction is from the target</p>
                <p><strong>Variance:</strong> How much predictions vary between different training sets</p>
                <p><strong>Goal:</strong> Minimize total error = Bias² + Variance + Noise</p>
            </div>
        </div>
    </div>

    <script>
        function generateShots(dartboardId, biasOffset, varianceLevel) {
            const dartboard = document.getElementById(dartboardId);
            const centerX = 100;
            const centerY = 100;
            
            // Clear existing shots
            const existingShots = dartboard.querySelectorAll('.shot');
            existingShots.forEach(shot => shot.remove());
            
            // Generate 8 shots
            for (let i = 0; i < 8; i++) {
                const shot = document.createElement('div');
                shot.className = 'shot';
                
                let x, y;
                
                if (varianceLevel === 'high') {
                    // High variance: shots spread widely
                    const angle = Math.random() * 2 * Math.PI;
                    const distance = Math.random() * 70 + 10;
                    x = centerX + biasOffset.x + Math.cos(angle) * distance;
                    y = centerY + biasOffset.y + Math.sin(angle) * distance;
                } else {
                    // Low variance: shots grouped tightly
                    const angle = Math.random() * 2 * Math.PI;
                    const distance = Math.random() * 15 + 5;
                    x = centerX + biasOffset.x + Math.cos(angle) * distance;
                    y = centerY + biasOffset.y + Math.sin(angle) * distance;
                }
                
                // Keep shots within dartboard bounds
                x = Math.max(10, Math.min(190, x));
                y = Math.max(10, Math.min(190, y));
                
                shot.style.left = x + 'px';
                shot.style.top = y + 'px';
                
                dartboard.appendChild(shot);
            }
        }
        
        function generateAllShots() {
            // High bias, high variance - shots far from center and scattered
            generateShots('dartboard1', {x: 40, y: -30}, 'high');
            
            // High bias, low variance - shots far from center but grouped
            generateShots('dartboard2', {x: -45, y: 35}, 'low');
            
            // Low bias, high variance - shots around center but scattered
            generateShots('dartboard3', {x: 0, y: 0}, 'high');
            
            // Low bias, low variance - shots around center and grouped (ideal)
            generateShots('dartboard4', {x: 0, y: 0}, 'low');
        }
        
        function animateShots() {
            const shots = document.querySelectorAll('.shot');
            shots.forEach((shot, index) => {
                shot.style.opacity = '0';
                shot.style.transform = 'translate(-50%, -50%) scale(0)';
                
                setTimeout(() => {
                    shot.style.transition = 'all 0.5s ease';
                    shot.style.opacity = '1';
                    shot.style.transform = 'translate(-50%, -50%) scale(1)';
                }, index * 100);
            });
        }
        
        // Initialize with shots
        generateAllShots();
        
        // Add some interactive hover effects
        document.querySelectorAll('.scenario').forEach(scenario => {
            scenario.addEventListener('mouseenter', function() {
                const shots = this.querySelectorAll('.shot');
                shots.forEach(shot => {
                    shot.style.transform = 'translate(-50%, -50%) scale(1.2)';
                    shot.style.transition = 'transform 0.2s ease';
                });
            });
            
            scenario.addEventListener('mouseleave', function() {
                const shots = this.querySelectorAll('.shot');
                shots.forEach(shot => {
                    shot.style.transform = 'translate(-50%, -50%) scale(1)';
                });
            });
        });
    </script>
</body>
</html>