## Posted By

vinotht on 06/17/11

# Calculate SD

/ Published in: Java

this simple program will calculate the SD(standard deviation) for the given input. Know more theory about standard deviation @ Standard Deviation Calculator

`public class StandardDeviation{   public static double mean ( double[] data )       {       double mean = 0;      final int n = data.length;      if ( n < 2 )         return Double.NaN;      for ( int i=0; i<n; i++ )         mean += data[i];      mean /= n;      double sum = 0;      for ( int i=0; i<n; i++ )         {         final double v = data[i] - mean;         sum += v * v;         }      return Math.sqrt( sum / ( n - 1 ) );      }   public static double sd  ( double[] data ){       final int n = data.length;       if ( n < 2 )         return Double.NaN;      double avg = data[0];      double sum = 0;      for ( int i = 1; i < data.length; i++ )         {         double newavg = avg + ( data[i] - avg ) / ( i + 1 );         sum += ( data[i] - avg ) * ( data [i] -newavg ) ;         avg = newavg;         }      return Math.sqrt( sum / ( n - 1 ) );      }   public static void main ( String[] args )      {      double[] data = { 10, 100 , 50};      System.out.println(mean(data));      System.out.println(sd (data));      }   }`

You need to login to post a comment.