Factor Risk Model¶
A factor risk model is a statistical model that is used to quantify the risk of an investment or portfolio. It takes into account one or more risk factors, or variables that are known to affect the risk of the investment. The model aims to identify the factors that contribute to risk and to measure their impact on the investment’s return.
Description¶
A factor risk model describes the instrument / portfolio returns \(R\) by factor exposures \(B\), factor returns \(F\) and residual returns (sometimes named as idiosyncratic returns) \(U\).
Therefore, a factor risk model object contains the following attributes
Factor returns: The time series of factor returns, in dimension of (T, n)
Factor covariance: The covariance of the factors derived from the factor returns, in dimension of (n, n)
Factor exposures: The exposure of the instrument / portfolio on each factor, in dimension of (n, N)
Residual returns: The residual returns specific for each instrument / portfolio, in dimension of (T, N)
where T, N and n are the number of timeframes, instruments and factors.
Transform¶
Transformation allows the risk model to be expanded from estimation universe to model universe.
Transformation requires passing the instrument returns of the model universe in
Same length and granularity of time series
Homogeneity
and the factor returns to examine their exposures and residual returns.
One usage is to transform the trained risk model to a bigger universe
risk_model.fit(estimation_returns)
transformed_risk_model = risk_model.transform(model_returns)
Another usage is to combine the factor returns from risk models derived from different methodologies, e.g. statistical and fundamental, and then transform into a more generic risk model.
For example, model1 and model2 contain the statistical and fundamental
factors respectively. To combine them and then transform into a new risk
model
import pandas as pd
from fpm_risk_model import FactorRiskModel
risk_model = FactorRiskModel(
factor_returns=pd.concat([model1.factor_returns, model2.factor_returns])
)
risk_model.transform(returns)
The transformed risk model is always updated in place. To retain the original
risk model, please always use copy as a backup.
Module¶
- class fpm_risk_model.factor_risk_model.FactorRiskModel(factor_exposures: Optional[ndarray] = None, factor_returns: Optional[ndarray] = None, residual_returns: Optional[ndarray] = None, **kwargs)¶
Factor Risk Model.
The class is an abstract class to fit the factor risk model.
The factor risk model contains the data attribute factor_exposures, factors and residual_returns.
The factor exposures are the exposures of each instrument to the specified factors.
The factor returns are returns among the date / time series for each factor.
The residual returns are the idiosyncratic returns of the instruments regarding the specified factor exposures and returns.
- __init__(factor_exposures: Optional[ndarray] = None, factor_returns: Optional[ndarray] = None, residual_returns: Optional[ndarray] = None, **kwargs)¶
Constructor
Parameters¶
- factor_exposuresndarray
Factor exposures of the factor risk model.
- factors_returnsndarray
Factor returns of the factor risk model.
- residual_returnsndarray
Residual returns of the factor risk model.
- property factor_exposures: ndarray¶
Return the factor exposures.
Return¶
- ndarray
Matrix in dimension (n, N) where N is the number of instruments and n is the number of factors.
- property factor_returns: ndarray¶
Return the factor returns.
Return¶
- ndarray
Matrix in dimension (T, n) where n is the number of factors and T is the number of time frames.
- property residual_returns: ndarray¶
Return the residual returns.
Return¶
- ndarray
Matrix in dimension (T, N) where N is the number of instruments and T is the number of time frames.
- fit(X: ndarray, weights: Optional[ndarray] = None) object¶
Fit the model.
Parameters¶
- Xndarray
Input array of shape (T, N) where N is the number of instruments and T is the number of timeframes.
- weights: Optional[ndarray]
Weights array of shape (N,) where N is the number of instruments.
- specific_variances(weights=None, ddof=1) ndarray¶
Get specific variances.
Parameters¶
- ddofint
Degrees of freedom.
Returns¶
- ndarray
Specific variances of the instruments.
- transform(y: ndarray, regressor: Optional[object] = None) object¶
Transform the factor risk model.
The method is used to transform the factor risk model by passing another set of returns. Most of the time, the factor risk model is fitted by the estimation universe, and then transformed by the model universe.
Parameters¶
- yndarray
The instrument returns.
- regressorobject, default=None
Regressor to transform the input y into factor exposures. If None, the regressor is set to the default WLS.
Returns¶
- ndarray
The transformed factor risk model.
- cov(halflife: Optional[float] = None, ddof=1) ndarray¶
Get the covariance matrix.
Parameters¶
- halflifeOptional[float]
Half life in applying the exponential weighting on factor returns for computing the factor covariance matrix. If None is passed, no exponential weighting is applied.
Returns¶
- numpy.ndarray
A square pairwise covariance matrix which its diagonal entries are the variances.
- write_directory(path: str, format='parquet', **kwargs)¶
Write the factor risk model to directory.
Parameters¶
- path: str
Destination path.
- format: str
Supported formats. Default is “parquet”. Options are “csv”, “parquet” and “hdf”.
- **kwargs: dict
Optional keyword arguments for the write operation.
- classmethod read_directory(path: str, format: str = 'parquet', **kwargs)¶
Read model from specified directory.
Parameters¶
- path: str
Directory to read model from.
- format: str
Supported formats. Default is “parquet”. Options are “csv”, “parquet” and “hdf”.
- **kwargs: dict
Optional keyword arguments for the write operation.