B2B#

class mvpy.estimators.B2B(alphas: Tensor | ndarray | float | int = 1, **kwargs)[source]#

Implements a back-to-back regression to disentangle causal contributions of correlated features.

The back-to-back estimator is a two-step estimator that consists of a decoder and an encoder. Effectively, the idea is to split the data \(X\) and \(y\) into two folds, decode all features in fold a, then use predictions from the decoder to encode all true features from all predictions in fold b. Consequently, this allows us to obtain a disentangled estimate of the causal contribution of each feature.

In practice, this is implemented as:

\[\hat{G} = (Y^T Y + \alpha_Y)^{-1}Y^T X\]
\[\hat{H} = (X^T X + \alpha_X)^{-1}X^T Y\hat{G}\]

where \(\hat{G}\) is the decoder and \(\hat{H}\) is the encoder, and \(\alpha\) are regularisation parameters. Consequently, the diagonal of \(\hat{H}\) contains the estimated causal contributions of our features.

For more information on back-to-back regression, please see [1].

Parameters:
alphastorch.Tensor | np.ndarray, default=torch.tensor([1])

The penalties to use for estimation.

fit_interceptbool, default=True

Whether to fit an intercept.

alpha_per_targetbool, default=False

Whether to use a different penalty for each target.

normalise_decoderbool, default=False

Whether to normalise decoder ouputs.

Attributes:
alphastorch.Tensor | np.ndarray

The penalties to use for estimation.

fit_interceptbool

Whether to fit an intercept.

alpha_per_targetbool

Whether to use a different penalty for each target.

normalise_decoderbool

Whether to normalise decoder ouputs.

decoder_mvpy.estimators.RidgeDecoder

The decoder.

encoder_mvpy.estimators.RidgeDecoder

The encoder.

scaler_mvpy.estimators.Scaler

The scaler.

causal_torch.Tensor | np.ndarray

The causal contribution of each feature of shape (n_features,).

pattern_torch.Tensor | np.ndarray

The decoded patterns of shape (n_channels, n_features).

metric_mvpy.metrics.r2

The default metric to use.

See also

mvpy.preprocessing.Scaler

If applied, scalers used in this class.

mvpy.estimators.RidgeDecoder

Ridge decoders used for the two-step procedure here.

Notes

When penalising per target by setting alpha_per_target to True, you may want to consider normalising the decoder by also setting normalise_decoder to True. This is because otherwise decoder outputs may live on very different scales, potentially distorting the causal estimates per predictor.

Patterns are computed as per [2]. However, these patterns are not disentangled and may, consequently, be less informative than desired, depending on strength of existing correlations.

Warning

Back-to-back regression does not naturally lend itself to prediction. Therefore, predict() and score() use and evaluate the first-stage decoder, not the full back-to-back regression. Causal coefficients from the encoder are available from causal_ instead.

References

[1]

King, J.R., Charton, F., Lopez-Paz, D., & Oquab, M. (2020). Back-to-back regression: Disentangling the influence of correlated factors from multivariate observations. NeuroImage, 220, 117028. 10.1016/j.neuroimage.2020.117028

[2]

Haufe, S., Meinecke, F., Görgen, K., Dähne, S., Haynes, J.D., Blankertz, B., & Bießmann, F. (2014). On the interpretation of weight vectors of linear models in multivariate neuroimaging. NeuroImage, 87, 96-110. 10.1016/j.neuroimage.2013.10.067

Examples

>>> import torch
>>> from mvpy.estimators import B2B
>>> ß = torch.normal(0, 1, (2, 60))
>>> X = torch.normal(0, 1, (100, 2))
>>> y = X @ ß + torch.normal(0, 1, (100, 60))
>>> X, y = y, X
>>> y = torch.cat((y, y.mean(1).unsqueeze(-1) + torch.normal(0, 5, (100, 1))), 1)
>>> b2b = B2B()
>>> b2b.fit(X, y).causal_
tensor([0.4470, 0.4594, 0.0060])
clone() → B2B[source]#

Clone this class.

Returns:
b2bmvpy.estimators.B2B

The cloned estimator.

fit(X: ndarray | Tensor, y: ndarray | Tensor) → B2B[source]#

Fit the estimator.

Parameters:
Xnp.ndarray | torch.Tensor

The neural data of shape (n_trials, n_channels).

ynp.ndarray | torch.Tensor

The targets of shape (n_trials, n_features).

Returns:
b2bmvpy.estimators.B2B

The fitted estimator.

predict(X: ndarray | Tensor) → ndarray | Tensor[source]#

Predict from the estimator.

Parameters:
Xnp.ndarray | torch.Tensor

The neural data of shape (n_trials, n_channels).

Returns:
y_hnp.ndarray | torch.Tensor

The predictions from decoder of shape (n_trials, n_features).

Warning

Outputs from predict() are outputs of the first-stage decoding model. This is because back-to-back regression does not principally have meaningful predictions.

score(X: ndarray | Tensor, y: ndarray | Tensor, metric: Metric | Tuple[Metric] | None = None) → Tensor | Dict[str, ndarray | Tensor][source]#

Make predictions from \(X\) and score against \(y\).

Parameters:
Xtorch.Tensor | np.ndarray

Input data of shape (n_samples, n_channels).

ytorch.Tensor | np.ndarray

Output data of shape (n_samples, n_features).

metricOptional[Metric], default=None

Metric or tuple of metrics to compute. If None, defaults to metric_.

Returns:
scoretorch.Tensor | np.ndarray | Dict[str, torch.Tensor | np.ndarray]

Scores of shape (n_features,) or, for multiple metrics, a dictionary of metric names and scores of shape (n_features,).

Warning

If multiple values are supplied for metric, this function will output a dictionary of {Metric.name: score, ...} rather than a stacked array. This is to provide consistency across cases where metrics may or may not differ in their output shapes.

Warning

Calling score() scores the first-stage decoder, not the full back-to-back regression model. This is because back-to-back regression has no meaningful predictions.