linearmodels.asset_pricing.model.LinearFactorModel.from_formula

classmethod LinearFactorModel.from_formula(formula: str, data: DataFrame, *, portfolios: DataFrame | None = None, risk_free: bool = False, sigma: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None = None) → LinearFactorModel[source]

Specify a model using the formula interface.

Parameters:
formula: str

Formula modified for the syntax described in the notes.

data: DataFrame

DataFrame containing the variables used in the formula.

portfolios: DataFrame | None = None

Portfolios to be used in the model. If provided, must use formula syntax containing only factors.

risk_free: bool = False

Flag indicating whether the risk-free rate should be estimated from returns along other risk premia. If False, the returns are assumed to be excess returns using the correct risk-free rate. The default is False.

sigma: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None = None

Positive definite residual covariance (nportfolio by nportfolio) that weights the test portfolios in the second stage. A ValueError is raised if it does not have this shape, contains missing or infinite values, or is not symmetric and positive definite. If not provided, the identity matrix is used.

Returns:

Model instance.

Return type:

linearmodels.asset_pricing.model.LinearFactorModel

Notes

The formula can be used in one of two ways. The first specifies only the factors and uses the data provided in portfolios as the test portfolios. The second specifies the portfolio using + to separate the test portfolios and ~ to separate the test portfolios from the factors.

The formula can use the variables and functions that are available where from_formula is called, in addition to the columns of data and the transforms of formulaic.

Changed in version 7.1: Variables and functions of the caller could not be used in the formula, and raised a FactorEvaluationError.

Examples

>>> from linearmodels.datasets import french
>>> from linearmodels.asset_pricing import LinearFactorModel
>>> data = french.load()
>>> formula = "S1M1 + S1M5 + S3M3 + S5M1 + S5M5 ~ MktRF + SMB + HML"
>>> mod = LinearFactorModel.from_formula(formula, data)

Using only factors

>>> portfolios = data[["S1M1", "S1M5", "S3M1", "S3M5", "S5M1", "S5M5"]]
>>> formula = "MktRF + SMB + HML"
>>> mod = LinearFactorModel.from_formula(formula, data, portfolios=portfolios)