Follow ______ on twitter.

2.1.3 Harmonic Mean as a measure of central tendency

Code

Vega-Lite Chart

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "config": {
    "view": {
      "continuousWidth": 250,
      "continuousHeight": 250
    }
  },
  "data": {
    "sequence": {
      "start": 1,
      "stop": 101,
      "step": 1,
      "as": "x"
    }
  },
  "transform": [
    {
      "calculate": "exp(20 * 0.01 + 10 * sqrt(0.01) * random())",
      "as": "y"
    },
    {
      "calculate": "1 / datum.y",
      "as": "inv_y"
    }
  ],
  "hconcat": [
    {
      "title": "GBM Time Series",
      "width": 250,
      "height": 250,
      "layer": [
        {
          "mark": "line",
          "encoding": {
            "x": {
              "field": "x",
              "type": "quantitative",
              "axis": {
                "title": "Time"
              }
            },
            "y": {
              "field": "y",
              "type": "quantitative",
              "axis": {
                "title": "Value"
              },
              "scale": {
                "domain": [
                  1,
                  3.5
                ]
              }
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "purple",
            "strokeDash": [
              4,
              4
            ],
            "strokeWidth": 4
          },
          "encoding": {
            "y": {
              "aggregate": "mean",
              "field": "y"
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "teal",
            "strokeDash": [
              4,
              4
            ],
            "strokeWidth": 4
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "y": {
              "field": "harm_mean",
              "type": "quantitative"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 15,
            "dy": -10,
            "color": "purple"
          },
          "encoding": {
            "y": {
              "aggregate": "mean",
              "field": "y"
            },
            "text": {
              "aggregate": "mean",
              "field": "y",
              "format": ".2f"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 15,
            "dy": 10,
            "color": "teal"
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "y": {
              "field": "harm_mean",
              "type": "quantitative"
            },
            "text": {
              "field": "harm_mean",
              "format": ".2f"
            }
          }
        }
      ]
    },
    {
      "title": "Box Plot",
      "width": 250,
      "height": 250,
      "layer": [
        {
          "mark": {
            "type": "boxplot",
            "extent": "min-max",
            "median": {
              "color": "black"
            },
            "ticks": true
          },
          "encoding": {
            "y": {
              "field": "y",
              "type": "quantitative",
              "scale": {
                "domain": [
                  1,
                  3.5
                ]
              },
              "axis": {
                "title": "Value"
              }
            },
            "size": {
              "value": 60
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "purple",
            "strokeDash": [
              6,
              4
            ],
            "strokeWidth": 3
          },
          "encoding": {
            "y": {
              "aggregate": "mean",
              "field": "y"
            },
            "x": {
              "value": 0
            },
            "x2": {
              "value": 250
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "teal",
            "strokeDash": [
              6,
              4
            ],
            "strokeWidth": 3
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "y": {
              "field": "harm_mean",
              "type": "quantitative"
            },
            "x": {
              "value": 0
            },
            "x2": {
              "value": 250
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 65,
            "dy": -10,
            "color": "purple"
          },
          "encoding": {
            "y": {
              "aggregate": "mean",
              "field": "y"
            },
            "text": {
              "aggregate": "mean",
              "field": "y",
              "format": ".2f"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 65,
            "dy": 10,
            "color": "teal"
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "y": {
              "field": "harm_mean",
              "type": "quantitative"
            },
            "text": {
              "field": "harm_mean",
              "format": ".2f"
            }
          }
        }
      ]
    },
    {
      "title": "Histogram",
      "width": 250,
      "height": 250,
      "layer": [
        {
          "mark": "bar",
          "encoding": {
            "x": {
              "bin": {
                "maxbins": 30
              },
              "field": "y",
              "axis": {
                "title": "Value"
              },
              "scale": {
                "domain": [
                  1,
                  3.5
                ]
              }
            },
            "y": {
              "aggregate": "count",
              "axis": {
                "title": "Frequency"
              }
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "purple",
            "strokeDash": [
              4,
              4
            ],
            "strokeWidth": 4
          },
          "encoding": {
            "x": {
              "aggregate": "mean",
              "field": "y"
            }
          }
        },
        {
          "mark": {
            "type": "rule",
            "color": "teal",
            "strokeDash": [
              4,
              4
            ],
            "strokeWidth": 4
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "x": {
              "field": "harm_mean",
              "type": "quantitative"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "center",
            "dx": 0,
            "dy": -10,
            "color": "purple"
          },
          "encoding": {
            "x": {
              "aggregate": "mean",
              "field": "y"
            },
            "y": {
              "value": 0
            },
            "text": {
              "aggregate": "mean",
              "field": "y",
              "format": ".2f"
            }
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "center",
            "dx": 0,
            "dy": 10,
            "color": "teal"
          },
          "transform": [
            {
              "aggregate": [
                {
                  "op": "mean",
                  "field": "inv_y",
                  "as": "harm_mean_inv"
                }
              ],
              "groupby": []
            },
            {
              "calculate": "1 / datum.harm_mean_inv",
              "as": "harm_mean"
            }
          ],
          "encoding": {
            "x": {
              "field": "harm_mean",
              "type": "quantitative"
            },
            "y": {
              "value": 0
            },
            "text": {
              "field": "harm_mean",
              "format": ".2f"
            }
          }
        }
      ]
    }
  ]
}