Published
Edited
Jan 22, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.rectY(humans, Plot.binX({y:'count'}, {x:'weight', fill:'sex'})).plot()
Insert cell
humans = Array.from(new Array(10000).keys())
.map(ea => {
const stature_ratio = 1.08
const sex = ['male', 'female'][Math.floor(Math.random() * 2)]
const mu = sex === 'male' ? mean : mean / stature_ratio
const height = skew_normal(Math.random, mu, std_dev, skew)
const bmi = skew_normal(Math.random, 20, 4, 2)
const weight = bmi * height * height / 10000
return {height, weight, sex}
})
Insert cell
function random_normal(rng) {
let u1 = 0, u2 = 0
// convert [0,1) to (0,1)
while (u1 === 0) u1 = rng()
while (u2 === 0) u2 = rng()
const r = Math.sqrt(-2.0 * Math.log(u1))
const theta = 2.0 * Math.PI * u2
return [r * Math.cos(theta), r * Math.sin(theta)]
}
Insert cell
/*
* zeta = mean of the distribution
* omega = standard deviation
* alpha = skewness of the distribution
*/
function skew_normal(rng, zeta, omega, alpha = 0) {
const [u0, v] = random_normal(rng)
if (alpha === 0) {
return zeta + omega * u0
}
const delta = alpha / Math.sqrt(1 + alpha * alpha)
const u1 = delta * u0 + Math.sqrt(1 - delta * delta) * v
const z = u0 >= 0 ? u1 : -u1
return zeta + omega * z
}
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