Hazard Rate Distribution

Histogram over 100 people

by Tricia Seow

HTML

<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<head>
<!-- Plotly.js -->
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
        </head>
        <body>
        <!-- Plotly chart will be drawn inside this DIV -->
        <div id="myDiv"></div>
        <div id="myDiv2"></div>
        <script>
        /* JAVASCRIPT CODE GOES HERE */
        </script>
        </body>

JavaScript

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

// function to caluclate experiment condition
function stim_locVariables(nTrialsPerCond,stim_HazRate){

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

      // Change of reference location for stable block
      for (t = 1; t < nTrialsPerCond + 1 ; t++){

        if (Math.random() <= stim_HazRate || mod(t,nTrialsPerCond) == 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_changePoint = stim_changePoint.concat(1); //to note which trial the CPP happens
          stim_refLocMean = stim_refLocMean.concat(currLoc);
        }//to note the position it changes to
        else  {
          currLoc = Math.round(stim_refLocMean[t-2]);
          stim_changePoint = stim_changePoint.concat(0);
          stim_refLocMean=stim_refLocMean.concat(currLoc);//the dist mean stays the same as the prev trial when CPP doesnt happen
        }
      }

      return  stim_changePoint 
    }

var stim_std = 12;
var stim_cirNPos = 360;

// function for adding two numbers. Easy!
const add = (a, b) =>
  a + b
  
  // function for adding two numbers. Easy!
const sub = (a, b) =>
  a - b

var locVarVolNumTotal= [];
var locVarMidNumTotal=[];
var locVarStaNumTotal=[];
var locVarVolNumTotalHaz= [];
var locVarMidNumTotalHaz=[];
var locVarStaNumTotalHaz=[];
var locAll=[];

var numTrials= 100;
var numSubjs = 500;

for  (w = 1; w < numSubjs ; w++){

var locVarVol = stim_locVariables(numTrials,.125);
var locVarVolNum =locVarVol.reduce(add);
var locVarVolHaz = locVarVolNum/numTrials;
locVarVolNumTotal...