Java code To Calculate Standard Deviation – In this article, we will brief in on all the possible ways to calculate standard deviation in Java Programming. Suitable examples and sample programs have been included in order to make you understand simply. The compiler has also been added so that you can execute the programs yourself.
The methods used in this article are as follows:
- Using Standard Method
- Using Scanner Class
- Using Command Line Arguments
- Using Static Method
- Using Separate Class
Standard Deviation in general terms can be explained as the divergence of the participants from the mean value among the group of values.
Mathematically, it is the same. It is the quantity which expresses the variation of the group from the mean value.
As you can see, the formula of Standard Deviation is as follows:
S = [(Sum of (xi – x)2)/n-1]^1/2
where
- n = number of data points
- xi = values of the data
x= Mean
Thus, the various ways to calculate the standard deviation in Java Programming is as follows:
Standard Deviation Java Code
This methodology is where the entire code is written in the main method itself.
Here, the input is an array of integers or decimal numbers hence we take the input type to be double.
Using double type, after calculations performed on this, our end result will also be a double. So our result Standard Deviation is of double type as well.
In the standard method we take inputs in the code itself which are fixed and the length of array is also fixed. In order to arrive at the standard deviation of the set of numbers we have step wise process.
- Firstly, the sum of all the numbers in the array has to be calculated. So we traverse through the entire array and add every element to find the sum.
sum=sum+input[i];
- After this the mean has to be found. The formula for mean is: Mean=Sum/Total Number.
mean=sum/n;
- After finding the mean, we find the variance of the numbers. The formula to find the variance is Variance= Sum of Square of Difference of mean with individual element/Total Number.
sum+=Math.pow((input[i]-mean),2); mean=sum/(n-1);
- Lastly, Standard Deviation is square-root of variance.
deviation=Math.sqrt(mean);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class MSD { public static void main(String args[]) { double[] input={5,10,15,20,25}; double n=5,sum=0,mean; for(int i=0;i<n;i++) { sum=sum+input[i]; } mean=sum/n; System.out.println("Mean :"+mean); sum=0; for(int i=0;i<n;i++) { sum+=Math.pow((input[i]-mean),2); } mean=sum/(n-1); double deviation=Math.sqrt(mean); System.out.println("standard deviation :"+deviation); } } |
Output:
1 2 |
Mean :15.0 standard deviation :7.905694150420948 |
Using Scanner Class
Any value predetermined decreases the efficiency of code. In such a scenario, if something as important as input is fixed then the code is not at its optimal state.
Using Scanner Class in Java, inputs can be read at runtime and can be given according to the user/tester and can always be changed without the strain of going through the code.
So at run time itself when the input is being read using scanner class, the sum is being performed. Followed by which we find the mean, variance and standard deviation as mentioned above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class MeanAndStandardDeviation { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter a number"); int n=sc.nextInt(); double[] input=new double[n]; double sum=0,mean; System.out.println("enter n elements"); for(int i=0;i<n;i++) { input[i]=sc.nextDouble(); sum=sum+input[i]; } mean=sum/n; System.out.println("Mean :"+mean); sum=0; for(int i=0;i<n;i++) { sum+=Math.pow((input[i]-mean),2); } mean=sum/(n-1); double deviation=Math.sqrt(mean); System.out.println("standard deviation :"+deviation); } } |
Output:
1 2 3 4 5 6 7 8 9 |
enter a number 5 enter n elements 13 23 12 44 55 Mean :29.4 |
Using Command Line Arguments
Another way of taking input from the user is via command line arguments. When giving the run command, the number of elements followed by the elements can be given in the command itself with a space in between each of them.
These arguments can then be stored in the variables such that arg[0] is the number of elements (n) or size of array followed by n elements as individual elements of the array.
While storing these values, they are converted into integers or long type based on requirement. After gathering these values the same steps as mentioned above are to be followed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; class MeanAndStandardDeviation { public static void main(String args[]) { int n=Integer.parseInt(args[0]); System.out.println("n="+n); long[] input=new long[n]; System.out.println("n elements are"); for(int i=0;i<n;i++) { System.out.println(args[i+1]); } long sum=0,mean; for(int i=0;i<n;i++) { input[i]=Long.parseLong(args[i+1]); sum=sum+input[i]; } mean=sum/n; System.out.println("Mean :"+mean); sum=0; for(int i=0;i<n;i++) { sum+=Math.pow((input[i]-mean),2); } mean=sum/(n-1); double deviation=Math.sqrt(mean); System.out.println("standard deviation :"+deviation); } } |
Using Static method
In all the above methods, the entire code was written in the main method only.
Doing that could give rise to difficulties when someone wants to make changes.
So, to overcome this we can make use of a separate static method within the same class.
The gathering of essential inputs can be performed in the main method and then this separate static method (Calculations) can be called in the main method.
This static method (Calculations) can then contain all the necessary operations that are to be performed by finding mean and finally the standard deviation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.Scanner; class MeanAndStandardDeviation { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("enter a number"); int n=sc.nextInt(); double[] input=new double[n]; double sum=0,mean; System.out.println("enter "+n+" elements"); for(int i=0;i<n;i++) { input[i]=sc.nextDouble(); } Calculation(n, input); } static void Calculation(int n,double in[]) { double sum=0; for(int i=0;i<n;i++) { sum=sum+in[i]; } double mean=sum/n; System.out.println("Mean :"+mean); sum=0; for(int i=0;i<n;i++) { sum+=Math.pow((in[i]-mean),2); } mean=sum/(n-1); double deviation=Math.sqrt(mean); System.out.println("standard deviation :"+deviation); } } |
Output1:
1 2 3 4 5 6 7 8 9 |
enter a number 4 enter 4 elements 25 50 75 100 Mean :62.5 standard deviation :32.274861218395145 |