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

            
              <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 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: https://github.com/rasmusab/bayes.js/blob/master/distributions.js

// 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>' +
    '</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
🕑 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 ..................

Console