def implied_volatility(price, S, K, t, r, flag): """Calculate the Black-Scholes implied volatility. :param price: the Black-Scholes option price :type price: float :param S: underlying asset price :type S: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float :param r: risk-free interest rate :type r: float :param flag: 'c' or 'p' for call or put. :type flag: str >>> S = 100 >>> K = 100 >>> sigma = .2 >>> r = .01 >>> flag = 'c' >>> t = .5 >>> price = black_scholes(flag, S, K, t, r, sigma) >>> iv = implied_volatility(price, S, K, t, r, flag) >>> print price, iv 5.87602423383 0.2 """ adjusted_price = price / e**(-r * t) return lets_be_rational.implied_volatility_from_a_transformed_rational_guess( adjusted_price, forward_price(S, t, r), K, t, binary_flag[flag])
def implied_volatility_of_undiscounted_option_price(undiscounted_option_price, F, K, t, flag): """Calculate the implied volatility of the undiscounted Black option price :param undiscounted_option_price: undiscounted Black price of a futures option :type undiscounted_option_price: float :param F: underlying futures price :type F: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float >>> F = 100 >>> K = 100 >>> sigma = .2 >>> flag = 'c' >>> t = .5 >>> undiscounted_call_price = undiscounted_black(F, K, sigma, t, flag) >>> iv = implied_volatility_of_undiscounted_option_price( ... undiscounted_call_price, F, K, t, flag) >>> print undiscounted_call_price, iv 5.6371977797 0.2 """ return lets_be_rational.implied_volatility_from_a_transformed_rational_guess( undiscounted_option_price, F, K, t, binary_flag[flag] )
def implied_volatility_of_undiscounted_option_price(undiscounted_option_price, F, K, t, flag): """Calculate the implied volatility of the undiscounted Black option price :param undiscounted_option_price: undiscounted Black price of a futures option :type undiscounted_option_price: float :param F: underlying futures price :type F: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float >>> F = 100 >>> K = 100 >>> sigma = .2 >>> flag = 'c' >>> t = .5 >>> undiscounted_call_price = undiscounted_black(F, K, sigma, t, flag) >>> iv = implied_volatility_of_undiscounted_option_price( ... undiscounted_call_price, F, K, t, flag) >>> print undiscounted_call_price, iv 5.6371977797 0.2 """ return lets_be_rational.implied_volatility_from_a_transformed_rational_guess( undiscounted_option_price, F, K, t, binary_flag[flag])
def implied_volatility_of_discounted_option_price(discounted_option_price, F, K, r, t, flag): """Calculate the implied volatility of the Black option price :param discounted_option_price: discounted Black price of a futures option :type discounted_option_price: float :param F: underlying futures price :type F: float :param K: strike price :type K: float :param r: the risk-free interest rate :type r: float :param t: time to expiration in years :type t: float :param flag: 'p' or 'c' for put or call :type flag: str >>> F = 100 >>> K = 100 >>> sigma = .2 >>> flag = 'c' >>> t = .5 >>> r = .02 >>> discounted_call_price = black(flag, F, K, t, r, sigma) >>> iv = implied_volatility_of_discounted_option_price( ... discounted_call_price, F, K, r, t, flag) >>> print discounted_call_price, iv 5.5811067246 0.2 """ discount_factor = numpy.exp(-r*t) undiscounted_option_price = discounted_option_price / discount_factor return lets_be_rational.implied_volatility_from_a_transformed_rational_guess( undiscounted_option_price, F, K, t, binary_flag[flag] )
def implied_volatility(price, S, K, t, r, flag): """Calculate the Black-Scholes implied volatility. :param price: the Black-Scholes option price :type price: float :param S: underlying asset price :type S: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float :param r: risk-free interest rate :type r: float :param flag: 'c' or 'p' for call or put. :type flag: str >>> S = 100 >>> K = 100 >>> sigma = .2 >>> r = .01 >>> flag = 'c' >>> t = .5 >>> price = black_scholes(flag, S, K, t, r, sigma) >>> iv = implied_volatility(price, S, K, t, r, flag) >>> print price, iv 5.87602423383 0.2 """ adjusted_price = price / e**(-r*t) return lets_be_rational.implied_volatility_from_a_transformed_rational_guess( adjusted_price, forward_price(S, t, r), K, t, binary_flag[flag] )
data = {} data['input'] = [] data['output'] = [] for i in range(TestCases): F = _F[i] K = _K[i] sigma = _sigma[i] T = _T[i] q = _q[i] z = _z[i] N = _N[i] black = lets_be_rational.black(F, K, sigma, T, q) iv = lets_be_rational.implied_volatility_from_a_transformed_rational_guess( black, F, K, T, q) ivi = lets_be_rational.implied_volatility_from_a_transformed_rational_guess_with_limited_iterations( black, F, K, T, q, N) x = _x[i] s = _s[i] nblack = lets_be_rational.normalised_black(x, s, q) nblackcall = lets_be_rational.normalised_black_call(x, s) niv = lets_be_rational.normalised_implied_volatility_from_a_transformed_rational_guess( nblack, x, q) nivi = lets_be_rational.normalised_implied_volatility_from_a_transformed_rational_guess_with_limited_iterations( nblack, x, q, N) vega = lets_be_rational.normalised_vega(x, s) norm_cdf = lets_be_rational.norm_cdf(z) data['input'].append({
data = {} data['input'] = [] data['output'] = [] for i in range(TestCases): F = _F[i] K = _K[i] sigma = _sigma[i] T = _T[i] q = _q[i] z = _z[i] N = _N[i] black = lets_be_rational.black(F, K, sigma, T, q) iv = lets_be_rational.implied_volatility_from_a_transformed_rational_guess(black, F, K, T, q) ivi = lets_be_rational.implied_volatility_from_a_transformed_rational_guess_with_limited_iterations(black, F, K, T, q, N) x = _x[i] s = _s[i] nblack = lets_be_rational.normalised_black(x, s, q) nblackcall = lets_be_rational.normalised_black_call(x, s) niv = lets_be_rational.normalised_implied_volatility_from_a_transformed_rational_guess(nblack, x, q) nivi = lets_be_rational.normalised_implied_volatility_from_a_transformed_rational_guess_with_limited_iterations(nblack, x, q, N) vega = lets_be_rational.normalised_vega(x, s) norm_cdf = lets_be_rational.norm_cdf(z) data['input'].append({'F':F, 'K':K, 'sigma':sigma, 'T':T,