Public
Edited
Jun 30, 2023
Insert cell
Insert cell
sampleDist(10, d3.randomNormal(3, 1))
Insert cell
function sampleDist(n, random) {
return Float64Array.from({ length: n }, random);
}
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.selectAll("circle")
.data(data)
.join("circle")
.filter(d => d.body_mass_g)
.attr("cx", d => x(d.flipper_length_mm))
.attr("cy", d => y(d.body_mass_g))
.attr("r", 4);

return svg.node();
}
Insert cell
data = FileAttachment("penguins.csv").csv({typed: true})
Insert cell
height = 400
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.flipper_length_mm)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.body_mass_g)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
Insert cell
margin = ({top: 25, right: 20, bottom: 35, left: 40})
Insert cell
obs = Array.from({ length: 10 }, () => ({
x: d3.randomNormal(0, 1)(),
y: d3.randomNormal(0, 1)(),
z: d3.randomNormal(0, 1)()
}))
Insert cell
targets = ({
mean: {
x: 3,
y: 1
},
sd: {
x: 2,
y: 1
},
rho: 0.4
})
Insert cell
function mean(xs) {
return xs.reduce((sum, x) => sum + x) / xs.length;
}
Insert cell
function variance(xs) {
let mean_x = mean(xs);
return xs.reduce((terms, x) => terms + (x - mean_x)**2) / xs.length;
}
Insert cell
function covariance(xs, ys) {
console.assert(xs.length === ys.length);
let mean_x = mean(xs);
let mean_y = mean(ys);
let N = xs.length;
let cov = 0;

for (let i = 0; i < N; i++) {
cov += (xs[i] - mean_x) * (ys[i] - mean_y);
}

return cov / N;
}
Insert cell
function correlation(xs, ys) {
let sd_x = Math.sqrt(variance(xs));
let sd_y = Math.sqrt(variance(ys));
return covariance(xs, ys) / (sd_x * sd_y);
}
Insert cell
function sample(n, dist) {
return Array.from({ length: n }, dist);
}
Insert cell
params = ({
x: sample(3, d3.randomNormal()),
y: sample(3, d3.randomNormal()),
zs: sample(obs.length, () => sample(3, d3.randomNormal()))
})
Insert cell
function model(params) {
return params.zs.map((z) =>
({
x: z.mul(params.x).sum(),
y: z.mul(params.y).sum()
})
)
}
Insert cell
Array.prototype.sum = function(initialValue=0) {
return this.reduce((terms, x) => terms + x, initialValue)
}
Insert cell
Array.prototype.mul = function(others) {
return d3.zip(this, others).map(([x, y]) => x * y);
}
Insert cell
Array.prototype.add = function(others) {
return d3.zip(this, others).map(([x, y]) => x + y);
}
Insert cell
Array.prototype.div = function(others) {
return d3.zip(this, others).map(([x, y]) => x / y);
}
Insert cell
Array.prototype.sub = function(others) {
return d3.zip(this, others).map(([x, y]) => x - y);
}
Insert cell
function gradMseMeanTarget(variable, targets, params) {
let predictions = model(params);
let N = obs.length;

let partialTerms = d3.zip(predictions, params.zs).map(([ps, zs]) =>
zs.map(z => z * (variable(ps) - variable(targets.mean)))
);

return partialTerms.reduce((zs, z) => zs.add(z), [0,0,0]).map((z) => 2/N * z);
}
Insert cell
function gradMseSDTarget(variable, target, params) {
let predictions = model(params);
let N = obs.length;

let partialTerms = d3.zip(predictions, params.zs).map(([ps, zs]) =>
zs.map(z => z * (variable(ps) - variable(targets.sd)))
);

return partialTerms.reduce((zs, z) => zs.add(z), [0,0,0]).map((z) => 2/N * z);
}
Insert cell
model(params)
Insert cell
gradMseMeanTarget((d) => d.x, targets, params)
Insert cell
targets["mean"]
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