Public
Edited
Sep 20, 2023
Fork of Untitled
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof sigma_1 = setSigma()
Insert cell
md`#### Imports`
Insert cell
import {slider, button} from "@jashkenas/inputs"
Insert cell
d3 = require("d3@5")
Insert cell
Plotly = require("https://cdn.plot.ly/plotly-latest.min.js")
Insert cell
math = require("https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.4.0/math.js")
Insert cell
_ = require("underscore")
Insert cell
import {toc} from "@bluprince13/toc/2"
Insert cell
stdlib = require("https://unpkg.com/@stdlib/stdlib@0.0.32/dist/stdlib-flat.min.js")
Insert cell
// https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dists/normal/pdf

pdf = stdlib.base.dists.normal.pdf
Insert cell
md`#### Defaults`
Insert cell
muMin = -10
Insert cell
muMax = 10
Insert cell
muDefault = 0
Insert cell
sigmaMin = 0
Insert cell
sigmaMax = 10
Insert cell
sigmaDefault = 5
Insert cell
rangeVarMean = [muMin - sigmaMax, muMax + sigmaMax]
Insert cell
range = [-sigmaMax, sigmaMax]
Insert cell
nMin = 3
Insert cell
nMax = 100
Insert cell
nDefault = 10
Insert cell
numberOfSamplesDefault = 10
Insert cell
xText = "observations (x)"
Insert cell
muText = "pop mean (μ)"
Insert cell
sigmaText = "pop std dev (σ)"
Insert cell
sampleText = "sample (i)"
Insert cell
meanText = "sample mean (x̅)"
Insert cell
sText = "sample std dev (s)"
Insert cell
nText = "sample size (n)"
Insert cell
NText = "population size (N)"
Insert cell
numberOfSamplesText = "no. of samples"
Insert cell
stdErrorSigmaText = "std error (σ<sub>x̅</sub>)"
Insert cell
stdErrorSText = "std error (s<sub>x̅</sub>)"
Insert cell
probabilityDensityText = "Probability Density Function (PDF)"
Insert cell
frequencyText = "frequency (f)"
Insert cell
scoreText = "score"
Insert cell
meanTex = `\\textcolor{red}{\\overline{x}}`
Insert cell
sigmaTex = "\\textcolor{green}{\\sigma}"
Insert cell
muTex = "\\textcolor{green}{\\mu}"
Insert cell
sTex = "\\textcolor{red}{s}"
Insert cell
nTex = "\\textcolor{red}{n}"
Insert cell
stdErrorSigmaTex = "\\textcolor{green}{{\\sigma_{\\overline{x}}}}"
Insert cell
stdErrorSTex = "\\textcolor{red}{{s_{\\overline{x}}}}"
Insert cell
NTex = "\\textcolor{green}{N}"
Insert cell
md`#### Slider generators`
Insert cell
setMu = () =>
slider({
min: muMin,
max: muMax,
value: muDefault,
step: 0.1,
description: muText
})
Insert cell
setSigma = () =>
slider({
min: sigmaMin,
max: sigmaMax,
value: sigmaDefault,
step: 0.1,
description: sigmaText
})
Insert cell
setn = () =>
slider({
min: nMin,
max: nMax,
value: nDefault,
step: 1,
description: nText
})
Insert cell
setNumberOfSamples = () =>
slider({
min: 3,
max: 100,
value: numberOfSamplesDefault,
step: 1,
description: "Number of samples"
})
Insert cell
setS = () =>
slider({
min: sigmaMin,
max: sigmaMax,
value: sigmaDefault,
step: 0.1,
description: sText
})
Insert cell
setNumberOfStdDev = () =>
slider({
min: 1,
max: 3,
value: 1,
step: 1,
description: "Number of standard deviations"
})
Insert cell
setPop = ({
name = "Inputs",
muInitial = muDefault,
sigmaInitial = sigmaDefault,
nInitial = nDefault
} = {}) => {
const form = html`
<form>
<h4> ${name} </h4>
<div>
<input type=range name="mu" min=${muMin} max=${muMax} step=0.1 value=${muInitial}>
<i>${muText} = </i>
<output id="muResult"></output>
</div>

<div>
<input type=range name="sigma" min=${sigmaMin} max=${sigmaMax} step=0.1 value=${sigmaInitial}>
<i>${sigmaText} = </i>
<output id="sigmaResult"></output>
</div>

<div>
<input type=range name="n" min=${nMin} max=${nMax} step=1 value=${nInitial}>
<i>${nText} = </i>
<output id="nResult"></output>
</div>
</form>`;
form.oninput = () => {
const mu = form.mu.valueAsNumber;
const sigma = form.sigma.valueAsNumber;
const n = form.n.valueAsNumber;

form.muResult.value = mu;
form.sigmaResult.value = sigma;
form.nResult.value = n;

return (form.value = { mu, sigma, n });
};
form.oninput();
return form;
}
Insert cell
md`#### Helper functions`
Insert cell
getTex = symbolTex => html`${tex`${symbolTex}`}`
Insert cell
getInfo = ({ mu, sigma, n, numberOfSamples }) => {
var info = [];
mu != undefined && info.push(`μ = ${mu}`);
sigma != undefined && info.push(`σ = ${sigma}`);
n != undefined && info.push(`n = ${n}`);
numberOfSamples != undefined &&
info.push(`${numberOfSamplesText} = ${numberOfSamples}`);
return info.join(", ");
}
Insert cell
getTraceErrorsEnvelope = ({
x,
y,
yError,
name,
axis,
fillcolor = lighterBlue,
linecolor = lightBlue
}) => {
const { xaxis, yaxis, showlegend } = getAxis(axis);

const polygonX = [...x, ...[...x].reverse()];

const polygonY = polygonX.map((item, idx) => {
if (idx < x.length) {
return y[item] + yError[item];
} else {
return y[item] - yError[item];
}
});

var traceStds = {
x: polygonX,
y: polygonY,
fill: "tozerox",
fillcolor,
line: { color: linecolor },
name: name,
type: "scatter",
xaxis,
yaxis,
showlegend
};

return traceStds;
}
Insert cell
getPDF = ({ mu, sigma, numberOfStdDev }) => {
const getX = (mu, sigma, numberOfStdDev) => {
const min = mu - numberOfStdDev * sigma;
const max = mu + numberOfStdDev * sigma;
const unit = (max - min) / 100;
return _.range(min, max, unit);
};
const x = getX(mu, sigma, numberOfStdDev);

const getPDFs = (x, mu, sigma) => pdf(x, mu, sigma);
const y = x.map(score => getPDFs(score, mu, sigma));

const fractionsOfPopulation = {
1: 68,
2: 95,
3: 99.7
};

const fractionOfPopulation = fractionsOfPopulation[numberOfStdDev];

return { x, y, fractionOfPopulation };
}
Insert cell
getAxis = axis => {
let xaxis, yaxis, showlegend;

if (axis == 1) {
xaxis = "x1";
yaxis = "y1";
showlegend = true;
} else if (axis == 2) {
xaxis = "x2";
yaxis = "y2";
showlegend = false;
}

// if axis is not defined, then it returns undefined which is required when there aren't subplots
return { xaxis, yaxis, showlegend };
}
Insert cell
md`#### Generate samples and traces`
Insert cell
getRandomNormal = () => {
const mu = mu_1;
const sigma = sigma_1;
const n = n_1;

return {
x: d3.range(n).map(() => d3.randomNormal(mu, sigma)()),
y: d3.range(n).map(() => d3.randomNormal(muDefault, sigmaDefault)())
};
}
Insert cell
getExperiment = function({
numberOfSamples = numberOfSamplesDefault,
n = nDefault,
mu = muDefault,
sigma = sigmaDefault
} = {}) {
var samples = Array.from({ length: numberOfSamples }, (x, i) => i);

var data = samples.map(() => {
return d3.range(n).map(() => d3.randomNormal(mu, sigma)());
});

var means = data.map(sample => math.mean(sample));
var stds = data.map(sample => math.std(sample));

var stdErrors = stds.map(std => std / math.sqrt(n));

var stdErrorPopulation = math.std(means);
var stdErrorsPopulation = Array(numberOfSamples).fill(stdErrorPopulation);

// Now let's flatten the data for plotting
const flatSamples = samples.reduce((res, current) => {
return res.concat(Array(n).fill(current));
}, []);
const flatData = [].concat.apply([], data);

const mus = Array(numberOfSamples).fill(mu);
const sigmas = Array(numberOfSamples).fill(sigma);

return {
numberOfSamples,
n,
samples,
flatSamples,
data,
flatData,
means,
stds,
stdErrors,
stdErrorPopulation,
stdErrorsPopulation,
mus,
sigmas
};
}
Insert cell
getTracesExperiment = ({
mu = muDefault,
sigma = sigmaDefault,
n = nDefault,
numberOfSamples = numberOfSamplesDefault,
axis = 1,
meanMode = "markers",
showMeanError = true
}) => {
const { xaxis, yaxis, showlegend } = getAxis(axis);

const experiment = getExperiment({ mu, n, sigma, numberOfSamples });

const trace_x = {
name: xText,
x: experiment.flatSamples,
y: experiment.flatData,
type: "scatter",
mode: "markers",
marker: {
color: "black"
},
xaxis,
yaxis,
showlegend
};

const trace_stdErrorsSample = getTraceStdErrorsEnvelopeExperiment({
experiment,
axis,
linecolor: lightRed,
fillcolor: lighterRed
});

var trace_mean = {
name: meanText,
x: experiment.samples,
y: experiment.means,
type: "scatter",
mode: meanMode,
marker: {
color: "red"
},
xaxis,
yaxis,
showlegend
};

if (showMeanError) {
trace_mean.error_y = {
type: "data",
array: experiment.stdErrors,
visible: true,
color: "red"
};
}

const trace_mu = {
name: muText,
x: experiment.samples,
y: experiment.mus,
type: "scatter",
mode: "lines",
marker: {
color: "green"
},
line: {
width: 3
},
xaxis,
yaxis,
showlegend
};

const trace_stdErrorPopulation = getTraceErrorsEnvelope({
x: experiment.samples,
y: experiment.mus,
yError: experiment.stdErrorsPopulation,
name: stdErrorSText,
fillcolor: lighterGreen,
linecolor: lightGreen
});

return {
trace_x,
trace_stdErrorsSample,
trace_mean,
trace_mu,
trace_stdErrorPopulation
};
}
Insert cell
getTraceStdErrorsEnvelopeExperiment = ({
experiment,
axis,
linecolor,
fillcolor
}) =>
getTraceErrorsEnvelope({
x: experiment.samples,
y: experiment.means,
yError: experiment.stdErrors,
name: stdErrorSText,
axis,
linecolor,
fillcolor
})
Insert cell
md`#### Colors`
Insert cell
lighterGreen = "rgba(0, 255, 0, 0.2)";

Insert cell
lightGreen = "rgba(0, 255, 0, 0.4)";
Insert cell
lighterBlue = "rgba(0, 199, 255, 0.2)"
Insert cell
lightBlue = "rgba(0, 199, 255, 0.4)"
Insert cell
lighterRed = "rgba(255, 0, 0, 0.2)"
Insert cell
lightRed = "rgba(255, 0, 0, 0.2)"
Insert cell
transparent = "rgba(0, 0, 0, 0)"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more