This repository has been archived by the owner on May 11, 2021. It is now read-only.
/
plotting_the_line_of_best_fit.html
524 lines (466 loc) · 22.9 KB
/
plotting_the_line_of_best_fit.html
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!DOCTYPE html>
<html data-require="math math-format graphie interactive">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Plotting the line of best fit</title>
<script src="../khan-exercise.js"></script>
<style type="text/css">
.graphie.float {
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="exercise">
<div class="problems">
<div id="linear">
<div class="vars">
<var id="SLOPE">randRange(-80, 80) / 20</var>
<var id="INTERCEPT">randRange(-5, 5)</var>
<var id="FUNC">function(x) { return SLOPE * x + INTERCEPT }</var>
<var id="TOP">((SLOPE < 0 ? -1 : 1) * 11 - INTERCEPT) / SLOPE</var>
<var id="BOTTOM">((SLOPE < 0 ? 1 : -1) * 11 - INTERCEPT) / SLOPE</var>
<var id="BOUND">function(x, low, high) {
return (x < low) ? low : (x > high) ? high : x;
}</var>
<var id="OFFSET">random() + 0.5</var>
<var id="LEFT">(BOTTOM < -11) ? -11 : BOTTOM</var>
<var id="RIGHT">(TOP > 11) ? 11 : TOP</var>
<var id="XS">sortNumbers((function() {
var range = (RIGHT - LEFT) / 20;
return _.map(shuffle(_.range(-8, 9), 9), function(x) {
return x * range;
});
})())</var>
<var id="UNIQARRAYS">function(a) {
// make sure the points are unique in a
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (_.isEqual(a[i], a[j])) {
a.splice(j, 1);
--j;
}
}
}
return a;
}</var>
<var id="RANDRANGE">function(low, high) {
return random() * (high - low) + low;
}</var>
<var id="ANG">atan2(SLOPE, 1) + PI / 2</var>
<var id="PERP">[cos(ANG), sin(ANG)]</var>
<var id="TOTALOFFSET">[]</var>
<var id="POINTS">UNIQARRAYS(_.map(XS, function(x, index) {
// calculate the sum of the offsets so far
var total = _.reduce(TOTALOFFSET, function(sum, num) {
return sum + num;
}, 0);
var offset;
if (index < 2 || index > 6) {
// make sure the first two and last
// two points are on the same side
offset = RANDRANGE(OFFSET/2, OFFSET);
} else if (total < 0) {
// if the offset is negative, choose less
// negative numbers
offset = RANDRANGE(-OFFSET - total, OFFSET);
} else {
// if it is positive, choose less
//positive numbers
offset = RANDRANGE(-OFFSET, OFFSET - total);
}
// store this offset
TOTALOFFSET.push(offset);
// calculate the rounded point created by this offset
return [BOUND(round(x + PERP[0] * offset), -9, 9),
BOUND(round(FUNC(x) + PERP[1] * offset), -9, 9)];
}))</var>
<var id="REALSLOPE,REALINTERCEPT">(function() {
// calculate the linear square regression
// line for our points
var xAve = _.reduce(POINTS, function(sum, pt) {
return sum + pt[0];
}, 0) / POINTS.length;
var yAve = _.reduce(POINTS, function(sum, pt) {
return sum + pt[1];
}, 0) / POINTS.length;
var xi2 = _.reduce(POINTS, function(sum, pt) {
return sum + pow(pt[0], 2);
}, 0);
var xiyi = _.reduce(POINTS, function(sum, pt) {
return sum + pt[0] * pt[1];
}, 0);
var realIntercept = (yAve * xi2 - xAve * xiyi) /
(xi2 - POINTS.length * pow(xAve, 2));
var realSlope = (xiyi - POINTS.length * xAve * yAve) /
(xi2 - POINTS.length * pow(xAve, 2));
return [realSlope, realIntercept];
})()</var>
<var id="GRAPH">{}</var>
</div>
<p class="question">Find the line of best fit,
or mark that there is no linear correlation.</p>
<div class="problem">
<div class="graphie">
graphInit({
range: 11,
scale: 20,
axisArrows: "<->",
tickStep: 1,
labelStep: 1,
gridOpacity: 0.05,
axisOpacity: 0.2,
tickOpacity: 0.4,
labelOpacity: 0.5
});
addMouseLayer();
// add the points
_.each(POINTS, function(pt) {
circle(pt, 0.2, { fill: "black" });
});
// add our movable line
graph.pointA = addMovablePoint({
coord: [-5, 5],
snapX: 0.5,
snapY: 0.5,
normalStyle: {
stroke: KhanUtil.BLUE,
fill: KhanUtil.BLUE
}
});
graph.pointB = addMovablePoint({
coord: [5, 5],
snapX: 0.5,
snapY: 0.5,
normalStyle: {
stroke: KhanUtil.BLUE,
fill: KhanUtil.BLUE
}
});
graph.line1 = addMovableLineSegment({
pointA: graph.pointA,
pointZ: graph.pointB,
fixed: true,
extendLine: true
});
// A and B can't be in the same place
graph.pointA.onMove = function(x, y) {
return (x != graph.pointB.coord[0] ||
y != graph.pointB.coord[1]);
};
graph.pointB.onMove = function(x, y) {
return (x != graph.pointA.coord[0] ||
y != graph.pointA.coord[1]);
};
graph.pointA.toFront();
graph.pointB.toFront();
var shown = false;
graph.showLine = function() {
graph.pointA.visibleShape.show();
graph.pointA.mouseTarget.show();
graph.pointB.visibleShape.show();
graph.pointB.mouseTarget.show();
graph.line1.visibleLine.show();
};
graph.hideLine = function() {
graph.pointA.visibleShape.hide();
graph.pointA.mouseTarget.hide();
graph.pointB.visibleShape.hide();
graph.pointB.mouseTarget.hide();
graph.line1.visibleLine.hide();
};
// show the true least square regression line
graph.showSolution = function() {
if (shown) {
return;
} else {
shown = true;
}
var roundToHalf = function(x) {
return round(x * 2) / 2;
};
var realFunc = function(x) {
return REALSLOPE * x + REALINTERCEPT;
};
$("html, body").animate({
scrollTop: $(".question").offset().top
}, {
duration: 500,
easing: "swing",
complete: function() {
line([-11, realFunc(-11)],
[11, realFunc(11)],
{ stroke: ORANGE, opacity: 0 })
.animate({ opacity: 1 }, 750);
}
});
};
GRAPH = graph;
</div>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
Move the line on the graph to show a best fit line,
if it exists.<br>
<ul>
<li>
<label>
<input type="radio" name="linear" id="exists"
onclick="KhanUtil.tmpl.getVAR('GRAPH').showLine()"
checked>
<span>A good best fit line exists</span>
</label>
</li>
<li>
<label>
<input type="radio" name="linear" id="notexists"
onclick="KhanUtil.tmpl.getVAR('GRAPH').hideLine()">
<span>No good best fit line exists</span>
</label>
</li>
</ul>
</div>
<div class="guess">[
GRAPH.pointA.coord, GRAPH.pointB.coord,
$("input[@name='linear']:checked").attr("id")
]</div>
<div class="validator-function">
if (_.isEqual(guess, [[-5, 5], [5, 5], "exists"])) {
return "";
}
var low = _.first(POINTS), high = _.last(POINTS);
var slopeadd = 1 / REALSLOPE + REALSLOPE;
lowx = (1 / REALSLOPE * low[0] + low[1] - REALINTERCEPT) /
slopeadd;
highx = (1 / REALSLOPE * high[0] + high[1] - REALINTERCEPT) /
slopeadd;
var lowfunc = function(x) {
return -1 / REALSLOPE * (x - low[0]) + low[1];
};
var highfunc = function(x) {
return -1 / REALSLOPE * (x - high[0]) + high[1];
};
var slope = (guess[1][1] - guess[0][1]) /
(guess[1][0] - guess[0][0]);
var intercept = slope * -guess[0][0] + guess[0][1];
lowIntersectx = (1 / REALSLOPE * low[0] +
low[1] - intercept) /
(slope + 1 / REALSLOPE);
highIntersectx = (1 / REALSLOPE * high[0] +
high[1] - intercept) /
(slope + 1 / REALSLOPE);
// the differences between the least squares line and the
// given line, at the highest and lowest points
var lowDiff = sqrt(pow(lowfunc(lowx) -
lowfunc(lowIntersectx), 2) +
pow(lowx - lowIntersectx, 2));
var highDiff = sqrt(pow(highfunc(highx) -
highfunc(highIntersectx), 2) +
pow(highx - highIntersectx, 2));
// whether or not each of the points are above or below
// the given line
var updown = _.map(POINTS, function(pt) {
var x = pt[0], y = pt[1],
est = slope * x + intercept;
return y >= est ? 1 : -1;
});
// sort and reverse-sort updown
var updownSorted = sortNumbers(updown);
var updownReversed = updownSorted.slice(0).reverse();
var normLength = pow((guess[1][0] - guess[0][0]), 2) +
pow((guess[1][1] - guess[0][1]), 2);
// calculate the distance from a point to the line
var distTo = function(pt) {
return abs((pt[0] - guess[0][0]) * (guess[1][1] - guess[0][1]) -
(pt[1] - guess[0][1]) * (guess[1][0] - guess[0][0])) / normLength;
};
// ensure:
// all the points are not up, ..., up, down, ..., down
return !_.isEqual(updown, updownSorted) &&
// all the points are not down, ..., down, up, ..., up
!_.isEqual(updown, updownReversed) &&
// one point is above/below
_.include(updown, 1) &&
_.include(updown, -1) &&
// the differences are between some proportion
// of the offset
lowDiff < 1.3 * OFFSET &&
highDiff < 1.3 * OFFSET &&
// they left 'a line exists' checked
guess[2] === "exists";
</div>
<div class="show-guess">
GRAPH.pointA.setCoord(guess[0]);
GRAPH.pointB.setCoord(guess[1]);
GRAPH.line1.transform(true);
</div>
<div class="show-guess-solutionarea">
$("#"+guess[2]).attr('checked', 'checked');
</div>
</div>
<div class="hints">
<p>The line of best fit is the line that best approximates
the data points.</p>
<div>
<p>There are three main criteria to use when finding a good
best fit line.</p>
<p>First, make sure that your line passes through the points,
and does not lie completely above or below the points.</p>
<div class="clearfix">
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-5, -4], [5, -3], { stroke: BLUE });
label([0, 4], "Bad", "center", false)
.css("color", "red")
.css("font-size", "20px");
</div>
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-5, -2.5], [5, -1.5], { stroke: BLUE });
label([0, 4], "Okay", "center", false)
.css("color", "orange")
.css("font-size", "20px");
</div>
</div>
</div>
<div>
<p>Next, make sure that your line alternates between
passing above and then below points, and doesn't simply
go above some points and then below the rest.</p>
<div class="clearfix">
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-5, -2.5], [5, -1.5], { stroke: BLUE });
label([0, 4], "Okay", "center", false)
.css("color", "orange")
.css("font-size", "20px");
</div>
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-5, -3.5], [3, 5], { stroke: BLUE });
label([0, 4], "Better", "center", false)
.css("color", "#8EEB00")
.css("font-size", "20px");
</div>
</div>
</div>
<div>
<p>Last, make sure that the line goes through the middle
of all the points, so that it is close to all of
the points.</p>
<div class="clearfix">
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-5, -3.5], [3, 5], { stroke: BLUE });
label([0, 4], "Better", "center", false)
.css("color", "#9FEE00")
.css("font-size", "20px");
</div>
<div class="graphie float">
init({ range: [[-5, 5], [-5, 5]], scale: [20, 20] });
circle([-2, -3], 0.2, { fill: "black" });
circle([ 0, -1], 0.2, { fill: "black" });
circle([-1, 1], 0.2, { fill: "black" });
circle([ 1, 1], 0.2, { fill: "black" });
circle([ 0, 2], 0.2, { fill: "black" });
line([-4, -5], [3, 5], { stroke: BLUE });
label([0, 4], "Good", "center", false)
.css("color", "#00C322")
.css("font-size", "20px");
</div>
</div>
</div>
<p>
There are several lines that satisfy this.
<input type="submit" value="Click here"
onclick="javascript:KhanUtil.tmpl
.getVAR('GRAPH').showSolution()"/>
to show one of them.
</p>
</div>
</div>
<div id="nonlinear" data-type="linear">
<div class="vars">
<var id="SLOPE">0</var>
<var id="INTERCEPT">0</var>
<var id="OFFSET">11</var>
<var id="GRAPH">{}</var>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
Move the line on the graph to show a best fit line,
if it exists.<br>
<ul>
<li>
<label>
<input type="radio" name="nonlinear" id="exists"
onclick="KhanUtil.tmpl.getVAR('GRAPH').showLine()"
checked>
<span>A good best fit line exists</span>
</label>
</li>
<li>
<label>
<input type="radio" name="nonlinear" id="notexists"
onclick="KhanUtil.tmpl.getVAR('GRAPH').hideLine()">
<span>No good best fit line exists</span>
</label>
</li>
</ul>
</div>
<div class="guess">[
GRAPH.pointA.coord, GRAPH.pointB.coord,
$("input[@name='linear']:checked").attr("id")
]</div>
<div class="validator-function">
if (_.isEqual(guess, [[-5, 5], [5, 5], "exists"])) {
return "";
}
return guess[2] === "notexists";
</div>
<div class="show-guess">
GRAPH.pointA.setCoord(guess[0]);
GRAPH.pointB.setCoord(guess[1]);
GRAPH.line1.transform(true);
</div>
<div class="show-guess-solutionarea">
$("#"+guess[2]).attr('checked', 'checked');
</div>
</div>
<div class="hints">
<p>Look at the data, and try to figure out if there is
a linear relationship.</p>
<p>Because these data are mostly random, there is no
correlation, so no good best fit line exists.</p>
</div>
</div>
</div>
</div>
</body>
</html>