Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Some Python code I did for a blog article.
import scipy.stats as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def z_score(x, m, s):
return (x - m) / s
def p(x, m, s):
z = z_score(x, m, s)
return st.norm.cdf(z)
def main():
# z-scores
print(z_score(95, 100, 15), z_score(130, 100, 15), z_score(7, 100, 15))
# We should see -0.3333333333333333 2.0 -6.2 or 1/3 deviation below average, 2 above and 6.2 below.
# probabilities
scores = np.arange(60, 161, 20)
z_scores = list(map(lambda x: z_score(x, 100, 15), scores))
less_intelligent = list(map(lambda x: p(x, 100, 15), scores))
df = pd.DataFrame()
df['test_score'] = scores
df['z_score'] = z_scores
df['less_intelligent'] = less_intelligent
print(df)
# visualization
get_ipython().magic('matplotlib inline')
mu, sigma = 100, 15. # mean and standard deviation
s = sorted(np.random.normal(mu, sigma, 1000))
count, bins, ignored = plt.hist(s, 30, normed=True)
plt.style.use('ggplot')
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
linewidth=2)
plt.show()
# IQ Score of 7
z = z_score(7, 100, 15)
prob = p(7, 100, 15)
rounded_prob = round(prob, 15)
print("The z-score {0} and probability {1} of a test score of 7.".format(z, rounded_prob))
instances_per_billion = round((1/prob) / 1000000000, 2)
people_on_the_planet = 7.125 # billion
instances_on_the_planet = people_on_the_planet / instances_per_billion
instances_on_the_planet
# Likely Voters
votes = pd.Series([46.3, 45.3, 46.3, 46.3, 49.4, 47.8, 42.7, 43.3, 49.0, 47.7, 48.3, 46.5, 46.5, 49.0, 48.0])
# I thought it was easier to read percentages as 46.3, but I'm converting those numbers here to fit
# in the set [0,1] as well-behaved probabilities do.
votes = votes.apply(lambda x: x / 100)
votes.describe()
hillary_z_score = st.norm.ppf(votes.mean())
hillary_z_score
iq = 15 * hillary_z_score + 100
iq
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment