Here is an answer to the original question. It is an algorithm to compute IV, using the Method of Bisections. Developed by Bernt Odegaard.
#include <cmath>
#include "fin_recipes.h"
double option price implied volatility call_black_scholes_bisections(const double& S,
const double& K,
const double& r,
const double& time,
const double& option price){
if (option price<0.99*(S-K*exp(-time*r))) { // check for arbitrage violations.
return 0.0; // Option price is too low if this happens
};
// simple binomial search for the implied volatility.
// relies on the value of the option increasing in volatility
const double ACCURACY = 1.0e-5; // make this smaller for higher accuracy
const int MAX_ITERATIONS = 100;
const double HIGH_VALUE = 1e10;
const double ERROR = -1e40;
// want to bracket sigma first. find a maximum sigma by finding a sigma
// with a estimated price higher than the actual price.
double sigma low=1e-5;
double sigma high=0.3;
double price = option_price_call_ black_ scholes(S,K,r,sigma high,time);
while (price < option price) {
sigma high = 2.0 * sigma high; // keep doubling.
price = option price call black scholes(S,K,r,sigma high,time);
if (sigma high>HIGH_VALUE) return ERROR; // panic, something wrong.
};
for (int i=0;i<MAX_ITERATIONS;i++){
double sigma = (sigma low+sigma high)*0.5;
price = option_price_call_black_scholes(S,K,r,sigma,time);
double test = (price-option price);
if (fabs(test)<ACCURACY) { return sigma; };
if (test < 0.0) { sigma low = sigma; }
else { sigma high = sigma; }
};
return ERROR;
};
PS: The last for loop will not display properly for some reason. Google Bernt Odegaard to get the original recipe.
#include <cmath>
#include "fin_recipes.h"
double option price implied volatility call_black_scholes_bisections(const double& S,
const double& K,
const double& r,
const double& time,
const double& option price){
if (option price<0.99*(S-K*exp(-time*r))) { // check for arbitrage violations.
return 0.0; // Option price is too low if this happens
};
// simple binomial search for the implied volatility.
// relies on the value of the option increasing in volatility
const double ACCURACY = 1.0e-5; // make this smaller for higher accuracy
const int MAX_ITERATIONS = 100;
const double HIGH_VALUE = 1e10;
const double ERROR = -1e40;
// want to bracket sigma first. find a maximum sigma by finding a sigma
// with a estimated price higher than the actual price.
double sigma low=1e-5;
double sigma high=0.3;
double price = option_price_call_ black_ scholes(S,K,r,sigma high,time);
while (price < option price) {
sigma high = 2.0 * sigma high; // keep doubling.
price = option price call black scholes(S,K,r,sigma high,time);
if (sigma high>HIGH_VALUE) return ERROR; // panic, something wrong.
};
for (int i=0;i<MAX_ITERATIONS;i++){
double sigma = (sigma low+sigma high)*0.5;
price = option_price_call_black_scholes(S,K,r,sigma,time);
double test = (price-option price);
if (fabs(test)<ACCURACY) { return sigma; };
if (test < 0.0) { sigma low = sigma; }
else { sigma high = sigma; }
};
return ERROR;
};
PS: The last for loop will not display properly for some reason. Google Bernt Odegaard to get the original recipe.