pyportfoliopt


Namepyportfoliopt JSON
Version 1.5.6 PyPI version JSON
download
home_page
SummaryFinancial portfolio optimization in python
upload_time2023-04-17 17:00:54
maintainer
docs_urlNone
authorRobert Andrew Martin
requires_python>=3.5
licenseMIT
keywords portfolio finance optimization quant trading investing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
**PyPortfolioOpt is looking for maintainers! Please reach out to the email address at the bottom of the readme if you're interested**

PyPortfolioOpt is a library that implements portfolio optimization methods, including
classical mean-variance optimization techniques and Black-Litterman allocation, as well as more
recent developments in the field like shrinkage and Hierarchical Risk Parity.

It is **extensive** yet easily **extensible**, and can be useful for either a casual investors, or a professional looking for an easy prototyping tool. Whether you are a fundamentals-oriented investor who has identified a
handful of undervalued picks, or an algorithmic trader who has a basket of
strategies, PyPortfolioOpt can help you combine your alpha sources
in a risk-efficient way.

**PyPortfolioOpt has been [published](https://joss.theoj.org/papers/10.21105/joss.03066) in the Journal of Open Source Software 🎉**

Head over to the **[documentation on ReadTheDocs](https://pyportfolioopt.readthedocs.io/en/latest/)** to get an in-depth look at the project, or check out the [cookbook](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/cookbook) to see some examples showing the full process from downloading data to building a portfolio.





## Table of contents

-   [Table of contents](#table-of-contents)
-   [Getting started](#getting-started)
    -   [For development](#for-development)
-   [A quick example](#a-quick-example)
-   [An overview of classical portfolio optimization methods](#an-overview-of-classical-portfolio-optimization-methods)
-   [Features](#features)
    -   [Expected returns](#expected-returns)
    -   [Risk models (covariance)](#risk-models-covariance)
    -   [Objective functions](#objective-functions)
    -   [Adding constraints or different objectives](#adding-constraints-or-different-objectives)
    -   [Black-Litterman allocation](#black-litterman-allocation)
    -   [Other optimizers](#other-optimizers)
-   [Advantages over existing implementations](#advantages-over-existing-implementations)
-   [Project principles and design decisions](#project-principles-and-design-decisions)
-   [Testing](#testing)
-   [Citing PyPortfolioOpt](#citing-pyportfolioopt)
-   [Contributing](#contributing)
-   [Getting in touch](#getting-in-touch)

## Getting started

If you would like to play with PyPortfolioOpt interactively in your browser, you may launch Binder [here](https://mybinder.org/v2/gh/robertmartin8/pyportfolioopt/master). It takes a
while to set up, but it lets you try out the cookbook recipes without having to deal with all of the requirements.

_Note: macOS users will need to install [Command Line Tools](https://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/)._

_Note: if you are on windows, you first need to installl C++. ([download](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16), [install instructions](https://drive.google.com/file/d/0B4GsMXCRaSSIOWpYQkstajlYZ0tPVkNQSElmTWh1dXFaYkJr/view))_

This project is available on PyPI, meaning that you can just:

```bash
pip install PyPortfolioOpt
```

(you may need to follow separate installation instructions for [cvxopt](https://cvxopt.org/install/index.html#) and [cvxpy](https://www.cvxpy.org/install/))).

However, it is best practice to use a dependency manager within a virtual environment.
My current recommendation is to get yourself set up with [poetry](https://github.com/sdispater/poetry) then just run

```bash
poetry add PyPortfolioOpt
```

Otherwise, clone/download the project and in the project directory run:

```bash
python setup.py install
```

PyPortfolioOpt supports Docker. Build your first container with `docker build -f docker/Dockerfile . -t pypfopt`. You can use the image to run tests or even launch a Jupyter server.

```bash
# iPython interpreter:
docker run -it pypfopt poetry run ipython

# Jupyter notebook server:
docker run -it -p 8888:8888 pypfopt poetry run jupyter notebook --allow-root --no-browser --ip 0.0.0.0
# click on http://127.0.0.1:8888/?token=xxx

# Pytest
docker run -t pypfopt poetry run pytest

# Bash
docker run -it pypfopt bash
```

For more information, please read [this guide](https://docker-curriculum.com/#introduction).

### For development

If you would like to make major changes to integrate this with your proprietary system, it probably makes sense to clone this repository and to just use the source code.

```bash
git clone https://github.com/robertmartin8/PyPortfolioOpt
```

Alternatively, you could try:

```bash
pip install -e git+https://github.com/robertmartin8/PyPortfolioOpt.git
```

## A quick example

Here is an example on real life stock data, demonstrating how easy it is to find the long-only portfolio that maximises the Sharpe ratio (a measure of risk-adjusted returns).

```python
import pandas as pd
from pypfopt import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns

# Read in price data
df = pd.read_csv("tests/resources/stock_prices.csv", parse_dates=True, index_col="date")

# Calculate expected returns and sample covariance
mu = expected_returns.mean_historical_return(df)
S = risk_models.sample_cov(df)

# Optimize for maximal Sharpe ratio
ef = EfficientFrontier(mu, S)
raw_weights = ef.max_sharpe()
cleaned_weights = ef.clean_weights()
ef.save_weights_to_file("weights.csv")  # saves to file
print(cleaned_weights)
ef.portfolio_performance(verbose=True)
```

This outputs the following weights:

```txt
{'GOOG': 0.03835,
 'AAPL': 0.0689,
 'FB': 0.20603,
 'BABA': 0.07315,
 'AMZN': 0.04033,
 'GE': 0.0,
 'AMD': 0.0,
 'WMT': 0.0,
 'BAC': 0.0,
 'GM': 0.0,
 'T': 0.0,
 'UAA': 0.0,
 'SHLD': 0.0,
 'XOM': 0.0,
 'RRC': 0.0,
 'BBY': 0.01324,
 'MA': 0.35349,
 'PFE': 0.1957,
 'JPM': 0.0,
 'SBUX': 0.01082}

Expected annual return: 30.5%
Annual volatility: 22.2%
Sharpe Ratio: 1.28
```

This is interesting but not useful in itself. However, PyPortfolioOpt provides a method which allows you to convert the above continuous weights to an actual allocation that you could buy. Just enter the most recent prices, and the desired portfolio size ($10,000 in this example):

```python
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices


latest_prices = get_latest_prices(df)

da = DiscreteAllocation(weights, latest_prices, total_portfolio_value=10000)
allocation, leftover = da.greedy_portfolio()
print("Discrete allocation:", allocation)
print("Funds remaining: ${:.2f}".format(leftover))
```

```txt
12 out of 20 tickers were removed
Discrete allocation: {'GOOG': 1, 'AAPL': 4, 'FB': 12, 'BABA': 4, 'BBY': 2,
                      'MA': 20, 'PFE': 54, 'SBUX': 1}
Funds remaining: $11.89
```

_Disclaimer: nothing about this project constitues investment advice, and the author bears no responsibiltiy for your subsequent investment decisions. Please refer to the [license](https://github.com/robertmartin8/PyPortfolioOpt/blob/master/LICENSE.txt) for more information._

## An overview of classical portfolio optimization methods

Harry Markowitz's 1952 paper is the undeniable classic, which turned portfolio optimization from an art into a science. The key insight is that by combining assets with different expected returns and volatilities, one can decide on a mathematically optimal allocation which minimises the risk for a target return – the set of all such optimal portfolios is referred to as the **efficient frontier**.





Although much development has been made in the subject, more than half a century later, Markowitz's core ideas are still fundamentally important and see daily use in many portfolio management firms.
The main drawback of mean-variance optimization is that the theoretical treatment requires knowledge of the expected returns and the future risk-characteristics (covariance) of the assets. Obviously, if we knew the expected returns of a stock life would be much easier, but the whole game is that stock returns are notoriously hard to forecast. As a substitute, we can derive estimates of the expected return and covariance based on historical data – though we do lose the theoretical guarantees provided by Markowitz, the closer our estimates are to the real values, the better our portfolio will be.

Thus this project provides four major sets of functionality (though of course they are intimately related)

-   Estimates of expected returns
-   Estimates of risk (i.e covariance of asset returns)
-   Objective functions to be optimized
-   Optimizers.

A key design goal of PyPortfolioOpt is **modularity** – the user should be able to swap in their
components while still making use of the framework that PyPortfolioOpt provides.

## Features

In this section, we detail some of PyPortfolioOpt's available functionality. More examples are offered in the Jupyter notebooks [here](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/cookbook). Another good resource is the [tests](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/tests).

A far more comprehensive version of this can be found on [ReadTheDocs](https://pyportfolioopt.readthedocs.io/en/latest/), as well as possible extensions for more advanced users.

### Expected returns

-   Mean historical returns:
    -   the simplest and most common approach, which states that the expected return of each asset is equal to the mean of its historical returns.
    -   easily interpretable and very intuitive
-   Exponentially weighted mean historical returns:
    -   similar to mean historical returns, except it gives exponentially more weight to recent prices
    -   it is likely the case that an asset's most recent returns hold more weight than returns from 10 years ago when it comes to estimating future returns.
-   Capital Asset Pricing Model (CAPM):
    -   a simple model to predict returns based on the beta to the market
    -   this is used all over finance!

### Risk models (covariance)

The covariance matrix encodes not just the volatility of an asset, but also how it correlated to other assets. This is important because in order to reap the benefits of diversification (and thus increase return per unit risk), the assets in the portfolio should be as uncorrelated as possible.

-   Sample covariance matrix:
    -   an unbiased estimate of the covariance matrix
    -   relatively easy to compute
    -   the de facto standard for many years
    -   however, it has a high estimation error, which is particularly dangerous in mean-variance optimization because the optimizer is likely to give excess weight to these erroneous estimates.
-   Semicovariance: a measure of risk that focuses on downside variation.
-   Exponential covariance: an improvement over sample covariance that gives more weight to recent data
-   Covariance shrinkage: techniques that involve combining the sample covariance matrix with a structured estimator, to reduce the effect of erroneous weights. PyPortfolioOpt provides wrappers around the efficient vectorised implementations provided by `sklearn.covariance`.
    -   manual shrinkage
    -   Ledoit Wolf shrinkage, which chooses an optimal shrinkage parameter. We offer three shrinkage targets: `constant_variance`, `single_factor`, and `constant_correlation`.
    -   Oracle Approximating Shrinkage
-   Minimum Covariance Determinant:
    -   a robust estimate of the covariance
    -   implemented in `sklearn.covariance`


    


(This plot was generated using `plotting.plot_covariance`)

### Objective functions

-   Maximum Sharpe ratio: this results in a _tangency portfolio_ because on a graph of returns vs risk, this portfolio corresponds to the tangent of the efficient frontier that has a y-intercept equal to the risk-free rate. This is the default option because it finds the optimal return per unit risk.
-   Minimum volatility. This may be useful if you're trying to get an idea of how low the volatility _could_ be, but in practice it makes a lot more sense to me to use the portfolio that maximises the Sharpe ratio.
-   Efficient return, a.k.a. the Markowitz portfolio, which minimises risk for a given target return – this was the main focus of Markowitz 1952
-   Efficient risk: the Sharpe-maximising portfolio for a given target risk.
-   Maximum quadratic utility. You can provide your own risk-aversion level and compute the appropriate portfolio.

### Adding constraints or different objectives

-   Long/short: by default all of the mean-variance optimization methods in PyPortfolioOpt are long-only, but they can be initialised to allow for short positions by changing the weight bounds:

```python
ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
```

-   Market neutrality: for the `efficient_risk` and `efficient_return` methods, PyPortfolioOpt provides an option to form a market-neutral portfolio (i.e weights sum to zero). This is not possible for the max Sharpe portfolio and the min volatility portfolio because in those cases because they are not invariant with respect to leverage. Market neutrality requires negative weights:

```python
ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
ef.efficient_return(target_return=0.2, market_neutral=True)
```

-   Minimum/maximum position size: it may be the case that you want no security to form more than 10% of your portfolio. This is easy to encode:

```python
ef = EfficientFrontier(mu, S, weight_bounds=(0, 0.1))
```

One issue with mean-variance optimization is that it leads to many zero-weights. While these are
"optimal" in-sample, there is a large body of research showing that this characteristic leads
mean-variance portfolios to underperform out-of-sample. To that end, I have introduced an
objective function that can reduce the number of negligible weights for any of the objective functions. Essentially, it adds a penalty (parameterised by `gamma`) on small weights, with a term that looks just like L2 regularisation in machine learning. It may be necessary to try several `gamma` values to achieve the desired number of non-negligible weights. For the test portfolio of 20 securities, `gamma ~ 1` is sufficient

```python
ef = EfficientFrontier(mu, S)
ef.add_objective(objective_functions.L2_reg, gamma=1)
ef.max_sharpe()
```

### Black-Litterman allocation

As of v0.5.0, we now support Black-Litterman asset allocation, which allows you to combine
a prior estimate of returns (e.g the market-implied returns) with your own views to form a
posterior estimate. This results in much better estimates of expected returns than just using
the mean historical return. Check out the [docs](https://pyportfolioopt.readthedocs.io/en/latest/BlackLitterman.html) for a discussion of the theory, as well as advice
on formatting inputs.

```python
S = risk_models.sample_cov(df)
viewdict = {"AAPL": 0.20, "BBY": -0.30, "BAC": 0, "SBUX": -0.2, "T": 0.131321}
bl = BlackLittermanModel(S, pi="equal", absolute_views=viewdict, omega="default")
rets = bl.bl_returns()

ef = EfficientFrontier(rets, S)
ef.max_sharpe()
```

### Other optimizers

The features above mostly pertain to solving mean-variance optimization problems via quadratic programming (though this is taken care of by `cvxpy`). However, we offer different optimizers as well:

-   Mean-semivariance optimization
-   Mean-CVaR optimization
-   Hierarchical Risk Parity, using clustering algorithms to choose uncorrelated assets
-   Markowitz's critical line algorithm (CLA)

Please refer to the [documentation](https://pyportfolioopt.readthedocs.io/en/latest/OtherOptimizers.html) for more.

## Advantages over existing implementations

-   Includes both classical methods (Markowitz 1952 and Black-Litterman), suggested best practices
    (e.g covariance shrinkage), along with many recent developments and novel
    features, like L2 regularisation, shrunk covariance, hierarchical risk parity.
-   Native support for pandas dataframes: easily input your daily prices data.
-   Extensive practical tests, which use real-life data.
-   Easy to combine with your proprietary strategies and models.
-   Robust to missing data, and price-series of different lengths (e.g FB data
    only goes back to 2012 whereas AAPL data goes back to 1980).

## Project principles and design decisions

-   It should be easy to swap out individual components of the optimization process
    with the user's proprietary improvements.
-   Usability is everything: it is better to be self-explanatory than consistent.
-   There is no point in portfolio optimization unless it can be practically
    applied to real asset prices.
-   Everything that has been implemented should be tested.
-   Inline documentation is good: dedicated (separate) documentation is better.
    The two are not mutually exclusive.
-   Formatting should never get in the way of coding: because of this,
    I have deferred **all** formatting decisions to [Black](https://github.com/ambv/black).

## Testing

Tests are written in pytest (much more intuitive than `unittest` and the variants in my opinion), and I have tried to ensure close to 100% coverage. Run the tests by navigating to the package directory and simply running `pytest` on the command line.

PyPortfolioOpt provides a test dataset of daily returns for 20 tickers:

```python
['GOOG', 'AAPL', 'FB', 'BABA', 'AMZN', 'GE', 'AMD', 'WMT', 'BAC', 'GM',
'T', 'UAA', 'SHLD', 'XOM', 'RRC', 'BBY', 'MA', 'PFE', 'JPM', 'SBUX']
```

These tickers have been informally selected to meet several criteria:

-   reasonably liquid
-   different performances and volatilities
-   different amounts of data to test robustness

Currently, the tests have not explored all of the edge cases and combinations
of objective functions and parameters. However, each method and parameter has
been tested to work as intended.

## Citing PyPortfolioOpt

If you use PyPortfolioOpt for published work, please cite the [JOSS paper](https://joss.theoj.org/papers/10.21105/joss.03066).

Citation string:

```text
Martin, R. A., (2021). PyPortfolioOpt: portfolio optimization in Python. Journal of Open Source Software, 6(61), 3066, https://doi.org/10.21105/joss.03066
```

BibTex::

```bibtex
@article{Martin2021,
  doi = {10.21105/joss.03066},
  url = {https://doi.org/10.21105/joss.03066},
  year = {2021},
  publisher = {The Open Journal},
  volume = {6},
  number = {61},
  pages = {3066},
  author = {Robert Andrew Martin},
  title = {PyPortfolioOpt: portfolio optimization in Python},
  journal = {Journal of Open Source Software}
}
```

## Contributing

Contributions are _most welcome_. Have a look at the [Contribution Guide](https://github.com/robertmartin8/PyPortfolioOpt/blob/master/CONTRIBUTING.md) for more.

I'd like to thank all of the people who have contributed to PyPortfolioOpt since its release in 2018.
Special shout-outs to:

-   Philipp Schiele
-   Carl Peasnell
-   Felipe Schneider
-   Dingyuan Wang
-   Pat Newell
-   Aditya Bhutra
-   Thomas Schmelzer
-   Rich Caputo
-   Nicolas Knudde

## Getting in touch

If you are having a problem with PyPortfolioOpt, please raise a GitHub issue. For anything else, you can reach me at:





            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pyportfoliopt",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "",
    "keywords": "portfolio finance optimization quant trading investing",
    "author": "Robert Andrew Martin",
    "author_email": "martin.robertandrew@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e6/9f/00e2c0e6013038bfa635ab0ea2269fcc2303b9d5d24fe68583af8c52ccba/pyportfoliopt-1.5.6.tar.gz",
    "platform": null,
    "description": "\r\n**PyPortfolioOpt is looking for maintainers! Please reach out to the email address at the bottom of the readme if you're interested**\r\n\r\nPyPortfolioOpt is a library that implements portfolio optimization methods, including\r\nclassical mean-variance optimization techniques and Black-Litterman allocation, as well as more\r\nrecent developments in the field like shrinkage and Hierarchical Risk Parity.\r\n\r\nIt is **extensive** yet easily **extensible**, and can be useful for either a casual investors, or a professional looking for an easy prototyping tool. Whether you are a fundamentals-oriented investor who has identified a\r\nhandful of undervalued picks, or an algorithmic trader who has a basket of\r\nstrategies, PyPortfolioOpt can help you combine your alpha sources\r\nin a risk-efficient way.\r\n\r\n**PyPortfolioOpt has been [published](https://joss.theoj.org/papers/10.21105/joss.03066) in the Journal of Open Source Software \u00f0\u0178\u017d\u2030**\r\n\r\nHead over to the **[documentation on ReadTheDocs](https://pyportfolioopt.readthedocs.io/en/latest/)** to get an in-depth look at the project, or check out the [cookbook](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/cookbook) to see some examples showing the full process from downloading data to building a portfolio.\r\n\r\n\r\n\r\n\r\n\r\n## Table of contents\r\n\r\n-   [Table of contents](#table-of-contents)\r\n-   [Getting started](#getting-started)\r\n    -   [For development](#for-development)\r\n-   [A quick example](#a-quick-example)\r\n-   [An overview of classical portfolio optimization methods](#an-overview-of-classical-portfolio-optimization-methods)\r\n-   [Features](#features)\r\n    -   [Expected returns](#expected-returns)\r\n    -   [Risk models (covariance)](#risk-models-covariance)\r\n    -   [Objective functions](#objective-functions)\r\n    -   [Adding constraints or different objectives](#adding-constraints-or-different-objectives)\r\n    -   [Black-Litterman allocation](#black-litterman-allocation)\r\n    -   [Other optimizers](#other-optimizers)\r\n-   [Advantages over existing implementations](#advantages-over-existing-implementations)\r\n-   [Project principles and design decisions](#project-principles-and-design-decisions)\r\n-   [Testing](#testing)\r\n-   [Citing PyPortfolioOpt](#citing-pyportfolioopt)\r\n-   [Contributing](#contributing)\r\n-   [Getting in touch](#getting-in-touch)\r\n\r\n## Getting started\r\n\r\nIf you would like to play with PyPortfolioOpt interactively in your browser, you may launch Binder [here](https://mybinder.org/v2/gh/robertmartin8/pyportfolioopt/master). It takes a\r\nwhile to set up, but it lets you try out the cookbook recipes without having to deal with all of the requirements.\r\n\r\n_Note: macOS users will need to install [Command Line Tools](https://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/)._\r\n\r\n_Note: if you are on windows, you first need to installl C++. ([download](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16), [install instructions](https://drive.google.com/file/d/0B4GsMXCRaSSIOWpYQkstajlYZ0tPVkNQSElmTWh1dXFaYkJr/view))_\r\n\r\nThis project is available on PyPI, meaning that you can just:\r\n\r\n```bash\r\npip install PyPortfolioOpt\r\n```\r\n\r\n(you may need to follow separate installation instructions for [cvxopt](https://cvxopt.org/install/index.html#) and [cvxpy](https://www.cvxpy.org/install/))).\r\n\r\nHowever, it is best practice to use a dependency manager within a virtual environment.\r\nMy current recommendation is to get yourself set up with [poetry](https://github.com/sdispater/poetry) then just run\r\n\r\n```bash\r\npoetry add PyPortfolioOpt\r\n```\r\n\r\nOtherwise, clone/download the project and in the project directory run:\r\n\r\n```bash\r\npython setup.py install\r\n```\r\n\r\nPyPortfolioOpt supports Docker. Build your first container with `docker build -f docker/Dockerfile . -t pypfopt`. You can use the image to run tests or even launch a Jupyter server.\r\n\r\n```bash\r\n# iPython interpreter:\r\ndocker run -it pypfopt poetry run ipython\r\n\r\n# Jupyter notebook server:\r\ndocker run -it -p 8888:8888 pypfopt poetry run jupyter notebook --allow-root --no-browser --ip 0.0.0.0\r\n# click on http://127.0.0.1:8888/?token=xxx\r\n\r\n# Pytest\r\ndocker run -t pypfopt poetry run pytest\r\n\r\n# Bash\r\ndocker run -it pypfopt bash\r\n```\r\n\r\nFor more information, please read [this guide](https://docker-curriculum.com/#introduction).\r\n\r\n### For development\r\n\r\nIf you would like to make major changes to integrate this with your proprietary system, it probably makes sense to clone this repository and to just use the source code.\r\n\r\n```bash\r\ngit clone https://github.com/robertmartin8/PyPortfolioOpt\r\n```\r\n\r\nAlternatively, you could try:\r\n\r\n```bash\r\npip install -e git+https://github.com/robertmartin8/PyPortfolioOpt.git\r\n```\r\n\r\n## A quick example\r\n\r\nHere is an example on real life stock data, demonstrating how easy it is to find the long-only portfolio that maximises the Sharpe ratio (a measure of risk-adjusted returns).\r\n\r\n```python\r\nimport pandas as pd\r\nfrom pypfopt import EfficientFrontier\r\nfrom pypfopt import risk_models\r\nfrom pypfopt import expected_returns\r\n\r\n# Read in price data\r\ndf = pd.read_csv(\"tests/resources/stock_prices.csv\", parse_dates=True, index_col=\"date\")\r\n\r\n# Calculate expected returns and sample covariance\r\nmu = expected_returns.mean_historical_return(df)\r\nS = risk_models.sample_cov(df)\r\n\r\n# Optimize for maximal Sharpe ratio\r\nef = EfficientFrontier(mu, S)\r\nraw_weights = ef.max_sharpe()\r\ncleaned_weights = ef.clean_weights()\r\nef.save_weights_to_file(\"weights.csv\")  # saves to file\r\nprint(cleaned_weights)\r\nef.portfolio_performance(verbose=True)\r\n```\r\n\r\nThis outputs the following weights:\r\n\r\n```txt\r\n{'GOOG': 0.03835,\r\n 'AAPL': 0.0689,\r\n 'FB': 0.20603,\r\n 'BABA': 0.07315,\r\n 'AMZN': 0.04033,\r\n 'GE': 0.0,\r\n 'AMD': 0.0,\r\n 'WMT': 0.0,\r\n 'BAC': 0.0,\r\n 'GM': 0.0,\r\n 'T': 0.0,\r\n 'UAA': 0.0,\r\n 'SHLD': 0.0,\r\n 'XOM': 0.0,\r\n 'RRC': 0.0,\r\n 'BBY': 0.01324,\r\n 'MA': 0.35349,\r\n 'PFE': 0.1957,\r\n 'JPM': 0.0,\r\n 'SBUX': 0.01082}\r\n\r\nExpected annual return: 30.5%\r\nAnnual volatility: 22.2%\r\nSharpe Ratio: 1.28\r\n```\r\n\r\nThis is interesting but not useful in itself. However, PyPortfolioOpt provides a method which allows you to convert the above continuous weights to an actual allocation that you could buy. Just enter the most recent prices, and the desired portfolio size ($10,000 in this example):\r\n\r\n```python\r\nfrom pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices\r\n\r\n\r\nlatest_prices = get_latest_prices(df)\r\n\r\nda = DiscreteAllocation(weights, latest_prices, total_portfolio_value=10000)\r\nallocation, leftover = da.greedy_portfolio()\r\nprint(\"Discrete allocation:\", allocation)\r\nprint(\"Funds remaining: ${:.2f}\".format(leftover))\r\n```\r\n\r\n```txt\r\n12 out of 20 tickers were removed\r\nDiscrete allocation: {'GOOG': 1, 'AAPL': 4, 'FB': 12, 'BABA': 4, 'BBY': 2,\r\n                      'MA': 20, 'PFE': 54, 'SBUX': 1}\r\nFunds remaining: $11.89\r\n```\r\n\r\n_Disclaimer: nothing about this project constitues investment advice, and the author bears no responsibiltiy for your subsequent investment decisions. Please refer to the [license](https://github.com/robertmartin8/PyPortfolioOpt/blob/master/LICENSE.txt) for more information._\r\n\r\n## An overview of classical portfolio optimization methods\r\n\r\nHarry Markowitz's 1952 paper is the undeniable classic, which turned portfolio optimization from an art into a science. The key insight is that by combining assets with different expected returns and volatilities, one can decide on a mathematically optimal allocation which minimises the risk for a target return \u00e2\u20ac\u201c the set of all such optimal portfolios is referred to as the **efficient frontier**.\r\n\r\n\r\n\r\n\r\n\r\nAlthough much development has been made in the subject, more than half a century later, Markowitz's core ideas are still fundamentally important and see daily use in many portfolio management firms.\r\nThe main drawback of mean-variance optimization is that the theoretical treatment requires knowledge of the expected returns and the future risk-characteristics (covariance) of the assets. Obviously, if we knew the expected returns of a stock life would be much easier, but the whole game is that stock returns are notoriously hard to forecast. As a substitute, we can derive estimates of the expected return and covariance based on historical data \u00e2\u20ac\u201c though we do lose the theoretical guarantees provided by Markowitz, the closer our estimates are to the real values, the better our portfolio will be.\r\n\r\nThus this project provides four major sets of functionality (though of course they are intimately related)\r\n\r\n-   Estimates of expected returns\r\n-   Estimates of risk (i.e covariance of asset returns)\r\n-   Objective functions to be optimized\r\n-   Optimizers.\r\n\r\nA key design goal of PyPortfolioOpt is **modularity** \u00e2\u20ac\u201c the user should be able to swap in their\r\ncomponents while still making use of the framework that PyPortfolioOpt provides.\r\n\r\n## Features\r\n\r\nIn this section, we detail some of PyPortfolioOpt's available functionality. More examples are offered in the Jupyter notebooks [here](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/cookbook). Another good resource is the [tests](https://github.com/robertmartin8/PyPortfolioOpt/tree/master/tests).\r\n\r\nA far more comprehensive version of this can be found on [ReadTheDocs](https://pyportfolioopt.readthedocs.io/en/latest/), as well as possible extensions for more advanced users.\r\n\r\n### Expected returns\r\n\r\n-   Mean historical returns:\r\n    -   the simplest and most common approach, which states that the expected return of each asset is equal to the mean of its historical returns.\r\n    -   easily interpretable and very intuitive\r\n-   Exponentially weighted mean historical returns:\r\n    -   similar to mean historical returns, except it gives exponentially more weight to recent prices\r\n    -   it is likely the case that an asset's most recent returns hold more weight than returns from 10 years ago when it comes to estimating future returns.\r\n-   Capital Asset Pricing Model (CAPM):\r\n    -   a simple model to predict returns based on the beta to the market\r\n    -   this is used all over finance!\r\n\r\n### Risk models (covariance)\r\n\r\nThe covariance matrix encodes not just the volatility of an asset, but also how it correlated to other assets. This is important because in order to reap the benefits of diversification (and thus increase return per unit risk), the assets in the portfolio should be as uncorrelated as possible.\r\n\r\n-   Sample covariance matrix:\r\n    -   an unbiased estimate of the covariance matrix\r\n    -   relatively easy to compute\r\n    -   the de facto standard for many years\r\n    -   however, it has a high estimation error, which is particularly dangerous in mean-variance optimization because the optimizer is likely to give excess weight to these erroneous estimates.\r\n-   Semicovariance: a measure of risk that focuses on downside variation.\r\n-   Exponential covariance: an improvement over sample covariance that gives more weight to recent data\r\n-   Covariance shrinkage: techniques that involve combining the sample covariance matrix with a structured estimator, to reduce the effect of erroneous weights. PyPortfolioOpt provides wrappers around the efficient vectorised implementations provided by `sklearn.covariance`.\r\n    -   manual shrinkage\r\n    -   Ledoit Wolf shrinkage, which chooses an optimal shrinkage parameter. We offer three shrinkage targets: `constant_variance`, `single_factor`, and `constant_correlation`.\r\n    -   Oracle Approximating Shrinkage\r\n-   Minimum Covariance Determinant:\r\n    -   a robust estimate of the covariance\r\n    -   implemented in `sklearn.covariance`\r\n\r\n\r\n    \r\n\r\n\r\n(This plot was generated using `plotting.plot_covariance`)\r\n\r\n### Objective functions\r\n\r\n-   Maximum Sharpe ratio: this results in a _tangency portfolio_ because on a graph of returns vs risk, this portfolio corresponds to the tangent of the efficient frontier that has a y-intercept equal to the risk-free rate. This is the default option because it finds the optimal return per unit risk.\r\n-   Minimum volatility. This may be useful if you're trying to get an idea of how low the volatility _could_ be, but in practice it makes a lot more sense to me to use the portfolio that maximises the Sharpe ratio.\r\n-   Efficient return, a.k.a. the Markowitz portfolio, which minimises risk for a given target return \u00e2\u20ac\u201c this was the main focus of Markowitz 1952\r\n-   Efficient risk: the Sharpe-maximising portfolio for a given target risk.\r\n-   Maximum quadratic utility. You can provide your own risk-aversion level and compute the appropriate portfolio.\r\n\r\n### Adding constraints or different objectives\r\n\r\n-   Long/short: by default all of the mean-variance optimization methods in PyPortfolioOpt are long-only, but they can be initialised to allow for short positions by changing the weight bounds:\r\n\r\n```python\r\nef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))\r\n```\r\n\r\n-   Market neutrality: for the `efficient_risk` and `efficient_return` methods, PyPortfolioOpt provides an option to form a market-neutral portfolio (i.e weights sum to zero). This is not possible for the max Sharpe portfolio and the min volatility portfolio because in those cases because they are not invariant with respect to leverage. Market neutrality requires negative weights:\r\n\r\n```python\r\nef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))\r\nef.efficient_return(target_return=0.2, market_neutral=True)\r\n```\r\n\r\n-   Minimum/maximum position size: it may be the case that you want no security to form more than 10% of your portfolio. This is easy to encode:\r\n\r\n```python\r\nef = EfficientFrontier(mu, S, weight_bounds=(0, 0.1))\r\n```\r\n\r\nOne issue with mean-variance optimization is that it leads to many zero-weights. While these are\r\n\"optimal\" in-sample, there is a large body of research showing that this characteristic leads\r\nmean-variance portfolios to underperform out-of-sample. To that end, I have introduced an\r\nobjective function that can reduce the number of negligible weights for any of the objective functions. Essentially, it adds a penalty (parameterised by `gamma`) on small weights, with a term that looks just like L2 regularisation in machine learning. It may be necessary to try several `gamma` values to achieve the desired number of non-negligible weights. For the test portfolio of 20 securities, `gamma ~ 1` is sufficient\r\n\r\n```python\r\nef = EfficientFrontier(mu, S)\r\nef.add_objective(objective_functions.L2_reg, gamma=1)\r\nef.max_sharpe()\r\n```\r\n\r\n### Black-Litterman allocation\r\n\r\nAs of v0.5.0, we now support Black-Litterman asset allocation, which allows you to combine\r\na prior estimate of returns (e.g the market-implied returns) with your own views to form a\r\nposterior estimate. This results in much better estimates of expected returns than just using\r\nthe mean historical return. Check out the [docs](https://pyportfolioopt.readthedocs.io/en/latest/BlackLitterman.html) for a discussion of the theory, as well as advice\r\non formatting inputs.\r\n\r\n```python\r\nS = risk_models.sample_cov(df)\r\nviewdict = {\"AAPL\": 0.20, \"BBY\": -0.30, \"BAC\": 0, \"SBUX\": -0.2, \"T\": 0.131321}\r\nbl = BlackLittermanModel(S, pi=\"equal\", absolute_views=viewdict, omega=\"default\")\r\nrets = bl.bl_returns()\r\n\r\nef = EfficientFrontier(rets, S)\r\nef.max_sharpe()\r\n```\r\n\r\n### Other optimizers\r\n\r\nThe features above mostly pertain to solving mean-variance optimization problems via quadratic programming (though this is taken care of by `cvxpy`). However, we offer different optimizers as well:\r\n\r\n-   Mean-semivariance optimization\r\n-   Mean-CVaR optimization\r\n-   Hierarchical Risk Parity, using clustering algorithms to choose uncorrelated assets\r\n-   Markowitz's critical line algorithm (CLA)\r\n\r\nPlease refer to the [documentation](https://pyportfolioopt.readthedocs.io/en/latest/OtherOptimizers.html) for more.\r\n\r\n## Advantages over existing implementations\r\n\r\n-   Includes both classical methods (Markowitz 1952 and Black-Litterman), suggested best practices\r\n    (e.g covariance shrinkage), along with many recent developments and novel\r\n    features, like L2 regularisation, shrunk covariance, hierarchical risk parity.\r\n-   Native support for pandas dataframes: easily input your daily prices data.\r\n-   Extensive practical tests, which use real-life data.\r\n-   Easy to combine with your proprietary strategies and models.\r\n-   Robust to missing data, and price-series of different lengths (e.g FB data\r\n    only goes back to 2012 whereas AAPL data goes back to 1980).\r\n\r\n## Project principles and design decisions\r\n\r\n-   It should be easy to swap out individual components of the optimization process\r\n    with the user's proprietary improvements.\r\n-   Usability is everything: it is better to be self-explanatory than consistent.\r\n-   There is no point in portfolio optimization unless it can be practically\r\n    applied to real asset prices.\r\n-   Everything that has been implemented should be tested.\r\n-   Inline documentation is good: dedicated (separate) documentation is better.\r\n    The two are not mutually exclusive.\r\n-   Formatting should never get in the way of coding: because of this,\r\n    I have deferred **all** formatting decisions to [Black](https://github.com/ambv/black).\r\n\r\n## Testing\r\n\r\nTests are written in pytest (much more intuitive than `unittest` and the variants in my opinion), and I have tried to ensure close to 100% coverage. Run the tests by navigating to the package directory and simply running `pytest` on the command line.\r\n\r\nPyPortfolioOpt provides a test dataset of daily returns for 20 tickers:\r\n\r\n```python\r\n['GOOG', 'AAPL', 'FB', 'BABA', 'AMZN', 'GE', 'AMD', 'WMT', 'BAC', 'GM',\r\n'T', 'UAA', 'SHLD', 'XOM', 'RRC', 'BBY', 'MA', 'PFE', 'JPM', 'SBUX']\r\n```\r\n\r\nThese tickers have been informally selected to meet several criteria:\r\n\r\n-   reasonably liquid\r\n-   different performances and volatilities\r\n-   different amounts of data to test robustness\r\n\r\nCurrently, the tests have not explored all of the edge cases and combinations\r\nof objective functions and parameters. However, each method and parameter has\r\nbeen tested to work as intended.\r\n\r\n## Citing PyPortfolioOpt\r\n\r\nIf you use PyPortfolioOpt for published work, please cite the [JOSS paper](https://joss.theoj.org/papers/10.21105/joss.03066).\r\n\r\nCitation string:\r\n\r\n```text\r\nMartin, R. A., (2021). PyPortfolioOpt: portfolio optimization in Python. Journal of Open Source Software, 6(61), 3066, https://doi.org/10.21105/joss.03066\r\n```\r\n\r\nBibTex::\r\n\r\n```bibtex\r\n@article{Martin2021,\r\n  doi = {10.21105/joss.03066},\r\n  url = {https://doi.org/10.21105/joss.03066},\r\n  year = {2021},\r\n  publisher = {The Open Journal},\r\n  volume = {6},\r\n  number = {61},\r\n  pages = {3066},\r\n  author = {Robert Andrew Martin},\r\n  title = {PyPortfolioOpt: portfolio optimization in Python},\r\n  journal = {Journal of Open Source Software}\r\n}\r\n```\r\n\r\n## Contributing\r\n\r\nContributions are _most welcome_. Have a look at the [Contribution Guide](https://github.com/robertmartin8/PyPortfolioOpt/blob/master/CONTRIBUTING.md) for more.\r\n\r\nI'd like to thank all of the people who have contributed to PyPortfolioOpt since its release in 2018.\r\nSpecial shout-outs to:\r\n\r\n-   Philipp Schiele\r\n-   Carl Peasnell\r\n-   Felipe Schneider\r\n-   Dingyuan Wang\r\n-   Pat Newell\r\n-   Aditya Bhutra\r\n-   Thomas Schmelzer\r\n-   Rich Caputo\r\n-   Nicolas Knudde\r\n\r\n## Getting in touch\r\n\r\nIf you are having a problem with PyPortfolioOpt, please raise a GitHub issue. For anything else, you can reach me at:\r\n\r\n\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Financial portfolio optimization in python",
    "version": "1.5.6",
    "split_keywords": [
        "portfolio",
        "finance",
        "optimization",
        "quant",
        "trading",
        "investing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e69f00e2c0e6013038bfa635ab0ea2269fcc2303b9d5d24fe68583af8c52ccba",
                "md5": "87e5961a2db8f1a459192360e091ec5d",
                "sha256": "9528aef6880feddc2a051f6727e2f4392eea253dd94e876a307a52fbdb371f11"
            },
            "downloads": -1,
            "filename": "pyportfoliopt-1.5.6.tar.gz",
            "has_sig": false,
            "md5_digest": "87e5961a2db8f1a459192360e091ec5d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 133636,
            "upload_time": "2023-04-17T17:00:54",
            "upload_time_iso_8601": "2023-04-17T17:00:54.218445Z",
            "url": "https://files.pythonhosted.org/packages/e6/9f/00e2c0e6013038bfa635ab0ea2269fcc2303b9d5d24fe68583af8c52ccba/pyportfoliopt-1.5.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-17 17:00:54",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "pyportfoliopt"
}
        
Elapsed time: 0.83085s