Calculating CPs in Predictive Inference Task

by Tricia Seow

HTML

<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.min.js"></script>

JavaScript

//function to calculate normal distribution
// Standard Normal variate using Box-Muller transform.
function randn() {
    var u = 0, v = 0;
    while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
    while(v === 0) v = Math.random();
    return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}


//summing numbers in arrays together
Array.prototype.SumArray = function (arr) {
        var sum = this.map(function (num, idx) {
          return num + arr[idx];
        });

        return sum;
    }

//modulus after division functions
function mod(x,y){
  var mod = x - Math.floor(x/y)*y;
  return mod
}


var nTrialsPerBlock=90;
var stim_cirNPos=360;
var stim_volHazRate=0.125;
var stim_staHazRate=0.005;

// where the dots will land is precaluclated beore experiment starts and positions are all stored in the vars below
     var stim_refLocMeanSta=[]; //where the current mean is
     var stim_changePointSta=[]; // 0 or 1. whether it is a CP trial
     var stim_refLocMeanVol=[];
     var stim_changePointVol=[];
     var stim_locDiffSta =[]; //the deviation from mean of 0
     var stim_locDiffVol =[]; //the deviation from mean of 0


     // Change of reference location for stable block
     for (t = 1; t < nTrialsPerBlock + 1 ; t++){
     stim_locDiffSta      = stim_locDiffSta.concat(Math.round(randn()*12)); // Draw particles location deviation around a zero mean

 
     if (Math.random() <= stim_staHazRate || mod(t,nTrialsPerBlock) == 1) { //this is where the CPP comes in, also changes every start of new block (when nTrialsperBlock=1)
                 var currLoc = Math.round((stim_cirNPos-1)*Math.random()); //its ok to be zero? since its 0 to 359 here
                 stim_changePointSta = stim_changePointSta.concat(1); //to note which trial the CPP happens
                 stim_refLocMeanSta = stim_refLocMeanSta.concat(currLoc);
               }//to note the position it changes to
     else  {
                currLoc =...