Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


  <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>

<div id="mcmc_plots_div"> </div>
<div id='text_output_div'></div>




                //A small demo of analyzing . 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:

// Setting up the data, parameter definitions and the defining the log posterior

// Data story: We caught and marked 20 fish in a lake and threw them back.
// A week later we went back and caught 20 more fish and out of them
// 5 were marked. So how many fish are in the lake?
var data = {
  n_marked: 20,
  n_captured: 20,
  n_recaptured: 5 };

var params = { n_fish: {type: "int", lower: data.n_marked} };

var norm_post = function(state, d) {
  var log_post = 0;
  // Priors
  log_post += ld.cauchy(state.n_fish, 0, 500);
  // Likelihood
  log_post += ld.hyper(d.n_recaptured, d.n_marked, state.n_fish, d.n_captured)
  return log_post;

// Initializing the sampler and generate a sample of size 1000
var sampler =  new mcmc.AmwgSampler(params, norm_post, data);
var samples = sampler.burn(100);
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 = Object.keys(params);

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>' +
  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)

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])
  sample_loop_timeout_id = setTimeout(sample_loop, 1)

var stop_sample_loop = function() {