更新Permuation的解释。
Optimal parameters | Python
按照这个图上的理解,实际上,我们假设一个参数方程,就是要让sample distribution和population distribution 相似,这样的情况下,参数就是最优的。
How often do we get no-hitters? | Python
# Seed random number generator
np.random.seed(42)
# Compute mean no-hitter time: tau
tau = np.mean(nohitter_times)
# Draw out of an exponential distribution with parameter tau: inter_nohitter_time
inter_nohitter_time = np.random.exponential(tau, 100000)
# Plot the PDF and label axes
_ = plt.hist(inter_nohitter_time,
bins=50, normed=True, histtype='step')
_ = plt.xlabel('Games between no-hitters')
_ = plt.ylabel('PDF')
# Show the plot
plt.show()
Nice work! We see the typical shape of the Exponential distribution, going from a maximum at 0 and decaying to the right.
Do the data follow our story? | Python
# Create an ECDF from real data: x, y
x, y = ecdf(nohitter_times)
# Create a CDF from theoretical samples: x_theor, y_theor
x_theor, y_theor = ecdf(inter_nohitter_time)
# Overlay the plots
plt.plot(x_theor, y_theor)
plt.plot(x, y, marker='.', linestyle='none')
# Margins and axis labels
plt.margins(0.02)
plt.xlabel('Games between no-hitters')
plt.ylabel('CDF')
# Show the plot
plt.show()
It looks like no-hitters in the modern era of Major League Baseball are Exponentially distributed. Based on the story of the Exponential distribution, this suggests that they are a random process; when a no-hitter will happen is independent of when the last no-hitter was.
Linear regression | Python
In [1]: help(np.polyfit)
Help on function polyfit in module numpy.lib.polynomial:
polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
Least squares polynomial fit.
Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
to points `(x, y)`. Returns a vector of coefficients `p` that minimises
the squared error.
Parameters
----------
x : array_like, shape (M,)
x-coordinates of the M sample points ``(x[i], y[i])``.
y : array_like, shape (M,) or (M, K)
y-coordinates of the sample points. Several data sets of sample
points sharing the same x-coordinates can be fitted at once by
passing in a 2D-array that contains one dataset per column.
# Plot the illiteracy rate versus fertility
_ = plt.plot(illiteracy, fertility, marker='.', linestyle='none')
plt.margins(0.02)
_ = plt.xlabel('percent illiterate')
_ = plt.ylabel('fertility')
# Perform a linear regression using np.polyfit(): a, b
a, b = np.polyfit(x = illiteracy, y = fertility, deg = 1)
# Print the results to the screen
print('slope =', a, 'children per woman / percent illiterate')
print('intercept =', b, 'children per woman')
# Make theoretical line to plot
x = np.array([0,100])
y = a * x + b
# Add regression line to your plot
_ = plt.plot(x, y)
# Draw the plot
plt.show()
如何添加OLS回归曲线。
How is it optimal? | Python
In [1]: help(np.linspace)
Parameters
----------
start : scalar
The starting value of the sequence.
stop : scalar
The end value of the sequence, unless `endpoint` is set to False.
In that case, the sequence consists of all but the last of ``num + 1``
evenly spaced samples, so that `stop` is excluded. Note that the step
size changes when `endpoint` is False.
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
OLS实现的图像方法
# Specify slopes to consider: a_vals
a_vals = np.linspace(0,0.1,200)
# Initialize sum of square of residuals: rss
rss = np.empty_like(a_vals)
# Compute sum of square of residuals for each value of a_vals
for i, a in enumerate(a_vals):
rss[i] = np.sum((fertility - a*illiteracy - b)**2)
# Plot the RSS
plt.plot(a_vals, rss, '-')
plt.xlabel('slope (children per woman / percent illiterate)')
plt.ylabel('sum of square of residuals')
plt.show()
The importance of EDA: Anscombe’s quartet | Python
OLS回归打脸的情况。 所以在回归前,先看看数据长什么样子。
Generating bootstrap replicates | Python
resampling replacing
Bootstrapping: The use of resampled data to perform statistical inference.
bootstrap sample: 每一次resampling的样本
np.random.choice
用来实现bootstrap,并且size
来控制样本大小。
Visualizing bootstrap samples | Python
Notice how the bootstrap samples give an idea of how the distribution of rainfalls is spread.
for i in range(50):
# Generate bootstrap sample: bs_sample
bs_sample = np.random.choice(rainfall, size=len(rainfall))
# Compute and plot ECDF from bootstrap sample
x, y = ecdf(bs_sample)
_ = plt.plot(x, y, marker='.', linestyle='none',
color='gray', alpha=0.1)
# Compute and plot ECDF from original data
x, y = ecdf(rainfall)
_ = plt.plot(x, y, marker='.')
# Make margins and label axes
plt.margins(0.02)
_ = plt.xlabel('yearly rainfall (mm)')
_ = plt.ylabel('ECDF')
# Show the plot
plt.show()
其中,
for i in range(50):
# Generate bootstrap sample: bs_sample
bs_sample = np.random.choice(rainfall, size=len(rainfall))
# Compute and plot ECDF from bootstrap sample
x, y = ecdf(bs_sample)
_ = plt.plot(x, y, marker='.', linestyle='none',
color='gray', alpha=0.1)
就是resample50次,每次都把点打到图上,这些点都是随机产生的,理论上,哪个区间概率大,哪个区间点密集程度高。
# Compute and plot ECDF from original data
x, y = ecdf(rainfall)
_ = plt.plot(x, y, marker='.')
# Make margins and label axes
plt.margins(0.02)
_ = plt.xlabel('yearly rainfall (mm)')
_ = plt.ylabel('ECDF')
最后再把真实的图,用线画上去。
Bootstrap confidence intervals | Python
Confidence interval of a statistic: If we repeated measurements over and over again, p% of the observed values would lie within the p% confidence interval.
如果我们反复这样操作,那么p%表示有p%的概率会落在图中的CI中。
这里就理解了,我们通过bootstrap,复制了50个resampling 样本,因此得到50个\(\bar x\), 因此做histogram,我们认为这种方法把\(\bar x\)的population distribution弄出来了, 注意这里不是\(x\)的population distribution。 然后我们把原样本的\(\bar x\)拿出来比较,要是在95%内,那么就说明不能拒绝假设\(\bar x = \bar {\bar x}\)。
Generating many bootstrap replicates | Python
def bootstrap_replicate_1d(data, func):
return func(np.random.choice(data, size=len(data)))
def draw_bs_reps(data, func, size=1):
"""Draw bootstrap replicates."""
# Initialize array of replicates: bs_replicates
bs_replicates = np.empty(size)
# Generate replicates
for i in range(size):
bs_replicates[i] = bootstrap_replicate_1d(data, func)
return bs_replicates
Bootstrap replicates of the mean and the SEM | Python
In this exercise, you will compute a bootstrap estimate of the probability distribution function of the mean annual rainfall at the Sheffield Weather Station. Remember, we are estimating the mean annual rainfall we would get if the Sheffield Weather Station could repeat all of the measurements from 1883 to 2015 over and over again. This is a probabilistic estimate of the mean. You will plot the PDF as a histogram, and you will see that it is Normal.
In fact, it can be shown theoretically that under not-too-restrictive conditions, the value of the mean will always be Normally distributed. (This does not hold in general, just for the mean and a few other statistics.) The standard deviation of this distribution, called the standard error of the mean, or SEM, is given by the standard deviation of the data divided by the square root of the number of data points. I.e., for a data set,
sem = np.std(data) / np.sqrt(len(data))
. Using hacker statistics, you get this same result without the need to derive it, but you will verify this result from your bootstrap replicates.
因此\(\sigma_{\bar x}\)不是\(\sigma_{x}\),经过了平均\(x \to \bar x\),波动本来就应该减小一点。 并且SEM是正态分布,通过图像直观理解即可。
# Take 10,000 bootstrap replicates of the mean: bs_replicates
bs_replicates = draw_bs_reps(rainfall,np.mean,10000)
# Compute and print SEM
sem = np.std(rainfall) / np.sqrt(len(rainfall))
print(sem)
# Compute and print standard deviation of bootstrap replicates
bs_std = np.std(bs_replicates)
print(bs_std)
# Make a histogram of the results
_ = plt.hist(bs_replicates, bins=50, normed=True)
_ = plt.xlabel('mean annual rainfall (mm)')
_ = plt.ylabel('PDF')
# Show the plot
plt.show()
\[SEM = \sigma_{\bar x} = \frac{\sigma_x}{n^{1/2}}\]
Bootstrap replicates of other statistics | Python
def draw_bs_reps(data, func, size=1):
return np.array([bootstrap_replicate_1d(data, func) for _ in range(size)])
这里def function中,func
的点很好,以前批量使用函数,都是用.apply
,都不知道什么骚操作,现在终于明白了。
# Generate 10,000 bootstrap replicates of the variance: bs_replicates
bs_replicates = draw_bs_reps(rainfall,np.var,10000)
# Put the variance in units of square centimeters
bs_replicates = bs_replicates/100
# Make a histogram of the results
_ = plt.hist(bs_replicates, normed=True, bins=50)
_ = plt.xlabel('variance of annual rainfall (sq. cm)')
_ = plt.ylabel('PDF')
# Show the plot
plt.show()
Great work! This is not normally distributed, as it has a longer tail to the right. Note that you can also compute a confidence interval on the variance, or any other statistic, using
np.percentile()
with your bootstrap replicates.
这个时候,发现不是正态分布,并且有很长的right tial
。
Pairs bootstrap | Python
Pairs bootstrap for linear regression
- Resample data in pairs resample index,理论上所有的\((X, y)\)都可以搞!理解了bootstrap了。
- Compute slope and intercept from resampled data
- Each slope and intercept is a bootstrap replicate
- Compute confidence intervals from percentiles of bootstrap replicates
A function to do pairs bootstrap | Python
笔记还没有整理,关键是链接中的内容,可以点击标题查看,关键的实现代码,我摘录过来了。
def draw_bs_pairs_linreg(x, y, size=1):
"""Perform pairs bootstrap for linear regression."""
# Set up array of indices to sample from: inds
inds = np.arange(len(x))
# Initialize replicates: bs_slope_reps, bs_intercept_reps
bs_slope_reps = np.empty(size)
bs_intercept_reps = np.empty(size)
# Generate replicates
for i in range(size):
bs_inds = np.random.choice(inds, size=len(inds))
bs_x, bs_y = x[bs_inds], y[bs_inds]
bs_slope_reps[i], bs_intercept_reps[i] = np.polyfit(bs_x, bs_y, 1)
return bs_slope_reps, bs_intercept_reps
Pairs bootstrap of literacy/fertility data | Python
# Generate replicates of slope and intercept using pairs bootstrap
bs_slope_reps, bs_intercept_reps = draw_bs_pairs_linreg(illiteracy, fertility,size=1000)
# Compute and print 95% CI for slope
print(np.percentile(bs_slope_reps, [2.5,97.5]))
# Plot the histogram
_ = plt.hist(bs_slope_reps, bins=50, normed=True)
_ = plt.xlabel('slope')
_ = plt.ylabel('PDF')
plt.show()
理解了,为什么\(\beta\)理解起来是一个distribution。 实际上,bootstrap的过程,使得\(\beta\)出来了,并且\(\bata\)作为一个statistics, 当bootstrap的过程无穷大时,近似 是满足正态分布或者t分布的,因此可以直接用t分布的参数解决问题。
Plotting bootstrap regressions | Python
# Generate array of x-values for bootstrap lines: x
x = np.array([0,100])
# Plot the bootstrap lines
for i in range(100):
_ = plt.plot(x, bs_slope_reps[i]*x + bs_intercept_reps[i],
linewidth=0.5, alpha=0.2, color='red')
# Plot the data
_ = plt.plot(illiteracy, fertility, marker='.', linestyle='none')
# Label axes, set the margins, and show the plot
_ = plt.xlabel('illiteracy')
_ = plt.ylabel('fertility')
plt.margins(0.02)
plt.show()
Generating a permutation sample | Python
这是讲解的视频,我觉得还不错,蛮清晰的。 Formulating and simulating a hypothesis | Python 。
Permuation是对已有的样本进行重新排序,打乱之前对照组和实验组的差异。 而Bootstrap是从样本中重复抽样。 (Yu 2012)
In the video, you learned that permutation sampling is a great way to simulate the hypothesis that two variables have identical probability distributions. This is often a hypothesis you want to test, so in this exercise, you will write a function to generate a permutation sample from two data sets.
如果两个样本一致,那么这种随机排序,绝对不影响两者的分布。
Remember, a permutation sample of two arrays having respectively n1 and n2 entries is constructed by concatenating the arrays together, scrambling the contents of the concatenated array, and then taking the first n1 entries as the permutation sample of the first array and the last n2 entries as the permutation sample of the second array.
In [2]: help(np.concatenate)
Help on built-in function concatenate in module numpy.core.multiarray:
concatenate(...)
concatenate((a1, a2, ...), axis=0)
Join a sequence of arrays along an existing axis.
注意这里要加()
在(a1, a2, ...)
中。
np.concatenate
合并两个向量。
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12])
np.random.permutation
随机置换。
Random reordering of entries in an array.
def permutation_sample(data1, data2):
"""Generate a permutation sample from two data sets."""
# Concatenate the data sets: data
data = np.concatenate((data1, data2))
# Permute the concatenated array: permuted_data
permuted_data = np.random.permutation(data)
# Split the permuted array into two: perm_sample_1, perm_sample_2
perm_sample_1 = permuted_data[:len(data1)]
perm_sample_2 = permuted_data[len(data1):]
return perm_sample_1, perm_sample_2
Visualizing permutation sampling | Python
for _ in range(50):
# Generate permutation samples
perm_sample_1, perm_sample_2 =
permutation_sample(rain_july, rain_november)
# Compute ECDFs
x_1, y_1 = ecdf(perm_sample_1)
x_2, y_2 = ecdf(perm_sample_2)
# Plot ECDFs of permutation sample
_ = plt.plot(x_1, y_1, marker='.', linestyle='none',
color='red', alpha=0.02)
_ = plt.plot(x_2, y_2, marker='.', linestyle='none',
color='blue', alpha=0.02)
# Create and plot ECDFs from original data
x_1, y_1 = ecdf(rain_july)
x_2, y_2 = ecdf(rain_november)
_ = plt.plot(x_1, y_1, marker='.', linestyle='none', color='red')
_ = plt.plot(x_2, y_2, marker='.', linestyle='none', color='blue')
# Label axes, set margin, and show plot
plt.margins(0.02)
_ = plt.xlabel('monthly rainfall (mm)')
_ = plt.ylabel('ECDF')
plt.show()
Notice that the permutation samples ECDFs overlap and give a purple haze. None of the ECDFs from the permutation samples overlap with the observed data, suggesting that the hypothesis is not commensurate with the data. July and November rainfall are not identically distributed.
在图像中,新产生的紫色图像更像是两个图像的合并版本,且和两个图形不重叠,因此可以肯定两个图像不是同一分布。
当然,这是直观的理解,更准确�,我们需要引入 Test statistics 和 p-value。
Generating permutation replicates | Python
def draw_perm_reps(data_1, data_2, func, size=1):
"""Generate multiple permutation replicates."""
# Initialize array of replicates: perm_replicates
perm_replicates = np.empty(size)
for i in range(size):
# Generate permutation sample
perm_sample_1, perm_sample_2 = permutation_sample(data_1,data_2)
# Compute the test statistic
perm_replicates[i] = func(perm_sample_1, perm_sample_2)
return perm_replicates
Permutation test on frog data | Python
The average strike force of Frog A was 0.71 Newtons (N), and that of Frog B was 0.42 N for a difference of 0.29 N. It is possible the frogs strike with the same force and this observed difference was by chance. You will compute the probability of getting at least a 0.29 N difference in mean strike force under the hypothesis that the distributions of strike forces for the two frogs are identical. We use a permutation test with a test statistic of the difference of means to test this hypothesis.
目前假设:两个分布是相似的,这种mean的差异是可以接受的,很常见。 现在要在统计上证明这种偶然存在的可能性大小。 如果可能性很小, 那么两个分布是相似的,这种mean的差异是可以接受的,很常见。(这种话可能性很小), 那么假设就是不成立的。
def diff_of_means(data_1, data_2):
"""Difference in means of two arrays."""
# The difference of means of data_1, data_2: diff
diff = np.mean(data_1)-np.mean(data_2)
return diff
# Compute difference of mean impact force from experiment: empirical_diff_means
empirical_diff_means = diff_of_means(force_a, force_b)
# Draw 10,000 permutation replicates: perm_replicates
perm_replicates = draw_perm_reps(force_a, force_b,
diff_of_means, size=10000)
# Compute p-value: p
p = np.sum(perm_replicates >= empirical_diff_means) / len(perm_replicates)
# Print the result
print('p-value =', p)
<script.py> output:
p-value = 0.0063
The p-value tells you that there is about a 0.6% chance that you would get the difference of means observed in the experiment if frogs were exactly the same. A p-value below 0.01 is typically said to be “statistically significant,”, but: warning! warning! warning! You have computed a p-value; it is a number. I encourage you not to distill it to a yes-or-no phrase. p = 0.006 and p = 0.000000006 are both said to be “statistically significant,” but they are definitely not the same!
A one-sample bootstrap hypothesis test | Python
One sample test: Compare one set of data to a single number.
Two sample test: Compare two sets of data.
Another juvenile frog was studied, Frog C, and you want to see if Frog B and Frog C have similar impact forces. Unfortunately, you do not have Frog C’s impact forces available, but you know they have a mean of 0.55 N. Because you don’t have the original data, you cannot do a permutation test, and you cannot assess the hypothesis that the forces from Frog B and Frog C come from the same distribution. You will therefore test another, less restrictive hypothesis: The mean strike force of Frog B is equal to that of Frog C.
To set up the bootstrap hypothesis test, you will take the mean as our test statistic. Remember, your goal is to calculate the probability of getting a mean impact force less than or equal to what was observed for Frog B if the hypothesis that the true mean of Frog B’s impact forces is equal to that of Frog C is true. You first translate all of the data of Frog B such that the mean is 0.55 N. This involves adding the mean force of Frog C and subtracting the mean force of Frog B from each measurement of Frog B. This leaves other properties of Frog B’s distribution, such as the variance, unchanged.
# Make an array of translated impact forces: translated_force_b
translated_force_b = force_b - np.mean(force_b) + 0.55
# Take bootstrap replicates of Frog B's translated impact forces: bs_replicates
bs_replicates = draw_bs_reps(translated_force_b, np.mean, 10000)
# Compute fraction of replicates that are less than the observed Frog B force: p
p = np.sum(bs_replicates <= np.mean(force_b)) / 10000
# Print the p-value
print('p = ', p)
<script.py> output:
p = 0.0046
说实话,不太懂。
The low p-value suggests that the null hypothesis that Frog B and Frog C have the same mean impact force is false.
假设\(\bar x_b\)和\(\bar x_c\)相等的情况下, \(\bar x_{b}^\prime < \bar x_{b}\)的可能性。 但是又一个更加强的假设, \(\bar x_{b}^\prime \not= x_{b}\)的可能性。
因此假设
\[\bar x_{b}^\prime \to \bar x_{b} - (\bar x_{b} - \bar x_{c})\]
但是这里的p = 0.0046
,已经说明了, 两者相等的可能性就是很极端的。
A bootstrap test for identical distributions | Python
复习
def diff_of_means(data_1, data_2):
"""Difference in means of two arrays."""
# The difference of means of data_1, data_2: diff
diff = np.mean(data_1)-np.mean(data_2)
return diff
# Compute difference of mean impact force from experiment: empirical_diff_means
empirical_diff_means = diff_of_means(force_a, force_b)
# Draw 10,000 permutation replicates: perm_replicates
perm_replicates = draw_perm_reps(force_a, force_b,
diff_of_means, size=10000)
# Compute p-value: p
p = np.sum(perm_replicates >= empirical_diff_means) / len(perm_replicates)
# Print the result
print('p-value =', p)
# Compute difference of mean impact force from experiment: empirical_diff_means
empirical_diff_means = diff_of_means(force_a, force_b)
# Concatenate forces: forces_concat
forces_concat = np.concatenate((force_a,force_b))
# Initialize bootstrap replicates: bs_replicates
bs_replicates = np.empty(10000)
for i in range(10000):
# Generate bootstrap sample
bs_sample = np.random.choice(forces_concat, size=len(forces_concat))
# Compute replicate
bs_replicates[i] = diff_of_means(bs_sample[:len(force_a)],
bs_sample[len(force_a):])
# Compute and print p-value: p
p = np.sum(bs_replicates > empirical_diff_means)/ len(bs_replicates)
print('p-value =', p)
<script.py> output:
p-value = 0.0055
Great work! You may remember that we got p = 0.0063 from the permutation test, and here we got p = 0.0055. These are very close, and indeed the tests are testing the same thing. However, the permutation test exactly simulates the null hypothesis that the data come from the same distribution, whereas the bootstrap test approximately simulates it. As we will see, though, the bootstrap hypothesis test, while approximate, is more versatile.
这里就要比较一下bootstrap和permutation两种的区别了。
Permutation:
bootstrap,然后每次bootstrap分别计算a和b样本的\(\bar x_a\)和\(\bar x_b\)。 然后对\(\bar x_a - \bar x_b\)做分布。
def diff_of_means(data_1, data_2):
"""Difference in means of two arrays."""
# The difference of means of data_1, data_2: diff
diff = np.mean(data_1)-np.mean(data_2)
return diff
# Compute difference of mean impact force from experiment: empirical_diff_means
empirical_diff_means = diff_of_means(force_a, force_b)
# Draw 10,000 permutation replicates: perm_replicates
perm_replicates = draw_perm_reps(force_a, force_b,
diff_of_means, size=10000)
# Compute p-value: p
p = np.sum(perm_replicates >= empirical_diff_means) / len(perm_replicates)
# Print the result
print('p-value =', p)
Bootstrap:
现将两个样本合并,因为现在假设它们是一致的。 然后按照index重新分a和b样本,分别计算\(\bar x\),然后计算差值。 然后对\(\bar x_a - \bar x_b\)做分布。
因此两者的区别在于, Bootstrap多一步操作,将样本混合,按照index重新分a和b样本。 这里的混合已经假设了两者distribution一致了,只是看看mean一不一样了!
而Permutation谨慎,不会故意将样本混合,而是单纯的重复抽样,比较\(\Delta \bar x\)是否显著!
We now want to test the hypothesis that Frog A and Frog B have the same mean impact force, but not necessarily the same distribution. This, too, is impossible with a permutation test.
# Compute mean of all forces: mean_force
mean_force = np.mean(forces_concat)
# Generate shifted arrays
force_a_shifted = force_a - np.mean(force_a) + mean_force
force_b_shifted = force_b - np.mean(force_b) + mean_force
# Compute 10,000 bootstrap replicates from shifted arrays
bs_replicates_a = draw_bs_reps(force_a_shifted, np.mean, 10000)
bs_replicates_b = draw_bs_reps(force_b_shifted, np.mean, 10000)
# Get replicates of difference of means: bs_replicates
bs_replicates = bs_replicates_a-bs_replicates_b
# Compute and print p-value: p
p = np.sum(bs_replicates >= empirical_diff_means)/ len(bs_replicates)
print('p-value =', p)
<script.py> output:
p-value = 0.0043
Nice work! Not surprisingly, the more forgiving hypothesis, only that the means are equal as opposed to having identical distributions, gives a higher p-value. Again, it is important to carefully think about what question you want to ask. Are you only interested in the mean impact force, or the distribution of impact forces?
这里要重新好好看看分布,理解不同的意思。 这是分水岭,一直没有弄清楚。
https://videos.datacamp.com/transcoded_mp4/1550_python_stat_thinking_II/v2/ch3_1.mp4
看分布,ECDF是很好的模型。
Permutation: Random reordering of entries in an array.
这完全假设了两者的分布活着ECDF相似,然后去检验,应该是最严格的假设了。
https://videos.datacamp.com/transcoded_mp4/1550_python_stat_thinking_II/v2/ch3_2.mp4
对Test statistcis的选择,是基于distribution一致,那么mean一致而言的,这里是简化步骤。
但是这里还是很严格的,因为Random reordering
下,不是同一个distribution,mean也很难一致。
Permutation下的\(\Delta \bar x\)分布中,真实\(bar x\)如果是rare值,那么就说明,如果我们假设两个分布一致的情况下,我们的真实数据是不在大概率的情况下的,是小概率事件,因此拒绝假设。这是反证法。
https://videos.datacamp.com/transcoded_mp4/1550_python_stat_thinking_II/v2/ch3_3.mp4
如果distribution不可能,我们就不能假设distribution一致,但是可以假设均值相等。
但是在这里,显然不可能用permutation的方法了。
原因很简单,distribution都没有,怎么合并样本Random reordering
呢?
这里的话,我们bootstrap有distribution的样本,很多次,得到mean,然后和cosntant的mean,求差值。 然后看实际均值差位于分布的位置。
\[x_{a,i}^\prime = x_{a,i} - \bar x_{a} + \bar x_{b}\]
bootstrap 第\(j\)次,得到:
\[\bar x_{a,j}^\prime - \bar x_{b}\]
\[F([\bar x_{a,j}^\prime - \bar x_{b}] < [\bar x_{a} - \bar x_{b}])\]
\(\bar x_{a} + \bar x_{b}\)有种把假设融于公式的感觉。
这是one sample test。
A/B testing | Python
A/B 检验,平均值的高低的单纯比较,会忽略一个问题: 计算的结果可能是受到随机严重干扰的。 无论是统计上证明,两组检验变量同分布还是同均值, 比较好的方法是,使用大数定理。 使用大数定理,实操中常见的方法是,重复抽样,也就是 Bootstrap,或者更加严密的Permutations。
两者的思想是,对A、B分布的数据,进行重复抽样,形成若干个对照组,计算同样检验变量的测试值,然后再来比较。
具体可以看Python代码来理解。
def draw_perm_reps(data_1, data_2, func, size=1):
"""Generate multiple permutation replicates."""
# Initialize array of replicates: perm_replicates
perm_replicates = np.empty(size)
for i in range(size):
# Generate permutation sample
perm_sample_1, perm_sample_2 = permutation_sample(data_1,data_2)
# Compute the test statistic
perm_replicates[i] = func(perm_sample_1, perm_sample_2)
return perm_replicates
假设函数draw_perm_reps
,
data_1, data_2
分别代表两组数,也就是A组和B组。
func
代表使用的函数,如果是均值,可以使用numpy
包的np.mean
。
size
代表重复抽样次数。
permutation_sample
函数定义如下:
def permutation_sample(data1, data2):
"""Generate a permutation sample from two data sets."""
# Concatenate the data sets: data
data = np.concatenate((data1, data2))
# Permute the concatenated array: permuted_data
permuted_data = np.random.permutation(data)
# Split the permuted array into two: perm_sample_1, perm_sample_2
perm_sample_1 = permuted_data[:len(data1)]
perm_sample_2 = permuted_data[len(data1):]
return perm_sample_1, perm_sample_2
假设两组数来自统一分布,一次可以直接合并。
这里隐含的条件是如果表示统一分布,最后直接拒绝原假设即可。
np.concatenate
函数将两组数合并,
np.random.permutation
将数组随机重复抽样。
permuted_data[:len(data1)]
和permuted_data[len(data1):]
重新分a
和b
组。
例子如下。
假设:dems
和reps
来自同分布。
# Construct arrays of data: dems, reps
dems = np.array([True] * 153 + [False] * 91)
reps = np.array([True] * 136 + [False] * 35)
def frac_yay_dems(dems, reps):
"""Compute fraction of Democrat yay votes."""
frac = np.sum(dems) / len(dems)
return frac
# Acquire permutation samples: perm_replicates
perm_replicates = draw_perm_reps(dems, reps, frac_yay_dems, 10000)
# Compute and print p-value: p
p = np.sum(perm_replicates <= 153/244) / len(perm_replicates)
print('p-value =', p)
<script.py> output:
p-value = 0.0002
p-value = 0.0002
,因此显著,可以拒绝原假设。
Great work! This small p-value suggests that party identity had a lot to do with the voting. Importantly, the South had a higher fraction of Democrat representatives, and consequently also a more racist bias.
没太理解,是不是算反了。
To answer this question, you will evaluate the hypothesis that the party of a House member has no bearing on his or her vote. You will use the fraction of Democrats voting in favor as your test statistic and evaluate the probability of observing a fraction of Democrats voting in favor at least as small as the observed fraction of 153/244. (That’s right, at least as small as. In 1964, it was the Democrats who were less progressive on civil rights issues.) To do this, permute the party labels of the House voters and then arbitrarily divide them into “Democrats” and “Republicans” and compute the fraction of Democrats voting yay.
也就是说153/244这个结果是非常靠后的,因此Democrats是非常不努力,不进步的。
A time-on-website analog | Python
书签
We return to the no-hitter data set. In 1920, Major League Baseball implemented important rule changes that ended the so-called dead ball era.1 Importantly, the pitcher [^pitcher] was no longer allowed to spit on or scuff the ball,2 an activity that greatly favors pitchers. In this problem you will perform an A/B test to determine if these rule changes resulted in a slower rate of no-hitters (i.e., longer average time between no-hitters) using the difference in mean inter-no-hitter time as your test statistic. The inter-no-hitter times for the respective eras are stored in the arrays nht_dead and nht_live, where “nht” is meant to stand for “no-hitter time.”
这项规则修改支持了pitcher,因为hitter受到了限制。棒球规则我不是特别了解了。 \(\Box\)有空可以,把规则在这里稍微展开讲一下。
[^pitcher] 美音 /’pɪtʃɚ/ n. 投手;大水罐
[^hitter] 美音 /’hɪtɚ/ n. 打者,击手,(棒球)击球员
# Compute the observed difference in mean inter-no-hitter times: nht_diff_obs
nht_diff_obs = diff_of_means(nht_dead,nht_live)
# Acquire 10,000 permutation replicates of difference in mean no-hitter time: perm_replicates
perm_replicates = draw_perm_reps(nht_dead,nht_live,diff_of_means,size=10000)
# Compute and print the p-value: p
p = np.sum(perm_replicates <= nht_diff_obs) / len(perm_replicates)
print('p-val =',p)
这里的A/B test就是Permutation了。
Test of correlation | Python
\(\rho\)的测试也是用Permutation一轮推就好了。这里Permutation具体说一下,就是bootstrap样本,然后算\(\rho\)。
Simulating a null hypothesis concerning correlation | Python
The observed correlation between female illiteracy and fertility in the data set of 162 countries may just be by chance; the fertility of a given country may actually be totally independent of its illiteracy. You will test this null hypothesis in the next exercise.
To do the test, you need to simulate the data assuming the null hypothesis is true. Of the following choices, which is the best way to to do it?
\(\checkmark\)Do a permutation test: Permute the illiteracy values but leave the fertility values fixed to generate a new set of (illiteracy, fertility) data.
\(\times\)Do a permutation test: Permute both the illiteracy and fertility values to generate a new set of (illiteracy, fertility data).
This works perfectly, and is exact because it uses all data and eliminates any correlation because which illiteracy value pairs to which fertility value is shuffled. However, it is computationally inefficient not necessary to permute both illiteracy and fertility.
Hypothesis test on Pearson correlation | Python
def pearson_r(x, y):
"""Compute Pearson correlation coefficient between two arrays."""
# Compute correlation matrix: corr_mat
corr_mat = np.corrcoef(x,y)
# Return entry [0,1]
return corr_mat[0,1]
# Compute observed correlation: r_obs
r_obs = pearson_r(illiteracy, fertility)
# Initialize permutation replicates: perm_replicates
perm_replicates = np.empty(10000)
# Draw replicates
for i in range(10000):
# Permute illiteracy measurments: illiteracy_permuted
illiteracy_permuted = np.random.permutation(illiteracy)
# Compute Pearson correlation
perm_replicates[i] = pearson_r(illiteracy_permuted, fertility)
# Compute p-value: p
p = np.sum(perm_replicates > r_obs) / len(perm_replicates)
print('p-val =', p)
Do neonicotinoid insecticides have unintended consequences? | Python
As a final exercise in hypothesis testing before we put everything together in our case study in the next chapter, you will investigate the effects of neonicotinoid3 insecticides on bee reproduction. These insecticides are very widely used in the United States to combat aphids and other pests that damage plants.
# Compute x,y values for ECDFs
x_control, y_control = ecdf(control)
x_treated, y_treated = ecdf(treated)
# Plot the ECDFs
plt.plot(x_control, y_control, marker='.', linestyle='none')
plt.plot(x_treated, y_treated, marker='.', linestyle='none')
# Set the margins
plt.margins(0.02)
# Add a legend
plt.legend(('control', 'treated'), loc='lower right')
# Label axes and show plot
plt.xlabel('millions of alive sperm per mL')
plt.ylabel('ECDF')
plt.show()
Nice plot! The ECDFs show a pretty clear difference between the treatment and control; treated bees have fewer alive sperm. Let’s now do a hypothesis test in the next exercise.
还搞什么permutation on identical distribution了。
Bootstrap hypothesis test on bee sperm counts | Python
Now, you will test the following hypothesis: On average, male bees treated with neonicotinoid insecticide have the same number of active sperm per milliliter of semen than do untreated male bees. You will use the difference of means as your test statistic.
Bootstrap hypothesis test on bee sperm counts | Python
Now, you will test the following hypothesis: On average, male bees treated with neonicotinoid insecticide have the same number of active sperm per milliliter of semen than do untreated male bees. You will use the difference of means as your test statistic.
只测试样本的均值。
control - np.mean(control) + mean_count
在进行transfer的时候,使用的是
mean_count = np.mean(np.concatenate((control, treated)))
, 因此,这是一种变形的改良吗?
# Compute the difference in mean sperm count: diff_means
diff_means = np.mean(control) - np.mean(treated)
# Compute mean of pooled data: mean_count
mean_count = np.mean(np.concatenate((control, treated)))
# Generate shifted data sets
control_shifted = control - np.mean(control) + mean_count
treated_shifted = treated - np.mean(treated) + mean_count
# Generate bootstrap replicates
bs_reps_control = draw_bs_reps(control_shifted,
np.mean, size=10000)
bs_reps_treated = draw_bs_reps(treated_shifted,
np.mean, size=10000)
# Get replicates of difference of means: bs_replicates
bs_replicates = bs_reps_control - bs_reps_treated
# Compute and print p-value: p
p = np.sum(bs_replicates >= np.mean(control) - np.mean(treated)) \
/ len(bs_replicates)
print('p-value =', p)
Nice work! The p-value is small, most likely less than 0.0001, since you never saw a bootstrap replicated with a difference of means at least as extreme as what was observed. In fact, when I did the calculation with 10 million replicates, I got a p-value of 2e-05.
ECDFs of beak depths | Python
开始case study了,这个好像是说isolation的一个话题,有印象,搞起来。
Parameter estimates of beak depths | Python
这里是各自bootstrap,求\(\bar x\)的分布,然后求\(\Delta \bar x\)的分布,然后看真实值在分布中哪,比较好懂。
# Compute the difference of the sample means: mean_diff
mean_diff = np.mean(bd_2012) - np.mean(bd_1975)
# Get bootstrap replicates of means
bs_replicates_1975 = draw_bs_reps(bd_1975, np.mean, 10000)
bs_replicates_2012 = draw_bs_reps(bd_2012, np.mean, 10000)
# Compute samples of difference of means: bs_diff_replicates
bs_diff_replicates = bs_replicates_2012 - bs_replicates_1975
# Compute 95% confidence interval: conf_int
conf_int = np.percentile(bs_diff_replicates, [2.5, 97.5])
# Print the results
print('difference of means =', mean_diff, 'mm')
print('95% confidence interval =', conf_int, 'mm')
Hypothesis test: Are beaks deeper in 2012? | Python
Be careful! The hypothesis we are testing is not that the beak depths come from the same distribution. For that we could use a permutation test. The hypothesis is that the means are equal. To perform this hypothesis test, we need to shift the two data sets so that they have the same mean and then use bootstrap sampling to compute the difference of means.
# Compute mean of combined data set: combined_mean
combined_mean = np.mean(np.concatenate((bd_1975, bd_2012)))
# Shift the samples
bd_1975_shifted = bd_1975 - np.mean(bd_1975) + combined_mean
bd_2012_shifted = bd_2012 - np.mean(bd_2012) + combined_mean
# Get bootstrap replicates of shifted data sets
bs_replicates_1975 = draw_bs_reps(bd_1975_shifted, np.mean,10000)
bs_replicates_2012 = draw_bs_reps(bd_2012_shifted, np.mean,10000)
# Compute replicates of difference of means: bs_diff_replicates
bs_diff_replicates = bs_replicates_2012-bs_replicates_1975
# Compute the p-value
p = np.sum(bs_diff_replicates >= mean_diff) / len(bs_diff_replicates)
# Print p-value
print('p =', p)
The observed difference in means you computed in the last exercise is still in your namespace as
mean_diff
.
逻辑是 求出总体的mean, 两个都进行transfer, 分别bootstrap, 求\(\Delta \bar x\)的分布, 看位置。
We get a p-value of 0.0034, which suggests that there is a statistically significant difference. But remember: it is very important to know how different they are! In the previous exercise, you got a difference of 0.2 mm between the means. You should combine this with the statistical significance. Changing by 0.2 mm in 37 years is substantial by evolutionary standards. If it kept changing at that rate, the beak depth would double in only 400 years.
终于找到一个很好的理解practical significance的例子了。
同时,关联性也不是很大的一种情况, 也许这种原因可能是其他因素导致的,比如shape,因此引入OLS考虑。
在这,我们考虑这种因素是进化,但是我们现在否认了是进化导致的,而是shape,只要我们假设shape和进化没关系,那么我们就可以断定, 首先,practical significance不强,因此可忽略; 其次,shape导致其显著变化, 因此,两点体现了非进化所致。
EDA of beak length and depth | Python
# Make scatter plot of 1975 data
_ = plt.plot(bl_1975, bd_1975, marker='.',
linestyle='none', color='blue', alpha=0.5)
# Make scatter plot of 2012 data
_ = plt.plot(bl_2012, bd_2012, marker='.',
linestyle='none', color='red', alpha=0.5)
# Label axes and make legend
_ = plt.xlabel('beak length (mm)')
_ = plt.ylabel('beak depth (mm)')
_ = plt.legend(('1975', '2012'), loc='upper left')
# Show the plot
plt.show()
Linear regressions | Python
# Compute the linear regressions
slope_1975, intercept_1975 = np.polyfit(bl_1975, bd_1975, 1)
slope_2012, intercept_2012 = np.polyfit(bl_2012, bd_2012, 1)
# Perform pairs bootstrap for the linear regressions
bs_slope_reps_1975, bs_intercept_reps_1975 = \
draw_bs_pairs_linreg(bl_1975, bd_1975, size = 1000)
bs_slope_reps_2012, bs_intercept_reps_2012 = \
draw_bs_pairs_linreg(bl_2012, bd_2012, size = 1000)
# Compute confidence intervals of slopes
slope_conf_int_1975 = np.percentile(bs_slope_reps_1975, [2.5, 97.5])
slope_conf_int_2012 = np.percentile(bs_slope_reps_2012, [2.5, 97.5])
intercept_conf_int_1975 = np.percentile(bs_intercept_reps_1975, [2.5, 97.5])
intercept_conf_int_2012 = np.percentile(bs_intercept_reps_2012, [2.5, 97.5])
# Print the results
print('1975: slope =', slope_1975,
'conf int =', slope_conf_int_1975)
print('1975: intercept =', intercept_1975,
'conf int =', intercept_conf_int_1975)
print('2012: slope =', slope_2012,
'conf int =', slope_conf_int_2012)
print('2012: intercept =', intercept_2012,
'conf int =', intercept_conf_int_2012)
<script.py> output:
1975: slope = 0.465205169161 conf int = [ 0.33851226 0.59306491]
1975: intercept = 2.39087523658 conf int = [ 0.64892945 4.18037063]
2012: slope = 0.462630358835 conf int = [ 0.33137479 0.60695527]
2012: intercept = 2.97724749824 conf int = [ 1.06792753 4.70599387]
Displaying the linear regression results | Python
# Make scatter plot of 1975 data
_ = plt.plot(bl_1975, bd_1975, marker='.',
linestyle='none', color='blue', alpha=0.5)
# Make scatter plot of 2012 data
_ = plt.plot(bl_2012, bd_2012, marker='.',
linestyle='none', color='red', alpha=0.5)
# Label axes and make legend
_ = plt.xlabel('beak length (mm)')
_ = plt.ylabel('beak depth (mm)')
_ = plt.legend(('1975', '2012'), loc='upper left')
# Generate x-values for bootstrap lines: x
x = np.array([10, 17])
# Plot the bootstrap lines
for i in range(100):
plt.plot(x, bs_slope_reps_1975[i] * x + bs_intercept_reps_1975[i],
linewidth=0.5, alpha=0.2, color='blue')
plt.plot(x, bs_slope_reps_2012[i] * x + bs_intercept_reps_2012[i],
linewidth=0.5, alpha=0.2, color='red')
# Draw the plot again
plt.show()
The linear regressions showed interesting information about the beak geometry. The slope was the same in 1975 and 2012, suggesting that for every millimeter gained in beak length, the birds gained about half a millimeter in depth in both years.
Beak length to depth ratio | Python
# Compute length-to-depth ratios
ratio_1975 = bl_1975 / bd_1975
ratio_2012 = bl_2012 / bd_2012
# Compute means
mean_ratio_1975 = np.mean(ratio_1975)
mean_ratio_2012 = np.mean(ratio_2012)
# Generate bootstrap replicates of the means
bs_replicates_1975 = draw_bs_reps(ratio_1975,np.mean,10000)
bs_replicates_2012 = draw_bs_reps(ratio_2012,np.mean,10000)
# Compute the 99% confidence intervals
conf_int_1975 = np.percentile(bs_replicates_1975,[0.5,99.5])
conf_int_2012 = np.percentile(bs_replicates_2012,[0.5,99.5])
# Print the results
print('1975: mean ratio =', mean_ratio_1975,
'conf int =', conf_int_1975)
print('2012: mean ratio =', mean_ratio_2012,
'conf int =', conf_int_2012)
1975: mean ratio = 1.57888237719 conf int = [ 1.55668803 1.60073509]
2012: mean ratio = 1.46583422768 conf int = [ 1.44363932 1.48729149]
The mean beak length-to-depth ratio decreased by about 0.1, or 7%, from 1975 to 2012. The 99% confidence intervals are not even close to overlapping, so this is a real change. The beak shape changed.
Yes! When the confidence intervals are not even close to overlapping, the effect is much bigger than variation. You can do a p-value, but the result is already clear.
Correlation of offspring and parental data | Python
def draw_bs_pairs(x, y, func, size=1):
"""Perform pairs bootstrap for linear regression."""
# Set up array of indices to sample from: inds
inds = np.arange(len(x))
# Initialize replicates
bs_replicates = np.empty(size)
# Generate replicates
for i in range(size):
bs_inds = np.random.choice(inds, len(inds))
bs_x, bs_y = x[bs_inds], y[bs_inds]
bs_replicates[i] = func(bs_x, bs_y)
return bs_replicates
Pearson correlation of offspring and parental data | Python
# Compute the Pearson correlation coefficients
r_scandens = pearson_r(bd_parent_scandens, bd_offspring_scandens, )
r_fortis = pearson_r(bd_parent_fortis, bd_offspring_fortis)
# Acquire 1000 bootstrap replicates of Pearson r
bs_replicates_scandens = draw_bs_pairs(bd_parent_scandens, bd_offspring_scandens, pearson_r, 1000)
bs_replicates_fortis = draw_bs_pairs(bd_parent_fortis, bd_offspring_fortis, pearson_r, 1000)
# Compute 95% confidence intervals
conf_int_scandens = np.percentile(bs_replicates_scandens, [2.5,97.5])
conf_int_fortis = np.percentile(bs_replicates_fortis, [2.5,97.5])
# Print results
print('G. scandens:', r_scandens, conf_int_scandens)
print('G. fortis:', r_fortis, conf_int_fortis)
<script.py> output:
G. scandens: 0.41170636294 [ 0.26564228 0.54388972]
G. fortis: 0.728341239552 [ 0.6694112 0.77840616]
It is clear from the confidence intervals that beak depth of the offspring of G. fortis parents is more strongly correlated with their offspring than their G. scandens counterparts.
Measuring heritability | Python
Remember that the Pearson correlation coefficient is the ratio of the covariance to the geometric mean of the variances of the two data sets. This is a measure of the correlation between parents and offspring, but might not be the best estimate of heritability. If we stop and think, it makes more sense to define heritability as the ratio of the covariance between parent and offspring to the variance of the parents alone. In this exercise, you will estimate the heritability and perform a pairs bootstrap calculation to get the 95% confidence interval.
\[=\frac{Cov(a,b)}{Var(b)}\]
这个不就是传说中的OLS吗? 这哥们的统计真特么好!
This exercise highlights a very important point. Statistical inference (and data analysis in general) is not a plug-n-chug enterprise. You need to think carefully about the questions you are seeking to answer with your data and analyze them appropriately. If you are interested in how heritable traits are, the quantity we defined as the heritability is more apt than the off-the-shelf statistic, the Pearson correlation coefficient.
def heritability(parents, offspring):
"""Compute the heritability from parent and offspring samples."""
covariance_matrix = np.cov(parents, offspring)
return covariance_matrix[0,1] / covariance_matrix[0,0]
# Compute the heritability
heritability_scandens = heritability(bd_parent_scandens, bd_offspring_scandens)
heritability_fortis = heritability(bd_parent_fortis, bd_offspring_fortis)
# Acquire 1000 bootstrap replicates of heritability
replicates_scandens = draw_bs_pairs(
bd_parent_scandens, bd_offspring_scandens, heritability, size=1000)
replicates_fortis = draw_bs_pairs(
bd_parent_fortis, bd_offspring_fortis, heritability, size=1000)
# Compute 95% confidence intervals
conf_int_scandens = np.percentile(replicates_scandens,[2.5,97.5])
conf_int_fortis = np.percentile(replicates_fortis,[2.5,97.5])
# Print results
print('G. scandens:', heritability_scandens, conf_int_scandens)
print('G. fortis:', heritability_fortis, conf_int_fortis)
<script.py> output:
G. scandens: 0.548534086869 [ 0.34395487 0.75638267]
G. fortis: 0.722905191144 [ 0.64655013 0.79688342]
Here again, we see that G. fortis has stronger heritability than G. scandens. This suggests that the traits of G. fortis may be strongly incorporated into G. scandens by introgressive hybridization.
Is beak depth heritable at all in G. scandens? | Python
# Initialize array of replicates: perm_replicates
perm_replicates = np.empty(10000)
# Draw replicates
for i in range(10000):
# Permute parent beak depths
bd_parent_permuted = np.random.permutation(bd_parent_scandens)
perm_replicates[i] = heritability(bd_parent_permuted,
bd_offspring_scandens)
# Compute p-value: p
p = np.sum(perm_replicates >= heritability_scandens) / len(perm_replicates)
# Print the p-value
print('p-val =', p)
You get a p-value of zero, which means that none of the 10,000 permutation pairs replicates you drew had a heritability high enough to match that which was observed. This strongly suggests that beak depth is heritable in G. scandens, just not as much as in G. fortis. If you like, you can plot a histogram of the heritability replicates to get a feel for how extreme of a value of heritability you might expect by chance.
Statement of Accomplishment
全部完成的证书
Yu, Lijia. 2012. “置换检验.” 2012. http://yulijia.net/cn/%E7%BB%9F%E8%AE%A1%E5%8E%9F%E7%90%86/2012/12/20/permutation-test.html.
In baseball, the dead-ball era was the period between around 1900 (though some date it to the beginning of baseball) and the emergence of Babe Ruth as a power hitter [^hitter] in 1919. That year, Ruth hit a then-league record 29 home runs, a spectacular feat at that time.↩
拖着球。↩
新烟碱(英语:Neonicotinoid,又称为新烟碱)是一类和尼古丁相关的神经活性杀虫剂的总称。2013年4月29日,欧盟鉴于其对蜜蜂的巨大潜在危害(可能导致蜂群崩坏症候群的原因之一)而同意暂时禁止该类杀虫剂两年。↩