# HIDDEN
from datascience import *
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
import numpy as np
# HIDDEN
def standard_units(any_numbers):
"Convert any array of numbers to standard units."
return (any_numbers - np.mean(any_numbers))/np.std(any_numbers)
def correlation(t, x, y):
return np.mean(standard_units(t.column(x))*standard_units(t.column(y)))
def slope(table, x, y):
r = correlation(table, x, y)
return r * np.std(table.column(y))/np.std(table.column(x))
def intercept(table, x, y):
a = slope(table, x, y)
return np.mean(table.column(y)) - a * np.mean(table.column(x))
galton = Table.read_table('galton.csv')
galton
family | father | mother | midparentHeight | children | childNum | gender | childHeight |
---|---|---|---|---|---|---|---|
1 | 78.5 | 67 | 75.43 | 4 | 1 | male | 73.2 |
1 | 78.5 | 67 | 75.43 | 4 | 2 | female | 69.2 |
1 | 78.5 | 67 | 75.43 | 4 | 3 | female | 69 |
1 | 78.5 | 67 | 75.43 | 4 | 4 | female | 69 |
2 | 75.5 | 66.5 | 73.66 | 4 | 1 | male | 73.5 |
2 | 75.5 | 66.5 | 73.66 | 4 | 2 | male | 72.5 |
2 | 75.5 | 66.5 | 73.66 | 4 | 3 | female | 65.5 |
2 | 75.5 | 66.5 | 73.66 | 4 | 4 | female | 65.5 |
3 | 75 | 64 | 72.06 | 2 | 1 | male | 71 |
3 | 75 | 64 | 72.06 | 2 | 2 | female | 68 |
... (924 rows omitted)
correlation(galton, 'father', 'mother')
0.060366119036644697
def galton_multiple_mse(a, b, c):
y = galton.column('childHeight')
fitted = a*galton.column('father') + b*galton.column('mother') + c
return np.mean((y - fitted) ** 2)
best_quad = minimize(galton_multiple_mse)
best_quad
array([ 0.36828233, 0.29050997, 22.64327978])
correlation(galton, 'father', 'childHeight')
0.266038538922719
correlation(galton, 'mother', 'childHeight')
0.20132194862210062
best_quad
array([ 0.36828233, 0.29050997, 22.64327978])
intercept(galton, 'midparentHeight', 'childHeight')
22.636240549589751
slope(galton, 'midparentHeight', 'childHeight')
0.63736089696947895
best_quad.item(0) + best_quad.item(1)
0.6587923058163002
best_quad.item(0) + best_quad.item(1)/1.08
0.6372730487145057