LinearRegression.priors_from_data#

LinearRegression.priors_from_data(X, y)#

Generate priors dynamically based on the input data.

This method allows models to set sensible priors that adapt to the scale and characteristics of the actual data being analyzed. It’s called during the fit() method before model building, allowing data-driven prior specification that can improve model performance and convergence.

The priors returned by this method are merged with any user-specified priors (passed via the priors parameter in __init__), with user-specified priors taking precedence in case of conflicts.

Parameters:
  • X (xarray.DataArray) – Input features/covariates with dimensions [“obs_ind”, “coeffs”]. Used to understand the scale and structure of predictors.

  • y (xarray.DataArray) – Target variable with dimensions [“obs_ind”, “treated_units”]. Used to understand the scale and structure of the outcome.

Returns:

Dictionary mapping parameter names to Prior objects. The keys should match parameter names used in the model’s build_model() method.

Return type:

Dict[str, Prior]

Notes

The base implementation returns an empty dictionary, meaning no data-driven priors are set by default. Subclasses should override this method to implement data-adaptive prior specification.

Priority Order for Priors: 1. User-specified priors (passed to __init__) 2. Data-driven priors (from this method) 3. Default priors (from default_priors property)

Examples

A typical implementation might scale priors based on data variance:

>>> def priors_from_data(self, X, y):
...     y_std = float(y.std())
...     return {
...         "sigma": Prior("HalfNormal", sigma=y_std, dims="treated_units"),
...         "beta": Prior(
...             "Normal",
...             mu=0,
...             sigma=2 * y_std,
...             dims=["treated_units", "coeffs"],
...         ),
...     }

Or set shape parameters based on data dimensions:

>>> def priors_from_data(self, X, y):
...     n_predictors = X.shape[1]
...     return {
...         "beta": Prior(
...             "Dirichlet",
...             a=np.ones(n_predictors),
...             dims=["treated_units", "coeffs"],
...         )
...     }

See also

WeightedSumFitter.priors_from_data

Example implementation that sets Dirichlet prior shape based on number of control units.