Skip to content

Instantly share code, notes, and snippets.

@farzadhallaji
Created February 18, 2023 22:25
Show Gist options
  • Save farzadhallaji/fad30f6b20511cf5f882a15895363c9f to your computer and use it in GitHub Desktop.
Save farzadhallaji/fad30f6b20511cf5f882a15895363c9f to your computer and use it in GitHub Desktop.
standard deviation for harmonic mean
from scipy.stats import hmean
# https://stats.stackexchange.com/questions/7471/can-the-standard-deviation-be-calculated-for-harmonic-mean
harmonic_var = lambda S : (np.power(S,-1).var())/(len(S)*np.power(np.power(S,-1).mean(),4))
# example
import numpy as np
np.random.seed(100)
s = np.random.dirichlet((1,5),10)
S = s.T[1]
print("S: ", S)
print("harmonic variance S: ", harmonic_var(S))
"""
>>> S: [0.81934176 0.99756006 0.79016822 0.69804071 0.95343128 0.70649118 0.68389881 0.53041986 0.9714669 0.78779674]
>>> harmonic variance S: 0.0021948755349154346
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment