Simple riskengine for cvxpy
Project description
cvxrisk: Convex Optimization for Portfolio Risk Management
📋 Overview
cvxrisk is a Python library for portfolio risk management using convex optimization. It provides a flexible framework for implementing various risk models that can be used with CVXPY to solve portfolio optimization problems.
The library is built around an abstract Model class that standardizes
the interface for different risk models, making it easy to swap between
them in your optimization problems.
🚀 Installation
# Install from PyPI (without any convex solver)
pip install cvxrisk
# For development installation
git clone https://github.com/cvxgrp/cvxrisk.git
cd cvxrisk
make install
# For experimenting with the notebooks (after cloning)
make marimo
⚠️ Warning! The package does not install a convex solver if not explicitly desired.
It relies on cvxpy-base. If you use cvxrisk as a dependency
in your projects you may want to install clarabel
using pip install cvxrisk[clarabel] or mosek
using pip install cvxrisk[mosek].
🔧 Quick Start
cvxrisk makes it easy to formulate and solve portfolio optimization problems:
import cvxpy as cp
import numpy as np
from cvx.risk.sample import SampleCovariance
from cvx.risk.portfolio import minrisk_problem
# Create a risk model
riskmodel = SampleCovariance(num=2)
# Update the model with data
riskmodel.update(
cov = np.array([[1.0, 0.5], [0.5, 2.0]]),
lower_assets = np.zeros(2),
upper_assets = np.ones(2)
)
# Define portfolio weights variable
weights = cp.Variable(2)
# Create and solve the optimization problem
problem = minrisk_problem(riskmodel, weights)
problem.solve(solver=cp.CLARABEL)
# Print the optimal weights with deterministic spacing
print(np.array2string(np.round(weights.value, 2), separator=" "))
[0.75 0.25]
📊 Features
cvxrisk provides several risk models:
Sample Covariance
The simplest risk model based on the sample covariance matrix:
from cvx.risk.sample import SampleCovariance
import numpy as np
riskmodel = SampleCovariance(num=2)
riskmodel.update(
cov=np.array([[1.0, 0.5], [0.5, 2.0]]),
lower_assets=np.zeros(2),
upper_assets=np.ones(2),
)
# Reconstruct covariance from Cholesky factor and print
cov_est = riskmodel.parameter["chol"].value.T @ riskmodel.parameter["chol"].value
print(np.array2string(cov_est, precision=1))
[[1. 0.5]
[0.5 2. ]]
Factor Risk Models
Factor models reduce dimensionality by projecting asset returns onto a smaller set of factors:
import numpy as np
from cvx.risk.factor import FactorModel
from cvx.risk.linalg import pca
import pandas as pd
# Create some sample returns data
a = 100
m = 25
returns = pd.DataFrame(np.random.randn(a, m))
# Compute principal components (deterministic using a fixed seed for reproducibility)
np.random.seed(0)
factors = pca(returns, n_components=10)
# Create and update the factor model
model = FactorModel(assets=m, k=10)
model.update(
cov=factors.cov.values,
exposure=factors.exposure.values,
idiosyncratic_risk=factors.idiosyncratic.std().values,
lower_assets=np.zeros(m),
upper_assets=np.ones(m),
lower_factors=-0.1*np.ones(10),
upper_factors=0.1*np.ones(10),
)
# Verify the model has the correct dimensions
print(model.parameter["exposure"].value.shape)
(10, 25)
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 factors.
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.
Conditional Value at Risk (CVaR)
CVaR measures the expected loss in the worst-case scenarios:
import numpy as np
from cvx.risk.cvar import CVar
# Create some sample historical returns (deterministic)
np.random.seed(0)
historical_returns = np.random.randn(50, 14)
# Create and update the CVaR model
model = CVar(alpha=0.95, n=50, m=14)
model.update(
returns=historical_returns,
lower_assets=np.zeros(14),
upper_assets=np.ones(14),
)
# Verify the model parameters
print(model.alpha)
print(model.parameter["R"].value.shape)
0.95
(50, 14)
📚 Documentation
For more detailed documentation and examples, visit our documentation site.
🛠️ Development
cvxrisk uses modern Python development tools:
# Install development dependencies
make install
# Run tests
make test
# Format code
make fmt
# Start interactive notebooks
make marimo
📄 License
cvxrisk is licensed under the Apache License 2.0. See LICENSE for details.
👥 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
For more information, see CONTRIBUTING.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cvxrisk-1.4.14.tar.gz.
File metadata
- Download URL: cvxrisk-1.4.14.tar.gz
- Upload date:
- Size: 295.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8e642cbebc5c4ccbe8cb40ec24ded3876e32eb14ab6362b962d54711e2e106f
|
|
| MD5 |
14fbba16ea29e03e54879f800b096287
|
|
| BLAKE2b-256 |
525242ef1bb150d4774347fb48604b18f4f5157d7752f0bd42b26cbfe94b037a
|
Provenance
The following attestation bundles were made for cvxrisk-1.4.14.tar.gz:
Publisher:
release.yml on cvxgrp/cvxrisk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvxrisk-1.4.14.tar.gz -
Subject digest:
b8e642cbebc5c4ccbe8cb40ec24ded3876e32eb14ab6362b962d54711e2e106f - Sigstore transparency entry: 1006570214
- Sigstore integration time:
-
Permalink:
cvxgrp/cvxrisk@e8651be0197e1c0ff7d6b3e13d0ae1d0878f9048 -
Branch / Tag:
refs/tags/v1.4.14 - Owner: https://github.com/cvxgrp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e8651be0197e1c0ff7d6b3e13d0ae1d0878f9048 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file cvxrisk-1.4.14-py3-none-any.whl.
File metadata
- Download URL: cvxrisk-1.4.14-py3-none-any.whl
- Upload date:
- Size: 36.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25cbfd092dccfd3481a5027fc99ff93e55019b690fd7de4890a46102a4f12148
|
|
| MD5 |
32cd55b36d6ed95e96eb610ad4ff60cb
|
|
| BLAKE2b-256 |
8396d56ac2ccf8515232833a20bcecaf08a5d5a1c0f184433f8908965917e60c
|
Provenance
The following attestation bundles were made for cvxrisk-1.4.14-py3-none-any.whl:
Publisher:
release.yml on cvxgrp/cvxrisk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvxrisk-1.4.14-py3-none-any.whl -
Subject digest:
25cbfd092dccfd3481a5027fc99ff93e55019b690fd7de4890a46102a4f12148 - Sigstore transparency entry: 1006570218
- Sigstore integration time:
-
Permalink:
cvxgrp/cvxrisk@e8651be0197e1c0ff7d6b3e13d0ae1d0878f9048 -
Branch / Tag:
refs/tags/v1.4.14 - Owner: https://github.com/cvxgrp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e8651be0197e1c0ff7d6b3e13d0ae1d0878f9048 -
Trigger Event:
workflow_dispatch
-
Statement type: