Published
Edited
Nov 13, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function step_automaton(a, coin) {
let state = a.states[a.state_index]
a.state_index = (coin === 'heads') ? state.heads : state.tails
if (a.state_index == a.states.length) {
a.state_index = all_heads(a.flips) ? 0 : 1
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
coinflip = (p) => (Math.random() < p) ? 'heads' : 'tails'
Insert cell
Insert cell
Insert cell
function run_trial() {
let a = automaton
a.state_index = 0
let coin = Math.round(Math.random())
let p = [.99, .9][coin]
return range(max_steps).map(() => {
step_automaton(a, coinflip(p))
return a.states[a.state_index].decision == coin
})
}
Insert cell
accuracy = {
let a = (model == 'simple deterministic') ? simple_deterministic_automaton:
(model == 'complex deterministic') ? complex_deterministic_automaton:
probabilistic_automaton
let trial_results = range(trials).map(run_trial)
let accuracy = []
for (let i in range(max_steps)) {
let step_results = trial_results.map(trial => trial[i])
accuracy[ i ] = {
accuracy: step_results.reduce((tot, res) => tot + res) / trials,
steps: i
}
}
return accuracy
}
Insert cell
Insert cell
all_heads = (n) => range(n).map(Math.random).every(x => x > .5)
Insert cell
Insert cell
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