mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add Standard Deviation (#3039)
This commit is contained in:
22
src/main/java/com/thealgorithms/maths/StandardDeviation.java
Normal file
22
src/main/java/com/thealgorithms/maths/StandardDeviation.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
public class StandardDeviation {
|
||||
|
||||
public static double stdDev(double[] data)
|
||||
{
|
||||
double var = 0;
|
||||
double avg = 0;
|
||||
for (int i = 0; i < data.length; i++)
|
||||
{
|
||||
avg += data[i];
|
||||
}
|
||||
avg /= data.length;
|
||||
for (int j = 0; j < data.length; j++)
|
||||
{
|
||||
var += Math.pow((data[j] - avg), 2);
|
||||
}
|
||||
var /= data.length;
|
||||
return Math.sqrt(var);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user