Example #1
0
        private void compute_coefficients(Linalg.Matrix A, double[] y, double[] w)
        {
            Linalg.Matrix b = new Linalg.Matrix(y, false);

            Linalg.Matrix          coeffs;
            Linalg.Matrix          cov;
            Linalg.SvDecomposition sv;
            if (A.Rows > A.Columns)
            {
                sv     = new Linalg.SvDecomposition(A);
                coeffs = sv.Solve(b, false);
            }
            else
            {
                sv     = new Linalg.SvDecomposition(A.Transpose());
                coeffs = sv.Solve(b, true);
            }
            cov = sv.Cov();

            Linalg.Matrix fit       = A * coeffs;
            Linalg.Matrix residuals = fit - b;


            double mss = m_intercept ? Math.Stats.SumOfSquaredDev(fit.RowPackedData(), w) : Math.Stats.SumOfSquares(fit.RowPackedData());
            double tss = m_intercept ? Math.Stats.SumOfSquaredDev(y, w) : Math.Stats.SumOfSquares(y);
            double rss = Math.Stats.SumOfSquares(residuals.RowPackedData()); //without intercept residuals dont add up to zero

            m_observations = A.Rows;
            m_coeffs       = coeffs.RowPackedData();

            int p             = m_coeffs.Length;
            int n             = m_observations;
            int dof           = n - p;
            int dof_intercept = m_intercept ? 1 : 0;

            m_coeffs_stderr = new double[p];             //coefficient standard errors

            double stderr = System.Math.Sqrt(rss / dof); //residual sum of squares

            for (int i = 0; i < p; i++)
            {
                m_coeffs_stderr[i] = stderr * System.Math.Sqrt(cov[i, i]);
            }

            double r2     = mss / (mss + rss);
            double r2_adj = 1.0 - (n - dof_intercept) * (1.0 - r2) / dof;
            double fvalue = (mss / (p - dof_intercept)) / (rss / dof);
            double aic    = n + n * System.Math.Log(2 * System.Math.PI) + n * System.Math.Log(rss / n) + 2 * (m_coeffs.Length + 1); // sum(log(w))

            //generate summary
            m_summary = new Dictionary <string, double>();

            m_summary["n"]      = m_observations;
            m_summary["p"]      = m_coeffs.Length;
            m_summary["dof"]    = dof;
            m_summary["stderr"] = stderr;
            m_summary["rss"]    = rss;
            m_summary["tss"]    = tss;
            m_summary["mss"]    = mss;
            m_summary["r2"]     = r2;
            m_summary["r2_adj"] = r2_adj;
            m_summary["fvalue"] = fvalue;
            m_summary["aic"]    = aic;

            for (int i = 0; i < p; i++)
            {
                double coef     = m_coeffs[i];
                double coef_std = m_coeffs_stderr[i];
                double tvalue   = coef / coef_std;
                double pvalue   = Math.Special.incbet(0.5 * dof, 0.5, dof / (dof + tvalue * tvalue));

                m_summary[m_names[i]]             = coef;
                m_summary[m_names[i] + "_se"]     = coef_std;
                m_summary[m_names[i] + "_tvalue"] = tvalue;
                m_summary[m_names[i] + "_pvalue"] = pvalue;
            }

            m_summary["intercept"] = m_intercept ? 1.0 : 0.0;
        }
