Public
Edited
Sep 18, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
heads_flip = binompdf(
1, // number of flips
0.5, // probability of heads
1 // number of heads
)
Insert cell
Insert cell
Insert cell
heads_n_in_a_row = binompdf(
n, // number of flips
0.5, // probability of heads,
n // number of heads
)
Insert cell
Insert cell
Plot.plot({
height: 300,
marks: [
Plot.barY(d3.range(1, 11).map(n => ({n, p: binompdf(n, 0.5, n)})), { y: "p", x: "n", fill: "steelblue", tip: true })
]
})
Insert cell
Insert cell
Insert cell
binompdf(
1, // number of years
0.01, // probability of flood in a given year
1 // number of floods
)
Insert cell
Insert cell
p_1_100_year_flood = binompdf(
100, // number of years
0.01, // probability of flood in a given year
1 // number of floods
)
Insert cell
Insert cell
Insert cell
Insert cell
p_0_100_year_flood = binompdf(
100, // number of years
0.01, // probability of flood in a given year
0 // number of floods
)
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 300,
marks: [
Plot.barY(d3.range(7).map(f => ({f, p: binompdf(100, 0.01, f)})), { y: "p", x: "f", fill: "steelblue", tip: true })
]
})
Insert cell
binompdf = (n, p, x) => {
return combination(n, x) * Math.pow(p, x) * Math.pow(1 - p, n - x);
}
Insert cell
combination = (n, r) => {
return factorial(n) / (factorial(n - r) * factorial(r));
}
Insert cell
factorial = r => {
let o = 1;
const a = Math.abs(r), s = r === 0 ? 1 : Math.sign(r);
if (a > 1) {
o = a;
let i = a - 1;
while (i >= 1){
o *= i--;
}
}
return o * s;
}
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