 algorithms for calculating variance play a major role in statistical computing. a key problem in the design of good algorithms for this problem is that formulas for the variance may involve sums of squares, which can lead to numerical instability as well as to arithmetic overflow when dealing with large values. ==naÃ¯ve algorithm== a formula for calculating the variance of an entire population of size n is: :\sigma^2 = \bar{(x^2)} - \bar x^2 = \displaystyle\frac {\sum_{i=1}^n x_i^2 - (\sum_{i=1}^n x_i)^2/n}{n}. \! a formula for calculating an unbiased estimate of the population variance from a finite sample of n observations is: :s^2 = \displaystyle\frac {\sum_{i=1}^n x_i^2 - (\sum_{i=1}^n x_i)^2/n}{n-1}. \! therefore a naive algorithm to calculate the estimated variance is given by the following written in python programming language: def naive_variance(data): n = 0 sum = 0 sum_sqr = 0 for x in data: n = n + 1 sum = sum + x sum_sqr = sum_sqr + x*x variance = (sum_sqr - (sum*sum)/n)/(n - 1) return variance this algorithm can easily be adapted to compute the variance of a finite population: simply divide by n instead of n&nbsp;â&nbsp;1 on the last line. because sum_sqr and (sum*sum)/n can be very similar numbers, cancellation can lead to the precision of the result to be much less than the inherent precision of the floating-point arithmetic used to perform the computation. thus this algorithm should not be used in practice. this is particularly bad if the standard deviation is small relative to the mean. however, the algorithm can be improved by adopting the method of the assumed mean. ===computing shifted data=== we can use a property of the variance to avoid the catastrophic cancellation in this formula, namely the variance is invariant with respect to changes in a location parameter :\operatorname{var}(x-k)=\operatorname{var}(x). with k any constant, which leads to the new formula :s^2 = \displaystyle\frac {\sum_{i=1}^n (x_i-k)^2 - (\sum_{i=1}^n (x_i-k))^2/n}{n-1}. \! the closer is k to the mean value the more accurate will be the result, but just choosing
Statistical_algorithms Statistical_deviation_and_dispersion