implement black scholes formulas in python with numerical (using mpmath library) and…
- User
implement black scholes formulas in python with numerical (using mpmath library) and symbolic (using sympy library) methods
Assistant
Black-Scholes Formulas Implementation in Python
This implementation provides both numerical and symbolic methods for calculating the Black-Scholes option pricing model using the
mpmathandsympylibraries, respectively.Numerical Method using
mpmathimport mpmath as mp from mpmath import erf def black_scholes_numerical(S, K, T, r, sigma, option_type): """ Calculate the Black-Scholes option price using numerical methods. Parameters: S (float): Underlying asset price K (float): Strike price T (float): Time to maturity (in years) r (float): Risk-free interest rate sigma (float): Volatility of the underlying asset option_type (str): Type of option ('call' or 'put') Returns: float: Black-Scholes option price """ d1 = (mp.log(S / K) + (r + sigma**2 / 2) * T) / (sigma * mp.sqrt(T)) d2 = d1 - sigma * mp.sqrt(T) if option_type == 'call': option_price = S * mp.exp(-r * T) * (1 + erf(d1 / mp.sqrt(2))) / 2 - K * mp.exp(-r * T) * (1 + erf(d2 / mp.sqrt(2))) / 2 elif option_type == 'put': option_price = K * mp.exp(-r * T) * (1 + erf(-d2 / mp.sqrt(2))) / 2 - S * mp.exp(-r * T) * (1 + erf(-d1 / mp.sqrt(2))) / 2 else: raise ValueError("Invalid option type. Please specify 'call' or 'put'.") return option_price # Example usage: S = 100.0 K = 105.0 T = 1.0 r = 0.05 sigma = 0.2 option_type = 'call' option_price = black_scholes_numerical(S, K, T, r, sigma, option_type) print(f"Black-Scholes option price (numerical): {option_price}")Symbolic Method using
sympyimport sympy as sp def black_scholes_symbolic(S, K, T, r, sigma, option_type): """ Calculate the Black-Scholes option price using symbolic methods. Parameters: S (float): Underlying asset price K (float): Strike price T (float): Time to maturity (in years) r (float): Risk-free interest rate sigma (float): Volatility of the underlying asset option_type (str): Type of option ('call' or 'put') Returns: float: Black-Scholes option price """ S_sym = sp.symbols('S') K_sym = sp.symbols('K') T_sym = sp.symbols('T') r_sym = sp.symbols('r') sigma_sym = sp.symbols('sigma') d1_sym = (sp.log(S_sym / K_sym) + (r_sym + sigma_sym**2 / 2) * T_sym) / (sigma_sym * sp.sqrt(T_sym)) d2_sym = d1_sym - sigma_sym * sp.sqrt(T_sym) if option_type == 'call': option_price_sym = S_sym * sp.exp(-r_sym * T_sym) * (1 + sp.erf(d1_sym / sp.sqrt(2))) / 2 - K_sym * sp.exp(-r_sym * T_sym) * (1 + sp.erf(d2_sym / sp.sqrt(2))) / 2 elif option_type == 'put': option_price_sym = K_sym * sp.exp(-r_sym * T_sym) * (1 + sp.erf(-d2_sym / sp.sqrt(2))) / 2 - S_sym * sp.exp(-r_sym * T_sym) * (1 + sp.erf(-d1_sym / sp.sqrt(2))) / 2 else: raise ValueError("Invalid option type. Please specify 'call' or 'put'.") option_price = option_price_sym.subs({S_sym: S, K_sym: K, T_sym: T, r_sym: r, sigma_sym: sigma}) return option_price.evalf() # Example usage: S = 100.0 K = 105.0 T = 1.0 r = 0.05 sigma = 0.2 option_type = 'call' option_price = black_scholes_symbolic(S, K, T, r, sigma, option_type) print(f"Black-Scholes option price (symbolic): {option_price}")Note that the
sympylibrary uses a different implementation of the error function (sp.erf) compared to thempmathlibrary (mp.erf). The results may vary slightly due to the differences in the numerical methods used.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.