Class LinearRegression

java.lang.Object
microsim.statistics.regression.LinearRegression
All Implemented Interfaces:
ILinearRegression

public class LinearRegression extends Object implements ILinearRegression
Linear regression object.
Author:
Michele Sonnessa, Ross Richardson
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a linear regression object.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    To be used when the agent Object has member fields that correspond by name to all the regressors of the Regression's MultiKeyCoefficientMap map.
    static double
    Warning - only use when LinearRegression object contains a MultiKeyCoefficientMap with only one key.
    static <T extends Enum<T>>
    double
    computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumType)
    Uses reflection to obtain information from the iDblSrc object, so it is possibly slow.
    static <T extends Enum<T>>
    double
    computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumType, boolean singleKeyCoefficients)
    Use this method when the underlying agent does not have any additional conditioning regression keys (such as the gender or civil status) to determine the appropriate regression co-efficients, i.e. the regression co-efficients do not depend on any properties of the underlying model.
    static <T extends Enum<T>, U extends Enum<U>>
    double
    computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumTypeDouble, IObjectSource iObjSrc, Class<U> enumTypeObject)
    Requires the implementation of the IObjectSource to ascertain whether any additional conditioning regression keys are used (e.g. whether the underlying agent is female, married etc., where the regression co-efficients are conditioned on additional keys of gender and civil status, for example).
    double
     
    double
    To be used when the agent Object has member fields that correspond by name to all the regressors of the Regression's MultiKeyCoefficientMap map.
    double
    Warning - only use when LinearRegression object contains a MultiKeyCoefficientMap with only one key.
    <T extends Enum<T>>
    double
    getScore(IDoubleSource iDblSrc, Class<T> enumType)
     
    <T extends Enum<T>, U extends Enum<U>>
    double
    getScore(IDoubleSource iDblSrc, Class<T> enumTypeDouble, IObjectSource iObjSrc, Class<U> enumTypeObject)
    Requires the implementation of the IObjectSource to ascertain whether any additional conditioning regression keys are used (e.g. whether the underlying agent is female, married etc., where the regression co-efficients are conditioned on additional keys of gender and civil status, for example).
    static double
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LinearRegression

      public LinearRegression(MultiKeyCoefficientMap map)
      Construct a linear regression object.
      Parameters:
      map - - needs to fulfil two criteria: 1) Map must have a key in the MultiKey that matches the name specified in the RegressionColumnNames enum called Regressor. 2) Map must have a value key that matches the name specified in the RegressionColumnNames enum called Coefficient. If loading from an .xls spreadsheet using the ExcelAssistant.loadCoefficientMap(), the Regressor column must be situated to the left of the Coefficient column.
  • Method Details

    • getScore

      public double getScore(Map<String,Double> values)
      Warning - only use when LinearRegression object contains a MultiKeyCoefficientMap with only one key. This method only looks at the first key of the MultiKeyCoefficientMap field of LinearRegression, so any other keys that are used to distinguish a unique multiKey (i.e. if the first key occurs more than once) will be ignored! If the first key of the multiKey appears more than once, the method would return an incorrect value, so will throw an exception.
      Specified by:
      getScore in interface ILinearRegression
      Parameters:
      values -
      Returns:
    • computeScore

      public static double computeScore(MultiKeyCoefficientMap amap, Map<String,Double> values)
      Warning - only use when LinearRegression object contains a MultiKeyCoefficientMap with only one key. This method only looks at the first key of the MultiKeyCoefficientMap field of LinearRegression, so any other keys that are used to distinguish a unique multiKey (i.e. if the first key occurs more than once) will be ignored! If the first key of the multiKey appears more than once, the method would return an incorrect value, so will throw an exception.
      Parameters:
      values -
      Returns:
    • getScore

      public <T extends Enum<T>> double getScore(IDoubleSource iDblSrc, Class<T> enumType)
      Specified by:
      getScore in interface ILinearRegression
    • getCoefficient

      public double getCoefficient(String regressor)
    • computeScore

      public static <T extends Enum<T>> double computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumType, boolean singleKeyCoefficients)
      Use this method when the underlying agent does not have any additional conditioning regression keys (such as the gender or civil status) to determine the appropriate regression co-efficients, i.e. the regression co-efficients do not depend on any properties of the underlying model. Requires that the MultiKeyCoefficientMap only has one entry in the multiKey - that of the name of the regressor variables.
      Parameters:
      coeffMultiMap - is a MultiKeyCoefficientMap that has a MultiKey whose first Key is the name of the regressor variable.
      iDblSrc - is an object that implements the IDoubleSource interface, and hence has a method getDoubleValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the keys of coeffMultiMap's MultiKey
      enumType - specifies the enum type that is used in the getDoubleValue(Enum.valueOf(enumType, String)) method of the iDblSrc object. The String is the name of the enum case, used as a switch to determine the appropriate double value to return
    • computeScore

      public static <T extends Enum<T>> double computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumType)
      Uses reflection to obtain information from the iDblSrc object, so it is possibly slow. For time critical cases, use the other computeScore method that requires passing in an object that implements the IObjectSource interface; this has signature: public static <T extends Enum<T>, U extends Enum<>> double computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumTypeDouble, IObjectSource iObjSrc, Class<> enumTypeObject) Requires the first column entry of the MultiKeyCoefficientMap (i.e. the first entry of coeffMultiMap's multiKey) to be the name of the regressor variables. The names of the other keys of the coeffMultiMap must match the (case sensitive) name of the corresponding fields of the iDblSrc class.
      Parameters:
      coeffMultiMap - is a MultiKeyCoefficientMap that has a MultiKey whose first Key is the name of the regressor variable. The names of the other keys of the coeffMultiMap must match the (case sensitive) name of the corresponding fields of the iDblSrc class.
      iDblSrc - is an object that implements the IDoubleSource interface, and hence has a method getDoubleValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the keys of coeffMultiMap's MultiKey
      enumType - specifies the enum type that is used in the getDoubleValue(Enum.valueOf(enumType, String)) method of the iDblSrc object. The String is the name of the enum case, used as a switch to determine the appropriate double value to return
    • getScore

      public double getScore(Object agent)
      To be used when the agent Object has member fields that correspond by name to all the regressors of the Regression's MultiKeyCoefficientMap map. The agent must also have fields that specify any additional conditioning keys e.g. gender or civil status, of the regression map's MultiKeys.
      Specified by:
      getScore in interface ILinearRegression
      Parameters:
      agent - is the object whose fields must match by name, the regressors and additional conditioning keys of the Regression map's MultiKeys.
    • computeScore

      public static double computeScore(MultiKeyCoefficientMap coeffMultiMap, Object agent)
      To be used when the agent Object has member fields that correspond by name to all the regressors of the Regression's MultiKeyCoefficientMap map. The agent must also have fields that specify any additional conditioning keys e.g. gender or civil status, of the regression map's MultiKeys.
      Parameters:
      agent - is the object whose fields must match by name, the regressors and additional conditioning keys of the Regression map's MultiKeys.
    • multiplyCoeffsWithValues

      public static double multiplyCoeffsWithValues(Map<String,Double> regCoeffMap, Map<String,Double> valueMap)
    • getScore

      public <T extends Enum<T>, U extends Enum<U>> double getScore(IDoubleSource iDblSrc, Class<T> enumTypeDouble, IObjectSource iObjSrc, Class<U> enumTypeObject)
      Requires the implementation of the IObjectSource to ascertain whether any additional conditioning regression keys are used (e.g. whether the underlying agent is female, married etc., where the regression co-efficients are conditioned on additional keys of gender and civil status, for example). If the underlying agent does not implement IObjectSource but does have additional conditioning regression keys, use the computeScore method (that uses reflection, so is slower) with signature: public static <T extends Enum<T>> double getScore(IDoubleSource iDblSrc, Class<T> enumType) If the underlying agent does not have additional conditioning regression keys, use the computeScore method with signature:-
      Specified by:
      getScore in interface ILinearRegression
      Parameters:
      iDblSrc - is an object that implements the IDoubleSource interface (e.g. the underlying agent whose properties are the covariates), and hence has a method getDoubleValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the first key entry of the coeffMultiMap's MultiKey
      enumTypeDouble - specifies the enum type that is used in the getDoubleValue(Enum.valueOf(enumType, String)) method of the iDblSrc object. The String is the name of the enum case, used as a switch to determine the appropriate double value to return
      iObjSrc - is an object that implements the IObjectSource interface (e.g. the underlying agent whose properties are the covariates), and hence has a method getObjectValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the conditioning regression key entries of coeffMultiMap's MultiKey (not the first key entry, which is reserved for the regressor name)
      enumTypeObject - specifies the enum type that is used in the getObjectValue(Enum.valueOf(enumType, String)) method of the iObjSrc object. The String is the name of the enum case, used as a switch to determine the appropriate object value to return
    • computeScore

      public static <T extends Enum<T>, U extends Enum<U>> double computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumTypeDouble, IObjectSource iObjSrc, Class<U> enumTypeObject)
      Requires the implementation of the IObjectSource to ascertain whether any additional conditioning regression keys are used (e.g. whether the underlying agent is female, married etc., where the regression co-efficients are conditioned on additional keys of gender and civil status, for example). If the underlying agent does not implement IObjectSource but does have additional conditioning regression keys, use the computeScore method (that uses reflection, so is slower) with signature: public static <T extends Enum<T>> double computeScore(MultiKeyCoefficientMap coeffMultiMap, IDoubleSource iDblSrc, Class<T> enumType) If the underlying agent does not have additional conditioning regression keys, use the computeScore method with signature:- Requires the MultiKeyCoefficientMap coeffMultiMap to have a key in its multiKey that corresponds to the name of the regressor variables. The names of the other keys of the coeffMultiMap must match the (case sensitive) name of the corresponding fields of the iDblSrc class.
      Parameters:
      coeffMultiMap - is a MultiKeyCoefficientMap that has a MultiKey containing the name of the regressor variable. The names of the other keys of the coeffMultiMap must match the (case sensitive) name of the corresponding fields of the iDblSrc class.
      iDblSrc - is an object that implements the IDoubleSource interface (e.g. the underlying agent whose properties are the covariates), and hence has a method getDoubleValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the first key entry of the coeffMultiMap's MultiKey
      enumTypeDouble - specifies the enum type that is used in the getDoubleValue(Enum.valueOf(enumType, String)) method of the iDblSrc object. The String is the name of the enum case, used as a switch to determine the appropriate double value to return
      iObjSrc - is an object that implements the IObjectSource interface (e.g. the underlying agent whose properties are the covariates), and hence has a method getObjectValue(enum), where the enum determines the appropriate double value to return. It must have some fields that match the (case sensitive) name of the conditioning regression key entries of coeffMultiMap's MultiKey (not the first key entry, which is reserved for the regressor name)
      enumTypeObject - specifies the enum type that is used in the getObjectValue(Enum.valueOf(enumType, String)) method of the iObjSrc object. The String is the name of the enum case, used as a switch to determine the appropriate object value to return