css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

  <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 https://github.com/rasmusab/bayes.js running a standard Normal model. 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
// The heights of the last ten American presidents in cm, from Kennedy to Obama 
var data = [183, 192, 182, 183, 177, 185, 188, 188, 182, 185];

var params = {
  mu: {type: "real"},
  sigma: {type: "real", lower: 0}};

var log_post = function(state, data) {
  var log_post = 0;
  // Priors
  log_post += ld.norm(state.mu, 0, 100);
  log_post += ld.unif(state.sigma, 0, 100);
  // Likelihood
  for(var i = 0; i < data.length; i++) {
    log_post += ld.norm(data[i], state.mu, state.sigma);
  return log_post;

// Initializing the sampler and generate a sample of size 1000
var sampler =  new mcmc.AmwgSampler(params, log_post, data);
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() {


🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................