uT = {
const rnd = d3.randomLcg(7 + reseed);
return d3.range(30).map(() => rnd());
}
obsT = uT.slice(0, nT).map(u => u * thTrue)
mT = d3.max(obsT)
mEff = Math.max(mPrior, mT)
dT = mEff / 3000
thGrid = d3.range(dT / 2, mEff, dT)
postT = {
const v = thGrid.map(t => (t >= mT ? Math.exp(-nT * Math.log(t / mT)) : 0));
const z = d3.sum(v) * dT;
return v.map(x => x / z);
}
mmseT = d3.sum(thGrid, (t, i) => t * postT[i]) * dT
predT = {
// p(x | T) = integral over theta >= x of p(theta | T) / theta
const tail = new Array(thGrid.length).fill(0);
let acc = 0;
for (let i = thGrid.length - 1; i >= 0; i--) { acc += postT[i] / thGrid[i] * dT; tail[i] = acc; }
return thGrid.map((x, i) => ({x, y: tail[i]}));
}
tailT = d3.sum(predT.filter(d => d.x > mT), d => d.y) * dT
yTop = Math.min(1.6, Math.max(0.45, showT.includes("posterior") ? d3.max(postT) * 1.12 : 0, 1.3 / mT))
Plot.plot({
width: 1330, height: 780, marginLeft: 70, marginTop: 40, marginBottom: 60,
style: {fontSize: "20px"},
x: {label: "x, θ", domain: [0, Math.max(10, mEff)]},
y: {label: "density", domain: [0, yTop], grid: true},
marks: [
Plot.ruleX([thTrue], {stroke: "#7D3C98", strokeWidth: 3, strokeDasharray: "3 6"}),
Plot.text([thTrue], {x: d => d, y: yTop * 0.96, text: () => "true θ", fill: "#7D3C98", dx: 8, textAnchor: "start"}),
showT.includes("ML plug-in") ? Plot.line([[0, 1 / mT], [mT, 1 / mT], [mT, 0], [mEff, 0]], {stroke: "#3C7DD9", strokeWidth: 4}) : null,
showT.includes("posterior") ? Plot.line(thGrid.map((t, i) => [t, postT[i]]), {stroke: "#C0392B", strokeWidth: 3.5, clip: true}) : null,
showT.includes("posterior") ? Plot.dot([[mT, d3.max(postT)]], {r: 9, fill: "#E07812", clip: true}) : null,
showT.includes("MMSE") ? Plot.ruleX([mmseT], {stroke: "#0A9396", strokeWidth: 3, strokeDasharray: "8 6"}) : null,
showT.includes("predictive") ? Plot.line(predT, {x: "x", y: "y", stroke: "#2F7A52", strokeWidth: 5}) : null,
Plot.dot(obsT, {x: d => d, y: 0, r: 8, fill: "#0F8B8D", stroke: "white"}),
Plot.ruleY([0])
]
})