net.sourceforge.openforecast.models
Class MultipleLinearRegressionModel

java.lang.Object
  extended by net.sourceforge.openforecast.models.AbstractForecastingModel
      extended by net.sourceforge.openforecast.models.MultipleLinearRegressionModel
All Implemented Interfaces:
ForecastingModel

public class MultipleLinearRegressionModel
extends AbstractForecastingModel

Implements a multiple variable linear regression model using the variables named in the constructor as the independent variables, or the variables passed into one of the init methods. The cofficients of the regression, as well as the accuracy indicators are determined from the data set passed to init.

Once initialized, this model can be applied to another data set using the forecast method to forecast values of the dependent variable based on values of the dependent variable (the one named in the constructor).

A multiple variable linear regression model essentially attempts to put a hyperplane through the data points. Mathematically, assuming the independent variables are xi and the dependent variable is y, then this hyperplane can be represented as:

y = a0 + a1*x1 + a2*x2 + a3*x3 + ...
where the ai are the coefficients of the regression. The coefficient a0 is also referred to as the intercept. If all xi were zero (theoretically at least), it is the forecast value of the dependentVariable, y.

Since:
0.2
Author:
Steven R. Gould

Field Summary
 
Fields inherited from class net.sourceforge.openforecast.models.AbstractForecastingModel
accuracyIndicators, initialized
 
Constructor Summary
MultipleLinearRegressionModel()
          A default constructor that constructs a new multiple variable Linear regression model.
MultipleLinearRegressionModel(String[] independentVariable)
          Constructs a new multiple variable linear regression model, using the given array of names as the independent variables to use.
 
Method Summary
 double forecast(DataPoint dataPoint)
          Using the current model parameters (initialized in init), apply the forecast model to the given data point.
 Hashtable<String,Double> getCoefficients()
          Returns a Hashtable containing the coefficients that will be used by the current model.
 String getForecastType()
          Returns a short name for this type of forecasting model.
 double getIntercept()
          Returns the intercept that will be used by the current model.
 int getNumberOfPredictors()
          Returns the number of predictors used by the underlying model.
 void init(DataSet dataSet)
          Initializes the coefficients to use for this regression model.
 void init(double intercept, Hashtable<String,Double> coefficients)
          Initializes the coefficients to use for this regression model with the given values.
 String toString()
          Returns a detailed description of this forcasting model, including the intercept and slope.
 
Methods inherited from class net.sourceforge.openforecast.models.AbstractForecastingModel
calculateAccuracyIndicators, forecast, getAIC, getBias, getMAD, getMAPE, getMSE, getSAE
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultipleLinearRegressionModel

public MultipleLinearRegressionModel()
A default constructor that constructs a new multiple variable Linear regression model. When this constructor is used, it is assumed that all independent variables in the DataSet or list of coefficients passed to init are to be used in the model. To narrow down and explicitly define what independent variables to use in the model, use the alternate form of the constructor.

For a valid model to be constructed, you should call either implementation of init.

See Also:
init(DataSet), init(double,Hashtable)

MultipleLinearRegressionModel

public MultipleLinearRegressionModel(String[] independentVariable)
Constructs a new multiple variable linear regression model, using the given array of names as the independent variables to use. For a valid model to be constructed, you should call init and pass in a data set containing a series of data points involving the given independent variables.

Parameters:
independentVariable - an array of names of the independent variables to use in this model.
See Also:
init(DataSet), init(double,Hashtable)
Method Detail

init

public void init(DataSet dataSet)
Initializes the coefficients to use for this regression model. The coefficients are derived so as to give the best fit hyperplane for the given data set.

Additionally, the accuracy indicators are calculated based on this data set.

Parameters:
dataSet - the set of observations to use to derive the regression coefficients for this model.

init

public void init(double intercept,
                 Hashtable<String,Double> coefficients)
Initializes the coefficients to use for this regression model with the given values. The coefficients Hashtable must contain a single entry for each independent variable. That entry should be accessible via the exact same name (case sensitive) as the independent variable, and should contain the intended coefficient value (as a subclass of Number).

In addition to the coefficients, you must also specify the constant term - or the "intercept" - to use in the regression model.

Note that since this method does not require a data set, it is not possible to calculate the values of the accuracy indicators. Therefore these are all set to their worse-case values.

Parameters:
intercept - the constant term - or the "intercept" - to use in the regression model.
coefficients - the coefficients to be assigned for use in this regression model.
Throws:
ModelNotInitializedException - if a coefficient does not exist in the given Hashtable of coefficients for one or more of the independent variables used when constructing this model.
ClassCastException - if the "value" associated with one of the coefficients cannot be cast to a Number.

getIntercept

public double getIntercept()
Returns the intercept that will be used by the current model. This could be a user-defined value or internally calculated depending on which of the init methods was used to initialize the model.

Returns:
the intercept that will be used by this model.
Throws:
ModelNotInitializedException - if this method is called before the model has been initialized with a call to init.

getCoefficients

public Hashtable<String,Double> getCoefficients()
Returns a Hashtable containing the coefficients that will be used by the current model. These could be user-defined coefficients or internally calculated coefficients depending on which of the init methods were used to initialize the model.

Note that modifying the coefficients in the Hashtable returned from this method will not modify the coefficients used by the model. If you want to modify the coefficients used in the model, then use the init(double,Hashtable) method to re-initialize the model.

Returns:
a Hashtable containing the coefficients that will be used by this model.
Throws:
ModelNotInitializedException - if this method is called before the model has been initialized with a call to init.

getNumberOfPredictors

public int getNumberOfPredictors()
Returns the number of predictors used by the underlying model.

Returns:
the number of predictors used by the underlying model.

forecast

public double forecast(DataPoint dataPoint)
Using the current model parameters (initialized in init), apply the forecast model to the given data point. The data point must have valid values for the independent variables. Upon return, the value of the dependent variable will be updated with the forecast value computed for that data point.

Parameters:
dataPoint - the data point for which a forecast value (for the dependent variable) is required.
Returns:
the forecast value of the dependent variable for the given data point.
Throws:
ModelNotInitializedException - if forecast is called before the model has been initialized with a call to init.

getForecastType

public String getForecastType()
Returns a short name for this type of forecasting model. A more detailed explanation is provided by the toString method.

Returns:
a short string describing this type of forecasting model.

toString

public String toString()
Returns a detailed description of this forcasting model, including the intercept and slope. A shortened version of this is provided by the getForecastType method.

Specified by:
toString in interface ForecastingModel
Overrides:
toString in class Object
Returns:
a description of this forecasting model.


OpenForecast, Copyright (c) Steven Gould, 2002-2011