{"id":21779,"library":"pyportfolioopt","title":"PyPortfolioOpt","description":"PyPortfolioOpt is a financial portfolio optimization library for Python, providing methods for mean-variance optimization, Black-Litterman allocation, and risk parity. Version 1.6.0 supports CVXPY-based solvers and offers both classical and objective-based optimization approaches. Release cadence is irregular, with contributions from the community.","status":"active","version":"1.6.0","language":"python","source_language":"en","source_url":"https://github.com/pyportfolio/pyportfolioopt","tags":["finance","portfolio-optimization","mean-variance","risk-management"],"install":[{"cmd":"pip install pyportfolioopt","lang":"bash","label":"Default install"}],"dependencies":[{"reason":"Convex optimization solver, required for optimization","package":"cvxpy","optional":false},{"reason":"Numerical computations","package":"numpy","optional":false},{"reason":"Data handling and returns calculation","package":"pandas","optional":false},{"reason":"Scientific computing, used for some optimizers","package":"scipy","optional":true}],"imports":[{"note":"Actual package name is pypfopt, not pyportfolioopt","wrong":"from pyportfolioopt import EfficientFrontier","symbol":"EfficientFrontier","correct":"from pypfopt import EfficientFrontier"},{"note":"risk_models is a submodule of pypfopt","wrong":"from pyportfolioopt import risk_models","symbol":"risk_models","correct":"from pypfopt import risk_models"},{"note":"expected_returns is a submodule of pypfopt","wrong":"from pyportfolioopt import expected_returns","symbol":"expected_returns","correct":"from pypfopt import expected_returns"}],"quickstart":{"code":"import pandas as pd\nfrom pypfopt import EfficientFrontier, risk_models, expected_returns\n\n# Sample data: prices of 3 assets\ndata = pd.DataFrame({\n    'AAPL': [1.0, 0.9, 0.8, 0.85],\n    'GOOG': [1.0, 1.1, 1.0, 1.05],\n    'MSFT': [1.0, 0.95, 1.1, 1.0]\n})\n\n# Calculate expected returns and covariance matrix\nmu = expected_returns.mean_historical_return(data)\nSigma = risk_models.sample_cov(data)\n\n# Optimize for maximum Sharpe ratio\nef = EfficientFrontier(mu, Sigma)\nweights = ef.max_sharpe()\n\nprint(ef.clean_weights())","lang":"python","description":"Basic mean-variance optimization using historical returns and sample covariance."},"warnings":[{"fix":"Use 'from pypfopt import ...' instead of 'from pyportfolioopt import ...'","message":"Import package as pypfopt, not pyportfolioopt. The pip install name and the import name differ.","severity":"gotcha","affected_versions":"all"},{"fix":"Install CVXPY with pip install cvxpy or use the bundled solver (may not work for large problems).","message":"CVXPY may require a separate solver installation (e.g., 'pip install cvxpy') on some systems, especially for advanced solvers like ECOS or SCS.","severity":"gotcha","affected_versions":"all"},{"fix":"Use methods like `ef.efficient_return(target_return)` directly, not via a constructor parameter.","message":"In version 1.5+, the EfficientFrontier object no longer accepts a target return as a parameter to some methods; use objective functions instead.","severity":"breaking","affected_versions":">=1.5.0"},{"fix":"Use matplotlib directly to plot efficient frontier or weights.","message":"The `pypfopt.plotting` module is deprecated as of version 1.6.0 and will be removed in a future release.","severity":"deprecated","affected_versions":">=1.6.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Relax bounds, check for duplicate assets, or use a non-singular risk model like Ledoit-Wolf shrinkage.","cause":"The optimization constraints (e.g., weight bounds, target return) are too tight, or the covariance matrix is singular.","error":"pypfopt.exceptions.OptimizationError: No feasible solution found"},{"fix":"Change import to 'from pypfopt import EfficientFrontier'","cause":"Installed pyportfolioopt via pip but tried to import from 'pyportfolioopt' instead of 'pypfopt'.","error":"ImportError: cannot import name 'EfficientFrontier' from 'pypfopt'"},{"fix":"Install an alternative solver like SCS (`pip install scs`) or CVXOPT (`pip install cvxopt`) and specify `solver='SCS'` or `solver='CVXOPT'`.","cause":"The default solver ECOS may not be installed or fails on some problems (e.g., integer constraints).","error":"cvxpy.error.SolverError: Solver 'ECOS' failed. Try another solver."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}