macrosynergy.learning.forecasting.linear_model#

class LADRegressor(fit_intercept=True, positive=False, alpha=0, shrinkage_type='l1', tol=None, maxiter=None)[source]#

Bases: BaseEstimator, RegressorMixin

fit(X, y, sample_weight=None)[source]#

Learn LAD regression model parameters.

Parameters:
  • X (pd.DataFrame or np.ndarray) – Input feature matrix.

  • y (pd.Series or pd.DataFrame or np.ndarray) – Target vector associated with each sample in X.

  • sample_weight (np.ndarray, default=None) – Numpy array of sample weights to create a weighted LAD regression model.

predict(X)[source]#

Predict dependent variable using the fitted LAD regression model.

Parameters:

X (pd.DataFrame or np.ndarray) – Input feature matrix.

Returns:

y_pred – Numpy array of predictions.

Return type:

np.ndarray

Notes

If the model learning algorithm failed to converge, the predict method will return an array of zeros. This has the interpretation of no buy/sell signal being triggered based on this model.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LADRegressor#

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LADRegressor#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class SignWeightedLADRegressor(fit_intercept=True, positive=False, alpha=0, shrinkage_type='l1', tol=None, maxiter=None)[source]#

Bases: SignWeightedRegressor

set_params(**params)[source]#

Setter method to update the parameters of the SignWeightedLADRegressor.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The SignWeightedLADRegressor instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SignWeightedLADRegressor#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class TimeWeightedLADRegressor(fit_intercept=True, positive=False, half_life=252, alpha=0, shrinkage_type='l1', tol=None, maxiter=None)[source]#

Bases: TimeWeightedRegressor

set_params(**params)[source]#

Setter method to update the parameters of the TimeWeightedLADRegressor.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The TimeWeightedLADRegressor instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') TimeWeightedLADRegressor#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class SignWeightedLinearRegression(fit_intercept=True, positive=False, alpha=0, shrinkage_type='l1')[source]#

Bases: SignWeightedRegressor

set_params(**params)[source]#

Setter method to update the parameters of the SignWeightedLinearRegression.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The SignWeightedLinearRegression instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SignWeightedLinearRegression#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class TimeWeightedLinearRegression(fit_intercept=True, positive=False, half_life=252, alpha=0, shrinkage_type='l1')[source]#

Bases: TimeWeightedRegressor

set_params(**params)[source]#

Setter method to update the parameters of the TimeWeightedLinearRegression.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The TimeWeightedLinearRegression instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') TimeWeightedLinearRegression#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class ModifiedLinearRegression(method, fit_intercept=True, positive=False, error_offset=0.01, bootstrap_method='panel', bootstrap_iters=1000, resample_ratio=1, analytic_method=None)[source]#

Bases: BaseModifiedRegressor

adjust_analytical_se(model, X, y, analytic_method=None)[source]#

Adjust the coefficients of the OLS linear regression model by an analytical standard error formula.

Parameters:
  • model (LinearRegression) – The underlying OLS linear regression model to be modified.

  • X (pd.DataFrame) – Input feature matrix.

  • y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X.

  • analytic_method (str, default = None) – The analytic method used to calculate standard errors.

Returns:

  • intercept (float) – Adjusted intercept.

  • coef (np.ndarray) – Adjusted coefficients.

Notes

By default, the calculated standard errors use the usual standard error expression for OLS linear regression models under the assumption of multivariate normality, homoskedasticity and zero mean of the model errors. If analytic_method = “White”, the HC3 White estimator is used.

References

[1] https://online.stat.psu.edu/stat462/node/131/ [2] https://en.wikipedia.org/wiki/Heteroskedasticity-consistent_standard_errors

set_params(**params)[source]#

Setter method to update the parameters of the ModifiedLinearRegression

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The ModifiedLinearRegression instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ModifiedLinearRegression#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class ModifiedSignWeightedLinearRegression(method, fit_intercept=True, positive=False, error_offset=0.01, bootstrap_method='panel', bootstrap_iters=1000, resample_ratio=1, analytic_method=None)[source]#

Bases: BaseModifiedRegressor

