标准差与正态曲线
我们知道均值是直方图的平衡点。与均值不同,SD通常不容易通过观察直方图来识别。
然而,有一种分布形状中,SD几乎和均值一样容易识别。那就是钟形分布。本节将考察这种形状,因为它经常出现在概率直方图以及一些数据直方图中。
from datascience import *
%matplotlib inline
path_data = '../../../assets/data/'
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
import math
import numpy as np
一个大致钟形的数据直方图
让我们看一下我们熟悉的1,174对母婴样本中母亲身高的分布。母亲身高的均值为64英寸,SD为2.5英寸。与篮球运动员的身高不同,母亲的身高在均值周围以钟形曲线相当对称地分布。
baby = Table.read_table(path_data + 'baby.csv')
heights = baby.column('Maternal Height')
mean_height = np.round(np.mean(heights), 1)
mean_height
64.0sd_height = np.round(np.std(heights), 1)
sd_height
2.5baby.hist('Maternal Height', bins=np.arange(55.5, 72.5, 1), unit='inch')
positions = np.arange(-3, 3.1, 1)*sd_height + mean_height
plots.xticks(positions);
Histogram with 'Maternal Height (inch)' on the x-axis and 'Percent per unit' on the y-axis. The data is symmetric about x=64. The range of the data is approximately x=57 to x=71.5.上方单元格中最后两行代码更改了横轴的标签。现在,标签对应于 $z = 0, \pm 1, \pm 2$ 和 $\pm 3$ 时的“平均值 $\pm$ $z$ 个SD”。由于分布的形状,“中心”具有明确含义,并在64处清晰可见。
如何在钟形曲线上识别SD
要了解SD如何与曲线相关,从曲线的顶部开始向右看。注意有一个曲线从“倒扣的杯子”变为“正放的杯子”的地方;正式地说,曲线有一个拐点。该点比平均值高一个SD。它是 $z=1$ 的点,即“平均值加1个SD” = 66.5英寸。
对称地,在均值的左侧,拐点位于 $z=-1$,即“平均值减1个SD” = 61.5英寸。
一般来说,对于钟形分布,SD是均值与两侧拐点之间的距离。
标准正态曲线
我们看到的所有钟形直方图除了坐标轴标签外,看起来基本上相同。事实上,确实只有一个基本曲线,只需适当重新标记坐标轴,就可以从中绘制所有这些曲线。
为了绘制那条基本曲线,我们将使用我们可以将每个列表转换成的单位:标准单位。因此,得到的曲线称为“标准正态曲线”(standard normal curve)。
标准正态曲线有一个令人印象深刻的方程。但现在,最好将其视为一个以标准单位测量且具有钟形分布的变量的直方图的平滑轮廓。
$$ \phi(z) = {\frac{1}{\sqrt{2 \pi}}} e^{-\frac{1}{2}z^2}, ~~ -\infty < z < \infty $$
# The standard normal curve
plot_normal_cdf()
Plot is titled 'Normal Curve ~ (mu = 0, sigma = 1) negative infinity < z < infinity' with 'z' on the x-axis and 'phi(z)' on the y-axis. The curve has positive y values from about x=-3 to x=3 with a peak at x=0. The data is symmetric about 0 and bell shaped.与检查新直方图时一样,从观察横轴开始。在标准正态曲线的横轴上,值是标准单位。
以下是该曲线的一些性质。有些通过观察显而易见,其他则需要相当多的数学知识才能确立。
-
曲线下的总面积为1。因此,你可以将其视为以密度尺度绘制的直方图。
-
曲线关于0对称。因此,如果一个变量具有此分布,其均值和中位数均为0。
-
曲线的拐点在-1和+1处。
-
如果一个变量具有此分布,其SD为1。正态曲线是极少数在直方图上SD如此清晰可辨的分布之一。
由于我们将曲线视为平滑的直方图,我们将通过曲线下的面积来表示数据总量的比例。
平滑曲线下的面积通常通过微积分使用一种称为积分的方法来求得。然而,数学上的一个事实是,标准正态曲线不能通过微积分的任何通常方法进行积分。
因此,曲线下的面积必须近似计算。这就是为什么几乎所有的统计教科书都包含正态曲线下的面积表。这也是为什么所有统计系统,包括Python的一个模块,都包含提供这些面积的良好近似值的方法。
from scipy import stats
标准正态“cdf”
查找正态曲线下面积的基本函数是 stats.norm.cdf。它接受一个数值参数,并返回该数值左侧曲线下的所有面积。正式地,它被称为标准正态曲线的“累积分布函数”。这个相当拗口的术语缩写为cdf。
让我们使用这个函数来找出标准正态曲线下 $z=1$ 左侧的面积。
# Area under the standard normal curve, below 1
plot_normal_cdf(1)
The same graph showing the Normal Curve is reproduced with the addition of a shaded region. The area under the curve to the left of x=1 is shaded gold. Previous description of the graph is: Plot is titled 'Normal Curve ~ (mu = 0, sigma = 1) negative infinity < z < infinity' with 'z' on the x-axis and 'phi(z)' on the y-axis. The curve has positive y values from about x=-3 to x=3 with a peak at x=0. The data is symmetric about 0 and bell shaped.阴影区域的数值可以通过调用 stats.norm.cdf 来找到。
stats.norm.cdf(1)
0.8413447460685429大约是84%。我们现在可以利用曲线的对称性和曲线下总面积为1的事实来找出其他面积。
$z=1$ 右侧的面积约为100% - 84% = 16%。
# Area under the standard normal curve, above 1
plot_normal_cdf(lbound=1)
The same graph showing the Normal Curve is reproduced with a different region shaded. This time, the area under the curve the right of x=1 is shaded gold. The previous description provided for the graph is: Plot is titled 'Normal Curve ~ (mu = 0, sigma = 1) negative infinity < z < infinity' with 'z' on the x-axis and 'phi(z)' on the y-axis. The curve has positive y values from about x=-3 to x=3 with a peak at x=0. The data is symmetric about 0 and bell shaped.1 - stats.norm.cdf(1)
0.15865525393145707$z=-1$ 和 $z=1$ 之间的面积可以用几种不同的方式计算。它是下面曲线下的金色区域。
# Area under the standard normal curve, between -1 and 1
plot_normal_cdf(1, lbound=-1)
The same graph showing the Normal Curve is reproduced with different regions shaded. In dark blue, the area under the curve is shaded from about x=-3 to x=-1, and in gold the area under the curve from x=-1 to x=1 is shaded. The previous description for the graph is: Plot is titled 'Normal Curve ~ (mu = 0, sigma = 1) negative infinity < z < infinity' with 'z' on the x-axis and 'phi(z)' on the y-axis. The curve has positive y values from about x=-3 to x=3 with a peak at x=0. The data is symmetric about 0 and bell shaped.例如,我们可以将面积计算为“100% - 两个相等的尾部”,结果大约为100% - 2x16% = 68%。
或者我们可以注意到,$z=1$ 和 $z=-1$ 之间的面积等于 $z=1$ 左侧的所有面积减去 $z=-1$ 左侧的所有面积。
stats.norm.cdf(1) - stats.norm.cdf(-1)
0.6826894921370859通过类似的计算,我们看到 $-2$ 和 2 之间的面积约为95%。
# Area under the standard normal curve, between -2 and 2
plot_normal_cdf(2, lbound=-2)
The same graph showing the Normal Curve is reproduced with different regions shaded. In dark blue, the area under the curve is shaded from about x=-3 to x=-2, and in gold the area under the curve from x=-2 to x=2 is shaded. The previous description for the graph is: Plot is titled 'Normal Curve ~ (mu = 0, sigma = 1) negative infinity < z < infinity' with 'z' on the x-axis and 'phi(z)' on the y-axis. The curve has positive y values from about x=-3 to x=3 with a peak at x=0. The data is symmetric about 0 and bell shaped.stats.norm.cdf(2) - stats.norm.cdf(-2)
0.9544997361036416换句话说,如果直方图大致呈钟形,那么在“平均值 $\pm$ 2个SD”范围内的数据比例约为95%。
这比切比雪夫75%的下界高出不少。切比雪夫界较弱,因为它必须适用于所有分布。如果我们知道一个分布是正态的,我们对比例就有良好的近似,而不仅仅是界。
下表比较了我们对所有分布和正态分布的了解。注意当 $z=1$ 时,切比雪夫界虽然正确,但没什么启发性。
| 范围内百分比 | 所有分布:下界 | 正态分布:近似值 |
|---|---|---|
| 平均值 $\pm$ 1个SD | 至少0% | 约68% |
| 平均值 $\pm$ 2个SD | 至少75% | 约95% |
| 平均值 $\pm$ 3个SD | 至少88.888...% | 约99.73% |