Example #1
0
 private void UpdateStandardErrors(double[] coefficients, Matrix informationMatrix, int n, int k)
 {
     for (int index = 0; index < coefficients.Length; index++)
     {
         var coefficient       = coefficients[index];
         var standardError     = System.Math.Sqrt(informationMatrix.GetValue(index, index).GetNumber());
         var tStat             = TStatistic(coefficient, standardError);
         var pValue            = PValue(tStat, n, k);
         var coefficientResult = new CoefficientResult
         {
             Value         = coefficient,
             StandardError = standardError,
             TStatistic    = tStat,
             PValue        = pValue
         };
         if (index == 0)
         {
             _linearRegressionResult.Intercept = coefficientResult;
         }
         else
         {
             _linearRegressionResult.SlopeLst.Add(coefficientResult);
         }
     }
 }
Example #2
0
        public MultipleLinearRegression Regress(Matrix matrix, Vector output, bool addOne = true)
        {
            if (addOne)
            {
                matrix = matrix.AddColumn(1);
            }

            output.SetVertical();
            if (_alg == MatrixDecompositionAlgs.NAIVE)
            {
                _singularValueDecomposition.DecomposeNaive(matrix);
            }
            else
            {
                _singularValueDecomposition.DecomposeGolubReinsch(matrix);
            }

            var coefficients = _singularValueDecomposition.Inverse()
                               .Multiply((Matrix)output)
                               .Evaluate()
                               .GetColumnVector(0);
            var result = new LinearRegressionResult();

            for (int i = 0; i < coefficients.Length; i++)
            {
                var coefficient       = coefficients[i].GetNumber();
                var coefficientResult = new CoefficientResult
                {
                    Value = coefficient
                };
                if (i == 0)
                {
                    result.Intercept = coefficientResult;
                }
                else
                {
                    result.SlopeLst.Add(coefficientResult);
                }
            }

            _linearRegressionResult = result;
            UpdateInformation(matrix, output, addOne);
            return(this);
        }