Last active
October 23, 2019 16:45
-
-
Save chaipi-chaya/9eb72978dbbfd7fa4057b493cf6a32e7 to your computer and use it in GitHub Desktop.
Illustration with Python: Chebyshev’s Inequality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
import matplotlib.pyplot as plt | |
# Step 1 | |
# create a population with a gamma distribution | |
shape, scale = 2., 2. # mean=4, std=2*sqrt(2) | |
mu = shape*scale # mean and standard deviation | |
sigma = scale*np.sqrt(shape) | |
s = np.random.gamma(shape, scale, 1000000) | |
# Step 2 | |
# sample 10,000 sample | |
rs = random.choices(s, k=10000) | |
# Step 3 | |
# set k | |
ks = [0.1,0.5,1.0,1.5,2.0,2.5,3.0] | |
# prob list | |
probs = [] | |
# for each k | |
for k in ks: | |
# start count | |
c = 0 | |
# for each data sample | |
for i in rs: | |
# count if far from mean in k standard deviation | |
if abs(i - mu) > k * sigma : | |
c += 1 | |
# probability of sample has a distance from an expected value larger than k standrad deviation | |
probs.append(c/10000) | |
# Step 4 | |
# set figure size | |
plot = plt.figure(figsize=(20,10)) | |
# plot each probability | |
plt.plot(ks,probs, marker='o') | |
# show plot | |
plot.show() | |
# print each probability | |
print("Probability of a sample far from mean more than k standard deviation:") | |
for i, prob in enumerate(probs): | |
print("k:" + str(ks[i]) + ", probability: " \ | |
+ str(prob)[0:5] + \ | |
" | in theory, probability should less than: " \ | |
+ str(1/ks[i]**2)[0:5]) | |
input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment