Standard deviation java program

In this blog post, we will be discussing the Standard Deviation – what it is, why it is important and how to calculate it in Java with an example program.

What is Standard Deviation
Standard deviation is a statistical measure of the dispersion of a set of data from its mean. It is calculated as the square root of the variance of the data.
Standard deviation is represented by the Greek letter sigma (σ)
Why is Standard Deviation important
Standard Deviation is important because it is a measure of how spread out or dispersed the data is. It is used to determine how close the data is to the mean. It is also used to determine the variability of a data set.
This can be helpful in identifying trends or patterns
How to calculate Standard Deviation
Standard deviation can be calculated by finding the variance of the data and then taking the square root of the variance.
This is done by
1. Calculating the difference of each data point or data item from the mean.
2. Performing its square.
3. Adding 1 and 2. This is variance.
4. Square root of this variance is the standard deviation.

The Standard Deviation can be calculated in Java using the following formula:

Square root of ∑[(d[i] – μ) ^ 2)]/n

where ∑[(d[i] - μ) ^2)]/n is variance.

Here, d[i] is each data or array element,
μ is the mean of all elements,
n is the total number of elements.

Java Program to Calculate Standard Deviation
Following Java program calculates the Standard Deviation of a data set or an array of elements.

// define data set
int[] data = {35, 29, 65, 87, 51, 56, 44, 8};
double mean = 0, variance = 0;
// calculate mean
for(int i=0; i < data.length; i++) {
  mean += data[i];
}
mean = mean/data.length;
// calculate variance
for(int i=0; i < data.length; i++) {
  variance += (data[i] - mean)*(data[i] - mean);
}
variance = variance/data.length;
// calculate standard deviation
double stdDev = Math.sqrt(variance);
System.out.println("Standard Deviation is: "+ stdDev);

Output of this program is

Standard Deviation is: 22.413374913207516

Conclusion
In this article we looked into Standard Deviation – what it is, why it is important and how to calculate it with a java program.
Hope the article was useful.