robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.43k stars 948 forks source link

Example Data Files #478

Closed seahawk920 closed 2 years ago

seahawk920 commented 2 years ago

What are you trying to do? I am testing the package to try to replicate the results from the user guide.

What have you tried? I tried following the link to get the data but it's not successful.

What data are you using? Can you provide the data (Goog, Facebook, etc) shown in the userguide so that I can make sure the package is running properly?

robertmartin8 commented 2 years ago

I believe they are all here: https://github.com/robertmartin8/PyPortfolioOpt/tree/master/tests/resources

seahawk920 commented 2 years ago

Thanks I tried out your below example and get slightly different results:

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

(That's what's shown in your website) Expected annual return: 33.0% Annual volatility: 21.7% Sharpe Ratio: 1.43

import pandas as pd from pypfopt.efficient_frontier 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) weights = ef.max_sharpe() ef.portfolio_performance(verbose=True)

robertmartin8 commented 2 years ago

Ah in that case I must have been using a slightly different time period, hence the different results. Don't have that file to hand anymore – my apologies!

seahawk920 commented 2 years ago

No worry. Do you plan to update your documentation? I think it’s quite useful for the new users.

On Mon, Aug 29, 2022 at 5:33 AM Robert Martin @.***> wrote:

Ah in that case I must have been using a slightly different time period, hence the different results. Don't have that file to hand anymore – my apologies!

— Reply to this email directly, view it on GitHub https://github.com/robertmartin8/PyPortfolioOpt/issues/478#issuecomment-1230020966, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2XI4UYZOFS7KHZMULVHNFTV3R7XNANCNFSM57TP5POQ . You are receiving this because you authored the thread.Message ID: @.***>