Follow ______ on twitter.

9.2.2 Fundamentals of Monte Carlo simulations

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "hconcat": [
    {
      "title": "Normal Distribution PDF (Mean=1, SD=2)",
      "width": 200,
      "height": 200,
      "data": {
        "sequence": {
          "start": -5,
          "stop": 7,
          "step": 0.1,
          "as": "x"
        }
      },
      "transform": [
        {
          "calculate": "exp(-0.5 * pow((datum.x - 1) / 2, 2)) / (2 * sqrt(2 * PI))",
          "as": "normal_pdf"
        }
      ],
      "mark": {
        "type": "line",
        "point": false
      },
      "encoding": {
        "x": {
          "field": "x",
          "type": "quantitative",
          "title": "x"
        },
        "y": {
          "field": "normal_pdf",
          "type": "quantitative",
          "title": "Density"
        }
      }
    },
    {
      "title": "Random Sample from N(1,2)",
      "width": 200,
      "height": 200,
      "data": {
        "sequence": {
          "start": 1,
          "stop": 101,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "sampleNormal(1, 2)",
          "as": "random_normal"
        }
      ],
      "mark": "point",
      "encoding": {
        "x": {
          "field": "index",
          "type": "quantitative",
          "title": "Index"
        },
        "y": {
          "field": "random_normal",
          "type": "quantitative",
          "title": "Uniform Value"
        }
      }
    },
    {
      "title": "Histogram of 100 Samples from N(1, 2)",
      "width": 200,
      "height": 200,
      "data": {
        "sequence": {
          "start": 1,
          "stop": 101,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "1 + 2 * sampleNormal(0, 1)",
          "as": "sample_normal_50"
        }
      ],
      "mark": "bar",
      "encoding": {
        "x": {
          "bin": {
            "maxbins": 20
          },
          "field": "sample_normal_50",
          "type": "quantitative",
          "title": "Value"
        },
        "y": {
          "aggregate": "count",
          "type": "quantitative",
          "title": "Frequency"
        }
      }
    },
    {
      "title": "Histogram of 10,000 Samples from N(1, 2)",
      "width": 200,
      "height": 200,
      "data": {
        "sequence": {
          "start": 1,
          "stop": 10001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "1 + 2 * sampleNormal(0, 1)",
          "as": "sample_normal_1000"
        }
      ],
      "mark": "bar",
      "encoding": {
        "x": {
          "bin": {
            "maxbins": 50
          },
          "field": "sample_normal_1000",
          "type": "quantitative",
          "title": "Value"
        },
        "y": {
          "aggregate": "count",
          "type": "quantitative",
          "title": "Frequency"
        }
      }
    }
  ]
}