linearmodels.asset_pricing.model.TradedFactorModel.from_formula

classmethod TradedFactorModel.from_formula(formula: str, data: DataFrame, *, portfolios: DataFrame | None = None) TradedFactorModel[source]
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

Returns:

Model instance

Return type:

linearmodels.asset_pricing.model.TradedFactorModel

Notes

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

Examples

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

Using only factors

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