Public
Edited
Sep 28, 2024
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
let data = [];
for (let pi = 0 ; pi <= 1000 ; pi++) {
let p = pi/1000;
for (let k = 0 ; k < 9; k++) {
let d = {
p,
k,
cum: 1-binomialCum(k,9,p)
};
data.push(d);
}
}
return Plot.plot({
width: 400,
color: {
legend: true,
},
aspectRatio: 1,
x: {
grid: true,
},
y: {
grid: true,
},
marks: [
Plot.ruleX([0.5]),
Plot.ruleY([0.5]),
Plot.line(data, {
strokeWidth: 3,
stroke: d=>d.k,
x: "p",
y: "cum"
}),
]
});
}
Insert cell
factorial = {
const r=[1,1];
return function f(n) {
return r[n] || (n < 0 ? 0 : (r[n] = n*f(n-1)));
}
}
Insert cell
choose = {
function f(n, k) {
if (k <= 0 || k >= n) {
return 1;
}
let s = "" + n + "c" + k;
return f.mem[s] || (f.mem[s] = Math.round((n-k+1) / k * f(n,k-1)));
}
f.mem = {};
return f;
}
Insert cell
choose.mem
Insert cell
choose2(100,12)
Insert cell
choose(100,12)
Insert cell
function choose2(n,k) {
return factorial(n)/ factorial(k)/factorial(n-k);
}
Insert cell
function binomial(k,n,p) {
return choose(n,k)*Math.pow(p,k)*(Math.pow(1-p,n-k));
}
Insert cell
function binomialCum(k,n,p) {
let c = binomial(k,n,p);
if (k >= 1) {
c += binomialCum(k-1,n,p);
}
return c;
}
Insert cell
Insert cell
Insert cell
Plot.plot({
y: {
grid: true,
},
marks: [
Plot.barY(pmf, { x: "k", y: d=>binomialCum(d.k,9,0.1), tip: true }),
//Plot.barY(pmf, Plot.mapY("cumsum", {x: "k", y: "bernoulli"})),
]
})
Insert cell
build_samples(p=>binomialCum(4,9,p),0,1)
Insert cell
Insert cell
import { build_samples } from '@mcmcclur/adaptive-plotter'

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more