Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError when optimizing a portfolio with 3 indices #52

Closed
noobmaster29 opened this issue Apr 26, 2020 · 6 comments · Fixed by #53
Closed

ValueError when optimizing a portfolio with 3 indices #52

noobmaster29 opened this issue Apr 26, 2020 · 6 comments · Fixed by #53

Comments

@noobmaster29
Copy link

noobmaster29 commented Apr 26, 2020

I'm currently having an issue running the demo code when I try to run the mc_optimisation with 3 stocks in the portfolio. I've tried running with 2 stocks as well as portfolios with more than 3 stocks and everything seems to work fine. However, when I setup a portfolio with 3 stocks, I run into the following error.

ValueError: Shape of passed values is (2, 5000), indices imply (2, 3)

Code that produced the error:

from finquant.portfolio import build_portfolio

names = ['MSFT', 'AAPL', 'GOOG']

start_date = '2015-01-01'
end_date = '2020-04-25'
pf = build_portfolio(names=names,
start_date=start_date,
end_date=end_date, data_api="yfinance")

opt_w, opt_res = pf.mc_optimisation(num_trials=5000)

@fmilthaler
Copy link
Owner

This seems really odd to me. I will test this over the coming weekend.

However, are you sure there is data in each of the columns? Try to print out the data: print(pf.data). Does it show data in all three columns? If not, the data that was downloaded through yfinance is incomplete. This should then be caught by FinQuant and an appropriate error message should be given to the user.

Thinking about it, I thought that check should already be in FinQuant. I'll check this weekend. Sorry for the inconvenience.

@nuvious
Copy link
Contributor

nuvious commented May 1, 2020

I'm getting the exact same issue. Reproduction steps below:

https://pastebin.com/cap5ZGaP

If you set the allocation on line 25 to:

# Define portfolio and allocation
stocks = {'MSFT': 25, 'TSLA': 95, 'AAPL': 100, 'GNSS': 0}

the error does not occur.

At first I thought it was on my end because I'm downloading yahoo finance docs, caching them, and then reshaping dataframes to match the yahoo finance query output shape, but ended up ruling that out.

My best guess is np.stack(res).T[0] is the root of the issue, but that's a rough guess as I'm relatively green when it comes to numpy.

@nuvious
Copy link
Contributor

nuvious commented May 1, 2020

P.S. Here's a copy of the above repro in Jupyter Notebook format. Its prints out the data and shows it's in the proper shape for processing:

https://1drv.ms/u/s!Am1tgTvkI9ZOt5kYuQXQ6I0sJfEd2g?e=DnnM2u
(Above link expires on the 15th so message me if you don't download it before then)

Additionally I threw a breakpoint in and inspected pf.data (specifically pf_api.data in my reproduction) at the exception point and it doesn't have any missing data. You can verify that by calling dropna on the dataframe in an inspector and can see it's not dropping any rows.

Hope this helps narrow down the issue and thanks for your time!

@noobmaster29
Copy link
Author

The data is loaded correctly. I've tried with multiple 3 tickers combinations with long trading history and none of them work. If I load the same stocks with more than 3 tickers, the code runs fine.

@fmilthaler
Copy link
Owner

thanks, will investigate this in the next two days.

@noobmaster29
Copy link
Author

Thank you for the fix. I just wanted to note that the pip upgrade does not reflect these changes. I have replaced the new files with the fix in my install but upgrading or reinstalling via pip did not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants