zcD = altD === "dos" ? zQ[+(1 - alfaD / 2).toFixed(3)] : zQ[+(1 - alfaD).toFixed(2)]
despD = dD * Math.sqrt(nD)
potD = altD === "dos" ? (1 - Phi(zcD - despD)) + Phi(-zcD - despD) : 1 - Phi(zcD - despD)
nNecD = dD > 0 ? Math.ceil(((zcD + zQ[0.8]) / dD) ** 2) : null
curvasD = {
const hi = Math.min(Math.max(4, despD + 4), 22);
return d3.range(-4, hi + 1e-9, (hi + 4) / 500).map(x => ({x, h0: dnorm(x, 0, 1), h1: dnorm(x, despD, 1)}));
}
rechazaD = x => altD === "dos" ? Math.abs(x) >= zcD : x >= zcD
Plot.plot({
width: anchoLab, height: 300, marginLeft: 50,
x: {label: "Valor del estadístico Z", tickFormat: ejeES},
y: {label: "Densidad", domain: [0, 0.45], grid: true, tickFormat: ejeES},
marks: [
Plot.areaY(curvasD, {x: "x", y: d => rechazaD(d.x) ? d.h1 : 0, fill: pal.accent, fillOpacity: 0.3, curve: "step"}),
Plot.areaY(curvasD, {x: "x", y: d => rechazaD(d.x) ? 0 : d.h1, fill: pal.warning, fillOpacity: 0.22, curve: "step"}),
Plot.areaY(curvasD, {x: "x", y: d => rechazaD(d.x) ? d.h0 : 0, fill: pal.danger, fillOpacity: 0.45, curve: "step"}),
Plot.line(curvasD, {x: "x", y: "h0", stroke: pal.ink, strokeWidth: 2}),
Plot.line(curvasD, {x: "x", y: "h1", stroke: pal.accent, strokeWidth: 2.5, strokeDasharray: "6,3"}),
Plot.ruleX(altD === "dos" ? [-zcD, zcD] : [zcD], {stroke: pal.danger, strokeWidth: 1.5}),
Plot.text([{x: 0, y: 0.425, t: "H₀"}, {x: despD, y: 0.425, t: "H₁"}], {x: "x", y: "y", text: "t", fill: pal.ink, fontSize: 13, fontWeight: 700}),
Plot.ruleY([0])
],
ariaLabel: `Distribución del estadístico bajo H0 y bajo H1 con desplazamiento ${fmt(despD, 2)}; la región de rechazo empieza en ${fmt(zcD, 3)}. Potencia ${pct(potD)}.`
})