macrosynergy.learning.forecasting.bootstrap#
- class BasePanelBootstrap(bootstrap_method='panel', resample_ratio=1, max_features=None)[source]#
Bases:
ABC
- create_bootstrap_dataset(X, y)[source]#
Generate a bootstrap dataset based on a panel of features and a dependent variable.
- Parameters:
X (pd.DataFrame) – Input feature matrix
y (pd.DataFrame or pd.Series) – Dependent variable.
- Returns:
X_resampled (pd.DataFrame) – Bootstrap resampled feature matrix.
y_resampled (pd.DataFrame or pd.Series) – Bootstrap resampled dependent variable.
- class BaseModifiedRegressor(model, method, error_offset=1e-05, bootstrap_method='panel', bootstrap_iters=100, resample_ratio=1, max_features=None, analytic_method=None)[source]#
Bases:
BaseEstimator
,RegressorMixin
,BasePanelBootstrap
,ABC
- fit(X, y)[source]#
Fit a linear model and modify coefficients based on standard errors.
- Parameters:
X (pd.DataFrame) – Input feature matrix.
y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X.
- Returns:
Fitted estimator.
- Return type:
self
- predict(X)[source]#
Predict using the unadjusted linear model.
- Parameters:
X (pd.DataFrame) – Input feature matrix.
- Returns:
Predicted values.
- Return type:
np.ndarray or pd.Series
- create_signal(X)[source]#
Predict using the coefficient-adjusted linear model.
- Parameters:
X (pd.DataFrame) – Input feature matrix.
- Returns:
Signal from the adjusted factor model based on X.
- Return type:
np.ndarray or pd.Series
Notes
We define an additional create_signal method instead of using the predict method in order to not interfere with hyperparameter searches with standard metrics. Moreover, outputs from the adjusted factor model are not valid predictions, but are valid trading signals.
- adjust_bootstrap_se(model, X, y)[source]#
Adjust the coefficients of the linear model by bootstrap standard errors.
- Parameters:
model (RegressorMixin) – The underlying linear model to be modified.
X (pd.DataFrame) – Input feature matrix.
y (pd.DataFrame or pd.Series) – Target vector associated with each sample in X.
- Returns:
intercept (float) – Adjusted intercept.
coef (np.ndarray) – Adjusted coefficients.
- adjust_analytical_se(model, X, y, analytic_method)[source]#
Adjust the coefficients of the linear model by an analytical standard error formula.
- Parameters:
model (RegressorMixin) – The underlying linear 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) – The analytic method used to calculate standard errors.
- Returns:
intercept (float) – Adjusted intercept.
coef (np.ndarray) – Adjusted coefficients.
Notes
Analytical standard errors are model-specific, meaning that they must be implemented in a subclass of BaseModifiedRegressor.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') BaseModifiedRegressor #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if 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.
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.