Skip to main content

Portfolio allocation tools with views, robust optimization, and ensembling

Project description

Py-vAllocation

PyPI Python versions

Py-vAllocation is a research-to-production toolkit for scenario-based portfolio optimisation. Build mean-variance, CVaR, and relaxed risk parity frontiers; incorporate Black–Litterman and entropy pooling views; apply shrinkage-heavy statistics (NIW, Ledoit–Wolf, nonlinear shrinkage, Tyler, Huber, POET); ensemble strategies; and convert weights to discrete trades. Pandas labels are preserved throughout the workflow.

Highlights

  • Consistent optimisation surface - switch between mean-variance, CVaR, relaxed risk parity, and robust formulations without rewriting constraints.
  • View integration - Black–Litterman mean views plus entropy pooling constraints keep discretionary inputs consistent with posterior moments.
  • Robust models - relaxed risk parity, Bayesian NIW updates, and Meucci-style probability tilts.
  • Moment estimation - Ledoit–Wolf, James–Stein, nonlinear shrinkage, Tyler, Huber, POET, graphical lasso, and more via estimate_moments.
  • Production plumbing - ensemble builders, discrete allocation, plotting, and reporting helpers reduce friction between research and delivery.
  • Stress testing & PnL - one-line helpers for probability tilts, linear shocks, and compact risk reports.
  • Optional extras - install the robust extra only when heavy dependencies are needed.

Design principles

  • Pandas-first inputs/outputs with consistent labels when they are supplied.
  • Scenario-based risk by default, with clear risk labels across frontiers.
  • Minimal reformatting: utility helpers standardise shapes, weights, and probabilities.

Installation

pip install py-vallocation

For nonlinear shrinkage and POET estimators:

pip install py-vallocation[robust]

Requires cvxopt>=1.2.0. If you don't have it, see the installation guide.

Quickstart

Run the end-to-end ETF example (writes plots and CSVs to output/):

python examples/quickstart_etf_allocation.py

Key artefacts:

  • output/frontiers.png - in-sample vs out-of-sample efficient frontiers with robust overlay.
  • output/robust_uncertainty.png, robust_param_impact.png, robust_assumptions_3d.png - robust diagnostics.
  • output/stacked_weights.csv, selected_weights.csv, average_weights.csv - ensemble summaries.
  • Terminal output covering discrete trade sizing and stress results.

Or use the API directly:

import pandas as pd
from pyvallocation.portfolioapi import AssetsDistribution, PortfolioWrapper

scenarios = pd.DataFrame({
    "Stock_A": [0.01, -0.02, 0.015],
    "Stock_B": [0.007, 0.003, 0.004]
})

port = PortfolioWrapper(AssetsDistribution(scenarios=scenarios))
port.set_constraints({"long_only": True, "total_weight": 1.0})

frontier = port.variance_frontier(num_portfolios=20)
weights, ret, risk = frontier.get_tangency_portfolio(risk_free_rate=0.01)
print(weights)

# Pick a portfolio by CVaR even on a variance frontier (scenarios required)
weights_cvar, ret_cvar, cvar_val = frontier.portfolio_at_risk_target(
    max_risk=0.02, risk_label="CVaR (alpha=0.05)"
)
print(weights_cvar)

Examples

The examples/ directory contains runnable scripts (see examples/README.md):

  • quickstart_etf_allocation.py - moments → frontiers → ensemble → trades
  • mean_variance_frontier.py, cvar_allocation.py, robust_frontier.py (use variance_frontier / cvar_frontier)
  • relaxed_risk_parity_frontier.py, portfolio_ensembles.py, discrete_allocation.py
  • stress_and_pnl.py - probability tilts + linear shocks + performance reports

Notebooks (examples/*.ipynb) mirror the tutorials.

Documentation

  • Full documentation: https://py-vallocation.readthedocs.io
  • Tutorials live under docs/tutorials/ and mirror the runnable scripts.
  • API reference is generated from docstrings (docs/pyvallocation*.rst).
  • Build locally:
pip install -e .[robust]
sphinx-build -b html docs docs/_build/html

Repository layout

  • pyvallocation/ - library source code.
  • examples/ - runnable workflows (ETF quickstart, CVaR frontier, ensembles, stress testing, discrete allocation).
  • docs/ - Sphinx site (tutorials, API reference, bibliography).
  • tests/ - pytest suite covering numerical routines, ensembles, plotting, and discrete allocation.
  • output/ - artefacts written by example scripts.

Requirements

  • Python 3.8+
  • numpy, pandas, scipy, cvxopt

References

  • Markowitz (1952) - Portfolio Selection
  • Black & Litterman (1992) - Global Portfolio Optimization
  • Ledoit & Wolf (2004, 2020) - Covariance shrinkage
  • Meucci (2008) - Fully Flexible Views (entropy pooling)
  • Rockafellar & Uryasev (2000) - CVaR optimization

See the bibliography for the complete list.

Contributing

Issues and PRs welcome. See CONTRIBUTING.md.

License

GPL-3.0-or-later — see LICENSE for the full text. Portions of the optimisation routines are adapted (with attribution) from fortitudo-tech.

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

py_vallocation-0.4.0.tar.gz (124.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_vallocation-0.4.0-py3-none-any.whl (124.1 kB view details)

Uploaded Python 3

File details

Details for the file py_vallocation-0.4.0.tar.gz.

File metadata

  • Download URL: py_vallocation-0.4.0.tar.gz
  • Upload date:
  • Size: 124.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for py_vallocation-0.4.0.tar.gz
Algorithm Hash digest
SHA256 94e76ef9e1a0104092bd38c633e2f3647455f83db30a8204cece4a511352e4f2
MD5 cff8feeec803e72e2ab32cb06cef88e6
BLAKE2b-256 7fa62ac5ab13d6a4f99dbcd7710fe3ad964d2dc4ab4fb7b0f96b2b2ea044a956

See more details on using hashes here.

File details

Details for the file py_vallocation-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: py_vallocation-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 124.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for py_vallocation-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49854578c8febb95749caf9bccf57158b9ca7dbf6a5bb6a0349114221c31d0d3
MD5 655c473e9143939c132893a902556c3b
BLAKE2b-256 d1caad5ce285cf97b18e3269e1b57223198cdf65fa3e72d4060a84d99f90e6e9

See more details on using hashes here.

Supported by

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