cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <p>
  <button onclick="sample_loop()">Start sampling</button>
  <button onclick="stop_sample_loop()">Stop sampling and plot histograms</button>
  <button onclick="clear_samples()">Clear samples</button>
</p>

<div id="mcmc_plots_div"> </div>
<div id='text_output_div'></div>
            
          
!
            
              //A small demo of https://github.com/rasmusab/bayes.js implementing the "Pumps" example from the BUGS project: http://www.openbugs.net/Examples/Pumps.html. Just click "start sampling" to eh... start sampling...

// Feel free to change this model and/or data and see what happens! :) What distributions are available can be found here: https://github.com/rasmusab/bayes.js/blob/master/distributions.js

// Setting up the data, parameter definitions and the defining the log posterior
var data = {
  t: [94.3, 15.7, 62.9, 126, 5.24, 31.4, 1.05, 1.05, 2.1, 10.5],
  x: [5, 1, 5, 14, 3, 19, 1, 1, 4, 22], 
  N: 10 }; 

var params = {
  alpha: {type: "real", lower: 0},
  beta:  {type: "real", lower: 0},
  theta: {type: "real", lower: 0, dim: [data.N]}};

var log_post = function(s, data) {
  var log_post = 0;
  // Priors
  log_post += ld.exp(s.alpha, 1);
  log_post += ld.gamma(s.beta, 0.1, 1.0);
  for(var i = 0; i < data.N; i++) {
    log_post += ld.gamma(s.theta[i], s.alpha, s.beta)
    var lambda = s.theta[i] * data.t[i];
    log_post += ld.pois(data.x[i], lambda);
  } 
  return log_post;
};

// Initializing the sampler and generate a sample of size 1000
var sampler =  new mcmc.AmwgSampler(params, log_post, data);
sampler.burn(1000);
var samples = sampler.sample(1);

//// Below is just the code to run the sampler and
//// to plot the samples. It's somewhat of a hack...
////////////////////////////////////////////////////

// Setting up the plots
var plot_margins =  {l: 40, r: 10, b: 40, t: 40, pad: 4}

var param_names = Object.keys(params);
var params_to_plot = ["alpha", "beta"];

for(var i = 0; i < params_to_plot.length; i++) {
  var param = params_to_plot[i];
 $("div#mcmc_plots_div").append('<div>' +
    '<div id = "' + param + "_trace_div" + 
    '" style="width:350px;height:250px;display: inline-block;"></div>' +
    '<div id = "' +  param + "_hist_div" + 
    '" style="width:350px;height:250px;display: inline-block;"></div>' +
    '</div>')
  Plotly.plot( $("div#" + param + "_trace_div")[0], [{y: samples[param] }], 
            {margin: plot_margins, title: "Traceplot of " + param});
  Plotly.plot( $("div#" +  param + "_hist_div")[0], [{x: samples[param], type: 'histogram' }], 
            {margin: plot_margins, title: "Posterior of " + param });
}

var update_trace_plots = function() {
  for(var i = 0; i < params_to_plot.length; i++) {
    var param = params_to_plot[i]
    Plotly.restyle($("div#" + param + "_trace_div")[0], {y: [samples[param]]})
  }
}

var update_histograms = function() {
  for(var i = 0; i < params_to_plot.length; i++) {
    var param = params_to_plot[i]
    Plotly.restyle($("div#" + param + "_hist_div")[0], {x: [samples[param]], xbins: {}})
  }
}

// Below are the functions that enables starting and stopping the 
// sampling using the buttons

var clear_samples = function() {
  samples = sampler.sample(1)
  update_trace_plots();
  update_histograms();
}

var sample_loop_timeout_id;
var sample_loop = function() {
  var n_samples = Math.min(250, Math.ceil(samples[param_names[0]].length / 10) );
  var more_samples = sampler.sample(n_samples);
  for(var i = 0; i < param_names.length; i++) {
    var param = param_names[i]
    Array.prototype.push.apply(samples[param], more_samples[param])
  }
  update_trace_plots();
  sample_loop_timeout_id = setTimeout(sample_loop, 1)
}

var stop_sample_loop = function() {
  clearTimeout(sample_loop_timeout_id)
  update_trace_plots();
  update_histograms();
}

clear_samples()

            
          
!
999px
Loading ..................

Console