-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsequential_testing_app_book.html
More file actions
359 lines (317 loc) · 10.4 KB
/
Copy pathsequential_testing_app_book.html
File metadata and controls
359 lines (317 loc) · 10.4 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sequential Testing Simulation</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@600&family=Open+Sans:ital,wght@0,400;0,600;1,400&family=Fira+Code&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstat/1.9.6/jstat.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.26.0/plotly.min.js"></script>
<style>
:root {
--bg: #fffafa;
--bg2: #f7f7f7;
--ink: #1c1717;
--ink2: #3d3535;
--ink3: #666060;
--rule: #d6dadc;
--red: #a60a0a;
--red-l: #c41010;
--red-bg: #fff5f5;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
color: var(--ink);
font-family: 'Open Sans', sans-serif;
font-size: 15px;
line-height: 1.65;
padding: 20px 20px 0;
overflow: hidden;
}
.calc-label {
font-family: 'Open Sans', sans-serif;
font-size: 10px;
font-weight: 600;
letter-spacing: 0.18em;
text-transform: uppercase;
color: var(--ink3);
margin-bottom: 5px;
}
.calc-heading {
font-family: 'Montserrat', sans-serif;
font-weight: 600;
font-size: 1.1rem;
color: var(--ink);
margin-bottom: 16px;
padding-bottom: 9px;
border-bottom: 2px solid var(--red);
}
.input-panel {
background: var(--bg2);
border: 1px solid var(--rule);
border-left: 3px solid var(--red);
padding: 16px 18px;
margin-bottom: 20px;
}
.input-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 16px;
align-items: end;
}
.input-group { display: flex; flex-direction: column; gap: 5px; }
.input-label {
font-family: 'Open Sans', sans-serif;
font-size: 10px;
font-weight: 600;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--ink3);
}
.input-label .sym {
font-style: italic;
font-size: 0.88rem;
text-transform: none;
letter-spacing: 0;
font-weight: 400;
color: var(--red);
}
input[type="number"] {
font-family: 'Fira Code', monospace;
font-size: 13px;
background: var(--bg);
border: 1px solid var(--rule);
border-bottom: 2px solid var(--red);
padding: 6px 8px;
color: var(--ink);
outline: none;
width: 100%;
transition: border-color .2s;
}
input[type="number"]:focus { border-bottom-color: var(--red-l); }
input[type="range"] {
-webkit-appearance: none;
width: 100%;
height: 2px;
background: var(--rule);
outline: none;
cursor: pointer;
border-radius: 1px;
margin-top: 5px;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 14px;
height: 14px;
border-radius: 50%;
background: var(--bg);
border: 2px solid var(--red);
cursor: pointer;
transition: transform .15s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.25); }
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border-radius: 50%;
background: var(--bg);
border: 2px solid var(--red);
cursor: pointer;
}
.slider-value {
font-family: 'Fira Code', monospace;
font-size: 12px;
color: var(--red);
font-weight: 500;
}
.sim-btn {
font-family: 'Montserrat', sans-serif;
font-weight: 600;
font-size: 0.85rem;
letter-spacing: 0.06em;
background: var(--red);
color: #fff;
border: none;
padding: 10px 0;
cursor: pointer;
width: 100%;
transition: background .15s;
}
.sim-btn:hover { background: var(--red-l); }
.sim-btn:active { opacity: 0.82; }
.plot-wrap {
margin-bottom: 18px;
border: 1px solid var(--rule);
background: var(--bg);
}
#seqPlot { width: 100%; height: 380px; }
.text-block {
font-size: 0.9rem;
line-height: 1.6;
padding: 11px 14px;
margin-bottom: 10px;
background: var(--bg2);
border-left: 3px solid var(--rule);
color: var(--ink2);
}
.text-block strong { color: var(--ink); }
.text-block.result { border-left-color: var(--red); }
.text-block.sig { border-left-color: var(--red); background: var(--red-bg); }
@media (max-width: 500px) {
.input-grid { grid-template-columns: 1fr 1fr; }
.sim-btn { grid-column: span 2; }
#seqPlot { height: 280px; }
}
</style>
</head>
<body>
<p class="calc-label">Interactive simulation</p>
<h2 class="calc-heading">Sequential Testing and <em>p</em>-value Trajectories</h2>
<div class="input-panel">
<div class="input-grid">
<div class="input-group">
<label class="input-label" for="maxN">Sample size per group <span class="sym">n</span></label>
<input type="number" id="maxN" min="20" max="5000" value="200">
</div>
<div class="input-group">
<label class="input-label" for="d">Cohen's <span class="sym">d</span> (true effect)</label>
<input type="range" id="d" min="0" max="2" step="0.01" value="0">
<div class="slider-value" id="dVal">0.00</div>
</div>
<button class="sim-btn" id="simBtn" onclick="runSimulation()">Simulate</button>
</div>
</div>
<div class="plot-wrap">
<div id="seqPlot"></div>
</div>
<div id="textResult" class="text-block result"></div>
<script>
// ── Normal random variate (Box-Muller) ────────────────────────────────
function rnorm(mean, sd) {
var u1, u2;
do { u1 = Math.random(); } while (u1 === 0);
u2 = Math.random();
return mean + sd * Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
}
// ── Two-sample t-test p-value (equal variance / pooled) ──────────────
// Accepts running sums to keep the simulation O(n).
// Both groups have exactly n observations each.
function pooledPValue(sumX, sumX2, sumY, sumY2, n) {
if (n < 2) return 1;
var meanX = sumX / n;
var meanY = sumY / n;
var ssX = sumX2 - n * meanX * meanX; // sum of squared deviations
var ssY = sumY2 - n * meanY * meanY;
var sp2 = (ssX + ssY) / (2 * n - 2); // pooled variance
if (sp2 <= 0) return 1;
var se = Math.sqrt(sp2 * 2 / n); // SE of difference in means
var t = (meanX - meanY) / se;
var df = 2 * n - 2;
return 2 * (1 - jStat.studentt.cdf(Math.abs(t), df));
}
// ── postHeight for iframe auto-resize (debounced to prevent push-down) ───
var _phTimer = null;
function postHeight() {
clearTimeout(_phTimer);
_phTimer = setTimeout(function() {
var h = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
window.parent.postMessage({ iframeHeight: h }, '*');
}, 150);
}
// ── Main simulation ───────────────────────────────────────────────────
function runSimulation() {
var maxN = Math.max(20, Math.min(5000, parseInt(document.getElementById('maxN').value) || 200));
var trueD = parseFloat(document.getElementById('d').value) || 0;
document.getElementById('dVal').textContent = trueD.toFixed(2);
var sampleSizes = [];
var pValues = [];
var sumX = 0, sumX2 = 0;
var sumY = 0, sumY2 = 0;
for (var i = 0; i < maxN; i++) {
var xi = rnorm(0, 1);
var yi = rnorm(trueD, 1);
sumX += xi; sumX2 += xi * xi;
sumY += yi; sumY2 += yi * yi;
var n = i + 1;
if (n >= 10) {
sampleSizes.push(n);
pValues.push(pooledPValue(sumX, sumX2, sumY, sumY2, n));
}
}
// ── Statistics ───────────────────────────────────────────────────────
var minP = Math.min.apply(null, pValues);
var minIdx = pValues.indexOf(minP);
var minN = sampleSizes[minIdx];
var firstSigN = null;
for (var k = 0; k < pValues.length; k++) {
if (pValues[k] < 0.05) { firstSigN = sampleSizes[k]; break; }
}
// ── Plot ─────────────────────────────────────────────────────────────
Plotly.react('seqPlot', [
{
x: sampleSizes, y: pValues,
type: 'scatter', mode: 'lines',
name: 'p-value',
line: { color: '#a60a0a', width: 2 },
hovertemplate: 'n = %{x}<br>p = %{y:.4f}<extra></extra>'
}
], {
xaxis: {
title: { text: 'Sample size per group', font: { size: 13 } },
range: [10, maxN],
zeroline: false, gridcolor: '#ede8e8', linecolor: '#d6dadc'
},
yaxis: {
title: { text: 'p-value', font: { size: 13 } },
range: [0, 1],
zeroline: false, gridcolor: '#ede8e8', linecolor: '#d6dadc'
},
shapes: [
{
type: 'line', x0: 0, x1: 1, xref: 'paper',
y0: 0.05, y1: 0.05, yref: 'y',
line: { color: '#666060', dash: 'dash', width: 1.5 }
}
],
annotations: [
{
x: 1, y: 0.05, xref: 'paper', yref: 'y',
text: 'α = 0.05', showarrow: false,
xanchor: 'right', yanchor: 'bottom',
font: { family: "'Open Sans', sans-serif", size: 11, color: '#666060' }
}
],
plot_bgcolor: '#fffafa',
paper_bgcolor: '#fffafa',
font: { family: "'Open Sans', sans-serif", color: '#1c1717', size: 12 },
margin: { l: 55, r: 24, t: 16, b: 48 },
showlegend: false,
hovermode: 'x unified'
}, { responsive: true }).then(postHeight);
// ── Text output ───────────────────────────────────────────────────────
var sigText = firstSigN !== null
? 'The <em>p</em>-value first dropped below 0.05 at sample size <strong>' + firstSigN + '</strong>.'
: 'The <em>p</em>-value <strong>never</strong> dropped below 0.05.';
var el = document.getElementById('textResult');
el.className = 'text-block ' + (firstSigN !== null ? 'sig' : 'result');
el.innerHTML =
'Lowest <em>p</em>-value: <strong>' + minP.toFixed(4) + '</strong> ' +
'at sample size <strong>' + minN + '</strong>. ' + sigText;
postHeight();
}
// ── Slider live update ───────────────────────────────────────────────
document.getElementById('d').addEventListener('input', function() {
document.getElementById('dVal').textContent =
parseFloat(this.value).toFixed(2);
});
window.addEventListener('load', function() { setTimeout(postHeight, 400); });
if (typeof ResizeObserver !== 'undefined') {
new ResizeObserver(postHeight).observe(document.body);
}
// Run one simulation on load
runSimulation();
</script>
</body>
</html>