viewof propSigma = Inputs.range([0.05, 1.5], {value: 0.35, step: 0.05, label: "sigma_X"})
viewof propCurvature = Inputs.range([0, 1.5], {value: 0.35, step: 0.05, label: "кривизна a"})
viewof regenerateProp = Inputs.button("Новая выборка")
randnProp = () => {
const u1 = Math.max(Math.random(), 1e-12);
const u2 = Math.random();
return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
}
propSample = {
regenerateProp;
const n = 18000;
const values = new Array(n);
for (let i = 0; i < n; ++i) {
const x = 1 + propSigma * randnProp();
values[i] = x + propCurvature * x * x;
}
return values;
}
histProp = {
const bins = 80;
const lo = Math.min(...propSample);
const hi = Math.max(...propSample);
const width = (hi - lo) / bins;
const counts = Array(bins).fill(0);
for (const y of propSample) {
const j = Math.min(bins - 1, Math.max(0, Math.floor((y - lo) / width)));
counts[j] += 1;
}
return counts.map((count, j) => ({
x0: lo + j * width,
x1: lo + (j + 1) * width,
density: count / (propSample.length * width)
}));
}
propMeanLinear = 1 + propCurvature
propSigmaLinear = Math.abs(1 + 2 * propCurvature) * propSigma
propMeanExact = 1 + propCurvature + propCurvature * propSigma * propSigma
propSigmaExact = Math.sqrt(
(1 + 2 * propCurvature) ** 2 * propSigma ** 2
+ 2 * propCurvature ** 2 * propSigma ** 4
)
normalProp = Array.from({length: 401}, (_, i) => {
const lo = histProp[0].x0;
const hi = histProp[histProp.length - 1].x1;
const y = lo + (hi - lo) * i / 400;
const z = (y - propMeanLinear) / propSigmaLinear;
return {
y,
density: Math.exp(-0.5 * z * z) / (propSigmaLinear * Math.sqrt(2 * Math.PI))
};
})
propActualMean = propSample.reduce((a, b) => a + b, 0) / propSample.length
propActualSigma = Math.sqrt(propSample.reduce((s, y) => s + (y - propActualMean) ** 2, 0) / propSample.length)
propPlot = Plot.plot({
width: 940,
height: 410,
marginLeft: 70,
x: {label: "Y = X + aX^2", grid: true},
y: {label: "плотность", grid: true},
marks: [
Plot.rectY(histProp, {x1: "x0", x2: "x1", y: "density", fill: "#4ea1ff", fillOpacity: 0.7}),
Plot.line(normalProp, {x: "y", y: "density", stroke: "#ffb347", strokeWidth: 3})
]
})
html`
<div class="clt-panel">
${propPlot}
<div class="clt-summary">
<span>линейно: mean = ${propMeanLinear.toFixed(3)}, sigma = ${propSigmaLinear.toFixed(3)}</span>
<span>точно: mean = ${propMeanExact.toFixed(3)}, sigma = ${propSigmaExact.toFixed(3)}</span>
<span>Монте-Карло: mean = ${propActualMean.toFixed(3)}, sigma = ${propActualSigma.toFixed(3)}</span>
<span>синий: Монте-Карло; оранжевый: линейное приближение</span>
</div>
</div>
`