Follow ______ on twitter.

3.1.1 Introduction to Stochastic Modeling

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Geometric Brownian Motion of Asset Prices",
  "width": 400,
  "height": 300,
  "data": {
    "sequence": {
      "start": 0,
      "stop": 1260,
      "step": 1,
      "as": "seq"
    }
  },
  "transform": [
    {
      "calculate": "floor(datum.seq / 252)",
      "as": "stockIndex"
    },
    {
      "calculate": "datum.seq % 252",
      "as": "time"
    },
    {
      "calculate": "['Stock A', 'Stock B', 'Stock C', 'Stock D', 'Stock E'][datum.stockIndex]",
      "as": "asset"
    },
    {
      "calculate": "[100, 120, 90, 110, 130][datum.stockIndex]",
      "as": "initialPrice"
    },
    {
      "calculate": "[0.05, 0.2, 0.07, 0.02, 0.11][datum.stockIndex]",
      "as": "mu"
    },
    {
      "calculate": "[0.05, 0.35, 0.25, 0.1, 0.1][datum.stockIndex]",
      "as": "sigma"
    },
    {
      "calculate": "datum.initialPrice * exp((datum.mu - 0.5 * datum.sigma * datum.sigma) * (datum.time / 252) + datum.sigma * sqrt(datum.time / 252) * random())",
      "as": "price"
    },
    {
      "calculate": "max(0, min(datum.price, datum.initialPrice * 5))",
      "as": "boundedPrice"
    },
    {
      "calculate": "datum.asset + ' (μ=' + datum.mu + ', σ=' + datum.sigma + ')'",
      "as": "assetInfo"
    }
  ],
  "mark": "line",
  "encoding": {
    "x": {
      "field": "time",
      "type": "quantitative",
      "title": "Time (Days)"
    },
    "y": {
      "field": "boundedPrice",
      "type": "quantitative",
      "title": "Price",
      "scale": {
        "zero": false
      }
    },
    "color": {
      "field": "assetInfo",
      "type": "nominal",
      "title": "Asset (μ, σ)"
    },
    "strokeDash": {
      "field": "assetInfo",
      "type": "nominal",
      "title": "Asset (μ, σ)"
    }
  }
}