# Java Program to compute standard deviation,variance of population & sample with example.

In continuation to my earlier **Java code to find all permutations of a String using recursion**, let us write another useful java program to compute Variance and Standard Deviation. Before writing the java program, let us understand that what is Standard Deviation? and how to calculate the variance and the standard deviation with suitable steps and example. Also let us see that how standard deviation is applied in real life applications. The standard deviation gives an idea of how widely individual observations / values in a group are dispersed from the mean. (i.e. indicates that how much variation is there from the average). A large standard deviation indicates that the data points are far from the mean and a small standard deviation indicates that they are close to the mean. Volatility / Risk is measured using standard deviations in real life applications such as trading business. Investors can use standard deviation to measure the risk. The formula for computing standard deviation is given below. Also it is important to distinguish between the standard deviation of a population and the standard deviation of a sample. They are computed differently.

The standard deviation of a population is calculated by the below formula.

**std = sqrt [ sum (square( Xi – X )) / N ] **

where Xi is the data item and X is the mean.

OR

The standard deviation of a sample is calculated by different formula.

**std = sqrt [ sum (square( Xi – X )) / (n-1) ]**

where Xi is the data item and X is the mean.

OR

Let’s say we have a set of n numbers x1, x2, . . . , xn .

Here are the steps to compute Standard deviation

**1. Compute the mean.(Average of the given numbers)**

a) Compute the sum of all numbers

b) Divide the sum by the number of observations (n)

**2. Compute the Variance**

a) For each data value (Xi), Calculate its deviation from the mean (Substract the mean from the data value) and Square it (this removes the negative).

c) Sum the squared deviations.

d) Divide the sum by the number of observations (n) (For sample standard deviation, divide by n-1)

**3. Compute the Standard Deviation**

a) Compute the square root of the variance.

Consider a simple example. Let us calculate the standard deviation of price variations. Prices of stock ABC is given below for period of one week.

Day 1 : Rs. 99/-

Day 2 : Rs. 100/-

Day 3 : Rs. 96/-

Day 4 : Rs. 100/-

Day 5 : Rs. 106/-

Day 6 : Rs. 102/-

Day 7 : Rs. 98/-

Calculate the average price, add up all prices and divide by the number of days:

(99+100+96+100 + 106 + 102 + 98 ) / 7 = Rs. 100.14

Let us calculate the difference between each daily price and the average price and square of that:

**Periods Price Average Difference Square**

Day 1 : 99 – 100.14 -1.14 1.30

Day 2 : 100 – 100.14 -0.14 0.02

Day 3 : 96 – 100.14 -4.14 17.14

Day 4 : 100 – 100.14 -0.14 0.02

Day 5 : 106 – 100.14 5.86 34.34

Day 6 : 102 – 100.14 1.86 3.46

Day 7 : 98 – 100.14 -2.14 4.58

Standard deviation = Squareroot( 1.30+ 0.02+17.14+ 0.02+34.34+2.46 + 4.58)/7) = 2.94

In the above example, average is 100.14 , and 1 standard deviation is 2.94. So the price can deviate within plus or minus of Rs. 2.94 from the average price of Rs. 100.14. So 68.27% of the time, the price will be within the range of Rs. 97.2 and Rs. 103.08.

2 standard deviation is 2.94*2 = 5.88, So 95.45% of the time , the price will be within the range of Rs. 94.26 and Rs. 106.02.

package com.javaonline; //Calculate the standard deviation public class Stdev { double getStdDev(String s, double...args) { return Math.sqrt(getVariance(s, args)); } //Calculate the Variance double getVariance(String str, double...args) { double mean = getMean( args); double diff=0 , temp = 0, square=0 , variance=0.0; int i=0; System.out.println("Periods " + " Mean " + " Difference " + " Square " ); System.out.println("----------------------------------------------------------------------------------- " ); for(double data :args) { i=i+1; diff=data-mean; square=diff*diff; System.out.println("Day "+i + " " + data + " " + mean + " " + diff + " " + square); temp += square; } System.out.println("----------------------------------------------------------------------------------- " ); if (str.equals("P")) variance= temp/args.length; else variance= temp/(args.length-1); System.out.println("\nVariance = " + variance+"\n"); return variance; } //Calculate the Average double getMean( double...args) { double sum = 0.0; for(double data : args) sum += data; return sum/args.length; } public static void main(String args[]) { Stdev std=new Stdev(); System.out.println("standard deviation of a population = " + std.getStdDev("P", 99, 100, 96, 100, 106, 102 , 98 ) +"\n"); System.out.println("standard deviation of a sample = " + std.getStdDev("S", 99, 100, 96, 100, 106, 102 , 98 ) +"\n"); } }

**Output :**

## Leave a Reply

You must be logged in to post a comment.