danielhstahl / black_scholes_rust

Rust for black scholes
https://danielhstahl.github.io/black_scholes_rust/dev/bench/
23 stars 8 forks source link

call_iv() shouldn't panic if the implied volatility can't be found #2

Closed dgbustos closed 5 years ago

dgbustos commented 5 years ago

In call_iv(), if there is no volatility which matches the price, then nrfind::find_root() in call_iv_guess() will return Result.Err and unwrap() will panic.

call_iv() and the like should probably return Result<f64> instead of f64.

danielhstahl commented 5 years ago

Thanks for bringing up this issue @dgbustos ! I've gone back and forth when considering the API for the implied volatility functions. With the "approximate_vol" function providing a value close to the optimum value I have not ever had a panic. In the tests I randomly simulate parameters; with no panics.

Have you actually experienced a panic when using the implied volatility functions? If so, can you provide the parameters which resulted in the panic?

Thanks!

dgbustos commented 5 years ago

I have 500 examples from a recent file of SPY option quotes. They're all for deep-in-the-money calls. For example

black_scholes::call_iv( 50.275, 274.525, 225.000, 0.0244, 0.156 );
black_scholes::call_iv( 62.02, 274.525, 222.000, 0.0244, 1.823 );

When I encountered this in another library, I think I tracked the reason down to negative time value, and https://math.stackexchange.com/a/2959355 might be informative.

danielhstahl commented 5 years ago

That makes a lot of sense. I'll update the code (hopefully this weekend) and bump the crates version.