Public
Edited
Nov 3, 2023
1 fork
2 stars
Also listed in…
Teaching Statistics
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function graph_from_type(s) {
if (s == "A perfect linear relationship") {
let a = jstat.uniform.sample(1 / 3, 2 / 3);
let b = jstat.uniform.sample(-6, 6);
return make_perturbed_graph(
(x) => a * x + b,
(x) => 0,
-10,
10,
100
);
} else if (s == "A close to linear relationship") {
let a = jstat.uniform.sample(1 / 3, 2 / 3);
let b = jstat.uniform.sample(-6, 6);
return make_perturbed_graph(
(x) => a * x + b,
(x) => jstat.randn(),
-10,
10,
100
);
} else if (s == "A close to linear, but negative, relationship") {
let a = jstat.uniform.sample(1 / 3, 2 / 3);
let b = jstat.uniform.sample(-6, 6);
return make_perturbed_graph(
(x) => -a * x + b,
(x) => jstat.randn(),
-10,
10,
100
);
} else if (s == "A weaker relationship") {
let a = jstat.uniform.sample(-2 / 3, 2 / 3);
let b = jstat.uniform.sample(-6, 6);
return make_perturbed_graph(
(x) => a * x + b,
(x) => 4 * jstat.randn(),
-10,
10,
100
);
} else if (s == "A nonlinear relationship") {
let a = jstat.uniform.sample(-3, 1);
let b = jstat.uniform.sample(1, 3);
return make_perturbed_graph(
(x) => (x - a) * (x - b),
(x) => 0.6 * jstat.randn(),
-10,
10,
100
);
} else if (s == "No relationship") {
let a = jstat.uniform.sample(-3, 1);
let b = jstat.uniform.sample(1, 3);
return make_perturbed_graph(
(x) => 0,
(x) => jstat.uniform.sample(-10, 10),
-10,
10,
100
);
}
}
Insert cell
Insert cell
Insert cell
sir_with_fake_data = {
let w = 0.6 * width;
let h = 0.4 * w;
let plot = plotter({
xDomain: [-1, 150],
yDomain: [1, 0],
width: w,
height: h,
xLabel: 't',
yLabel: 'SIR',
yTickCount: 5,
grid: false
});
plot.polyline(model_result.map(y => ({ x: y.t, y: y[2] })), {
stroke: 'green',
width: 3
});

jstat.arange(model_result0.length).forEach(function(k) {
let x = model_result0[k].t;
let y = model_result0[k][2];
//console.log(x);
plot.point(x, y, { size: 2 });
});

return plot.node;
}
Insert cell
model_result = rk4(
([s, i, r], t) => [-b * s * i, b * s * i - k * i, k * i],
[1, 1.27 * 10 ** -6, 0],
0,
0,
150,
200
)
Insert cell
model_result0 = {
let b0 = 0.5;
let k0 = 0.3;
let n0 = 40;

let model_result0 = rk4(
([s, i, r], t) => [-b0 * s * i, b0 * s * i - k0 * i, k0 * i],
[1, 1.27 * 10 ** -6, 0],
0,
0,
150,
n0
);

model_result0.forEach(function(pt) {
pt[0] = pt[0] + jstat.randn() / 20;
pt[1] = pt[1] + jstat.randn() / 20;
pt[2] = pt[2] + jstat.randn() / 20;
pt.t = pt.t + jstat.rand() / 10;
});

return model_result0;
}
Insert cell
function make_perturbed_graph(f, r, a, b, n) {
let xs = jstat.arange(n).map(() => jstat.uniform.sample(a, b));
let ys = xs.map((x) => f(x) + r());
let plot = plotter({ width: 500, height: 400, grid: false });
jstat.arange(n).forEach((_, i) => plot.point(xs[i], ys[i]));

let R = jstat.corrcoeff(xs, ys);

return html`<div style="text-align:center; width:500px">R = ${d3.format(
"0.4f"
)(R)}</div>${plot.node}`;
}
Insert cell
import { rk4 } from '@mcmcclur/runge-kutta-for-systems-of-odes'
Insert cell
import { plotter } from '50dadfdec01c15a8'
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
import { slider } from "@jashkenas/inputs"
Insert cell
jstat = require('jstat')
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