Spaces:
Sleeping
Sleeping
Upload stats.py
Browse files
stats.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from numpy import random
|
| 3 |
+
from matplotlib import pyplot as plt
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def t_test(sample1, sample2):
|
| 7 |
+
# this is a t-test assuming equal sample sizes
|
| 8 |
+
assert len(sample1) == len(sample2)
|
| 9 |
+
difference = sample2.mean() - sample1.mean()
|
| 10 |
+
n_1 = len(sample1)
|
| 11 |
+
n_2 = len(sample2)
|
| 12 |
+
mu_measure_var1 = sample1.var(ddof=1) / len(sample1)
|
| 13 |
+
mu_measure_var2 = sample2.var(ddof=1) / len(sample2)
|
| 14 |
+
mu_std_err = np.sqrt(mu_measure_var1 + mu_measure_var2)
|
| 15 |
+
dof = mu_std_err**4 / (
|
| 16 |
+
mu_measure_var1**2 / (n_1 - 1) + mu_measure_var2**2 / (n_2 - 1)
|
| 17 |
+
)
|
| 18 |
+
t_val = difference / mu_std_err
|
| 19 |
+
t_null_dist = np.random.standard_t(dof, 100_000)
|
| 20 |
+
p_val = np.mean(np.abs(t_val) > t_null_dist) / 2
|
| 21 |
+
return f"""\
|
| 22 |
+
t-value: {t_val}
|
| 23 |
+
degrees of freedom: {dof}
|
| 24 |
+
p-value: {p_val}"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def freqpoly(x1, x2, binwidth, xlim):
|
| 28 |
+
all_data = np.concatenate([x1, x2])
|
| 29 |
+
x_low = min([all_data.min(), xlim[0]])
|
| 30 |
+
x_high = max([all_data.max(), xlim[1]])
|
| 31 |
+
bins = np.arange(x_low, x_high + binwidth, binwidth)
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
ax.hist(x1, bins, density=True, range=xlim, alpha=0.5)
|
| 34 |
+
ax.hist(x2, bins, density=True, range=xlim, alpha=0.5)
|
| 35 |
+
return fig
|