-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.html
More file actions
638 lines (595 loc) · 25.4 KB
/
Copy pathsample.html
File metadata and controls
638 lines (595 loc) · 25.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Impact of Sample Size on Hypothesis testing</title>
<link
rel="icon"
type="image/svg+xml"
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='12' ry='12' fill='%23C28E0E'/%3E%3Ctext x='32' y='40' font-family='Arial' font-size='28' text-anchor='middle' fill='white'%3ES%3C/text%3E%3C/svg%3E"
/>
<script src="https://cdn.tailwindcss.com"></script>
<script
crossorigin
src="https://unpkg.com/react@18/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body class="bg-white">
<div id="root"></div>
<script type="text/babel">
const { useState, useEffect } = React;
const SimpleBarChart = ({ data, metricLabel }) => {
if (!data || data.length === 0) {
return (
<div className="flex h-full items-center justify-center text-[#9D968D]">
No data to display
</div>
);
}
const maxValue = Math.max(
...data.map((item) => (typeof item.rate === "number" ? item.rate : 0)),
1
);
return (
<div className="h-full flex flex-col">
<div className="flex-1 flex items-end justify-center px-4 space-x-6">
{data.map((item) => {
const rawRate =
typeof item.rate === "number"
? item.rate
: parseFloat(item.rate) || 0;
const percentage = Math.max(
(rawRate / maxValue) * 100,
rawRate > 0 ? 6 : 2
);
const formattedRate = Number.isFinite(rawRate)
? rawRate.toFixed(2)
: "0.00";
return (
<div
key={item.name}
className="flex-1 flex flex-col items-center"
aria-label={`${item.name} ${formattedRate}%`}
>
<div className="flex-1 flex items-end w-full">
<div
className="w-full rounded-t-lg bg-gradient-to-t from-[#C28E0E] to-[#CEB888] shadow-lg transition-all duration-300"
style={{ height: `${percentage}%` }}
></div>
</div>
<div className="mt-3 text-center">
<p className="text-lg font-semibold text-[#000000]">
{formattedRate}%
</p>
<p className="text-sm text-[#9D968D]">
{item.count} / {item.total}
</p>
<p className="text-xs uppercase tracking-wide text-[#373A36]">
{metricLabel}
</p>
</div>
</div>
);
})}
</div>
<div className="border-t border-[#9D968D] mt-4 pt-2 text-center text-sm text-[#9D968D]">
Values expressed as percentages
</div>
</div>
);
};
const ABTestingDashboard = () => {
const [scenario, setScenario] = useState("website");
const [groupASize, setGroupASize] = useState(1000);
const [groupBSize, setGroupBSize] = useState(1000);
const [groupARate, setGroupARate] = useState(0.12);
const [groupBRate, setGroupBRate] = useState(0.15);
const [significanceLevel, setSignificanceLevel] = useState(0.05);
const [results, setResults] = useState(null);
const scenarios = {
website: {
title: "Website Conversion Rate Test",
description:
"Testing if a new website design improves conversion rates",
metric: "Conversion Rate",
groupALabel: "Original Design",
groupBLabel: "New Design",
},
email: {
title: "Email Marketing Campaign Test",
description:
"Testing if a new email subject line improves open rates",
metric: "Open Rate",
groupALabel: "Current Subject",
groupBLabel: "New Subject",
},
app: {
title: "Mobile App Feature Test",
description:
"Testing if a new button color increases click-through rates",
metric: "Click-through Rate",
groupALabel: "Blue Button",
groupBLabel: "Red Button",
},
};
const erf = (x) => {
const a1 = 0.254829592;
const a2 = -0.284496736;
const a3 = 1.421413741;
const a4 = -1.453152027;
const a5 = 1.061405429;
const p = 0.3275911;
const sign = x >= 0 ? 1 : -1;
const absX = Math.abs(x);
const t = 1.0 / (1.0 + p * absX);
const y =
1.0 -
(((((a5 * t + a4) * t + a3) * t + a2) * t + a1) *
t *
Math.exp(-absX * absX));
return sign * y;
};
const normalCDF = (x) => 0.5 * (1 + erf(x / Math.sqrt(2)));
const calculatePower = (n1, n2, p1, p2) => {
const pooledP = (n1 * p1 + n2 * p2) / (n1 + n2);
const se1 = Math.sqrt(
pooledP * (1 - pooledP) * (1 / n1 + 1 / n2)
);
const se2 = Math.sqrt(
(p1 * (1 - p1)) / n1 + (p2 * (1 - p2)) / n2
);
const criticalValue = 1.96; // for alpha = 0.05
const effectSize = Math.abs(p2 - p1);
const zBeta = (effectSize - criticalValue * se1) / se2;
return normalCDF(zBeta);
};
const calculateResults = () => {
const groupASuccesses = Math.round(groupASize * groupARate);
const groupBSuccesses = Math.round(groupBSize * groupBRate);
const p1 = groupASuccesses / groupASize;
const p2 = groupBSuccesses / groupBSize;
const pooledP =
(groupASuccesses + groupBSuccesses) /
(groupASize + groupBSize);
const se = Math.sqrt(
pooledP * (1 - pooledP) * (1 / groupASize + 1 / groupBSize)
);
const zStat = (p2 - p1) / se;
const pValue = 2 * (1 - normalCDF(Math.abs(zStat)));
const effectSize = p2 - p1;
const effectSizePercent = effectSize * 100;
const seDiff = Math.sqrt(
(p1 * (1 - p1)) / groupASize +
(p2 * (1 - p2)) / groupBSize
);
const criticalValue = 1.96; // 95% CI
const ciLower = effectSize - criticalValue * seDiff;
const ciUpper = effectSize + criticalValue * seDiff;
const power = calculatePower(
groupASize,
groupBSize,
p1,
p2,
significanceLevel
);
return {
groupASuccesses,
groupBSuccesses,
p1,
p2,
zStat,
pValue,
effectSize,
effectSizePercent,
ciLower: ciLower * 100,
ciUpper: ciUpper * 100,
isSignificant: pValue < significanceLevel,
power,
pooledP,
se,
};
};
useEffect(() => {
setResults(calculateResults());
}, [
scenario,
groupASize,
groupBSize,
groupARate,
groupBRate,
significanceLevel,
]);
const chartData = [
{
name: scenarios[scenario].groupALabel,
rate: parseFloat((groupARate * 100).toFixed(2)),
count: Math.round(groupASize * groupARate),
total: groupASize,
},
{
name: scenarios[scenario].groupBLabel,
rate: parseFloat((groupBRate * 100).toFixed(2)),
count: Math.round(groupBSize * groupBRate),
total: groupBSize,
},
];
const getSignificanceColor = (pValue) => {
if (pValue < 0.001)
return "text-[#000000] bg-[#C28E0E]";
if (pValue < 0.01)
return "text-[#000000] bg-[#CEB888]";
if (pValue < 0.05)
return "text-[#373A36] bg-white border border-[#C28E0E]";
return "text-[#373A36] bg-white border border-[#9D968D]";
};
const getInterpretation = () => {
if (!results) return "";
const { pValue, isSignificant, effectSizePercent } =
results;
if (isSignificant) {
return `🎉 Statistically Significant! The difference of ${effectSizePercent.toFixed(
2
)}% is unlikely due to chance alone (p = ${pValue.toFixed(
4
)}). We can reject the null hypothesis.`;
}
return `❌ Not Statistically Significant. The observed difference of ${effectSizePercent.toFixed(
2
)}% could reasonably be due to chance (p = ${pValue.toFixed(
4
)}). We fail to reject the null hypothesis.`;
};
return (
<div className="p-6 max-w-7xl mx-auto bg-white min-h-screen">
<div className="bg-white rounded-lg shadow-lg p-6 mb-6 border border-[#9D968D]">
<h1 className="text-3xl font-bold text-[#000000] mb-2">
A/B Testing Dashboard
</h1>
<p className="text-[#373A36]">
Interactive statistical learning tool for hypothesis
testing
</p>
</div>
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
<div className="space-y-6">
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#000000]">
Test Configuration
</h2>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
Test Scenario
</label>
<select
value={scenario}
onChange={(e) => setScenario(e.target.value)}
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
>
{Object.entries(scenarios).map(
([key, value]) => (
<option key={key} value={key}>
{value.title}
</option>
)
)}
</select>
</div>
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
Significance Level (α)
</label>
<select
value={significanceLevel}
onChange={(e) =>
setSignificanceLevel(
parseFloat(e.target.value)
)
}
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
>
<option value={0.1}>0.10 (10%)</option>
<option value={0.05}>0.05 (5%)</option>
<option value={0.01}>0.01 (1%)</option>
</select>
</div>
<div className="bg-white border-l-4 border-[#C28E0E] p-4 rounded-lg">
<h3 className="font-semibold text-[#000000]">
{scenarios[scenario].title}
</h3>
<p className="text-[#373A36] text-sm">
{scenarios[scenario].description}
</p>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#C28E0E]">
Group A: {scenarios[scenario].groupALabel}
</h2>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
Sample Size
</label>
<input
type="number"
value={groupASize}
onChange={(e) =>
setGroupASize(
parseInt(e.target.value, 10) || 0
)
}
min="50"
max="10000"
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
/>
</div>
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
{scenarios[scenario].metric} (%)
</label>
<input
type="number"
value={(groupARate * 100).toFixed(1)}
onChange={(e) =>
setGroupARate(
parseFloat(e.target.value) / 100 || 0
)
}
min="0"
max="100"
step="0.1"
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
/>
</div>
<div className="bg-white border-l-4 border-[#C28E0E] p-3 rounded">
<p className="text-sm text-[#373A36]">
<strong>Conversions:</strong>{" "}
{Math.round(groupASize * groupARate)} out of{" "}
{groupASize}
</p>
<p className="text-sm text-[#373A36]">
<strong>Rate:</strong>{" "}
{(groupARate * 100).toFixed(2)}%
</p>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#373A36]">
Group B: {scenarios[scenario].groupBLabel}
</h2>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
Sample Size
</label>
<input
type="number"
value={groupBSize}
onChange={(e) =>
setGroupBSize(
parseInt(e.target.value, 10) || 0
)
}
min="50"
max="10000"
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
/>
</div>
<div>
<label className="block text-sm font-medium text-[#373A36] mb-2">
{scenarios[scenario].metric} (%)
</label>
<input
type="number"
value={(groupBRate * 100).toFixed(1)}
onChange={(e) =>
setGroupBRate(
parseFloat(e.target.value) / 100 || 0
)
}
min="0"
max="100"
step="0.1"
className="w-full p-3 border border-[#9D968D] rounded-md focus:ring-2 focus:ring-[#C28E0E]"
/>
</div>
<div className="bg-white border-l-4 border-[#CEB888] p-3 rounded">
<p className="text-sm text-[#373A36]">
<strong>Conversions:</strong>{" "}
{Math.round(groupBSize * groupBRate)} out of{" "}
{groupBSize}
</p>
<p className="text-sm text-[#373A36]">
<strong>Rate:</strong>{" "}
{(groupBRate * 100).toFixed(2)}%
</p>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#000000]">
Learning Notes
</h2>
<div className="space-y-3 text-sm text-[#373A36]">
<p>
<strong>P-Value:</strong> The probability of
observing this difference (or more extreme) if
there's truly no difference between groups.
</p>
<p>
<strong>Z-Statistic:</strong> Measures how many
standard errors the observed difference is from
zero.
</p>
<p>
<strong>Effect Size:</strong> The actual
difference between groups, regardless of
statistical significance.
</p>
<p>
<strong>Statistical Power:</strong> The
probability of detecting a true difference when
it exists.
</p>
</div>
</div>
</div>
<div className="space-y-6">
{results && (
<>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#000000]">
Visualization
</h2>
<div className="h-64">
<SimpleBarChart
data={chartData}
metricLabel={scenarios[scenario].metric}
/>
</div>
</div>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#000000]">
Test Results
</h2>
<div
className={`p-4 rounded-lg mb-6 ${
results.isSignificant
? "bg-[#CEB888] text-[#000000]"
: "bg-white border border-[#9D968D] text-[#373A36]"
}`}
>
<p className="font-medium text-lg">
{getInterpretation()}
</p>
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<div className="bg-white border border-[#9D968D] p-4 rounded-lg text-center">
<h3 className="font-semibold text-[#373A36] mb-1">
Z-Statistic
</h3>
<p className="text-3xl font-bold text-[#C28E0E]">
{results.zStat.toFixed(3)}
</p>
</div>
<div
className={`p-4 rounded-lg text-center ${getSignificanceColor(
results.pValue
)}`}
>
<h3 className="font-semibold mb-1">P-Value</h3>
<p className="text-3xl font-bold">
{results.pValue.toFixed(4)}
</p>
</div>
<div className="bg-white border border-[#9D968D] p-4 rounded-lg text-center">
<h3 className="font-semibold text-[#373A36] mb-1">
Effect Size
</h3>
<p className="text-3xl font-bold text-[#C28E0E]">
{results.effectSizePercent.toFixed(2)}%
</p>
</div>
<div className="bg-white border border-[#9D968D] p-4 rounded-lg text-center">
<h3 className="font-semibold text-[#373A36] mb-1">
Statistical Power
</h3>
<p className="text-3xl font-bold text-[#C28E0E]">
{(results.power * 100).toFixed(1)}%
</p>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow-lg p-6 border border-[#9D968D]">
<h2 className="text-xl font-semibold mb-4 text-[#000000]">
Statistical Details
</h2>
<div className="space-y-4">
<div className="bg-white border-l-4 border-[#C28E0E] p-4 rounded-lg">
<h3 className="font-semibold text-[#000000] mb-2">
Hypothesis Test Setup
</h3>
<p className="text-[#373A36] text-sm">
<strong>H₀:</strong> p₁ = p₂ (No difference
between groups)
</p>
<p className="text-[#373A36] text-sm">
<strong>H₁:</strong> p₁ ≠ p₂ (There is a
difference between groups)
</p>
<p className="text-[#373A36] text-sm">
<strong>Significance Level:</strong> α ={" "}
{significanceLevel}
</p>
</div>
<div className="bg-white border border-[#9D968D] p-4 rounded-lg">
<h3 className="font-semibold text-[#373A36] mb-2">
Confidence Interval (95%)
</h3>
<p className="text-[#373A36] text-sm">
The true difference is likely between{" "}
<strong>
{results.ciLower.toFixed(2)}%
</strong>{" "}
and{" "}
<strong>
{results.ciUpper.toFixed(2)}%
</strong>
</p>
{results.ciLower <= 0 &&
results.ciUpper >= 0 && (
<p className="text-[#C28E0E] mt-1 text-sm">
⚠️ The confidence interval includes 0,
suggesting no significant difference.
</p>
)}
</div>
<div className="bg-white border border-[#CEB888] p-4 rounded-lg">
<h3 className="font-semibold text-[#000000] mb-2">
Key Metrics
</h3>
<div className="grid grid-cols-1 gap-2 text-sm sm:grid-cols-2">
<p className="text-[#373A36]">
<strong>Pooled Proportion:</strong>{" "}
{(results.pooledP * 100).toFixed(2)}%
</p>
<p className="text-[#373A36]">
<strong>Standard Error:</strong>{" "}
{results.se.toFixed(4)}
</p>
<p className="text-[#373A36]">
<strong>Group A Rate:</strong>{" "}
{(results.p1 * 100).toFixed(2)}%
</p>
<p className="text-[#373A36]">
<strong>Group B Rate:</strong>{" "}
{(results.p2 * 100).toFixed(2)}%
</p>
</div>
</div>
</div>
</div>
</>
)}
</div>
</div>
</div>
);
};
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<ABTestingDashboard />);
</script>
</body>
</html>