skillfed

pyportfolioopt

Financial portfolio optimization in python

pyportfolioopt v1.6.0 195.2K downloads/30d#9,819 on PyPI5,964
Permissive 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) Active released

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 pyportfolioopt

uv

uv add pyportfolioopt

poetry

poetry add pyportfolioopt

Installing 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

Development Status :: 4 - BetaEnvironment :: ConsoleIntended Audience :: Financial and Insurance IndustryIntended Audience :: Science/ResearchLicense :: OSI Approved :: MIT LicenseNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: Office/Business :: FinancialTopic :: Office/Business :: Financial :: Investment

Tags

portfolio optimizationmean-variance optimizationefficient frontierasset allocationBlack-LittermanSharpe ratio optimizationrisk-adjusted returnsportfolio weights
portfolio-optimizationquantitative-financeconvex-optimization

More Financial packages