Zho:找出数字的标准差

Finding the Standard Deviation of Numbers simply means figuring out how much the numbers deviate from each other, or basically how spread apart a set of numbers is. It is a common value used in Statistics.

Definition of Standard Deviation
Standard deviation is the square root of variance.

What is variance? Variance is the average of the squared differences from the mean. Both standard deviation and variance are measures of spread -- that is, how much much a set of numbers vary (of how far they are apart). Let's take a look at an example to make this clearer.

Let's say we have 5 flowers, whose heights are 25 centimeters, 60 centimeters, 40 centimeters, 45 centimeters, and 55 centimeters. The average of their heights is:

(25 + 60 + 40 + 45 + 55) / 5 = 45

This tells us that the average height of the flowers is 45 centimeters. What is the flowers' variance then?

Flower #1: ((25) - (45))^2 = (-20)^2 = 400

Flower #2: ((40) - (45))^2 = (-5)^2 = 25

Flower #3: ((45) - (45))^2 = (0)^2 = 0

Flower #4: ((55) - (45))^2 = (10)^2 = 100

Flower #5: ((60) - (45))^2 = (15)^2 = 225

(400 + 25 + 0 + 100 + 225) / 5 = 150

So the variance of the flowers is 150 centimeters. The standard deviation of the flowers is therefore equal to the square root of 150, or about 12.247...

There are two types of standard deviation though. One type of standard deviation, know as population standard deviation, is the standard deviation of an entire population. For example, if there were only 5 flowers in the world, then 12.247 would be the population standard deviation of the flowers' heights.

The other type of standard deviation is called sample standard deviation. Sample standard deviation is the standard deviation of only part of a population. For example, let's take the five flowers. There are obviously more than 5 flowers in the world, so the five flowers are only part of a population. A sample standard deviation would be needed.

The only difference between the two types of standard deviation is how variance is calculated. Population standard deviation would follow the rules described above. Sample standard deviation, though, would take the sum of the squared differences from the mean, and then divide that by the number of data points minus one. For example, let's take a look back at the flowers and recalculate their variance:

(400 + 25 + 0 + 100 + 225) / (5 - 1) = 187.5

Here, 5 is the number of flowers that have known heights. 1 is subtracted from that because this is a sample standard deviation. The last step in calculating the sample standard deviation is to take the square root of 187.5, which is about 13.693...

Variables
A 清单 will be needed during this tutorial:


 * Data

This list will hold all data samples, like the height of flowers. Meanwhile, seven 变量s will also be needed:


 * Average
 * Sum
 * Variance
 * Standard Deviation
 * Number
 * Sum2
 * Number2

Coding
The beginning of this tutorial will be for sample standard deviation.

The first step in figuring out sample standard deviation is figuring out the average of some numbers. The 程式 is shown below:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) // The variable (Number) helps keep track of what number the script is on. end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list))

The second step in calculating sample standard deviation is calculating the variance:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Sum2 v] 設為 (0) 變數 [Number2 v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum2 v] 改變 (((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average)) * ((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average))) 變數 [Number2 v] 改變 (1) end 變數 [Variance v] 設為 ((Sum2) / ((清單 [Data v] 的項目數 :: list) - (1)))

The final step in calculating sample standard deviation is taking the square root of variance:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Sum2 v] 設為 (0) 變數 [Number2 v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum2 v] 改變 (((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average)) * ((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average))) 變數 [Number2 v] 改變 (1) end 變數 [Variance v] 設為 ((Sum2) / ((清單 [Data v] 的項目數 :: list) - (1))) 變數 [Standard Deviation v] 設為 ([sqrt v] 數值 (Variance))

Only a small tweak is needed to calculate population standard deviation. The code for that would be:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Sum2 v] 設為 (0) 變數 [Number2 v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum2 v] 改變 (((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average)) * ((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average))) 變數 [Number2 v] 改變 (1) end 變數 [Variance v] 設為 ((Sum2) / ((清單 [Data v] 的項目數 :: list) - (1))) 變數 [Standard Deviation v] 設為 ([sqrt v] 數值 (Variance))

Final Product
The code for calculating sample standard deviation is:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Sum2 v] 設為 (0) 變數 [Number2 v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum2 v] 改變 (((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average)) * ((清單第 (Number2) 項項目\( [Data v] \) :: list) - (Average))) 變數 [Number2 v] 改變 (1) end 變數 [Variance v] 設為 ((Sum2) / ((清單 [Data v] 的項目數 :: list) - (1))) 變數 [Standard Deviation v] 設為 ([sqrt v] 數值 (Variance))

The code for calculating population standard deviation is:

當 @greenflag 被點擊 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (清單第 (Number) 項項目\( [Data v] \) :: list) 變數 [Number v] 改變 (1) end 變數 [Average v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Sum v] 設為 (0) // Resetting the variables. 變數 [Number v] 設為 (1) 重複 (清單 [Data v] 的項目數 :: list) 次 變數 [Sum v] 改變 (((清單第 (Number) 項項目\( [Data v] \) :: list) - (Average)) * ((清單第 (Number) 項項目\( [Data v] \) :: list) - (Average))) 變數 [Number v] 改變 (1) end 變數 [Variance v] 設為 ((Sum) / (清單 [Data v] 的項目數 :: list)) 變數 [Standard Deviation v] 設為 ([sqrt v] 數值 (Variance))

参见

 * Finding the Mode of Numbers
 * Finding the Mean of Numbers
 * Finding the Median of Numbers
 * Finding the Range of Numbers