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#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LADRegressor#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- 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#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- class GlobalLocalRegression(local_lambda=1, global_lambda=1, positive=False, fit_intercept=True, min_xs_samples=36)[source]#
Bases:
BaseEstimator,RegressorMixinLinear panel model with hierarchical shrinkage of country-specific (local) coefficients towards unknown global coefficients. Learning means that both country-specific and global coefficients are estimated from data.
- Parameters:
local_lambda (float, default=1) – Regularization strength to pull local coefficients towards global coefficients.
global_lambda (float, default=1) – Regularization strength to pull global coefficients towards zero.
positive (bool, default=False) – Whether to constrain all coefficients to be positive. Default is False.
fit_intercept (bool, default=True) – Whether to fit an intercept term. Default is True.
min_xs_samples (int, default=36) – Minimum number of samples required in each group for the group to be considered a contribution to the mean squared error component of the loss function.
Notes
A panel can be modelled from a global perspective, where time series of all countries are “pooled” or stacked together, meaning that samples from different countries are treated as independent. This is called a pooled regression. With one model fit on all countries’ data, this is a high-bias, low-variance model.
Alternatively, country-by-country regressions can be fit, with a separate model for each country. This is low-bias but high-variance, since each model sees less data.
This implies that a balance can be found between these two extremes by balancing this bias-variance trade-off. Introduction of bias to the country-by-country models can lead to a potentially substantial reduction in variance. Mathematically, this fit is found by minimizing the sum of squared residuals for each country, with a term that penalizes deviation of country-specific coefficients from a global coefficient. The global coefficient is also penalized to prevent it from growing too large.
The loss function is as follows:
\[L(\{\beta_i\}_{i=1}^{C}, \beta) = \frac{1}{C} \sum_{i = 1}^{C} \left [ \frac{1}{n_{i}} \sum_{t=1}^{n_{i}} (y_{it} - x_{it}^{\intercal} \beta_{i})^2 \right ] + \lambda_{\text{local}} \sum_{i=1}^{C} ||\beta_i - \beta||_{2}^{2} + \lambda_{\text{global}} ||\beta||_{2}^{2}\]- fit(X, y, sample_weight=None)[source]#
Fit the global-local model.
- Parameters:
X (pd.DataFrame) – Input feature matrix, multi-indexed by cid and real_date.
y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X, multi-indexed by cid and real_date.
sample_weight (np.ndarray, optional) – Sample weights for each sample in X. If provided, it should be a 1D array with the same length as the number of samples in X. If None, all samples are treated equally.
- Returns:
Fitted estimator.
- Return type:
self
- loss(weights)[source]#
Loss function for the global-local regression model.
- Parameters:
weights (np.ndarray) – Flattened array of weights, where the last n_features_ elements correspond with the global coefficients and the rest correspond to the local coefficients for each country.
- loss_derivative(weights)[source]#
Derivative of the loss function with respect to the weights.
- Parameters:
weights (np.ndarray) – Flattened array of weights, where the last n_features_ elements correspond with the global coefficients and the rest correspond to the local coefficients for each country.
- predict(X)[source]#
Predict the target values for the given input data.
- Parameters:
X (pd.DataFrame) – Input features for prediction.
- Returns:
Predicted target values.
- Return type:
np.ndarray
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') GlobalLocalRegression#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') GlobalLocalRegression#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
- class LinearMultiTargetRegression(fit_intercept=True, seemingly_unrelated=False, covariance_estimator='ewm', span=60, feature_selection=None)[source]#
Bases:
BaseEstimator,RegressorMixinLinear regression model with multiple targets, supporting seemingly unrelated regression (SUR) via feasible generalized least squares (FGLS).
- Parameters:
fit_intercept (bool, default=True) – Whether to include an intercept term in the regression.
seemingly_unrelated (bool, default=False) – Whether to make the regression seemingly unrelated.
covariance_estimator (Union[str, BaseEstimator], default="ewm") – Choice of covariance estimator. Options are “ml” for maximum likelihood, “ewm” for exponentially weighted moving covariance, or a custom scikit-learn compatible covariance estimator.
span (int, default=60) – Span parameter for exponentially weighted covariance estimation of residuals.
feature_selection (object, default=None) – A feature selection object inheriting from scikit-learn’s SelectorMixin base class in sklearn.feature_selection. If provided, feature selection is applied per target before fitting.
- fit(X, y, sample_weight=None)[source]#
Fit the linear multi-target regression model.
- Parameters:
X (pd.DataFrame) – Feature matrix of shape (n_samples, n_features). Should be multi-indexed by asset and real date.
y (pd.DataFrame) – Target matrix of shape (n_samples, n_assets). Should be multi-indexed by asset and real date.
sample_weight (array-like of shape (n_samples,), default=None) – Individual weights for each sample.
- predict(X)[source]#
Predict method to return predictions for each asset.
- Parameters:
X (pd.DataFrame) – Feature matrix of shape (n_samples, n_features). Should be multi-indexed by asset and real date.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearMultiTargetRegression#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LinearMultiTargetRegression#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.
Subpackages#
- macrosynergy.learning.forecasting.linear_model.lad_regressors
- macrosynergy.learning.forecasting.linear_model.ls_regressors
SignWeightedLinearRegressionTimeWeightedLinearRegressionModifiedLinearRegressionModifiedSignWeightedLinearRegressionModifiedTimeWeightedLinearRegression- Submodules