source('../Chapter13/utils.R') ## load in functions to compute the Black-Scholes call price t = 0 ## the current time T = 0.25 ## time in years till August expiration r = 0.07 ## yearly interest rate strikes = c(1600, 1700)/100. market_prices = c(1.29, 0.70) ## from Figure 16.8 S0 = mean(strikes) ## initial guess at the spot and the volatility sigma = 0.4 min_fn = function(x){ S = x[1] sigma = x[2] predictions = BS_Call(S, t, r, strikes, T, sigma) res = mean((market_prices - predictions)^2) return(res) } ## Test our function once: ##print(min_fn(c(S0, sigma))) res = optim(c(S0, sigma), min_fn, method='Nelder-Mead') S = res$par[1] sigma = res$par[2] predictions = BS_Call(S, t, r, strikes, T, sigma) print(sprintf('S= %f; sigma= %f; C(K=16)= %f; C(K=17)= %f', S, sigma, predictions[1], predictions[2]))