Published
Edited
Apr 6, 2020
Insert cell
md`# Risk and Ambiguity trial data generation`
Insert cell
md `## Initialization

First we set up some variables`
Insert cell
trials = [] // this will hold our final trials
Insert cell
// set RISK variables
RISK = [0.25, 0.5, 0.75]
Insert cell
// set AMB variables
AMB = [0.24, 0.5, 0.74]
Insert cell
// rewards
REWARD = [5, 6, 7, 8, 10, 12, 14, 16, 19, 23, 27, 31, 37, 44, 52, 61, 73, 86, 101, 120]
Insert cell
md `## Generate Risk trials first`
Insert cell
RISK.forEach(risk => {
REWARD.forEach(rew => {
// initialize riskSide and winningColor to left and red
// these are flipped for every trial
let riskSide = 'left';
let winningColor = 'red';
(riskSide === 'left') ? riskSide = 'right' : riskSide = 'left';
(winningColor === 'red') ? winningColor = 'blue' : winningColor = 'red';

// make a risk trial and add it to the list
let trial = { reward: rew, uncertainty: risk, type: 'risk', winning_color: winningColor }
trials.push(trial);
})
})
Insert cell
md `### Result of risk trial generation is 60 trials`
Insert cell
// now trials is
trials
Insert cell
md `## Now generate the ambiguity trials`
Insert cell
AMB.forEach(amb => {
let winningColor = 'red' // we start with red
REWARD.forEach(rew => {
// again we alternate between blue and red being the winning color
(winningColor === 'red') ? winningColor = 'blue' : winningColor = 'red';
let trial = { reward: rew, uncertainty: amb, type: 'ambiguity', winning_color: winningColor }
trials.push(trial);
})
})
Insert cell
md `

Trials now contains another 60 trials, these are the ambiguity trials

The final step is to shuffle them, we use a 'fisher-yates' shuffle:
`


Insert cell
shuffle = (a) => {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
Insert cell
md `the final step is shuffling the trials`
Insert cell
shuffle(trials)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more