Risk Model

Risk model is an abstract class to produce risk metrics. Pairwise covariance and other related metrics, like correlation, are supported.

Fit the model

In general, all the risk models are fitted by passing the instrument returns, It means after initialising the risk model object, call the method fit(X=...).

The method returns the object itself while the object is already stored with fitted attributes.

To keep the risk model fitted attributes, you should simply call the method copy() to ensure a deep clone of the fitted result.

Rolling risk model

Rolling risk model fits risk models at each time t given all the information provided at or before time t. Primarily rolling risk models are fitted in a rolling window of information, for example, instrument returns.

In technical point of view, rolling risk model object holds a dictionary of risk models and its keys are date / time. A pandas.DataFrame of instrument returns in ascending time series order should be passed into the method fit to ensure only retrospective information is used, rather than future one.

For example, to construct a rolling risk model fitted by PCA model in a rolling window of 63 days (3 business months equivalent)

from fpm_risk_model import RollingRiskModel
from fpm_risk_model.statistics.pca import PCA

rolling_risk_model = RollingRiskModel(
  model=PCA(n_components=5),
  window=63,
)

# Instrument returns must be a DataFrame
rolling_risk_model.fit(instrument_returns)

To retrieve a risk model, call method get with a specified date

from pandas import Timestamp
rolling_risk_model.get(Timestamp("2000-01-01"))

Module

class fpm_risk_model.risk_model.RiskModelConfig(*, show_all_instruments: bool = False)

Risk model configuration.

Parameters

show_all_instrumentsbool.

Indicate whether to show all instruments. Default is False. If True, the instruments outside of the universe in each period may not be filtered out.

show_all_instruments: bool
class fpm_risk_model.risk_model.RiskModel(show_all_instruments: bool = False, **kwargs)

Risk Model.

The class is an abstract class to compute covariance and other related metrics.

ConfigClass

alias of RiskModelConfig

__init__(show_all_instruments: bool = False, **kwargs)

Constructor.

Parameters

show_all_instrumentsbool.

Indicate whether to show all instruments. Default is False. If True, the instruments outside of the universe in each period may not be filtered out.

property config

Return the configuration object.

abstract cov(**kwargs) ndarray

Get the covariance matrix.

Returns

numpy.ndarray

A square pairwise covariance matrix which its diagonal entries are the variances.

vol(**kwargs) ndarray

Get the volatility series.

Returns

numpy.ndarray

Volatility series derived from covariance matrix.

corr(**kwargs) ndarray

Get the correlation matrix.

Returns

numpy.ndarray

A square pairwise correlation matrix which its diagonal entries are all ones.

asdict()

Returns a dict representation of the object.

class fpm_risk_model.rolling_risk_model.RollingRiskModelConfig(*, window: int, show_progress: bool = False)

Rollingrisk model configuration.

Parameters

window: Optional[int]

Number of rolling windows to use from the returns. Must be provided in fitting the model.

show_progress: Optional[bool]

Indicate to show progress bar in running.

window: int
show_progress: bool
class fpm_risk_model.rolling_risk_model.RollingRiskModel(model: Optional[RiskModel] = None, window: Optional[int] = None, show_progress: Optional[bool] = False, values: Optional[Dict[datetime, RiskModel]] = None)

Rolling risk model.

A rolling risk model simply holds a dictionary of risk models in a given date / time range. Each key and value pair is a date / time and risk model object respectively.

ConfigClass

alias of RollingRiskModelConfig

__init__(model: Optional[RiskModel] = None, window: Optional[int] = None, show_progress: Optional[bool] = False, values: Optional[Dict[datetime, RiskModel]] = None)

Constructor.

Parameters

model: Optional[RiskModel]

Risk model object to fit in rolling basis.

window: Optional[int]

Number of rolling windows to use from the returns. Must be provided in fitting the model.

show_progress: Optional[bool]

Indicate to show progress bar in running.

values: Optional[Dict[datetime, RiskModel]]

Rolling risk models values.

property config

Return the configuration object.

get(name, **kwargs) RiskModel

Return a factor risk model from the given name / key.

keys() Iterable[datetime]

Return a list of keys.

values() Iterable[RiskModel]

Return a list of values.

items() Iterable[Tuple[datetime, RiskModel]]

Return a list of tuples with keys and values.

fit(X: DataFrame, validity: Optional[DataFrame] = None, weights: Optional[DataFrame] = None) object

Fit the model.

Parameters

X: DataFrame

The instrument returns of which its index and columns are the date / time and return values.

validity: DataFrame

The instrument validity on the date.

weights: DataFrame

The weights of the instruments, same dimension as the instrument returns.

Returns

object

The object itself.

asdict()

Returns a dict representation of the object.