阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。
AB testing
Generate 100000 dice rolling results
import numpy as np
random_data = np.random.randint(1, 7, 100000)
print(random_data.mean())
print(random_data.std())3.4963
1.7111768786422987
print(random_data[0:100])[3 5 3 1 4 1 4 2 6 1 2 5 1 4 2 1 1 5 1 4 4 6 4 4 2 6 6 6 3 1 1 5 2 4 6 2 4
2 3 3 5 5 4 2 1 4 2 4 1 2 5 4 6 1 1 1 5 1 6 3 1 2 5 6 5 6 3 3 2 4 1 4 2 1
2 2 5 2 2 6 1 1 1 4 1 5 4 1 6 2 1 6 5 3 2 3 4 4 4 4]
sample1 = []
for i in range(0, 10):
sample1.append(random_data[int(np.random.random() * len(random_data))])
print(sample1)[6, 3, 2, 6, 1, 5, 1, 6, 1, 3]
np.mean(sample1)3.4
np.std(sample1)2.0591260281974
CLT Central Limit Theorem
samples = []
samples_mean = []
samples_std = []
for i in range(0, 1000):
sample = []
for j in range(0, 50):
sample.append(random_data[int(np.random.random() * len(random_data))])
sample_np = np.array(sample)
samples_mean.append(sample_np.mean())
samples_std.append(sample_np.std())
samples.append(sample_np)
samples_mean_np = np.array(samples_mean)
samples_std_np = np.array(samples_std)
print(samples_mean_np)[3.4 2.82 3.74 3.64 3.54 3.6 3.24 3.86 3.32 3.82 3.54 3.5 3.1 3.54
3.18 3.56 3.74 3.7 3.68 3.92 3.7 3.32 3.52 3.78 3.46 3.62 3.64 3.24
3.7 3.64 3.46 3.56 3.5 3.46 3.56 3.52 3.04 3.82 3.56 3.74 3.72 3.68
3.46 3.36 3.1 3.54 3.8 3.8 3.64 3.88 3.38 3.64 3.14 3.42 3.46 3.3
3.56 3.5 2.98 3.56 3.92 3.34 3.9 3.58 3.46 3.26 3.42 3.22 3.72 3.5
3.58 3.68 3.52 3.8 3.76 3.46 4.04 3.68 3.68 3.24 3.44 3.04 3.46 3.5
3.76 3.64 3.54 3.48 2.94 3.5 3.54 3.6 3.58 3.64 3.22 3.56 3.72 3.72
3.72 3.72 3.76 3.38 3.14 3.44 3.24 3.42 3.5 3.54 3.78 3.74 3.26 3.34
3.8 3.72 4.02 3.46 3.62 3.14 3.46 3.86 3.64 3.9 3.42 3.44 3.7 3.74
3.2 3.54 3.18 3.54 4. 3.36 3.68 3.36 3.54 3.48 3.34 3.04 3.42 3.7
3.44 3.36 3.78 3.48 3.7 3.26 3.1 3.58 3.48 3.62 3.34 3.56 3.5 3.58
3.54 3.5 3.32 3.46 3.34 3.74 3.26 3.68 2.98 3.62 3.62 3.3 3.54 3.74
3.26 3.38 3.9 3.38 3.54 3.82 3.24 3.38 3.72 3.38 3.34 3.5 3.98 3.84
3.48 3.3 3.48 3.6 3.62 3.66 3.48 3.28 3.4 3.68 3.6 3.44 3.38 3.74
3.6 3.4 3.66 3.36 3.94 3.56 3.54 3.18 3.98 3.12 3.82 3.2 3.64 3.56
3.86 3.48 3.72 3.52 4. 3.56 3.62 3.62 3.8 3.06 3.52 3.14 3.36 3.52
3.74 3.74 3.84 3.48 3.62 3.34 3.48 3.44 3.56 3.28 3.2 3.6 3.14 3.46
3.5 2.94 3.9 3.46 3.32 3.04 3.76 3.62 3.96 3.26 3.22 3.52 3.24 3.52
3.66 3.66 3.62 3.48 2.86 3.24 3.64 3.72 3.58 3.14 3.6 3.8 3.88 3.38
3.74 3.7 3.4 3.78 3.44 3.2 3.18 3.14 3.7 3.38 3.76 3.78 3.76 3.44
3.56 3.3 3.08 3.38 3.3 3.52 3.8 3.34 4.04 3.52 3.26 3.98 3.74 3.64
3.68 3.34 3.12 3.28 3.94 3.26 3.2 3.24 3.8 3.26 3.48 3.3 3.68 3.4
3.72 3.26 3.46 3.34 3.74 3.2 3.22 3.18 3.44 3.44 3.2 3.72 3.8 3.76
3.1 3.62 3.96 3.38 3.74 3.54 3.52 3.42 3.14 3.54 3.76 3.78 3.3 3.84
3.96 3.68 3.66 3.36 3.42 3.6 3.48 3.4 3.46 3.86 3.42 3.3 3.54 3.64
3.7 3.42 2.96 3.22 3.56 3.38 3.7 3.32 3.62 2.98 3.64 3.6 4.04 3.62
3.72 3.98 3.64 3.66 3.12 3.34 3.4 3.3 3.86 2.92 3.88 3.2 3.76 3.42
3.74 3.6 3.58 3.16 3.58 3.78 3.38 3.4 3.76 3.56 3.42 4.02 3.54 3.52
3.2 3.54 4.16 3.44 3.8 3.84 3.56 3.48 3.3 3.32 3.54 4.1 3.54 3.46
3.62 3.52 3.46 3.18 3.54 3.18 3.22 3.3 3.72 3.24 3.44 3.8 3.78 3.3
3.52 3.4 3.4 3.48 3.64 3.18 3.84 3.46 3.8 3.38 3.28 3.52 3.52 3.34
3.66 3.52 4.12 3.36 3.62 3.84 3.28 3.38 3.32 3.2 3.08 3.56 3.7 3.38
3.08 3.64 3.98 3.22 3.7 3.16 3.34 3.48 3.22 3.5 3.56 3.48 3.74 3.38
3.46 3.2 3.24 3.22 3.66 3.52 3.6 3.68 3.86 3.42 3.3 3.66 3.54 3.48
3.14 3.54 3.24 3.44 3.46 3.36 3.62 3.88 3.56 3.7 3.9 3.64 3.26 3.64
3.6 3.42 4.04 3.76 3.5 3.4 3.56 3.4 3.32 3.32 3.78 3.4 3.24 3.42
3.9 3.56 3.56 3.48 3.58 3.52 3.34 3.3 3.22 3.32 3.62 3.5 3.22 3.78
3.34 3.64 3.74 3.52 3.6 3.52 3.42 3.48 3.76 3.48 3.74 3.72 3.62 3.4
3.18 3.62 3.84 3.74 3.24 3.42 4.04 3.48 3.52 3.64 3.42 3.52 3.42 4.2
3.14 3.58 3.5 3.34 3.66 3.1 2.96 3.6 3.08 3.02 3.54 3.9 3.32 3.52
3.74 3.98 3.32 3.64 3.34 3.7 3.4 3.42 3.4 3.48 3.5 3.44 3.32 3.58
3.64 3.82 3.34 3.64 3.58 3.36 3.6 3.76 3.48 3.36 3.98 3.3 3.64 3.5
3.32 3.98 3.5 3.56 3.16 3.88 3.62 3.36 3.12 3.56 3.92 3.3 3.52 3.64
3.46 3.28 3.24 3.52 3.94 3.54 3.42 3.48 4.06 3.66 3.44 3.66 3.06 3.3
3.54 3.96 3.34 3.82 3.44 3.8 3.5 3.88 3.48 3.58 3.24 3.32 3.46 3.26
3.28 3.66 3.52 3.74 3.1 3.44 3.1 3.7 3.28 2.98 3.56 3.26 3.24 3.5
3.48 3.38 3.8 3.8 3.7 3.3 3.08 3.38 3.44 3.48 3.3 3.98 3.36 3.52
3.44 3.34 3.14 3.4 3.36 3.68 3.92 3.24 3.26 3.38 3.96 3.42 3.48 3.68
3.48 3.48 3.74 3.6 3.84 3.92 3.78 3.16 3.28 3.5 3.74 3.32 3.32 3.46
3.8 3.42 3.86 3.58 3.62 3.46 3.78 3.22 3.46 3.3 3.8 3.66 3.5 3.76
3.36 3.08 4.16 3.22 3.4 3.34 3.14 3.22 3.46 3.24 3.66 3.4 3.32 3.26
3.54 3.6 3.12 3.18 3.86 3.78 3.56 3.56 3.62 3.92 3.24 3.94 3.78 3.34
3.2 3.36 3.56 3.08 3.04 3.42 3.42 3.26 3.82 4.04 3.52 3.26 3.46 3.62
3.22 3.26 3.54 3.62 3.5 3.8 3.6 3.58 3.68 3.08 3.28 3.48 3.62 3.24
3.58 3.34 3.32 3.28 3.34 3.12 3.86 3.98 3.58 3.42 3.24 3.46 3.5 3.34
3.92 3.64 3.62 4.1 3.36 3.54 3.08 3.32 3.5 3.56 3.64 2.9 3.44 3.08
3.6 3.72 3.7 3.44 3.42 3.46 3.36 3.34 3.62 3.08 3.52 4.06 3.48 3.42
3.54 3.58 3.56 3.5 3.32 3.52 3.08 3.38 3.56 3.68 3.72 3.66 3.64 3.58
3.02 2.82 3.54 3.72 3.26 3.72 3.64 3.44 2.94 3.24 3.08 3.76 3.84 3.64
3.82 3.18 3.42 3.68 3.6 3.22 3.68 3.4 3.22 4.08 3.5 3.4 3.34 3.4
3.5 3.44 3.54 3.16 3.38 3.26 3.66 3.04 3.54 3.56 3.54 3.38 3.2 3.58
3.38 3.68 3.36 3.92 3.36 3.64 3.48 3.64 3.06 3.44 3.72 3.52 3.36 3.48
3.54 3.5 3.48 3.32 3.26 3.62 3.32 3.18 3.3 3.22 3.58 3.34 3.36 3.3
3.62 2.88 3.2 3.46 3.1 3.66 3.22 3. 3.6 3.46 3.74 3.82 3.12 3.52
3.52 3.56 3.34 3.78 3.68 3.5 3.46 3.44 3.34 3.68 3.38 3.42 3.54 3.68
3.64 3.12 4. 3.86 4.04 3.34 3.26 3.3 3.04 3.96 3.42 3.86 3.84 3.3
3.28 3.72 3.86 3.32 3.48 3.42 3.48 3.18 3.12 4. 3.44 3.4 3.62 3.28
3.58 3.22 3.4 3.58 3.66 3.68 3.28 3.54 3.84 3.24 4.08 3.6 3.3 3.64
3.36 3.52 3.84 3.7 3.56 3.2 3.68 3.64 3.42 3.98 3.82 3.66 3.3 3.62
3.3 3.64 3.68 3.88 3.36 3.56 3.8 3.44 3.42 3.68 3.8 3.24 3.3 3.64
3.66 3.6 3.68 3.24 3.8 3.26 3.56 3.32 3.44 3.98 3.8 3.44 3.84 3.4
3.52 3.26 3.62 3.24 3.6 3.18]
samples_mean_mean = samples_mean_np.mean()
print(samples_mean_mean)
samples_mean_std = samples_mean_np.std()
print(samples_mean_std)3.5054
0.23958221970755675
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams.update({'figure.figsize':(7,5), 'figure.dpi':100})
# Plot Histogram on x
#x = np.random.normal(size = 1000)
plt.hist(samples_mean_np, bins=20)
plt.gca().set(title='Frequency Histogram', ylabel='Frequency');<Figure size 700x500 with 1 Axes>
输出图整理: 样本均值围绕骰子总体均值3.5分布,50次抽样的均值标准差约为sqrt(35/12)/sqrt(50)≈0.2415;原统计输出约0.2396。用理论关系代替原直方图,不伪造柱高。
AB Testing examples
from scipy.stats import norm
norm.cdf(-0.632)0.2636934386118963
from scipy.stats import norm
norm.cdf(-2)0.022750131948179195
from scipy.stats import t
alpha = 0.05
t_stat = 2
df = 200
p = (1-t.cdf(abs(t_stat),df))*2
print("p value:", p)p value: 0.04685318618707113
norm.cdf(-1.18)0.11900010745520073
norm.cdf(-3.6)0.00015910859015753364
#t test example:
from math import sqrt
from numpy.random import seed
from numpy.random import randn
from numpy import mean
from scipy.stats import t
from scipy.stats import sem
# seed the random number generator
seed(1)
# generate two independent samples
data1 = 5 * randn(100) + 50
data2 = 5 * randn(100) + 51
# calculate the t test
df = len(data1) + len(data2) - 2
alpha = 0.05
mean1, mean2 = mean(data1), mean(data2)
se1, se2 = sem(data1), sem(data2)
sed = sqrt(se1**2.0 + se2**2.0)
t_stat = (mean1 - mean2) / sed
df = len(data1) + len(data2) - 2
# calculate the critical value
p = (1.0 - t.cdf(abs(t_stat), df)) * 2.0
print('t=%.3f, df=%d, p=%.3f' % (t_stat, df, p))
# interpret via p-value
if p > alpha:
print('Accept null hypothesis that the means are equal.')
else:
print('Reject the null hypothesis that the means are equal.')t=-2.262, df=198, p=0.025
Reject the null hypothesis that the means are equal.
整理注: 这里两组样本量相等;一般不等方差应使用Welch检验及其自由度。p较大只能未拒绝H0,不能接受或证明均值相等。
from statsmodels.stats.power import zt_ind_solve_power
from statsmodels.stats.proportion import proportion_effectsize as es
zt_ind_solve_power(effect_size=es(prop1=0.1, prop2=0.12), alpha=0.05, power=0.8, alternative="two-sided")3834.595739884031
# chi-squared test with similar proportions
from scipy.stats import chi2_contingency
from scipy.stats import chi2
# contingency table
#table = [[90, 165],[84, 307]]
table = [[90, 165, 34],[84, 307, 65]]
print(table)
U, p, dof, expected = chi2_contingency(table, correction=False)
print(U)
print(p)
print('dof=%d' % dof)
print(expected)
# interpret p-value
alpha = 0.05
print('significance=%.3f, p=%.3f' % (alpha, p))
if p <= alpha:
print('Dependent (reject H0)')
else:
print('Independent (fail to# reject H0)')[[90, 165, 34], [84, 307, 65]]
16.003556618937814
0.00033486660165148337
dof=2
[[ 67.49798658 183.09798658 38.40402685]
[106.50201342 288.90201342 60.59597315]]
significance=0.050, p=0.000
Dependent (reject H0)