Follow ______ on twitter.

3.5.4 Interpreting Data Using Probability Distributions

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Comparison of Unimodal, Bimodal, and Trimodal LogNormal Distributions",
  "hconcat": [
    {
      "title": "Unimodal",
      "data": {
        "sequence": {
          "start": 1,
          "stop": 1001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "sampleLogNormal(0, 0.5)",
          "as": "value"
        }
      ],
      "layer": [
        {
          "mark": "bar",
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative",
              "bin": {
                "step": 0.2
              },
              "axis": {
                "title": "Value",
                "values": [
                  0,
                  2,
                  4,
                  6,
                  8
                ]
              },
              "scale": {
                "domain": [
                  0,
                  8
                ]
              }
            },
            "y": {
              "aggregate": "count",
              "type": "quantitative",
              "axis": {
                "title": "Count"
              }
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "color": "red"
          },
          "transform": [
            {
              "density": "value",
              "bandwidth": 0.2,
              "extent": [
                0,
                8
              ],
              "as": [
                "value",
                "density"
              ]
            },
            {
              "calculate": "1000 * 0.2 * datum.density",
              "as": "scaled_density"
            }
          ],
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative"
            },
            "y": {
              "field": "scaled_density",
              "type": "quantitative"
            }
          }
        }
      ],
      "width": 200,
      "height": 200
    },
    {
      "title": "Bimodal",
      "data": {
        "sequence": {
          "start": 1,
          "stop": 1001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "random() < 0.7 ? sampleLogNormal(-0.5, 0.3) : sampleLogNormal(1, 0.3)",
          "as": "value"
        }
      ],
      "layer": [
        {
          "mark": "bar",
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative",
              "bin": {
                "step": 0.2
              },
              "axis": {
                "title": "Value",
                "values": [
                  0,
                  2,
                  4,
                  6,
                  8
                ]
              },
              "scale": {
                "domain": [
                  0,
                  8
                ]
              }
            },
            "y": {
              "aggregate": "count",
              "type": "quantitative",
              "axis": {
                "title": "Count"
              }
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "color": "red"
          },
          "transform": [
            {
              "density": "value",
              "bandwidth": 0.2,
              "extent": [
                0,
                8
              ],
              "as": [
                "value",
                "density"
              ]
            },
            {
              "calculate": "1000 * 0.2 * datum.density",
              "as": "scaled_density"
            }
          ],
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative"
            },
            "y": {
              "field": "scaled_density",
              "type": "quantitative"
            }
          }
        }
      ],
      "width": 200,
      "height": 200
    },
    {
      "title": "Trimodal",
      "data": {
        "sequence": {
          "start": 1,
          "stop": 1001,
          "step": 1,
          "as": "index"
        }
      },
      "transform": [
        {
          "calculate": "random() < 0.6 ? sampleLogNormal(-0.5, 0.2) : (random() < 0.5 ? sampleLogNormal(0.5, 0.2) : sampleLogNormal(1.5, 0.2))",
          "as": "value"
        }
      ],
      "layer": [
        {
          "mark": "bar",
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative",
              "bin": {
                "step": 0.2
              },
              "axis": {
                "title": "Value",
                "values": [
                  0,
                  2,
                  4,
                  6,
                  8
                ]
              },
              "scale": {
                "domain": [
                  0,
                  8
                ]
              }
            },
            "y": {
              "aggregate": "count",
              "type": "quantitative",
              "axis": {
                "title": "Count"
              }
            }
          }
        },
        {
          "mark": {
            "type": "line",
            "color": "red"
          },
          "transform": [
            {
              "density": "value",
              "bandwidth": 0.2,
              "extent": [
                0,
                8
              ],
              "as": [
                "value",
                "density"
              ]
            },
            {
              "calculate": "1000 * 0.2 * datum.density",
              "as": "scaled_density"
            }
          ],
          "encoding": {
            "x": {
              "field": "value",
              "type": "quantitative"
            },
            "y": {
              "field": "scaled_density",
              "type": "quantitative"
            }
          }
        }
      ],
      "width": 200,
      "height": 200
    }
  ],
  "config": {
    "view": {
      "stroke": null
    }
  }
}