LinearVAR#
- class skarf.var.LinearVAR(estimator: LinearModel, order: int = 1, lag: int = 1, per_target: bool = False, decomposition: PCA | None = None, transpose: bool = False, random_state: int | RandomState | None = None)#
Linear VAR model.
A linear VAR model as a meta-estimator, using sklearn linear models for parameter fitting.
- Parameters:
- estimatorestimator object
LinearModelestimator object.- orderint, default=1
VAR model order, i.e. the number of past “lags” to include when predicting a future time point.
- lagint, default=1
Base temporal prediction lag/offset.
- per_targetbool, default=False
Fit separate linear model per target time series. This also excludes the target time series from the model.
- decompositionestimator object, default=None
Decomposition estimator object. Used to compute a component dictionary for representing linear model coefficients.
- transposebool, default=False
Fit a transposed decomposition.
- random_stateint, RandomState instance, default=None
The seed of the pseudo random number generator used when sampling. Note that using an int will produce identical results on each call to
sample. Passing aRandomStateinstance will produce varying but reproducible sampling results.
- Attributes:
- coef_array of shape (order, n_features, n_features)
Estimated coefficients for the VAR model. The terms are ordered by increasing lag. The `i`th row of each term contains the prediction coefficients for the `i`th feature.
- intercept_array of shape (n_features,) or float
Model intercept.
- n_features_in_int
Number of features seen during fit.
- feature_names_in_array of shape (
n_features_in_,) Names of features seen during
fit. Defined only whenXhas feature names that are all strings.- estimator_Estimator object
Fit linear model estimator. Defined only when
per_target=False.- estimators_list Estimator objects
Fit per target linear model estimators. Defined only when
per_target=True.- decomposition_Estimator object
Fit decomposition estimator. Defined only when
decompositionnotNone.- components_array of shape (n_components, n_features)
Deomposition components dictionary. Defined only when
decompositionnotNone.
Methods
fit(X[, y, segments, sample_weight, groups])Fit the model with X.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
predict(X)Predict time series values for next time steps.
sample(n_samples[, X_init])Sample simulated data from the VAR model.
score(X[, y, segments, sample_weight])Return the prediction score for the model (by default R2).
scoring_function(y_true, y_pred, *[, ...])\(R^2\) (coefficient of determination) regression score function.
set_fit_request(*[, groups, sample_weight, ...])Request metadata passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_score_request(*[, sample_weight, segments])Request metadata passed to the
scoremethod.- estimator_: LinearModel#
Fit linear model estimator. Defined only when
per_target=False.
- estimators_: list[LinearModel]#
Fit per target linear model estimators. Defined only when
per_target=True.
- feature_names_in_: ndarray#
Names of features seen during
fit. Defined only whenXhas feature names.
- fit(X: ndarray, y: None = None, segments: ndarray | None = None, sample_weight: ndarray | None = None, groups: ndarray | None = None) Self#
Fit the model with X.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training multivariate time series.
- yIgnored
Not used, present here for API consistency by convention.
- segmentsarray-like of shape (n_samples,)
Indicator array of contiguous temporal segments in
X.- sample_weightarray-like of shape (n_samples,), default=None
Sample weights. Only binary sample weights indicating time points to include/exclude are currently supported.
- groupsarray-like of shape (n_samples,), default=None
Indicator array of CV groups.
- Returns:
- selfobject
Returns the instance itself.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X: ndarray) ndarray#
Predict time series values for next time steps.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input multivariate time series.
- Returns:
- X_predarray-like of shape (n_samples, n_features)
Next time step predictions, same shape as
X.
- sample(n_samples: int, X_init: ndarray | None = None) ndarray#
Sample simulated data from the VAR model.
- Parameters:
- n_samplesint
Number of samples to generate
- X_initarray-like of shape (n_init_samples, n_features) or None
Initial time series prefix. If
None, then a random initial vector sampled from a standard Gaussian distribution is used.
- Returns:
- X_samplesarray-like of shape (n_samples, n_features)
- score(X: ndarray, y: None = None, segments: ndarray | None = None, sample_weight: ndarray | None = None) ndarray#
Return the prediction score for the model (by default R2).
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training multivariate time series.
- yIgnored
Not used, present here for API consistency by convention.
- segmentsarray-like of shape (n_samples,)
Indicator array of contiguous temporal segments in
X.- sample_weightfloat or array-like of shape (n_samples,), default=None
Sample weights. Only binary sample weights indicating time points to include/exclude are currently supported.
- Returns:
- score: float
Mean VAR prediction score (by default R2, see
scoring_function).
- static scoring_function(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True)#
\(R^2\) (coefficient of determination) regression score function.
Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). In the general case when the true y is non-constant, a constant model that always predicts the average y disregarding the input features would get a \(R^2\) score of 0.0.
In the particular case when
y_trueis constant, the \(R^2\) score is not finite: it is eitherNaN(perfect predictions) or-Inf(imperfect predictions). To prevent such non-finite numbers to pollute higher-level experiments such as a grid search cross-validation, by default these cases are replaced with 1.0 (perfect predictions) or 0.0 (imperfect predictions) respectively. You can setforce_finitetoFalseto prevent this fix from happening.Note: when the prediction residuals have zero mean, the \(R^2\) score is identical to the
Explained Variance score.Read more in the User Guide.
- Parameters:
- y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)
Ground truth (correct) target values.
- y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)
Estimated target values.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’}, array-like of shape (n_outputs,) or None, default=’uniform_average’
Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”.
- ‘raw_values’ :
Returns a full set of scores in case of multioutput input.
- ‘uniform_average’ :
Scores of all outputs are averaged with uniform weight.
- ‘variance_weighted’ :
Scores of all outputs are averaged, weighted by the variances of each individual output.
Changed in version 0.19: Default value of multioutput is ‘uniform_average’.
- force_finitebool, default=True
Flag indicating if
NaNand-Infscores resulting from constant data should be replaced with real numbers (1.0if prediction is perfect,0.0otherwise). Default isTrue, a convenient setting for hyperparameters’ search procedures (e.g. grid search cross-validation).Added in version 1.1.
- Returns:
- zfloat or ndarray of floats
The \(R^2\) score or ndarray of scores if ‘multioutput’ is ‘raw_values’.
Notes
This is not a symmetric function.
Unlike most other scores, \(R^2\) score may be negative (it need not actually be the square of a quantity R).
This metric is not well-defined for single samples and will return a NaN value if n_samples is less than two.
References
Examples
>>> from sklearn.metrics import r2_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> r2_score(y_true, y_pred) 0.948... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> r2_score(y_true, y_pred, ... multioutput='variance_weighted') 0.938... >>> y_true = [1, 2, 3] >>> y_pred = [1, 2, 3] >>> r2_score(y_true, y_pred) 1.0 >>> y_true = [1, 2, 3] >>> y_pred = [2, 2, 2] >>> r2_score(y_true, y_pred) 0.0 >>> y_true = [1, 2, 3] >>> y_pred = [3, 2, 1] >>> r2_score(y_true, y_pred) -3.0 >>> y_true = [-2, -2, -2] >>> y_pred = [-2, -2, -2] >>> r2_score(y_true, y_pred) 1.0 >>> r2_score(y_true, y_pred, force_finite=False) nan >>> y_true = [-2, -2, -2] >>> y_pred = [-2, -2, -2 + 1e-8] >>> r2_score(y_true, y_pred) 0.0 >>> r2_score(y_true, y_pred, force_finite=False) -inf
- set_fit_request(*, groups: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$', segments: bool | None | str = '$UNCHANGED$') LinearVAR#
Request metadata passed to the
fitmethod.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 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.Added 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:
- groupsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
groupsparameter infit.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.- segmentsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
segmentsparameter infit.
- Returns:
- selfobject
The updated object.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$', segments: bool | None | str = '$UNCHANGED$') LinearVAR#
Request metadata passed to the
scoremethod.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 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.Added 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_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.- segmentsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
segmentsparameter inscore.
- Returns:
- selfobject
The updated object.