Example #2
0
        private void compute_coefficients(Linalg.Matrix A, double[] y, double[] w)
        {
            Linalg.Matrix b = new Linalg.Matrix(y, false);

            double[] w_sqrt = null;
            if (m_weighted)
            {
                w_sqrt = new double[b.Rows];
                for (int i = 0; i < b.Rows; i++)
                {
                    w_sqrt[i] = System.Math.Sqrt(w[i]);

                    b[i, 0] = b[i, 0] * w_sqrt[i]; //here we assume that specified weights are sigmas (i.e. not sigma square)

                    for (int j = 0; j < A.Columns; j++)
                    {
                        A[i, j] = A[i, j] * w_sqrt[i];
                    }
                }
            }

            Linalg.Matrix          coeffs;
            Linalg.Matrix          cov;
            Linalg.SvDecomposition sv;
            if (A.Rows > A.Columns)
            {
                sv     = new Linalg.SvDecomposition(A);
                coeffs = sv.Solve(b, false);
            }
            else
            {
                sv     = new Linalg.SvDecomposition(A.Transpose());
                coeffs = sv.Solve(b, true);
            }
            cov = sv.Cov();

            Linalg.Matrix fit       = A * coeffs;
            Linalg.Matrix residuals = fit - b;

            double aic_weight_correction = 0;
            double weight_sum            = fit.Rows;

            if (m_weighted)
            {
                for (int i = 0; i < fit.Rows; i++)
                {
                    fit[i, 0]       = fit[i, 0] / w_sqrt[i];
                    residuals[i, 0] = fit[i, 0] - b[i, 0] / w_sqrt[i];

                    aic_weight_correction += System.Math.Log(w[i]);
                }
                weight_sum = Math.Stats.Utils.Sum(w);
            }

            double mss = Math.Stats.Utils.SumOfSquaredDev(fit.RowPackedData(), w);
            double rss = Math.Stats.Utils.SumOfSquares(residuals.RowPackedData(), w); //without intercept residuals dont add up to zero
            double tss = Math.Stats.Utils.SumOfSquaredDev(y, w);


            m_observations = A.Rows;
            m_coeffs       = coeffs.RowPackedData();

            int p             = m_coeffs.Length;
            int n             = m_observations;
            int dof           = n - p;
            int dof_intercept = m_intercept ? 1 : 0;

            m_coeffs_stderr = new double[p];             //coefficient standard errors

            double stderr = System.Math.Sqrt(rss / dof); //residual sum of squares

            for (int i = 0; i < p; i++)
            {
                m_coeffs_stderr[i] = stderr * System.Math.Sqrt(cov[i, i]);
            }

            double r2      = mss / (mss + rss);
            double r2_adj  = 1.0 - (n - dof_intercept) * (1.0 - r2) / dof;
            double fvalue  = (mss / (p - dof_intercept)) / (rss / dof);
            double aic_bic = n + n * System.Math.Log(2 * System.Math.PI) + n * System.Math.Log(rss / n) - aic_weight_correction;
            double aic     = aic_bic + 2 * (m_coeffs.Length + 1);
            double bic     = aic_bic + System.Math.Log(m_observations) * (m_coeffs.Length + 1);

            //generate summary
            m_summary = new Dictionary <string, double>();

            m_summary["n"]      = m_observations;
            m_summary["p"]      = m_coeffs.Length;
            m_summary["dof"]    = dof;
            m_summary["stderr"] = stderr;
            m_summary["rss"]    = rss;
            m_summary["tss"]    = tss;
            m_summary["mss"]    = mss;
            m_summary["r2"]     = r2;
            m_summary["r2_adj"] = r2_adj;
            m_summary["fvalue"] = fvalue;
            m_summary["aic"]    = aic;
            m_summary["bic"]    = bic;

            for (int i = 0; i < p; i++)
            {
                double coef     = m_coeffs[i];
                double coef_std = m_coeffs_stderr[i];
                double tvalue   = coef / coef_std;
                double pvalue   = Math.Special.incbet(0.5 * dof, 0.5, dof / (dof + tvalue * tvalue));

                m_summary[m_names[i]]             = coef;
                m_summary[m_names[i] + "_se"]     = coef_std;
                m_summary[m_names[i] + "_tvalue"] = tvalue;
                m_summary[m_names[i] + "_pvalue"] = pvalue;
            }

            m_summary["intercept"] = m_intercept ? 1.0 : 0.0;
        }