Esempio n. 1
0
        private void CalculateParametersAndStatistic(bool calculateStatistics)
        {
            Matrix XprimeXinv = X.Transpose().Multiply(X).PseudoInverse();
            Matrix betaValues = XprimeXinv.Multiply(X.Transpose()).Multiply(Y);

            int t = Y.NRows;
            int n = betaValues.NRows;

            if (calculateStatistics == false)
            {
                Betas = new ParameterEstimate[n];
                for (int i = 0; i < n; i++)
                {
                    Betas[i] = new ParameterEstimate(betaValues[i, 0]);
                }
                return;
            }

            Matrix expectedValueY = X.Multiply(betaValues);
            Matrix Z = expectedValueY.DeviationsFromMean();

            ESS              = Z.Transpose().Multiply(Z)[0, 0];
            Residuals        = expectedValueY.Subtract(Y);
            RSS              = Residuals.Transpose().Multiply(Residuals)[0, 0];
            TrackingError    = Math.Sqrt(RSS / (t - 1));
            rSquared         = 1 - RSS / TSS;
            AdjustedRSquared = 1 - (1 - rSquared) * (t - 1) / (t - n);


            double varianceOfErrorTerm = Residuals.Transpose().Multiply(Residuals)[0, 0] / (t - n);

            Betas = new ParameterEstimate[n];
            for (int i = 0; i < n; i++)
            {
                double sigma  = Math.Sqrt(varianceOfErrorTerm * XprimeXinv[i, i]);
                double tStat  = betaValues[i, 0] == 0 ? 0 : Math.Abs(betaValues[i, 0] - 0) / sigma;
                double pValue = 1.0 - Distributions.StudentsTCumulativeDensityFunction(tStat, t - n);
                Betas[i] = new ParameterEstimate(betaValues[i, 0], sigma, tStat, pValue);
            }

            fStatistic = (rSquared / (n - 1)) / ((1 - rSquared) / (t - n));

            if (n > 1)
            {
                fValue = 1.0 - Distributions.FCumulativeDensityFunction(fStatistic, n - 1, t - n);
            }
        }