pyportfolioopt
Financial portfolio optimization in python
What it is and what it does
PyPortfolioOpt is a Python library for computing mathematically optimal portfolio allocations given historical asset prices or expected returns and risk estimates. It implements classical portfolio optimization methods rooted in Markowitz's efficient frontier theory, allowing investors to find allocations that maximize risk-adjusted returns such as Sharpe ratio or minimize portfolio volatility for a target return. The library depends on cvxpy for convex optimization, numpy and scipy for numerical computation, pandas for data handling, and scikit-learn and scikit-base for its extensible architecture.
The package is designed for both casual investors prototyping allocation strategies and professionals building quantitative trading systems. It handles the full workflow from computing expected returns and covariance matrices from historical data, through optimization with optional constraints, to converting continuous weights into discrete share counts for actual trading. The library is inspired by scikit-learn's design philosophy, emphasizing ease of use alongside extensibility for custom objective functions and risk models.
Use it for:
- Compute optimal long-only portfolio weights that maximize Sharpe ratio given historical stock price data
- Convert theoretical portfolio weights into discrete share counts and dollar allocations for a fixed investment amount
- Implement Black-Litterman allocation to incorporate subjective market views alongside historical return estimates
- Optimize asset allocation subject to custom constraints such as sector limits or position size bounds
- Backtest multi-asset strategies by computing efficient frontiers and comparing risk-return tradeoffs
- Prototype quantitative trading strategies that combine multiple alpha sources into a single risk-efficient portfolio
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
PyPortfolioOpt implements portfolio optimization methods including mean-variance optimization, Black-Litterman allocation, and Hierarchical Risk Parity to compute optimal asset allocations based on expected returns and risk models.
Yes. PyPortfolioOpt is actively maintained, has no known vulnerabilities, installs with low friction, and is licensed permissively under MIT. It provides a well-documented, scikit-learn-inspired interface to classical portfolio optimization methods suitable for both prototyping and production use. Install it if you need to compute optimal asset allocations from expected returns and risk estimates.
Install
pyportfolioopt on PyPI
pip
pip install pyportfoliooptuv
uv add pyportfoliooptpoetry
poetry add pyportfoliooptInstalling pyportfolioopt
Before you install
Low install friction with a pure-Python wheel distribution. Active maintenance with a recent release in February 2026 and continuous commits; the repository has 5964 stars and is not archived.
License in practice
MIT License permits free use, modification, and distribution with minimal restrictions, making it suitable for both commercial and open-source projects.
Quickstart
pip install pyportfolioopt
import pandas as pd
from pyportfolioopt import EfficientFrontier
from pyportfolioopt import risk_models, expected_returns
df = pd.read_csv("stock_prices.csv", parse_dates=True, index_col="date")
mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df)
ef = EfficientFrontier(mu, S)
raw_weights = ef.max_sharpe()
print(ef.portfolio_performance(verbose=True))
Verify before relying
- Whether the package requires specific versions of cvxpy, numpy, pandas, scikit-learn, or scipy beyond what pip resolves automatically
- Performance characteristics when optimizing portfolios with hundreds or thousands of assets
- Whether all optimization methods (mean-variance, Black-Litterman, Hierarchical Risk Parity) are equally mature or if some are experimental
Package facts
| License | MIT License Copyright (c) 2018 Robert Andrew Martin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),… (full text in the JSON record) (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 6 — cvxpy, numpy, pandas, scikit-base, scikit-learn, scipy |
| Maintenance | actively maintained — 169 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 195,241/month — #9,819 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pyportfolioopt-1.6.0-py3-none-any.whl
Keywords: finance, portfolio, optimization, quant, investing
Tags
More Financial packages
statsmodels provides statistical models,…
permissive · top 1,000 on PyPI
yfinanceFetches financial and market data from Yahoo…
permissive · top 1,000 on PyPI
bokehBokeh is an interactive visualization library…
permissive · top 5,000 on PyPI
python-stdnumParses, validates, and reformats standard…
copyleft · top 5,000 on PyPI
panelPanel is a Python framework for building…
permissive · top 5,000 on PyPI
databentoOfficial Python client for accessing live and…
permissive · top 5,000 on PyPI
riskfolio-libRiskfolio-Lib builds optimized investment…
permissive · top 15,000 on PyPI
skfolioskfolio is a Python library for portfolio…
permissive · top 15,000 on PyPI
quantstatsQuantStats computes portfolio performance…
permissive · top 15,000 on PyPI
numpy-financialProvides elementary financial functions (IRR,…
permissive · top 5,000 on PyPI
py-vollibCalculates option prices, implied volatility,…
permissive · top 15,000 on PyPI
py-lets-be-rationalComputes implied volatility from option prices…
permissive · top 15,000 on PyPI
vollibvollib calculates option prices, implied…
permissive · top 15,000 on PyPI
empyrical-reloadedComputes financial return and risk…
permissive · top 15,000 on PyPI
backtestingBacktesting.py lets you define and test trading…
agpl · top 15,000 on PyPI
pyomoPyomo is a Python framework for formulating and…
permissive · top 5,000 on PyPI