Public
Edited
Sep 18, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
numTrials = 100;
Insert cell
initialCash = 500
Insert cell
trials = d3.range(numTrials).map(trial => genGBM(mu, sigma, 1, 0.001, initialCash))
Insert cell
data = trials.map((data, trial) => {
return data.map((y,x) => ({x, y, trial, result: data[data.length-1]}))
}).flat()
Insert cell

pctPositive = trials.filter(data => data[data.length-1] > initialCash).length / trials.length
Insert cell
function genGBM(mu, sigma, T, dt, S0) {
// Number of time steps
const N = Math.floor(T / dt);

// Generate random noise
const dW = Array(N)
.fill(0)
.map(() => Math.sqrt(dt) * d3.randomNormal(0, 1)());
// Compute stock price dynamics
const S = Array(N).fill(0);
S[0] = S0
for (let i = 1; i < S.length; ++i) {
S[i] = S[i - 1] * Math.exp((mu -(sigma ** 2) / 2) * dt + sigma * dW[i - 1])
}

return S;
}
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