-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (151 loc) · 4.25 KB
/
Copy pathscript.js
File metadata and controls
181 lines (151 loc) · 4.25 KB
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
const graph = document.getElementById("graph");
const reveal = document.getElementById("reveal");
const pointsInput = document.getElementById("points-input");
const guessInput = document.getElementById("guess-input");
window.onload = function () {
pointsInput.value = "10";
numPoints = 10;
generateRandomPoints();
};
pointsInput.addEventListener("input", (event) => {
numPoints = pointsInput.value == "" ? 25 : pointsInput.value;
});
guessInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
if (!isNewData) {
generateRandomPoints();
} else {
revealData();
}
}
});
// Global start set constants
var numPoints = pointsInput.value;
var coorelation = 1;
var isNewData = true;
var numGuesses = 0;
var avgDev = 0;
// Starting plot points
var plotPoints = {
x: [3, 1, 2, 3, 4, 5, 6],
y: [1, 9, 4, 7, 5, 2, 4],
mode: "markers",
};
var data = [plotPoints];
var isMobile =
window.innerHeight < 600 && window.innerWidth < 500 ? true : false;
var layout = {
autosize: false,
width: isMobile ? window.innerWidth * 0.85 : 600,
height: isMobile ? window.innerHeight * 0.5 : 400,
showlegend: false,
autorange: true,
margin: {
l: 20,
r: 0,
b: 20,
t: 0,
},
xaxis: {
visible: true,
showline: true,
zeroline: false,
},
yaxis: {
visible: true,
showline: true,
zeroline: false,
},
};
const plot = Plotly.newPlot(graph, data, layout, { staticPlot: true });
// On button click, reveal real value
function revealData() {
if (isNewData) {
reveal.innerText = calculateR().toFixed(3);
var guess = guessInput.value;
updateAverageDev();
if (Math.abs(guess - calculateR().toFixed(3)) < 0.1) {
reveal.style.color = "green";
} else {
reveal.style.color = "red";
}
} else {
reveal.style.color = "red";
reveal.innerText = "Click Generate for a new Set!";
}
isNewData = false;
}
function generateRandomPoints() {
if (numPoints <= 2000) {
isNewData = true;
plotPoints.x = [];
plotPoints.y = [];
var neg = Math.sign(Math.random() * 2 - 1);
// 5000 is arbitrary, but works best with a fewer number of points
dev = lerp(4, 7, numPoints / 2000);
for (i = 0; i < numPoints; i++) {
coors = boxMuller(lerp(0, 10, i / numPoints), dev);
// Push random noise generated value to x
plotPoints.x.push(Math.abs(coors[0]));
// Push adjusted value to y, add 10 if is negative to keep axis numbers positive
plotPoints.y.push(neg * lerp(0, 10, i / numPoints) + (neg < 0 ? 10 : 0));
}
reveal.style.color = "black";
reveal.innerText = "???";
updatePlot();
} else {
reveal.innerText =
"Doing this many points won't yield a good result " +
"and consume lots of RAM, I'd advise against it.";
}
}
function updatePlot() {
Plotly.update(graph, data, layout);
}
// linear interpolation between setpoints, a and b
function lerp(a, b, t) {
return a + t * (b - a);
}
// Generate normal distrubution based on the Box-Muller Transform
function boxMuller(mean, stddev) {
var u1, u2, R;
do {
u1 = Math.random() * 2 - 1;
u2 = Math.random() * 2 - 1;
R = u1 * u1 + u2 * u2;
// Want on the closed interval from (0,1)
} while (R > 1);
var p1 = Math.sqrt((-2 * Math.log(R)) / R) * u1;
var p2 = Math.sqrt((-2 * Math.log(R)) / R) * u2;
return [mean + stddev * p1, mean + stddev * p2];
}
// Calculates the R value of the graph
function calculateR() {
var sumX = 0,
sumY = 0,
sumXY = 0,
sumXsq = 0,
sumYsq = 0;
for (i = 0; i < numPoints; i++) {
y = plotPoints.y[i];
x = plotPoints.x[i];
sumX += x;
sumY += y;
sumXY += x * y;
sumXsq += x * x;
sumYsq += y * y;
}
return (
(numPoints * sumXY - sumX * sumY) /
Math.sqrt(
(numPoints * sumXsq - sumX * sumX) * (numPoints * sumYsq - sumY * sumY)
)
);
}
// Updates avg dev and progress bar
function updateAverageDev() {
avgDev = (avgDev * numGuesses + Math.abs(calculateR().toFixed(3) - guessInput.value)) / (numGuesses + 1);
numGuesses++;
document.getElementById("progress-bar").setAttribute("x", Math.max(Math.min(198 - lerp(0, 198, avgDev), 198), 0) + "px");
document.getElementById("average-dev").textContent = avgDev.toFixed(3) > 1 ? ">1.000" : avgDev.toFixed(3);
}