Skip to content

Instantly share code, notes, and snippets.

@ryu577
Created November 7, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu577/420d6a9e56b21d3e7038660e3a9a88f5 to your computer and use it in GitHub Desktop.
Save ryu577/420d6a9e56b21d3e7038660e3a9a88f5 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.stats import lognorm, fisk
import matplotlib.pyplot as plt
def plot_bias(prcnt=90, dist=lognorm):
real_val = dist.ppf(prcnt/100, 1, 0)
for sampl in np.arange(11, 77, 4):
errs = []
ests = []
for _ in range(100000):
x = dist.rvs(1, 0, size=sampl)
#est_val = estimate_median(x)
est_val = np.percentile(x, prcnt)
err = (real_val-est_val)/real_val
errs.append(err)
ests.append(est_val)
print(np.mean(errs))
plt.hist(ests, bins=np.arange(0, 3, .1))
plt.axvline(real_val, label="actual percentile", color="black")
plt.axvline(np.mean(ests),
label="avg estimated value of percentile on sample size: "
+ str(sampl), color="purple")
plt.axvline(np.percentile(ests, 50),
label=("Median"
"estimated value of "
"percentile on sample size: ")
+ str(sampl), color="orange")
plt.legend()
plt.title("Sample size = " + str(sampl))
plt.savefig('plots/sample_' + str(sampl) + '.png')
plt.close()
print('processed sample size ' + str(sampl))
if __name__ == "__main__":
plot_bias(50, dist=fisk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment