Line data Source code
1 : // The heights of the last ten American presidents in cm, from Kennedy to Obama 2 1 : import * as ld from "../mentat/stats/distributions.ts"; 3 1 : import * as mcmc from "../bayesjs/mcmc.ts"; 4 : 5 1 : let data = [183, 192, 182, 183, 177, 185, 188, 188, 182, 185]; 6 : 7 1 : let params = { 8 1 : mu: { type: mcmc.ParameterType.Real }, 9 1 : sigma: { type: mcmc.ParameterType.Real, lower: 0 }, 10 1 : }; 11 : 12 23482 : function log_post(state: any, data: any) { 13 23482 : var log_post = 0; 14 : // Priors 15 23482 : log_post += ld.norm(state.mu, 0, 100); 16 23482 : log_post += ld.unif(state.sigma, 0, 100); 17 : // Likelihood 18 23482 : for (var i = 0; i < data.length; i++) { 19 258292 : log_post += ld.norm(data[i], state.mu, state.sigma); 20 23482 : } 21 23482 : return log_post; 22 1 : } // Initializing the sampler 23 : 24 1 : let sampler = new mcmc.AmwgSampler(params, log_post, data); 25 : // Burning some samples to the MCMC gods and sampling 5000 draws. 26 1 : sampler.burn(1000); 27 1 : let samples = sampler.sample(5000); 28 : 29 1 : (window as any).samples = samples;