RegressionResults.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.regression;

  22. import java.io.Serializable;
  23. import java.util.Arrays;

  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.MathUtils;

  27. /**
  28.  * Results of a Multiple Linear Regression model fit.
  29.  *
  30.  */
  31. public class RegressionResults implements Serializable {

  32.     /** INDEX of Sum of Squared Errors */
  33.     private static final int SSE_IDX = 0;
  34.     /** INDEX of Sum of Squares of Model */
  35.     private static final int SST_IDX = 1;
  36.     /** INDEX of R-Squared of regression */
  37.     private static final int RSQ_IDX = 2;
  38.     /** INDEX of Mean Squared Error */
  39.     private static final int MSE_IDX = 3;
  40.     /** INDEX of Adjusted R Squared */
  41.     private static final int ADJRSQ_IDX = 4;
  42.     /** UID */
  43.     private static final long serialVersionUID = 1l;
  44.     /** regression slope parameters */
  45.     private final double[] parameters;
  46.     /** variance covariance matrix of parameters */
  47.     private final double[][] varCovData;
  48.     /** boolean flag for variance covariance matrix in symm compressed storage */
  49.     private final boolean isSymmetricVCD;
  50.     /** rank of the solution */
  51.     @SuppressWarnings("unused")
  52.     private final int rank;
  53.     /** number of observations on which results are based */
  54.     private final long nobs;
  55.     /** boolean flag indicator of whether a constant was included*/
  56.     private final boolean containsConstant;
  57.     /** array storing global results, SSE, MSE, RSQ, adjRSQ */
  58.     private final double[] globalFitInfo;

  59.     /**
  60.      *  Set the default constructor to private access
  61.      *  to prevent inadvertent instantiation
  62.      */
  63.     @SuppressWarnings("unused")
  64.     private RegressionResults() {
  65.         this.parameters = null;
  66.         this.varCovData = null;
  67.         this.rank = -1;
  68.         this.nobs = -1;
  69.         this.containsConstant = false;
  70.         this.isSymmetricVCD = false;
  71.         this.globalFitInfo = null;
  72.     }

  73.     /**
  74.      * Constructor for Regression Results.
  75.      *
  76.      * @param parameters a double array with the regression slope estimates
  77.      * @param varcov the variance covariance matrix, stored either in a square matrix
  78.      * or as a compressed
  79.      * @param isSymmetricCompressed a flag which denotes that the variance covariance
  80.      * matrix is in symmetric compressed format
  81.      * @param nobs the number of observations of the regression estimation
  82.      * @param rank the number of independent variables in the regression
  83.      * @param sumy the sum of the independent variable
  84.      * @param sumysq the sum of the squared independent variable
  85.      * @param sse sum of squared errors
  86.      * @param containsConstant true model has constant,  false model does not have constant
  87.      * @param copyData if true a deep copy of all input data is made, if false only references
  88.      * are copied and the RegressionResults become mutable
  89.      */
  90.     public RegressionResults(
  91.             final double[] parameters, final double[][] varcov,
  92.             final boolean isSymmetricCompressed,
  93.             final long nobs, final int rank,
  94.             final double sumy, final double sumysq, final double sse,
  95.             final boolean containsConstant,
  96.             final boolean copyData) {
  97.         if (copyData) {
  98.             this.parameters = parameters.clone();
  99.             this.varCovData = new double[varcov.length][];
  100.             for (int i = 0; i < varcov.length; i++) {
  101.                 this.varCovData[i] = varcov[i].clone();
  102.             }
  103.         } else {
  104.             this.parameters = parameters; // NOPMD - storing a reference to the array is controlled by a user-supplied parameter
  105.             this.varCovData = varcov; // NOPMD - storing a reference to the array is controlled by a user-supplied parameter
  106.         }
  107.         this.isSymmetricVCD = isSymmetricCompressed;
  108.         this.nobs = nobs;
  109.         this.rank = rank;
  110.         this.containsConstant = containsConstant;
  111.         this.globalFitInfo = new double[5];
  112.         Arrays.fill(this.globalFitInfo, Double.NaN);

  113.         if (rank > 0) {
  114.             this.globalFitInfo[SST_IDX] = containsConstant ?
  115.                     (sumysq - sumy * sumy / nobs) : sumysq;
  116.         }

  117.         this.globalFitInfo[SSE_IDX] = sse;
  118.         this.globalFitInfo[MSE_IDX] = this.globalFitInfo[SSE_IDX] /
  119.                 (nobs - rank);
  120.         this.globalFitInfo[RSQ_IDX] = 1.0 -
  121.                 this.globalFitInfo[SSE_IDX] /
  122.                 this.globalFitInfo[SST_IDX];

  123.         if (!containsConstant) {
  124.             this.globalFitInfo[ADJRSQ_IDX] = 1.0-
  125.                     (1.0 - this.globalFitInfo[RSQ_IDX]) *
  126.                     ( (double) nobs / ( (double) (nobs - rank)));
  127.         } else {
  128.             this.globalFitInfo[ADJRSQ_IDX] = 1.0 - (sse * (nobs - 1.0)) /
  129.                     (globalFitInfo[SST_IDX] * (nobs - rank));
  130.         }
  131.     }

  132.     /**
  133.      * <p>Returns the parameter estimate for the regressor at the given index.</p>
  134.      *
  135.      * <p>A redundant regressor will have its redundancy flag set, as well as
  136.      *  a parameters estimated equal to {@code Double.NaN}</p>
  137.      *
  138.      * @param index Index.
  139.      * @return the parameters estimated for regressor at index.
  140.      * @throws MathIllegalArgumentException if {@code index} is not in the interval
  141.      * {@code [0, number of parameters)}.
  142.      */
  143.     public double getParameterEstimate(int index) throws MathIllegalArgumentException {
  144.         if (parameters == null) {
  145.             return Double.NaN;
  146.         }
  147.         MathUtils.checkRangeInclusive(index, 0, this.parameters.length - 1);
  148.         return this.parameters[index];
  149.     }

  150.     /**
  151.      * <p>Returns a copy of the regression parameters estimates.</p>
  152.      *
  153.      * <p>The parameter estimates are returned in the natural order of the data.</p>
  154.      *
  155.      * <p>A redundant regressor will have its redundancy flag set, as will
  156.      *  a parameter estimate equal to {@code Double.NaN}.</p>
  157.      *
  158.      * @return array of parameter estimates, null if no estimation occurred
  159.      */
  160.     public double[] getParameterEstimates() {
  161.         if (this.parameters == null) {
  162.             return null; // NOPMD
  163.         }
  164.         return parameters.clone();
  165.     }

  166.     /**
  167.      * Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard
  168.      * error of the parameter estimate at index</a>,
  169.      * usually denoted s(b<sub>index</sub>).
  170.      *
  171.      * @param index Index.
  172.      * @return the standard errors associated with parameters estimated at index.
  173.      * @throws MathIllegalArgumentException if {@code index} is not in the interval
  174.      * {@code [0, number of parameters)}.
  175.      */
  176.     public double getStdErrorOfEstimate(int index) throws MathIllegalArgumentException {
  177.         if (parameters == null) {
  178.             return Double.NaN;
  179.         }
  180.         MathUtils.checkRangeInclusive(index, 0, this.parameters.length - 1);
  181.         double var = this.getVcvElement(index, index);
  182.         if (!Double.isNaN(var) && var > Double.MIN_VALUE) {
  183.             return FastMath.sqrt(var);
  184.         }
  185.         return Double.NaN;
  186.     }

  187.     /**
  188.      * <p>Returns the <a href="http://www.xycoon.com/standerrorb(1).htm">standard
  189.      * error of the parameter estimates</a>,
  190.      * usually denoted s(b<sub>i</sub>).</p>
  191.      *
  192.      * <p>If there are problems with an ill conditioned design matrix then the regressor
  193.      * which is redundant will be assigned <code>Double.NaN</code>. </p>
  194.      *
  195.      * @return an array standard errors associated with parameters estimates,
  196.      *  null if no estimation occurred
  197.      */
  198.     public double[] getStdErrorOfEstimates() {
  199.         if (parameters == null) {
  200.             return null; // NOPMD
  201.         }
  202.         double[] se = new double[this.parameters.length];
  203.         for (int i = 0; i < this.parameters.length; i++) {
  204.             double var = this.getVcvElement(i, i);
  205.             if (!Double.isNaN(var) && var > Double.MIN_VALUE) {
  206.                 se[i] = FastMath.sqrt(var);
  207.                 continue;
  208.             }
  209.             se[i] = Double.NaN;
  210.         }
  211.         return se;
  212.     }

  213.     /**
  214.      * <p>Returns the covariance between regression parameters i and j.</p>
  215.      *
  216.      * <p>If there are problems with an ill conditioned design matrix then the covariance
  217.      * which involves redundant columns will be assigned {@code Double.NaN}. </p>
  218.      *
  219.      * @param i {@code i}th regression parameter.
  220.      * @param j {@code j}th regression parameter.
  221.      * @return the covariance of the parameter estimates.
  222.      * @throws MathIllegalArgumentException if {@code i} or {@code j} is not in the
  223.      * interval {@code [0, number of parameters)}.
  224.      */
  225.     public double getCovarianceOfParameters(int i, int j) throws MathIllegalArgumentException {
  226.         if (parameters == null) {
  227.             return Double.NaN;
  228.         }
  229.         MathUtils.checkRangeInclusive(i, 0, this.parameters.length - 1);
  230.         MathUtils.checkRangeInclusive(j, 0, this.parameters.length - 1);
  231.         return this.getVcvElement(i, j);
  232.     }

  233.     /**
  234.      * <p>Returns the number of parameters estimated in the model.</p>
  235.      *
  236.      * <p>This is the maximum number of regressors, some techniques may drop
  237.      * redundant parameters</p>
  238.      *
  239.      * @return number of regressors, -1 if not estimated
  240.      */
  241.     public int getNumberOfParameters() {
  242.         if (this.parameters == null) {
  243.             return -1;
  244.         }
  245.         return this.parameters.length;
  246.     }

  247.     /**
  248.      * Returns the number of observations added to the regression model.
  249.      *
  250.      * @return Number of observations, -1 if an error condition prevents estimation
  251.      */
  252.     public long getN() {
  253.         return this.nobs;
  254.     }

  255.     /**
  256.      * <p>Returns the sum of squared deviations of the y values about their mean.</p>
  257.      *
  258.      * <p>This is defined as SSTO
  259.      * <a href="http://www.xycoon.com/SumOfSquares.htm">here</a>.</p>
  260.      *
  261.      * <p>If {@code n < 2}, this returns {@code Double.NaN}.</p>
  262.      *
  263.      * @return sum of squared deviations of y values
  264.      */
  265.     public double getTotalSumSquares() {
  266.         return this.globalFitInfo[SST_IDX];
  267.     }

  268.     /**
  269.      * <p>Returns the sum of squared deviations of the predicted y values about
  270.      * their mean (which equals the mean of y).</p>
  271.      *
  272.      * <p>This is usually abbreviated SSR or SSM.  It is defined as SSM
  273.      * <a href="http://www.xycoon.com/SumOfSquares.htm">here</a></p>
  274.      *
  275.      * <p><strong>Preconditions</strong>:</p><ul>
  276.      * <li>At least two observations (with at least two different x values)
  277.      * must have been added before invoking this method. If this method is
  278.      * invoked before a model can be estimated, <code>Double.NaN</code> is
  279.      * returned.
  280.      * </li></ul>
  281.      *
  282.      * @return sum of squared deviations of predicted y values
  283.      */
  284.     public double getRegressionSumSquares() {
  285.         return this.globalFitInfo[SST_IDX] - this.globalFitInfo[SSE_IDX];
  286.     }

  287.     /**
  288.      * <p>Returns the <a href="http://www.xycoon.com/SumOfSquares.htm">
  289.      * sum of squared errors</a> (SSE) associated with the regression
  290.      * model.</p>
  291.      *
  292.      * <p>The return value is constrained to be non-negative - i.e., if due to
  293.      * rounding errors the computational formula returns a negative result,
  294.      * 0 is returned.</p>
  295.      *
  296.      * <p><strong>Preconditions</strong>:</p>
  297.      * <ul>
  298.      * <li>numberOfParameters data pairs
  299.      * must have been added before invoking this method. If this method is
  300.      * invoked before a model can be estimated, <code>Double,NaN</code> is
  301.      * returned.
  302.      * </li></ul>
  303.      *
  304.      * @return sum of squared errors associated with the regression model
  305.      */
  306.     public double getErrorSumSquares() {
  307.         return this.globalFitInfo[ SSE_IDX];
  308.     }

  309.     /**
  310.      * <p>Returns the sum of squared errors divided by the degrees of freedom,
  311.      * usually abbreviated MSE.</p>
  312.      *
  313.      * <p>If there are fewer than <strong>numberOfParameters + 1</strong> data pairs in the model,
  314.      * or if there is no variation in <code>x</code>, this returns
  315.      * <code>Double.NaN</code>.</p>
  316.      *
  317.      * @return sum of squared deviations of y values
  318.      */
  319.     public double getMeanSquareError() {
  320.         return this.globalFitInfo[ MSE_IDX];
  321.     }

  322.     /**
  323.      * <p>Returns the <a href="http://www.xycoon.com/coefficient1.htm">
  324.      * coefficient of multiple determination</a>,
  325.      * usually denoted r-square.</p>
  326.      *
  327.      * <p><strong>Preconditions</strong>:</p>
  328.      * <ul>
  329.      * <li>At least numberOfParameters observations (with at least numberOfParameters different x values)
  330.      * must have been added before invoking this method. If this method is
  331.      * invoked before a model can be estimated, {@code Double,NaN} is
  332.      * returned.
  333.      * </li></ul>
  334.      *
  335.      * @return r-square, a double in the interval [0, 1]
  336.      */
  337.     public double getRSquared() {
  338.         return this.globalFitInfo[ RSQ_IDX];
  339.     }

  340.     /**
  341.      * <p>Returns the adjusted R-squared statistic, defined by the formula
  342.      * \(
  343.      * R_\mathrm{adj}^2 = 1 - \frac{\mathrm{SSR} (n - 1)}{\mathrm{SSTO} (n - p)}
  344.      * \)
  345.      * where SSR is the sum of squared residuals},
  346.      * SSTO is the total sum of squares}, n is the number
  347.      * of observations and p is the number of parameters estimated (including the intercept).</p>
  348.      *
  349.      * <p>If the regression is estimated without an intercept term, what is returned is</p><pre>
  350.      * <code> 1 - (1 - {@link #getRSquared()} ) * (n / (n - p)) </code>
  351.      * </pre>
  352.      *
  353.      * @return adjusted R-Squared statistic
  354.      */
  355.     public double getAdjustedRSquared() {
  356.         return this.globalFitInfo[ ADJRSQ_IDX];
  357.     }

  358.     /**
  359.      * Returns true if the regression model has been computed including an intercept.
  360.      * In this case, the coefficient of the intercept is the first element of the
  361.      * {@link #getParameterEstimates() parameter estimates}.
  362.      * @return true if the model has an intercept term
  363.      */
  364.     public boolean hasIntercept() {
  365.         return this.containsConstant;
  366.     }

  367.     /**
  368.      * Gets the i-jth element of the variance-covariance matrix.
  369.      *
  370.      * @param i first variable index
  371.      * @param j second variable index
  372.      * @return the requested variance-covariance matrix entry
  373.      */
  374.     private double getVcvElement(int i, int j) {
  375.         if (this.isSymmetricVCD) {
  376.             if (this.varCovData.length > 1) {
  377.                 //could be stored in upper or lower triangular
  378.                 if (i == j) {
  379.                     return varCovData[i][i];
  380.                 } else if (i >= varCovData[j].length) {
  381.                     return varCovData[i][j];
  382.                 } else {
  383.                     return varCovData[j][i];
  384.                 }
  385.             } else {//could be in single array
  386.                 if (i > j) {
  387.                     return varCovData[0][(i + 1) * i / 2 + j];
  388.                 } else {
  389.                     return varCovData[0][(j + 1) * j / 2 + i];
  390.                 }
  391.             }
  392.         } else {
  393.             return this.varCovData[i][j];
  394.         }
  395.     }
  396. }