pymargins.ModelAdapter

class pymargins.ModelAdapter

Abstract base class for model adapters.

Concrete subclasses target specific frameworks (statsmodels, linearmodels, sklearn) and possibly specific model classes within them. The Margins session calls only the methods defined here; all framework- specific knowledge is encapsulated in the adapter.

Subclassing contract

Subclasses MUST implement:
  • coefficients()

  • covariance()

  • predict()

  • design_matrix_from_df()

  • column_index_of_variable()

  • variable_metadata()

  • training_data (property)

  • supported_inference_methods (property)

Subclasses MAY implement:
  • attach() to validate session compatibility

  • refit() if bootstrap inference should be supported

  • native_predict() if direct framework calls are needed elsewhere

__init__()

Methods

__init__()

attach(session)

Attach this adapter to a Margins session.

bootstrap_state()

Replay state for a refitted adapter.

coefficients()

Return β̂ as a 1D JAX array.

column_index_of_variable(name)

Return the design-matrix column index corresponding to a variable.

covariance([vcov_spec])

Return Σ̂ as a 2D JAX array.

design_matrix_from_df(df)

Build a design matrix from a concrete DataFrame of evaluation rows.

predict(beta, X[, offset])

Prediction on the response scale, JAX-compatible.

refit(resampled_data, *[, index])

Refit the model on resampled data, returning a new adapter.

variable_metadata()

Return per-variable metadata used by averaging and validation.

Attributes

gradient_backend_recommendation

The gradient backend the engine should use by default with this adapter.

n_outcomes

Number of outcome classes for multi-outcome models, default 1.

outcome_labels

Outcome class labels for multi-outcome models, or None.

supported_inference_methods

Which inference methods this adapter supports.

supports_jax_autodiff

True if predict() uses jax.numpy throughout, producing exact autodiff gradients.

training_data

The training data used to fit the model.