Follow ______ on twitter.

3.1.2 Stochastic Processes

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "HConcat of Discrete Time, Continuous Time, and Markov Process Outputs",
  "hconcat": [
    {
      "width": 250,
      "height": 250,
      "title": "Discrete Time Stochastic Process (Coin Toss)",
      "data": {
        "sequence": {
          "start": 0,
          "stop": 20,
          "step": 1
        }
      },
      "transform": [
        {
          "calculate": "random() > 0.5 ? 1 : 0",
          "as": "y"
        }
      ],
      "mark": {
        "type": "line",
        "point": true
      },
      "encoding": {
        "x": {
          "field": "data",
          "type": "quantitative",
          "title": "Toss Number"
        },
        "y": {
          "field": "y",
          "type": "quantitative",
          "title": "Outcome (0=Tails, 1=Heads)",
          "scale": {
            "domain": [
              -0.1,
              1.1
            ]
          }
        }
      }
    },
    {
      "width": 250,
      "height": 250,
      "title": "Continuous Time Stochastic Process (Brownian Motion)",
      "data": {
        "sequence": {
          "start": 0,
          "stop": 100,
          "step": 1
        }
      },
      "transform": [
        {
          "calculate": "random() - 0.5",
          "as": "random"
        },
        {
          "window": [
            {
              "op": "sum",
              "field": "random",
              "as": "y"
            }
          ],
          "frame": [
            null,
            0
          ]
        }
      ],
      "mark": "line",
      "encoding": {
        "x": {
          "field": "data",
          "type": "quantitative",
          "title": "Time"
        },
        "y": {
          "field": "y",
          "type": "quantitative",
          "title": "Value",
          "scale": {
            "zero": false
          }
        }
      }
    },
    {
      "width": 250,
      "height": 250,
      "title": "Markov Process (Simple Weather Model)",
      "data": {
        "sequence": {
          "start": 0,
          "stop": 20,
          "step": 1
        }
      },
      "transform": [
        {
          "window": [
            {
              "op": "lag",
              "field": "state",
              "as": "prevState"
            }
          ]
        },
        {
          "calculate": "datum.prevState == null ? floor(random() * 3) : (random() < 0.7 ? datum.prevState : floor(random() * 3))",
          "as": "state"
        }
      ],
      "layer": [
        {
          "mark": {
            "type": "point",
            "filled": true,
            "size": 100
          },
          "encoding": {
            "x": {
              "field": "data",
              "type": "quantitative",
              "title": "Day"
            },
            "y": {
              "field": "state",
              "type": "ordinal",
              "title": "Weather State",
              "scale": {
                "domain": [
                  0,
                  1,
                  2
                ],
                "range": [
                  200,
                  100,
                  0
                ]
              },
              "axis": {
                "labelExpr": "datum.value == 0 ? 'Rainy' : datum.value == 1 ? 'Cloudy' : 'Sunny'"
              }
            },
            "color": {
              "field": "state",
              "type": "ordinal",
              "scale": {
                "domain": [
                  0,
                  1,
                  2
                ],
                "range": [
                  "blue",
                  "gray",
                  "yellow"
                ]
              },
              "legend": null
            }
          }
        }
      ]
    }
  ]
}