ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.76k stars 835 forks source link

Rolling beta does not works on small dataframes (`ValueError: cannot convert float NaN to integer`) #193

Closed Caceresenzo closed 2 years ago

Caceresenzo commented 2 years ago

The 6 months rolling do not work when there is not enough data.

Here is the stack trace it generate:

...
  File "C:\Users\cacer\Desktop\datacrunch-trading-cli\backtest\export\quants.py", line 84, in finalize
    quantstats.reports.html(merged.daily_profit_pct, merged.close, output=True, download_filename=self.html_output_file)
  File "C:\Users\cacer\AppData\Local\Programs\Python\Python39\lib\site-packages\quantstats\reports.py", line 192, in html
    _plots.rolling_beta(returns, benchmark, grayscale=grayscale,
  File "C:\Users\cacer\AppData\Local\Programs\Python\Python39\lib\site-packages\quantstats\_plotting\wrappers.py", line 514, in rolling_beta
    fig = _core.plot_rolling_beta(returns, benchmark,
  File "C:\Users\cacer\AppData\Local\Programs\Python\Python39\lib\site-packages\quantstats\_plotting\core.py", line 515, in plot_rolling_beta
    mmin = min([-100, int(beta.min()*100)])
ValueError: cannot convert float NaN to integer

A simple fix can be to do fillna() on the beta after this line: https://github.com/ranaroussi/quantstats/blob/bfc247071fa1b80772cb101f2b31b72b8227cabf/quantstats/_plotting/core.py#L506

I can do a pull request if you want. Please tell me if you found anything that would make this fix incorrect.

jlk-issachar-ai commented 2 years ago

Anyone else solve this for version 0.0.57? I am just using 2022 data and throwing error

ValueError: cannot convert float NaN to integer

Caceresenzo commented 2 years ago

@Dynamic50, a temporary fix can be changing the library source code:

https://github.com/ranaroussi/quantstats/blob/bfc247071fa1b80772cb101f2b31b72b8227cabf/quantstats/_plotting/core.py#L504-L509

by adding a single line:

   ), fontsize=12, color='gray') 

beta = _stats.rolling_greeks(returns, benchmark, window1)['beta'] 
beta.fillna(0, inplace=True)  # <-- this line

ax.plot(beta, lw=lw, label=window1_label, color=colors[1]) 

if window2: 
Caceresenzo commented 2 years ago

@ranaroussi I can do a pull request if that okay with you.

jlk-issachar-ai commented 2 years ago

ValueError: Must specify a fill 'value' or 'method'.

Hmm... troubleshooting now

Caceresenzo commented 2 years ago

Oops, you are right. I will edit my comment.

jlk-issachar-ai commented 2 years ago

IndexError: index 0 is out of bounds for axis 0 with size 0

Caceresenzo commented 2 years ago

Is your dataframe empty?

Seem like another issue. Sorry I cannot help your further with that :/

jlk-issachar-ai commented 2 years ago

No, its not... very strange.

I use a longer set of data from the database and reads fine....

jlk-issachar-ai commented 2 years ago

core.py

beta = _stats.rolling_greeks(returns, benchmark, window1)['beta'] 
beta.fillna(0, inplace=True)
ax.plot(beta, lw=lw, label=window1_label, color=colors[1])

stats.py

*can adjust time period in line 227

Caceresenzo commented 2 years ago

Thanks