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
from collections import Counter | |
def mode(x): | |
counts = Counter(x) | |
max_count = max(counts.values()) | |
return [i for i, j in counts.items() if j == max_count] |
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
def quantile(x, p): | |
p_index = int(p*len(x)) | |
return sorted(x)[p_index] |
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
def range_(x): | |
return max(x) - min(x) |
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 | |
def variance(x): | |
x_bar = np.mean(x) | |
N = len(x) | |
deviation_mean = [x_i - x_bar for x_i in x] | |
return np.dot(deviation_mean, deviation_mean) / N |
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
def quantile(x, p): | |
p_index = int(p*len(x)) | |
return sorted(x)[p_index] | |
def interquartile_range(x): | |
return quantile(x, 0.75) - quantile(x, 0.25) |
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 | |
def standard_deviation(x): | |
x_bar = np.mean(x) | |
N = len(x) | |
deviation_mean = [x_i - x_bar for x_i in x] | |
return np.sqrt(np.dot(deviation_mean, deviation_mean) / N) |
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
#Provided that x and y have the same length. | |
import numpy as np | |
def covariance(x, y): | |
N = len(x) | |
x_bar = np.mean(x) | |
y_bar = np.mean(y) | |
deviation_mean_x = [x_i - x_bar for x_i in x] | |
deviation_mean_y = [y_i - y_bar for y_i in y] | |
return np.dot(deviation_mean_x, deviation_mean_y) / N |
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
#Provided that x and y have the same length. | |
import numpy as np | |
def standard_deviation(x): | |
x_bar = np.mean(x) | |
N = len(x) | |
deviation_mean = [x_i - x_bar for x_i in x] | |
return np.sqrt(np.dot(deviation_mean, deviation_mean) / N) | |
def covariance(x, y): | |
N = len(x) |
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 random | |
def random_kid(): | |
return random.choice(['boy','girl']) | |
both_girls = 0 | |
older_girl = 0 | |
either_girl = 0 | |
random.seed(7) | |
for i in range(1000000): |
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 matplotlib.pyplot as plt | |
def normal_pdf(x, mu=0, sigma=1): | |
sqrt_two_pi = np.sqrt(2*np.pi) | |
return (np.exp(-(x-mu)**2/2/sigma**2)/(sqrt_two_pi*sigma)) | |
xs = [x / 10.0 for x in range(-50, 50)] | |
plt.plot(xs, [normal_pdf(x, sigma=1) for x in xs], '-', label='$\mu=0$, $\sigma=1$') | |
plt.plot(xs, [normal_pdf(x, sigma=2) for x in xs], '--', label='$\mu=0$, $\sigma=2$') | |
plt.plot(xs, [normal_pdf(x, sigma=0.5) for x in xs], ':', label='$\mu=0$, $\sigma=0.5$') |