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

price_dataframe non-default source index breaks results #4

Closed
rhodge1 opened this issue Apr 8, 2021 · 3 comments
Closed

price_dataframe non-default source index breaks results #4

rhodge1 opened this issue Apr 8, 2021 · 3 comments

Comments

@rhodge1
Copy link

rhodge1 commented Apr 8, 2021

Thanks very much for making the library available @marcdemers. You've saved me a ton of time!

One issue I noticed straight away in my own code is that my source dataframe for the price_dataframe api function has a non-standard index and this breaks the results.

Three examples below show what I mean. The first two (failing) examples can be fixed by using the to_numpy() method on the price, sigma and greek[] returns. The third example (your original in the docs) is not impacted by this change.

tuples = [(1, 'red'), (2, 'blue')]
idx = pd.MultiIndex.from_tuples(tuples, names=('number', 'color'))
df = pd.DataFrame(index=idx)
df['Flag'] = ['c', 'p']
df['S'] = 95
df['K'] = [100, 90]
df['T'] = 0.2
df['R'] = 0.2
df['IV'] = 0.2

# first example multiindex (NaNs)
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

# second example index not starting at zero (results shifted/NaNs)
idx = pd.Index([1, 2])
df.index = idx
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

# third example index starting at zero (results OK)
idx = pd.Index([0, 1])
df.index = idx
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

The fix is to apply to_numpy() like this in price_dataframe:

...
        if inplace:
            df["Price"] = price.to_numpy()
        else:
            output_df["Price"] = price.to_numpy()
....
@marcdemers
Copy link
Owner

Good call! Will include in next version of the library. Thank you!

@behdadforghani
Copy link

behdadforghani commented Feb 18, 2022

I was going to report a similar issue. Mine was for the case of implied volatility calculation for line:

df["IV"] = sigma
My fix was to change it to:
df["IV"] = sigma["IV"].values

Thanks for writing this code!

Thanks very much for making the library available @marcdemers. You've saved me a ton of time!

One issue I noticed straight away in my own code is that my source dataframe for the price_dataframe api function has a non-standard index and this breaks the results.

Three examples below show what I mean. The first two (failing) examples can be fixed by using the to_numpy() method on the price, sigma and greek[] returns. The third example (your original in the docs) is not impacted by this change.

tuples = [(1, 'red'), (2, 'blue')]
idx = pd.MultiIndex.from_tuples(tuples, names=('number', 'color'))
df = pd.DataFrame(index=idx)
df['Flag'] = ['c', 'p']
df['S'] = 95
df['K'] = [100, 90]
df['T'] = 0.2
df['R'] = 0.2
df['IV'] = 0.2

# first example multiindex (NaNs)
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

# second example index not starting at zero (results shifted/NaNs)
idx = pd.Index([1, 2])
df.index = idx
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

# third example index starting at zero (results OK)
idx = pd.Index([0, 1])
df.index = idx
result = price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T',
                         riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
print(result)

The fix is to apply to_numpy() like this in price_dataframe:

...
        if inplace:
            df["Price"] = price.to_numpy()
        else:
            output_df["Price"] = price.to_numpy()
....

@marcdemers
Copy link
Owner

fixed by #21

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

No branches or pull requests

3 participants