-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
220 lines (193 loc) · 6.88 KB
/
Copy pathapp.js
File metadata and controls
220 lines (193 loc) · 6.88 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
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
// Correlation Game app.js
// - Generates a bivariate dataset
// - Plots as scatter using Chart.js
// - Computes Pearson, Spearman, Kendall
// - Checks user's guesses against actual values within tolerance
(() => {
// Helpers
function randn() {
// Box-Muller transform
let u = 0, v = 0;
while (u === 0) u = Math.random();
while (v === 0) v = Math.random();
return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
}
function generateBivariateNormal(n, rho) {
const xs = [];
const ys = [];
const s = Math.sqrt(1 - rho * rho);
for (let i = 0; i < n; i++) {
const x = randn();
const e = randn();
const y = rho * x + s * e;
xs.push(x);
ys.push(y);
}
return { xs, ys };
}
function mean(a) {
return a.reduce((s, v) => s + v, 0) / a.length;
}
function sd(a) {
const m = mean(a);
const v = a.reduce((s, x) => s + (x - m) ** 2, 0) / (a.length - 1);
return Math.sqrt(v);
}
function pearsonr(x, y) {
const n = x.length;
const mx = mean(x), my = mean(y);
let cov = 0;
for (let i = 0; i < n; i++) cov += (x[i] - mx) * (y[i] - my);
cov /= (n - 1);
return cov / (sd(x) * sd(y));
}
function rankArray(arr) {
// Return ranks (average ranks for ties)
const pairs = arr.map((v, i) => ({ v, i }));
pairs.sort((a, b) => a.v - b.v);
const ranks = new Array(arr.length);
let i = 0;
while (i < pairs.length) {
let j = i;
while (j + 1 < pairs.length && pairs[j + 1].v === pairs[i].v) j++;
const avgRank = (i + j + 2) / 2; // ranks are 1-based
for (let k = i; k <= j; k++) ranks[pairs[k].i] = avgRank;
i = j + 1;
}
return ranks;
}
function spearmanr(x, y) {
const rx = rankArray(x);
const ry = rankArray(y);
return pearsonr(rx, ry);
}
function kendallTau(x, y) {
const n = x.length;
let concordant = 0;
let discordant = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
const dx = x[i] - x[j];
const dy = y[i] - y[j];
const prod = dx * dy;
if (prod > 0) concordant++;
else if (prod < 0) discordant++;
// ties (prod == 0) are ignored in numerator in this simple implementation
}
}
const denom = (n * (n - 1)) / 2;
return (concordant - discordant) / denom;
}
// Format helpers
function fmt(v) {
if (!isFinite(v)) return "NaN";
return (Math.round(v * 1000) / 1000).toFixed(3);
}
// DOM elements
const sampleSizeInput = document.getElementById("sampleSize");
const targetRInput = document.getElementById("targetR");
const toleranceInput = document.getElementById("tolerance");
const newPlotBtn = document.getElementById("newPlotBtn");
const pearsonInput = document.getElementById("pearsonInput");
const spearmanInput = document.getElementById("spearmanInput");
const kendallInput = document.getElementById("kendallInput");
const guessForm = document.getElementById("guessForm");
const resultDiv = document.getElementById("result");
let chart = null;
let current = {
xs: [],
ys: [],
pearson: 0,
spearman: 0,
kendall: 0
};
function plotScatter(xs, ys) {
const ctx = document.getElementById("scatterChart").getContext("2d");
const points = xs.map((x, i) => ({ x, y: ys[i] }));
if (chart) {
chart.data.datasets[0].data = points;
chart.update();
return;
}
chart = new Chart(ctx, {
type: "scatter",
data: {
datasets: [{
label: "Data points",
data: points,
backgroundColor: "rgba(125,211,252,0.9)"
}]
},
options: {
responsive: true,
plugins: {
legend: { display: false }
},
scales: {
x: { title: { display: true, text: "x" } },
y: { title: { display: true, text: "y" } }
}
}
});
}
function generateAndPlot() {
const n = Math.max(10, Math.min(1000, parseInt(sampleSizeInput.value || "50", 10)));
// Choose a true Pearson target plus a small random jitter so players can't memorize
const target = Math.max(-0.95, Math.min(0.95, parseFloat(targetRInput.value || "0.5")));
const jitter = (Math.random() - 0.5) * 0.18; // +/- 0.09
const rho = Math.max(-0.98, Math.min(0.98, target + jitter));
const { xs, ys } = generateBivariateNormal(n, rho);
current.xs = xs;
current.ys = ys;
current.pearson = pearsonr(xs, ys);
current.spearman = spearmanr(xs, ys);
current.kendall = kendallTau(xs, ys);
plotScatter(xs, ys);
// Clear inputs and result
pearsonInput.value = "";
spearmanInput.value = "";
kendallInput.value = "";
resultDiv.innerHTML = `<p>New plot generated (n=${n}). Enter your estimates.</p>`;
}
function checkGuesses(e) {
e.preventDefault();
const tol = Math.abs(parseFloat(toleranceInput.value) || 0.1);
const gPearson = parseFloat(pearsonInput.value);
const gSpearman = parseFloat(spearmanInput.value);
const gKendall = parseFloat(kendallInput.value);
if (isNaN(gPearson) || isNaN(gSpearman) || isNaN(gKendall)) {
resultDiv.innerHTML = `<p class="feedback-fail">Please fill all three guesses with numeric values.</p>`;
return;
}
const aPearson = current.pearson;
const aSpearman = current.spearman;
const aKendall = current.kendall;
const pDiff = Math.abs(gPearson - aPearson);
const sDiff = Math.abs(gSpearman - aSpearman);
const kDiff = Math.abs(gKendall - aKendall);
const pPass = pDiff <= tol;
const sPass = sDiff <= tol;
const kPass = kDiff <= tol;
const allPass = pPass && sPass && kPass;
resultDiv.innerHTML = `
<div>
<p>Actual values: Pearson r = <strong>${fmt(aPearson)}</strong>, Spearman ρ = <strong>${fmt(aSpearman)}</strong>, Kendall τ = <strong>${fmt(aKendall)}</strong>.</p>
<ul>
<li>Pearson guess: ${fmt(gPearson)} (diff ${fmt(pDiff)}) — ${pPass ? '<span class="feedback-pass">OK</span>' : '<span class="feedback-fail">Wrong</span>'}</li>
<li>Spearman guess: ${fmt(gSpearman)} (diff ${fmt(sDiff)}) — ${sPass ? '<span class="feedback-pass">OK</span>' : '<span class="feedback-fail">Wrong</span>'}</li>
<li>Kendall guess: ${fmt(gKendall)} (diff ${fmt(kDiff)}) — ${kPass ? '<span class="feedback-pass">OK</span>' : '<span class="feedback-fail">Wrong</span>'}</li>
</ul>
<p style="font-weight:700; color:${allPass ? '#7dd3fc' : '#ef4444'}">${allPass ? 'You win! 🎉 All estimates are within tolerance.' : 'Not yet — try another estimate or generate a new plot.'}</p>
</div>
`;
}
// Wire events
newPlotBtn.addEventListener("click", generateAndPlot);
guessForm.addEventListener("submit", checkGuesses);
// Init first plot
generateAndPlot();
// Expose functions in console for testing
window._correlationGame = {
pearsonr, spearmanr, kendallTau, generateBivariateNormal
};
})();