pyportopt
Portfolio optimization: minimum variance, maximum return under a volatility cap, maximum Sharpe ratio, mean-variance utility, importance, Kelly, tracking error, maximum diversification and risk parity, with box bounds, a gross exposure limit, cash and the multipliers of every constraint. The numerical core is the PortOpt C++23 library, which this package bundles and calls through its C API.
pip install pyportopt
import numpy as np
import portopt as po
mu = np.array([0.05, 0.08, 0.11])
cov = np.array([[0.04, 0.006, 0.002], [0.006, 0.09, 0.012], [0.002, 0.012, 0.16]])
data = po.Data(mu, cov)
result = po.solve(po.Spec(po.Objective.MIN_VARIANCE, bounds=(0.0, 1.0)), data)
print(result.status.name, result.weights) # OPTIMAL [0.6234 0.2356 0.141 ] (rounded)
From a return panel to a portfolio
The estimators are pure functions on arrays. Here: sample moments of daily returns, the Black-Litterman posterior with one relative view (asset 0 beats asset 1 by 1%), and a maximum Sharpe solve.
import numpy as np
import portopt as po
returns = np.random.default_rng(0).normal(0.0004, 0.01, size=(500, 3)) # daily returns, periods by assets
_, cov = po.sample_moments(returns)
cov = cov * 252 # annualize after the estimation
pi = po.bl_equilibrium(cov, [0.5, 0.3, 0.2], delta=2.5) # implied returns, in excess of the rate
p, q = [[1.0, -1.0, 0.0]], [0.01]
views = po.Views(p, q, po.bl_omega_he_litterman(cov, p, tau=0.05))
posterior = po.bl_posterior(cov, pi, 0.05, views) # posterior.mu, posterior.cov
data = posterior.to_data(risk_free=0.02) # adds the rate back and sets the cash
result = po.solve(po.Spec(po.Objective.MAX_SHARPE, bounds=(0.0, 1.0)), data)
print(result.status.name, result.weights) # OPTIMAL [0.49 0.31 0.2 0.] (rounded)
Pi, q and the posterior mean are in excess of the risk-free rate, which is why to_data adds it back; without
risk_free the mean is left as it is and there is no cash. bayes_stein(mu_hat, cov, t) shrinks a mean, and
bl_omega_idzorek is the other Omega. Compute bayes_stein on the original (daily) data and annualize its mu
afterwards: its weight depends on the frequency.
The distribution is pyportopt; the module is portopt. Python 3.11 or newer, with numpy. Until 1.0 the API may
change between minor versions. Documentation, the C and C++ interfaces and the changelog:
https://github.com/GabrielMarquezMatte/PortfolioOptimization
Metadata
Release files for pyportopt 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 2.2 MB
Release files / pyportopt-1.1.0-py3-none-win_amd64.whl
| Download URL | pyportopt-1.1.0-py3-none-win_amd64.whl |
|---|---|
| Size | 376.4 kB |
| Tags | Python 3 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e46ab2f7b394a97ccdac638845702500b5cbb0a25f99054281c207df9695061f
|
|
BLAKE2b-256 checksum How to use checksums |
b40e6836521dd4131145745348530876879db35e11ae282c186ad231c8f130fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-musllinux_1_2_x86_64.whl
| Download URL | pyportopt-1.1.0-py3-none-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 427.5 kB |
| Tags | Linux musl 1.2+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
3b49a8fa76ae308f1dc3e0f5ddea3790a812ed18a98c829a2255a04b714fe0ce
|
|
BLAKE2b-256 checksum How to use checksums |
a8a6112cd27ac69a4bb9c0852b66216028563edebafe56631bc86b4ac9789d69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-musllinux_1_2_aarch64.whl
| Download URL | pyportopt-1.1.0-py3-none-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 387.4 kB |
| Tags | Linux musl 1.2+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
459da21c83bef0bcf493f6a1ec4ffc9adb2affe33121f59fbd3bf7fe4727da4a
|
|
BLAKE2b-256 checksum How to use checksums |
39eeeb9c9891e3ed10745e47f8176b38e7a5312f633c360caa753cc01088bc90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-manylinux_2_25_x86_64.whl
| Download URL | pyportopt-1.1.0-py3-none-manylinux_2_25_x86_64.whl |
|---|---|
| Size | 348.3 kB |
| Tags | Linux glibc 2.25+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
ff36001aacb8cd9e6aaf42d1de0bf31d1d7284ca12e4e7ace86e7369c38e1a8b
|
|
BLAKE2b-256 checksum How to use checksums |
17c935cb5d4180c00d557ff368cf60f618b55b9ce83150319bcc30a674161704
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-manylinux_2_25_aarch64.whl
| Download URL | pyportopt-1.1.0-py3-none-manylinux_2_25_aarch64.whl |
|---|---|
| Size | 307.5 kB |
| Tags | Linux glibc 2.25+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
eae3fb43ab1b0dfe32054ed3b220d747c0bc758cccaccc423d104c4f3000b434
|
|
BLAKE2b-256 checksum How to use checksums |
5136e5b8c282b359ab6d6d63e23c5eb79cae6332c5c4ee2e5493d5be780fcd85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-macosx_11_0_x86_64.whl
| Download URL | pyportopt-1.1.0-py3-none-macosx_11_0_x86_64.whl |
|---|---|
| Size | 170.6 kB |
| Tags | Python 3 macOS 11.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
3cb118e676a60177136ffb9cb80f3b0e1833b5e7154368d4921112a9c8d2583d
|
|
BLAKE2b-256 checksum How to use checksums |
d4e4b5ee3120344bdec060ca77eb7d66ca970bb7cf77e6c4d3c026dc37793d4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyportopt-1.1.0-py3-none-macosx_11_0_arm64.whl
| Download URL | pyportopt-1.1.0-py3-none-macosx_11_0_arm64.whl |
|---|---|
| Size | 150.8 kB |
| Tags | Python 3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
63089cbf601e679ba64341a04776e4bcf40db73438d37d8057bb67662636e88b
|
|
BLAKE2b-256 checksum How to use checksums |
a7fbf5f44567258dc7057e00f82e1c2af0772b2c6858be1824ee16a504329949
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pyportopt 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.