Public
Edited
Sep 15, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function dress_once() {
let pants = Math.random();
let shirt = Math.random();
if (pants < 2 / 3 && shirt < 3 / 5) {
return ["black", "black"];
} else if (2 / 3 < pants && shirt < 3 / 5) {
return ["gray", "black"];
} else if (pants < 2 / 3 && 3 / 5 < shirt) {
return ["black", "gray"];
} else {
return ["gray", "gray"];
}
}
Insert cell
Insert cell
one_try = dress_once()
Insert cell
Insert cell
d3
.range(100000)
.map(dress_once)
.filter((a) => a[0] == "gray" && a[1] == "gray")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// React to button pushes
{
if (simulate_button > 0) {
simulation_map.map_simulate(increase_variability);
}
}
Insert cell
trump_victory_county = {
simulate_lots_button;
let results = d3.range(1000).map(() => simulate(increase_variability));
return results.filter((o) => o["Donald Trump"] > 269).length;
}
Insert cell
simulate()
Insert cell
function simulate(add_variability) {
let results = d3.rollup(
data,
(a) => simulate_state_once(a, add_variability),
(o) => o.state
);
let tally = { "Donald Trump": 0, "Hillary Clinton": 0, state_results: [] };
results.forEach(function (o) {
if (o) {
tally[o.candidate] = tally[o.candidate] + o.votes;
tally.state_results.push(o);
}
});
return tally;
}
Insert cell
function simulate_state_once(data, shift) {
let hil_info = data.find((o) => o.candidate == "Hillary Clinton");
let don_info = data.find((o) => o.candidate == "Donald Trump");
let p1 = hil_info.winprob;
let p2 = don_info.winprob;
if (shift) {
let d = p1 - p2;
let p11 = p1;
let p22 = p2;
let s = Math.random() / 4;
p1 = p11 * (1 - s) + p22 * s;
p2 = p22 * (1 - s) + p11 * s;
}
let p = Math.random();
if (p < p1) {
return Object.assign({}, hil_info);
} else if (p1 < p && p < p1 + p2) {
return Object.assign({}, don_info);
} else {
return null;
}
}
Insert cell
Insert cell
data = {
let data = await FileAttachment("fiveThirtyEightStateAnalysis.csv").csv({
typed: true
});
data.forEach((o) => (o.winprob = o.winprob / 100));

return data;
}
Insert cell
import { stateShapes } from "@observablehq/us-county-datasets"
Insert cell
tippy_style = html`<link rel="stylesheet" href="${await require.resolve(
`tippy.js/themes/light-border.css`
)}">`
Insert cell
tippy = require("tippy.js")
Insert cell
default_parameters = ({
p1: 0.48, // The proportion of voters who lke Candidate 1
p2: 0.45, // The proportion of voters who lke Candidate 2
n: 1000, // The sample size
})
Insert cell
function pdf(parameters) {
let m0 = parameters.p1 - parameters.p2;
let s = Math.sqrt((parameters.p1 + parameters.p2 - m0 ** 2) / parameters.n);
let pdf = (x) =>
Math.exp(-((x - m0) ** 2 / (2 * s ** 2))) / (Math.sqrt(2 * Math.PI) * s);

pdf.m0 = m0;
pdf.s = s;

return pdf;
}
Insert cell
function integrate(ff, a, b) {
return adaptiveSimpson(ff, a, b, {
tolerance: 1e-11,
maxDepth: 8,
minDepth: 7
});
}
Insert cell
import { adaptiveSimpson } from "@rreusser/integration@3056"
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