Skip to content

Instantly share code, notes, and snippets.

View albertyumol's full-sized avatar
:octocat:
just bash-ing around

Albert 'Bash' Yumol albertyumol

:octocat:
just bash-ing around
  • Manila, Philippines
View GitHub Profile
@albertyumol
albertyumol / mode.py
Created October 22, 2019 23:02
Calculating mode in Python.
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]
@albertyumol
albertyumol / quantile.py
Created October 22, 2019 23:14
Calculate quantile in Python.
def quantile(x, p):
p_index = int(p*len(x))
return sorted(x)[p_index]
@albertyumol
albertyumol / range.py
Created October 22, 2019 23:15
Calculate the range in Python.
def range_(x):
return max(x) - min(x)
@albertyumol
albertyumol / variance.py
Created October 23, 2019 00:45
Calculate the variance in Python.
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
@albertyumol
albertyumol / interquartile_range.py
Created October 23, 2019 01:12
Calculate the interquartile range in Python.
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)
@albertyumol
albertyumol / standard_deviation.py
Created October 23, 2019 01:32
Calculate the standard deviation.
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)
@albertyumol
albertyumol / covariance.py
Created October 23, 2019 06:53
Calculate the covariance in Python.
#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
@albertyumol
albertyumol / correlation.py
Created October 23, 2019 06:59
Calculate the correlation in Python.
#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)
@albertyumol
albertyumol / boy_or_girl.py
Created October 24, 2019 05:47
Calculating conditional probabilities in Python.
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):
@albertyumol
albertyumol / normal.py
Created October 25, 2019 01:06
Plotting normal pdf's in Python.
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$')