public static double CalculateImplVolatility(double S, double T, double K, double r, double OptionPrice, bool CallOption, double x0) //CallOption is set to 'true' if the given option price is a call option, and to 'false' if it is a put option { Func <double, double> CalculateOptionPrice; //Assign the function to a method for call/put price calculation if (CallOption == true) //If the option price is that of a call option { CalculateOptionPrice = (sigma) => BlackScholesFormula.CalculateCallOptionPrice(S, T, K, sigma, r) - OptionPrice; } else if (CallOption == false) //If the option price is that of a put option { CalculateOptionPrice = (sigma) => BlackScholesFormula.CalculatePutOptionPrice(S, T, K, sigma, r) - OptionPrice; } else { throw new ArgumentOutOfRangeException("CallOption is TRUE if price is a call option price," + "and FALSE if put option price"); } //Find the roots of the function by passing it to the NewtonSolver double maxError = 1e-5; int maxIter = 10000; double ImplVolatility = NewtonSolver.Solver(CalculateOptionPrice, null, x0, maxError, maxIter); return(ImplVolatility); }
public static void Main() { //Use the given parameter values to test the implemented classes double S = 100; double K = 100; double[] T = { 1, 1, 1, 10, 1 }; double[] sigma = { 0.1, 0.1, 0.2, 0.1, 0.1 }; double[] r = { 0.05, 0, 0.05, 0.05, 0.05 }; Func <double, double> putPayoff = (S0) => Math.Max(K - S0, 0); Func <double, double> callPayoff = (S0) => Math.Max(S0 - K, 0); uint N = 200; uint M = 100; //For calculating the implied volatility // double CallPrice = 10; // double PutPrice = 3; double call; //Exact call option price using the Black-Scholes formula double put; //Exact put option price // double MCcall; //Monte Carlo estimate of the call option // double MCput; //MC estimate of the put option double FDcall, FDput; //Calculate the option prices both using the formula and the estimate for each set of parameters for (int i = 0; i < 5; i++) { // call = BlackScholesFormula.CalculateCallOptionPrice (S, T[i], K, sigma[i], r[i]); put = BlackScholesFormula.CalculatePutOptionPrice(S, T[i], K, sigma[i], r[i]); // BSFiniteDifferenceSolver CallSolver = new BSFiniteDifferenceSolver(T[i],callPayoff,r[i],sigma[i],5*K,N,M); BSFiniteDifferenceSolver PutSolver = new BSFiniteDifferenceSolver(T[i], putPayoff, r[i], sigma[i], 5 * K, N, M); // FDcall = CallSolver.Price(S); FDput = PutSolver.Price(S); // MCcall = BSMonteCarlo.CalculateCallOptionPrice (S, T[i], K[i], sigma[i], r[i]); // MCput = BSMonteCarlo.CalculatePutOptionPrice (S, T[i], K[i], sigma[i], r[i]); Console.Write("Put: {0} FD Estimate: {1}\n", put, FDput); } //Now compute the implied volatilities for given call and put prices // double Volatility1 = BSImpliedVolatility.CalculateImplVolatility (S, T[0], K[0], r[0], CallPrice, true, 0.5); // double Volatility2 = BSImpliedVolatility.CalculateImplVolatility (S, T[0], K[0], r[0], PutPrice, false, 0.5); // Console.Write ("Implied volatility: {0}\nImplied volatility: {1}\n", Volatility1, Volatility2); Console.ReadKey(); }
public static void Main() { // Use the given parameter values to test the implemented class double r = 0.05; double sigma = 0.1; double K = 100; double T = 1; double S0 = 100; double error; Func <double, double> putPayoff = (S) => Math.Max(K - S, 0); // = g(x), the payoff function uint N = 200; // Number of timesteps uint M = 100; // Number of space partitions int numberCalculations = 1; // Get the exact value using the Black-Scholes formula double putPrice = BlackScholesFormula.CalculatePutOptionPrice(S0, T, K, sigma, r); // Use finite difference estimation to approximate the option price and compare it to the exact value /* * for (int i = 0; i < numberCalculations; i++, M*=2) * { * BSFiniteDifferenceSolver solver = * new BSFiniteDifferenceSolver (T, putPayoff, r, sigma, 5*K, N, M); * * error = Math.Abs (putPrice - solver.Price (S0)); * Console.WriteLine ("Space partitions: {0}, time steps: {1}, error: {2}", M, N, error); * } * Console.WriteLine(); */ N = 10; M = 1600; for (int i = 0; i < numberCalculations; i++, N *= 2) { BSFiniteDifferenceSolver solver = new BSFiniteDifferenceSolver(T, putPayoff, r, sigma, 5 * K, N, M); error = Math.Abs(putPrice - solver.Price(S0)); Console.WriteLine("Space partitions: {0}, time steps: {1}, error: {2}", M, N, error); } Console.ReadKey(); }