Home Page   |   Products   |   Customer Service   |   About Us   |   Contact Us   |   Search
 
Home > Source Code > Statistics
 

Statistics


Download source code

A few weeks ago I needed to find the correlation between 2 variables at my work.

I began searching the net for the words correlation and pearson but couldn't find any decent piece

of code.

Let's define the word correlation first,

"A correlation gives the strength of the relationship between variables"

( mathworld.wolfram.com )

A normalized correlation is called Pearson Value. The Pearson Value ranges between -1 to +1.

A correlation of +1 means that there is a perfect positive linear relationship between the variables.

If it's -1 then there is a perfect negative relationship and 0 means no relationship at all.

In order to get the covariance and pearson we need to get a few things first.

Average

We sum up all values and divide it by the number of values.

  /// <summary>
  /// Get average
  /// </summary>
  public static double GetAverage( double[] data )
  {
   int len = data.Length;
   if ( len == 0 )
    throw new Exception("No data");
    
   double sum = 0;
   for ( int i = 0; i < data.Length; i++ )
    sum += data[i];
   return sum / len;
  }
   
  

Variance & standard deviation

The variance is the squared differences from the average.

The standard deviation is the square root of the variance. 

  /// <summary>
  /// Get variance
  /// </summary>
  public static double GetVariance( double[] data )
  {
   int len = data.Length;
   // Get average
   double avg = GetAverage( data );
    
   double sum = 0;
   for ( int i = 0; i < data.Length; i++ )
    sum += Math.Pow( ( data[i] - avg ), 2 );
   return sum / len;
  }
  /// <summary>
  /// Get standard deviation
  /// </summary>
  public static double GetStdev( double[] data )
  {
   return Math.Sqrt( GetVariance( data ) );
  } 

Covariance & Pearson

To calculate covariance we need to get the average and standard deviation for each variable.

We sum up the multiplication of x - Avg(x) and y - Avg(y) and finally divide it by the length of

the variables.

To get the pearson value we divide the covariance by the multiplication of stDevX and stDevY.

  /// <summary>
  /// Get correlation
  /// </summary>
  public static void GetCorrelation( double[] x, double[] y, ref double covXY, ref double pearson)
  {
   if ( x.Length != y.Length )
    throw new Exception("Length of sources is different");
   double avgX = GetAverage( x );
   double stdevX = GetStdev( x );
   double avgY = GetAverage( y );
   double stdevY = GetStdev( y );
   int len = x.Length;
   
   for ( int i = 0; i < len; i++ )
    covXY += ( x[i] - avgX ) * ( y[i] - avgY );
   covXY /= len;
   pearson = covXY / ( stdevX * stdevY );
  }

Moving Average

Moving averages are one of the oldest and most popular of technical analysis tools.

The code is too long to put in here.

 
Share with others:   
 
  Webmaster: Eran Aharonovich © All rights reserved to Eran Aharonovich 2007