Skip to main content

Simple riskengine for cvxpy

Project description

cvxrisk

PyPI version Apache 2.0 License Downloads

We provide an abstract Model class. The class is designed to be used in conjunction with cvxpy. Using this class, we can formulate a function computing a standard minimum risk portfolio as

import cvxpy as cp

from cvx.risk import Model


def minimum_risk(w: cp.Variable, risk_model: Model, **kwargs) -> cp.Problem:
    """Constructs a minimum variance portfolio.

    Args:
        w: cp.Variable representing the portfolio weights.
        risk_model: A risk model.

    Returns:
        A convex optimization problem.
    """
    return cp.Problem(
        cp.Minimize(risk_model.estimate(w, **kwargs)),
        [cp.sum(w) == 1, w >= 0] + risk_model.constraints(w, **kwargs)
    )

The risk model is injected into the function. The function is not aware of the precise risk model used. All risk models are required to implement the estimate method.

Note that factor risk models work with weights for the assets but also with weights for the factors. To stay flexible we are applying the **kwargs pattern to the function above.

A first example

A first example is a risk model based on the sample covariance matrix. We construct the risk model as follows

import numpy as np
import cvxpy as cp

from cvx.risk.sample import SampleCovariance

riskmodel = SampleCovariance(num=2)
w = cp.Variable(2)
problem = minimum_risk(w, riskmodel)

riskmodel.update(cov=np.array([[1.0, 0.5], [0.5, 2.0]]))
problem.solve()
print(w.value)

The risk model and the actual optimization problem are decoupled. This is good practice and keeps the code clean and maintainable.

In a backtest we don't have to reconstruct the problem in every iteration. We can simply update the risk model with the new data and solve the problem again. The implementation of the risk models is flexible enough to deal with changing dimensions of the underlying weight space.

Risk models

Sample covariance

We offer a SampleCovariance class as seen above.

Factor risk models

Factor risk models use the projection of the weight vector into a lower dimensional subspace, e.g. each asset is the linear combination of $k$ factors.

r_i = \sum_{j=1}^k f_j \beta_{ji} + \epsilon_i

The factor time series are $f_1, \ldots, f_k$. The loadings are the coefficients $\beta_{ji}$. The residual returns $\epsilon_i$ are assumed to be uncorrelated with the f actors.

Any position $w$ in weight space projects to a position $y = \beta^T w$ in factor space. The variance for a position $w$ is the sum of the variance of the systematic returns explained by the factors and the variance of the idiosyncratic returns.

Var(r) = Var(\beta^T w) + Var(\epsilon w)

We assume the residual returns are uncorrelated and hence

Var(r) = y^T \Sigma_f y + \sum_i w_i^2 Var(\epsilon_i)

where $\Sigma_f$ is the covariance matrix of the factors and $Var(\epsilon_i)$ is the variance of the idiosyncratic returns.

Factor risk models are widely used in practice. Usually two scenarios are distinguished. A first route is to rely on estimates for the factor covariance matrix $\Sigma_f$, the loadings $\beta$ and the volatilities of the idiosyncratic returns $\epsilon_i$. Usually those quantities are provided by external parties, e.g. Barra or Axioma.

An alternative would be to start with the estimation of factor time series $f_1, \ldots, f_k$. Usually they are estimated via a principal component analysis (PCA) of the asset returns. It is then a simple linear regression to compute the loadings $\beta$. The volatilities of the idiosyncratic returns $\epsilon_i$ are computed as the standard deviation of the observed residuals. The factor covariance matrix $\Sigma_f$ may even be diagonal in this case as the factors are orthogonal.

We expose a method to compute the first $k$ principal components.

cvar

We currently also support the conditional value at risk (CVaR) as a risk measure.

Poetry

We assume you share already the love for Poetry. Once you have installed poetry you can perform

make install

to replicate the virtual environment we have defined in pyproject.toml and locked in poetry.lock.

Jupyter

We install JupyterLab on fly within the aforementioned virtual environment. Executing

make jupyter

will install and start the jupyter lab.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cvxrisk-1.1.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

cvxrisk-1.1.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file cvxrisk-1.1.1.tar.gz.

File metadata

  • Download URL: cvxrisk-1.1.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for cvxrisk-1.1.1.tar.gz
Algorithm Hash digest
SHA256 cfc5a1bdba136e4159fcc145c40157be54c5060dfda8fc619d506fcccd23a93f
MD5 630516aef66dc37c7d8a8fcf375b8e97
BLAKE2b-256 ad53520ab9586b4d1ad054dc2fff058de70f635831fa01ff620470a67567fbe9

See more details on using hashes here.

File details

Details for the file cvxrisk-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: cvxrisk-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for cvxrisk-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e939c0c4019d54b2e77181bfa7d9b386b20f1ce72c17134606b65d4b4c32da57
MD5 d5b421fe8893a861fbf453e7b8d0779e
BLAKE2b-256 32852e99860315f11b77d9b3aef5b1ecad052e2125ed776a65256f0c9f311b86

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page