Follow ______ on twitter.

8.1.1 Application of Monte Carlo Simulation

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Monte Carlo Simulation for High Risk Loan Portfolio - 100 Scenarios across 12 Months",
  "data": {
    "sequence": {
      "start": 0,
      "stop": 1200,
      "step": 1,
      "as": "index"
    }
  },
  "transform": [
    {
      "calculate": "floor(datum.index / 12) + 1",
      "as": "scenario"
    },
    {
      "calculate": "(datum.index % 12) + 1",
      "as": "month"
    },
    {
      "calculate": "0.01 + (0.05 - 0.01) * random()",
      "as": "default_rate"
    },
    {
      "window": [
        {
          "op": "sum",
          "field": "default_rate",
          "as": "cumulative_default_rate"
        }
      ],
      "groupby": [
        "scenario"
      ],
      "sort": [
        {
          "field": "month",
          "order": "ascending"
        }
      ],
      "frame": [
        null,
        0
      ]
    },
    {
      "calculate": "round(1000 * datum.cumulative_default_rate)",
      "as": "cumulative_defaults"
    },
    {
      "calculate": "cumulativeNormal(datum.cumulative_defaults, 150, 50)",
      "as": "u1"
    },
    {
      "calculate": "cumulativeNormal(sampleNormal())",
      "as": "u2"
    },
    {
      "calculate": "sqrt(0.25) * quantileNormal(datum.u1) + sqrt(0.75) * quantileNormal(datum.u2)",
      "as": "correlated_normal"
    },
    {
      "calculate": "cumulativeNormal(datum.correlated_normal)",
      "as": "u_correlated"
    },
    {
      "calculate": "min(quantileLogNormal(datum.u_correlated, -1.2, 0.3),1)",
      "as": "lgd"
    },
    {
      "calculate": "datum.cumulative_default_rate * datum.lgd",
      "as": "loss_rate"
    }
  ],
  "hconcat": [
    {
      "width": 250,
      "height": 250,
      "title": "Cumulative Defaults",
      "mark": "line",
      "encoding": {
        "x": {
          "field": "month",
          "type": "quantitative",
          "title": "Month"
        },
        "y": {
          "field": "cumulative_defaults",
          "type": "quantitative",
          "title": "Cumulative Defaults"
        },
        "color": {
          "field": "scenario",
          "type": "nominal",
          "legend": null
        }
      }
    },
    {
      "width": 250,
      "height": 250,
      "title": "Cumulative Default Rates (T=12)",
      "transform": [
        {
          "filter": "datum.month == 12"
        }
      ],
      "mark": "bar",
      "encoding": {
        "x": {
          "field": "cumulative_default_rate",
          "bin": true,
          "type": "quantitative",
          "title": "Final Cumulative Default Rates"
        },
        "y": {
          "aggregate": "count",
          "type": "quantitative",
          "title": "Frequency"
        }
      }
    },
    {
      "width": 250,
      "height": 250,
      "title": "Conditional LGDs (LGD vs. Cumulative Defaults)",
      "mark": "point",
      "encoding": {
        "x": {
          "field": "cumulative_defaults",
          "type": "quantitative",
          "title": "Cumulative Defaults"
        },
        "y": {
          "field": "lgd",
          "type": "quantitative",
          "title": "LGD"
        },
        "color": {
          "field": "scenario",
          "type": "nominal",
          "legend": null
        }
      }
    },
    {
      "width": 250,
      "height": 250,
      "title": "Loss Rates",
      "mark": "line",
      "encoding": {
        "x": {
          "field": "month",
          "type": "quantitative",
          "title": "Month"
        },
        "y": {
          "aggregate": "mean",
          "field": "loss_rate",
          "type": "quantitative",
          "title": "Average Loss Rate"
        },
        "color": {
          "field": "scenario",
          "type": "nominal",
          "legend": null
        }
      }
    }
  ]
}