[Quantproject-developers] QuantProject/b1_ADT/Statistics BasicFunctions.cs,1.1,1.2
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2004-08-02 23:20:35
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Statistics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28233/b1_ADT/Statistics Modified Files: BasicFunctions.cs Log Message: Added new static functions to compute the PearsonCorrelationCoefficient (r). Index: BasicFunctions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Statistics/BasicFunctions.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BasicFunctions.cs 17 Dec 2003 19:34:56 -0000 1.1 --- BasicFunctions.cs 2 Aug 2004 23:20:26 -0000 1.2 *************** *** 66,69 **** --- 66,102 ---- return System.Math.Sqrt(BasicFunctions.Variance(data)); } + + static public double PearsonCorrelationCoefficient( double[] firstDataVariable, + double[] secondDataVariable ) + { + BasicFunctions.checkLengthOfDataVariables(firstDataVariable, secondDataVariable); + double simpleAvgOfProduct = BasicFunctions.SimpleAverageOfProduct(firstDataVariable, secondDataVariable); + double productOfSimpleAvgs = BasicFunctions.SimpleAverage(firstDataVariable) * + BasicFunctions.SimpleAverage(secondDataVariable); + double stdDevOfFirst = BasicFunctions.StdDev(firstDataVariable); + double stdDevOfSecond = BasicFunctions.StdDev(secondDataVariable); + + return (simpleAvgOfProduct - productOfSimpleAvgs)/(stdDevOfFirst*stdDevOfSecond); + } + + static public double SimpleAverageOfProduct( double[] firstDataVariable, + double[] secondDataVariable ) + { + BasicFunctions.checkLengthOfDataVariables(firstDataVariable, secondDataVariable); + double[] productDataVariable = new double[firstDataVariable.Length]; + + for( int i = 0; i < firstDataVariable.Length ; i ++ ) + { + productDataVariable[i]= firstDataVariable[i]*secondDataVariable[i]; + } + + return BasicFunctions.SimpleAverage(productDataVariable); + } + static private void checkLengthOfDataVariables(double[] firstDataVariable, + double[] secondDataVariable) + { + if(firstDataVariable.Length !=secondDataVariable.Length) + throw new Exception("The two variables haven't the same length!"); + } } } |