[Quantproject-developers] QuantProject/b1_ADT/Econometrics LinearRegression.cs, 1.2, 1.3
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2012-04-30 23:37:49
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Econometrics
In directory vz-cvs-3.sog:/tmp/cvs-serv30293/b1_ADT/Econometrics
Modified Files:
LinearRegression.cs
Log Message:
The read only property
public double[] Residuals
has been added.
The read only property
public double CenteredTotalSumOfSquares
has been added
IMPORTANT: the read only property
public double PredictedResidualsSumOfSquares
has been added.
It is the PRESS statistic, a statistic based on the leave-one-out technique. This statistic is much more robust than Rsquare, to outliers.
The read only property
public double PredictedCenteredTotalSumOfSquares
has been added.
IMPORTANT: the read only property
public double CenteredPSquare
has been added.
It is a normalized version of the the PRESS statistics, and it can be used as a fitness alternative the CenteredRSquare: the CenteredPSquare is much more robust with respect to outliers.
Index: LinearRegression.cs
===================================================================
RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Econometrics/LinearRegression.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** LinearRegression.cs 6 Jan 2011 18:31:18 -0000 1.2
--- LinearRegression.cs 30 Apr 2012 23:37:46 -0000 1.3
***************
*** 34,37 ****
--- 34,39 ----
public class LinearRegression : ILinearRegression
{
+ private double[] regressand;
+ private double[,] regressors;
private double[,] covarianceMatrix; // (XTransposeX)^-1
private double sumOfSquareResiduals;
***************
*** 51,54 ****
--- 53,116 ----
}
+ #region HatMatrixDiagonal
+ private double[] hatMatrixDiagonal;
+
+ #region setHatMatrixDiagonal
+
+ #region getHatMatrixDiagonal
+
+ #region get_i_th_row_of_X_XtransposeX_inverse
+ private double get_i_th_row_of_X_XtransposeX_inverse( int i , int j )
+ {
+ double partialSum = 0;
+ for( int q = 0 ; q < this.regressors.GetLength( 1 ) ; q++ )
+ partialSum += this.regressors[ i , q ] *
+ this.covarianceMatrix[ q , j ];
+ return partialSum;
+ }
+
+ private double[] get_i_th_row_of_X_XtransposeX_inverse( int i )
+ {
+ double[] i_th_row_of_X_XtransposeX_inverse = new double[ this.regressors.GetLength( 1 ) ];
+ for( int j = 0 ; j < this.regressors.GetLength( 1 ) ; j++ )
+ i_th_row_of_X_XtransposeX_inverse[ j ] =
+ this.get_i_th_row_of_X_XtransposeX_inverse( i , j );
+ return i_th_row_of_X_XtransposeX_inverse;
+ }
+ #endregion get_i_th_row_of_X_XtransposeX_inverse
+
+ private double getHatMatrixDiagonal( int i )
+ {
+ double[] i_th_row_of_X_XtransposeX_inverse =
+ this.get_i_th_row_of_X_XtransposeX_inverse( i );
+ double partialSum = 0;
+ for( int j = 0 ; j < this.regressors.GetLength( 1 ) ; j++ )
+ partialSum += i_th_row_of_X_XtransposeX_inverse[ j ] *
+ this.regressors[ i , j ];
+ return partialSum;
+ }
+ #endregion getHatMatrixDiagonal
+
+ private void setHatMatrixDiagonal()
+ {
+ this.hatMatrixDiagonal = new double[ this.regressand.Length ];
+ for( int i = 0 ; i < hatMatrixDiagonal.Length ; i++ )
+ this.hatMatrixDiagonal[ i ] = this.getHatMatrixDiagonal( i );
+ }
+ #endregion setHatMatrixDiagonal
+
+ /// <summary>
+ /// returns the diagonal of the hat matrix
+ /// </summary>
+ public double[] HatMatrixDiagonal
+ {
+ get{
+ if ( this.hatMatrixDiagonal == null )
+ this.setHatMatrixDiagonal();
+ return this.hatMatrixDiagonal;
+ }
+ }
+ #endregion HatMatrixDiagonal
+
private double centeredRSquare;
***************
*** 57,62 ****
--- 119,261 ----
}
+ #region Residuals
+
+ private double[] residuals;
+
+ #region setResiduals
+
+ #region setResidual
+
+ private double getPredictedValue( int i )
+ {
+ double predictedValue = 0;
+ for ( int j = 0 ; j < this.EstimatedCoefficients.Length ; j++ )
+ predictedValue +=
+ this.estimatedCoefficients[ j ] * this.regressors[ i , j ];
+ return predictedValue;
+ }
+
+ private void setResidual( int i )
+ {
+ double predictedValue = this.getPredictedValue( i );
+ this.residuals[ i ] = this.regressand[ i ] - predictedValue;
+ }
+ #endregion setResidual
+
+ private void setResiduals()
+ {
+ this.residuals = new double[ this.regressand.Length ];
+ for( int i = 0 ; i < this.regressand.Length ; i++ )
+ this.setResidual( i );
+ }
+ #endregion setResiduals
+
+ public double[] Residuals {
+ get {
+ if ( this.residuals == null )
+ this.setResiduals();
+ return this.residuals; }
+ }
+ #endregion Residuals
+
+
+ #region CenteredTotalSumOfSquares
+
+ #region setCenteredTotalSumOfSquares
+ private double getYBar()
+ {
+ double partialSum = 0;
+ for( int i = 0 ; i < this.regressand.Length ; i++ )
+ partialSum += this.regressand[ i ];
+
+ double yBar = partialSum / this.regressand.Length;
+ return yBar;
+ }
+ private void setCenteredTotalSumOfSquares()
+ {
+ double partialSum = 0;
+ double yBar = this.getYBar();
+ for( int i = 0 ; i < this.regressand.Length ; i++ )
+ partialSum += Math.Pow( this.regressand[ i ] - yBar , 2 );
+ this.centeredTotalSumOfSquares = partialSum;
+ }
+ #endregion setCenteredTotalSumOfSquares
+
+ public double CenteredTotalSumOfSquares
+ {
+ get
+ {
+ if ( this.centeredTotalSumOfSquares == double.MinValue )
+ this.setCenteredTotalSumOfSquares();
+ return this.centeredTotalSumOfSquares;
+ }
+ }
+ #endregion CenteredTotalSumOfSquares
+
+ #region PredictedResidualsSumOfSquares
+
+ private double predictedResidualsSumOfSquares;
+
+ #region setPredictedResidualsSumOfSquares
+ private double getExternalResidual( int i )
+ {
+ double externalResidual = this.Residuals[ i ] / ( 1 - this.HatMatrixDiagonal[ i ] );
+ return externalResidual;
+ }
+ private void setPredictedResidualsSumOfSquares()
+ {
+ double partialSum = 0;
+ for( int i = 0 ; i < this.regressand.Length ; i++ )
+ partialSum += Math.Pow( this.getExternalResidual( i ) , 2 );
+ this.predictedResidualsSumOfSquares = partialSum;
+ }
+ #endregion setPredictedResidualsSumOfSquares
+
+ /// <summary>
+ /// the PRESS statistic, a statistic based on the leave-one-out technique
+ /// </summary>
+ public double PredictedResidualsSumOfSquares {
+ get {
+ if ( this.predictedResidualsSumOfSquares == Double.MinValue )
+ // the PRESS statistic has not been set yet
+ this.setPredictedResidualsSumOfSquares();
+ return this.predictedResidualsSumOfSquares; }
+ }
+ #endregion PredictedResidualsSumOfSquares
+
+
+ /// <summary>
+ /// similar to the Total Sum of Squares, but each observation is subtracted
+ /// the mean of the other observations: in other words, the current observation
+ /// does not partecipate to the computationo of the current mean. It has been
+ /// proven that the Prodicted Total Sum Of Squares (defined as above) is equal to
+ /// the (usual) Centered Total Sum of Squares times (n/(n-1))^2
+ /// </summary>
+ public double PredictedCenteredTotalSumOfSquares
+ {
+ get
+ {
+ double n = Convert.ToDouble( this.regressand.Length );
+ double predictedTotalSumOfSquares =
+ this.CenteredTotalSumOfSquares * Math.Pow( n / ( n - 1 ) , 2 );
+ return predictedTotalSumOfSquares;
+ }
+ }
+
+ /// <summary>
+ /// a normalized version of the the PRESS statistics
+ /// </summary>
+ public double CenteredPSquare {
+ get
+ {
+ double centerdPSquare = 1 - this.PredictedResidualsSumOfSquares /
+ this.PredictedCenteredTotalSumOfSquares;
+ return centerdPSquare;
+ }
+ }
+
public LinearRegression()
{
+ this.predictedResidualsSumOfSquares = double.MinValue;
}
***************
*** 159,162 ****
--- 358,363 ----
{
this.runRegression_checkParameters( regressand , regressors );
+ this.regressand = regressand;
+ this.regressors = regressors;
double[,] xTransposeX = this.getXtransposeX( regressors );
this.covarianceMatrix = PositiveDefiniteMatrix.GetInverse( xTransposeX );
|