官术网_书友最值得收藏!

A confidence interval

A confidence interval is a type of interval statistics for a population parameter. The confidence interval helps in determining the interval at which the population mean can be defined.

A confidence interval

Let's try to understand this concept by using an example. Let's take the height of every man in Kenya and determine with 95% confidence interval the average of height of Kenyan men at a national level.

Let's take 50 men and their height in centimeters:

>>> height_data = np.array([ 186.0, 180.0, 195.0, 189.0, 191.0, 177.0, 161.0, 177.0, 192.0, 182.0, 185.0, 192.0,
 173.0, 172.0, 191.0, 184.0, 193.0, 182.0, 190.0, 185.0, 181.0, 188.0, 179.0, 188.0,
 170.0, 179.0, 180.0, 189.0, 188.0, 185.0, 170.0, 197.0, 187.0, 182.0, 173.0, 179.0,
 184.0, 177.0, 190.0, 174.0, 203.0, 206.0, 173.0, 169.0, 178.0, 201.0, 198.0, 166.0,
 171.0, 180.0])

Plotting the distribution, it has a normal distribution:

>>> plt.hist(height_data, 30, normed=True)
>>> plt.show()
A confidence interval

The mean of the distribution is as follows:

>>> height_data.mean()

183.24000000000001

So, the average height of a man from the sample is 183.4 cm.

To determine the confidence interval, we'll now define the standard error of the mean.

The standard error of the mean is the deviation of the sample mean from the population mean. It is defined using the following formula:

A confidence interval

Here, s is the standard deviation of the sample, and n is the number of elements of the sample.

This can be calculated using the sem() function of the SciPy package:

>>> stats.sem(height_data)

1.3787187190005252

So, there is a standard error of the mean of 1.38 cm. The lower and upper limit of the confidence interval can be determined by using the following formula:

Upper/Lower limit = mean(height) + / - sigma * SEmean(x)

For lower limit:

183.24 + (1.96 * 1.38) = 185.94

For upper limit:

183.24 - (1.96*1.38) = 180.53

A 1.96 standard deviation covers 95% of area in the normal distribution.

We can confidently say that the population mean lies between 180.53 cm and 185.94 cm of height.

A confidence interval

Let's assume we take a sample of 50 people, record their height, and then repeat this process 30 times. We can then plot the averages of each sample and observe the distribution.

A confidence interval

The commands that simulated the preceding plot is as follows:

>>> average_height = []
>>> for i in xrange(30):
>>> sample50 = np.random.normal(183, 10, 50).round()
>>> average_height.append(sample50.mean())

>>> plt.hist(average_height, 20, normed=True)
>>> plt.show()

You can observe that the mean ranges from 180 to 187 cm when we simulated the average height of 50 sample men, which was taken 30 times.

Let's see what happens when we sample 1000 men and repeat the process 30 times:

>>> average_height = []
>>> for i in xrange(30):
>>> sample1000 = np.random.normal(183, 10, 1000).round()
>>> average_height.append(sample1000.mean())

>>> plt.hist(average_height, 10, normed=True)
>>> plt.show()
A confidence interval

As you can see, the height varies from 182.4 cm and to 183.4 cm. What does this mean?

It means that as the sample size increases, the standard error of the mean decreases, which also means that the confidence interval becomes narrower, and we can tell with certainty the interval that the population mean would lie on.

主站蜘蛛池模板: 遂宁市| 文成县| 赣州市| 吉木乃县| 长武县| 东港市| 海伦市| 原平市| 邹城市| 佛山市| 贵州省| 苍溪县| 凭祥市| 南漳县| 雷波县| 三亚市| 梓潼县| 三台县| 汽车| 台安县| 东源县| 保康县| 江安县| 四会市| 且末县| 四平市| 大庆市| 咸丰县| 前郭尔| 安丘市| 墨玉县| 义乌市| 文山县| 开化县| 尼木县| 凤台县| 江口县| 娱乐| 英超| 遂平县| 杭锦旗|