Follow ______ on twitter.

8.2.3 Bayesian Modeling in Insurance and Healthcase

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Visual representation of prior, likelihood, and posterior hazard rate distributions.",
  "width": 400,
  "height": 300,
  "data": {
    "sequence": {
      "start": 0,
      "stop": 20,
      "step": 0.1,
      "as": "x"
    }
  },
  "transform": [
    {
      "calculate": "0.1 * exp(-0.1 * datum.x)",
      "as": "Prior"
    },
    {
      "calculate": "0.2 * exp(-0.2 * datum.x)",
      "as": "Likelihood"
    },
    {
      "calculate": "0.15 * exp(-0.15 * datum.x)",
      "as": "Posterior"
    },
    {
      "fold": [
        "Prior",
        "Likelihood",
        "Posterior"
      ],
      "as": [
        "Distribution",
        "HazardRate"
      ]
    }
  ],
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {
          "field": "x",
          "type": "quantitative"
        },
        "y": {
          "field": "HazardRate",
          "type": "quantitative"
        },
        "color": {
          "field": "Distribution",
          "type": "nominal",
          "legend": {
            "title": "Distributions"
          }
        }
      }
    },
    {
      "transform": [
        {
          "filter": "datum.Distribution === 'Prior'"
        },
        {
          "window": [
            {
              "op": "max",
              "field": "HazardRate",
              "as": "maxHazardRate"
            }
          ],
          "frame": [
            null,
            null
          ]
        }
      ],
      "layer": [
        {
          "mark": {
            "type": "rule",
            "color": "#e41a1c",
            "strokeDash": [
              4,
              2
            ],
            "strokeWidth": 1
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "fontSize": 16,
            "font": "Helvetica",
            "fontWeight": 100
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        }
      ]
    },
    {
      "transform": [
        {
          "filter": "datum.Distribution === 'Likelihood'"
        },
        {
          "window": [
            {
              "op": "max",
              "field": "HazardRate",
              "as": "maxHazardRate"
            }
          ],
          "frame": [
            null,
            null
          ]
        }
      ],
      "layer": [
        {
          "mark": {
            "type": "rule",
            "color": "royalblue",
            "strokeDash": [
              4,
              2
            ],
            "strokeWidth": 1
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "fontSize": 16,
            "font": "Arial",
            "fontWeight": 200
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        }
      ]
    },
    {
      "transform": [
        {
          "filter": "datum.Distribution === 'Posterior'"
        },
        {
          "window": [
            {
              "op": "max",
              "field": "HazardRate",
              "as": "maxHazardRate"
            }
          ],
          "frame": [
            null,
            null
          ]
        }
      ],
      "layer": [
        {
          "mark": {
            "type": "rule",
            "color": "orange",
            "strokeDash": [
              4,
              2
            ],
            "strokeWidth": 1
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "fontSize": 16,
            "font": "Arial",
            "fontWeight": 200
          },
          "encoding": {
            "x": {
              "datum": 0
            },
            "y": {
              "field": "maxHazardRate"
            }
          }
        }
      ]
    }
  ],
  "encoding": {
    "x": {
      "field": "x",
      "type": "quantitative",
      "title": "Time"
    },
    "y": {
      "field": "HazardRate",
      "type": "quantitative",
      "title": "Hazard Rate"
    }
  }
}