Code
viewof n_pow = {
const input = Inputs.range([10, 200], {value: 60, step: 5, label: "Sample size n:"});
['pointerdown','touchstart','mousedown','click','wheel','pointermove','touchmove']
.forEach(e => input.addEventListener(e, ev => ev.stopPropagation()));
return input;
}
viewof alpha_pow = {
const input = Inputs.range([0.01, 0.10], {value: 0.05, step: 0.01, label: "ฮฑ level:"});
['pointerdown','touchstart','mousedown','click','wheel','pointermove','touchmove']
.forEach(e => input.addEventListener(e, ev => ev.stopPropagation()));
return input;
}
viewof sigma_pow = {
const input = Inputs.range([0.5, 3.0], {value: 1.5, step: 0.1, label: "ฯ (std dev):"});
['pointerdown','touchstart','mousedown','click','wheel','pointermove','touchmove']
.forEach(e => input.addEventListener(e, ev => ev.stopPropagation()));
return input;
}
// Normal CDF
ncdf = function(x) {
const t = 1 / (1 + 0.2316419 * Math.abs(x));
const poly = t*(0.319381530 + t*(-0.356563782 + t*(1.781477937 + t*(-1.821255978 + t*1.330274429))));
const d = Math.exp(-0.5*x*x)/Math.sqrt(2*Math.PI);
const r = 1 - d*poly;
return x >= 0 ? r : 1 - r;
}
// Inverse normal (Beasley-Springer-Moro)
qnorm = function(p) {
const a = [2.515517, 0.802853, 0.010328];
const b = [1.432788, 0.189269, 0.001308];
const t = Math.sqrt(-2*Math.log(p < 0.5 ? p : 1-p));
const x = t - (a[0]+a[1]*t+a[2]*t*t)/(1+b[0]*t+b[1]*t*t+b[2]*t*t*t);
return p < 0.5 ? -x : x;
}
z_alpha = qnorm(1 - alpha_pow);
// Power at mu_a: 1 - beta
power_at = function(mu0, mu_a) {
const se = sigma_pow / Math.sqrt(n_pow);
const k = mu0 + z_alpha * se;
return 1 - ncdf((k - mu_a) / se);
}
// Summary stats at mu_a = 0.5 (chosen effect)
pw_05 = power_at(0, 0.5);
pw_10 = power_at(0, 1.0);
pw_15 = power_at(0, 1.5);
info_html = html`<div style="background:#f0f4ff; padding:8px; border-radius:6px; font-size:0.93em;">
<strong>z<sub>ฮฑ</sub> = ${z_alpha.toFixed(3)}</strong><br>
Power at ฮ=0.5: <strong>${(pw_05*100).toFixed(1)}%</strong><br>
Power at ฮ=1.0: <strong>${(pw_10*100).toFixed(1)}%</strong><br>
Power at ฮ=1.5: <strong>${(pw_15*100).toFixed(1)}%</strong>
</div>`;
md`${info_html}`