adjust_analytical_se(model, X, y, analytic_method=None)[source]#

Adjust the coefficients of the SWLS linear regression model by an analytical standard error formula.

Parameters:
  • model (SignWeightedLinearRegression) – The underlying SWLS linear regression model to be modified.

  • X (pd.DataFrame) – Input feature matrix.

  • y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X.

  • analytic_method (str, default = None) – The analytic method used to calculate standard errors.

Returns:

  • intercept (float) – Adjusted intercept.

  • coef (np.ndarray) – Adjusted coefficients.

Notes

The analytical parameter estimates for WLS are:

\[\hat{\beta}^{\text{WLS}} = (X^{\intercal}WX)^{-1}X^{\intercal}y\]
where:
  • X is the input feature matrix, possibly with a column of ones representing the choice of an intercept.

  • W is the positive-definite, symmetric weight matrix, a diagonal matrix with sample weights along the main diagonal.

  • y is the dependent variable vector.

Since W is a positive-definite, symmetric matrix, it has a square root equal to the diagonal matrix with square roots of the sample weights along the diagonal. Hence, the WLS estimator can be rewritten as:

\[\hat{\beta}^{\text{WLS}} = ((({W^{1/2}X})^{\intercal}(W^{1/2}X))^{-1}(W^{1/2}X)^{\intercal}(W^{1/2}y))\]

This is precisely the OLS estimator for a rescaled matrix

\[\tilde {X} = W^{1/2}X\]

and a rescaled dependent variable

\[\tilde {y} = W^{1/2}y\]

Hence, the usual standard error estimate and White’s estimator can be applied based on a rescaling of the design matrix and associated target vector.

set_params(**params)[source]#

Setter method to update the parameters of the ModifiedSignWeightedLinearRegression.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The ModifiedSignWeightedLinearRegression instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ModifiedSignWeightedLinearRegression#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class ModifiedTimeWeightedLinearRegression(method, fit_intercept=True, positive=False, half_life=252, error_offset=0.01, bootstrap_method='panel', bootstrap_iters=1000, resample_ratio=1, analytic_method=None)[source]#

Bases: BaseModifiedRegressor

adjust_analytical_se(model, X, y, analytic_method)[source]#

Adjust the coefficients of the TWLS linear regression model by an analytical standard error formula.

Parameters:
  • model (TimeWeightedLinearRegression) – The underlying TWLS linear regression model to be modified.

  • X (pd.DataFrame) – Input feature matrix.

  • y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X.

  • analytic_method (str, default = None) – The analytic method used to calculate standard errors.

Returns:

  • intercept (float) – Adjusted intercept.

  • coef (np.ndarray) – Adjusted coefficients.

Notes

The analytical parameter estimates for WLS are:

\[\hat{\beta}^{\text{WLS}} = (X^{\intercal}WX)^{-1}X^{\intercal}y\]
where:
  • X is the input feature matrix, possibly with a column of ones representing the choice of an intercept.

  • W is the positive-definite, symmetric weight matrix, a diagonal matrix with sample weights along the main diagonal.

  • y is the dependent variable vector.

Since W is a positive-definite, symmetric matrix, it has a square root equal to the diagonal matrix with square roots of the sample weights along the diagonal. Hence, the WLS estimator can be rewritten as:

\[\hat{\beta}^{\text{WLS}} = (({W^{1/2}X})^{\intercal}(W^{1/2}X))^{-1}(W^{1/2}X)^{\intercal}(W^{1/2}y))\]

This is precisely the OLS estimator for a rescaled matrix

\[\tilde {X} = W^{1/2}X\]

and a rescaled dependent variable

\[\tilde {y} = W^{1/2}y\]

Hence, the usual standard error estimate and White’s estimator can be applied based on a rescaling of the design matrix and associated target vector.

set_params(**params)[source]#

Setter method to update the parameters of the ModifiedTimeWeightedLinearRegression.

Parameters:

**params (dict) – Dictionary of parameters to update.

Returns:

The ModifiedTimeWeightedLinearRegression instance with updated parameters.

Return type:

self

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') ModifiedTimeWeightedLinearRegression#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

Subpackages#