explicit method for european put option code
TRANSCRIPT
//***********
//
// Explicit Method for European put Option
//
//***********
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#define M 633
#define N 1000
/* Code for finding the maximum between two numbers*/
double max(double x, double y){
if (x >= y)
return(x);
else
return(y);
}
/* Code for finding the Lagrange quadratic interpolating polynomial among three numbers.
Here a, b, and c are the x-coordinates for the interpolation; d,f,g are the corresponding y-
coordinates of the interpolation and x is the point that we are interpolating around. For
this program we are not interested in the interpolating polynomial, only in the price of the
option.*/
double interp(double a, double b, double c, double dd, double f, double g, double x){
double eqn;
eqn=((b-x)*(c-x))/((b-a)*(c-a))*dd +((x-a)*(c-x))/((b-a)*(c-b))*f+((x-a)*(x-b))/((c-a)*(c-b))*g;
return(eqn);
}
/* European put option price computed by explicit finite difference method. The entries are the rate,
the dividend, the volatility, the expiry and the number of space steps, M and the number of time steps,
N. This is computed on a finite domain first. Then we do conversions to get back to the original problem
and obtain the actual value of the option. For this code we assume rate, dividend and sigma are constants.*/
double efdieuropean(double rate, double dividend, double sigma, double expiry, double exercise, double price){
double alpha=0.0, delta_tau=0.0, delta_xi=0.0, xi_m=0.0, backstep_coeff=0.0,
place_coeff=0.0, forwardstep_coeff=0.0, xi_star=0.0, value=0.0;
int m=0,n=0,place=0;
delta_tau=expiry/(double)N;
delta_xi = sqrt(sigma*sigma*delta_tau)/4.0;
double V[M+1][N];
alpha = delta_tau/(delta_xi*delta_xi);
/* Initializing V */
for(m=0; m<=M; m++){
xi_m = (double) m*delta_xi;
V[m][0] = max(1.0-2.0*xi_m,0.0);
}
/* Black-Scholes Equation */
for(n=0; n<= (N-1); n++){ /*Time loop begins*/
for(m=0; m<=M; m++){ /*Space step loop begins*/
xi_m = m*delta_xi;
backstep_coeff = 0.5*(sigma*sigma*xi_m*xi_m*(1.0-xi_m)*(1.0-xi_m) - (rate - dividend)*xi_m*(1.0-xi_m)*delta_xi)*alpha; /* Coefficient for v_(m-1)*/
place_coeff = 1.0-sigma*sigma*xi_m*xi_m*(1.0-xi_m)*(1.0-xi_m) *alpha - delta_tau*(rate*(1.0-xi_m) + dividend*xi_m); /* Coefficient for v_m */
forwardstep_coeff = 0.5*(sigma*sigma*xi_m*xi_m*(1.0-xi_m)*(1.0-xi_m) + (rate - dividend)*xi_m*(1.0-xi_m)*delta_xi)*alpha; /* Coefficient for v_(m+1) */
if (m == 0)
V[m][n+1] = forwardstep_coeff*V[m+1][n] + place_coeff*V[m][n];
else if (m == M)
V[m][n+1] = place_coeff*V[m][n] + backstep_coeff*V[m-1][n];
else
V[m][n+1] = backstep_coeff * V[m-1][n] + place_coeff *V[m][n] + forwardstep_coeff *V[m+1][n];
} /* End space step loop */
} /* End time step loop */
/*Interpolation values and converting back to S,t-plane */
xi_star=price/(price+exercise);
for(m=0;m<=M; m++){
xi_m = m*delta_xi;
if(xi_m >=xi_star){
place=m;
break;
} /*end if statement*/
} /*end loop */
value=(price + exercise)*interp(((double)place - 1.0)/M, (double)place/M, ((double)place + 1.0)/M,V[place - 1][N], V[place][N], V[place+1][N], xi_star);
return(value);
}
/* End explicit finite difference scheme for european put option */
/* MAIN PART*/
int main(void){
double rate, dividend, sigma, T, price, exercise;
cout << "What is the rate?\n";
cin >> rate;
cout << "What is the dividend?\n";
cin >> dividend;
cout << "What is the volatility?\n";
cin >> sigma;
cout << "What is the expiry?\n";
cin >> T;
cout << "What is the exercise price?\n";
cin >> exercise;
cout << "What is the present price of stock?\n";
cin >> price;
printf("Value of put option is about %lf\n", efdieuropean(rate, dividend, sigma, T, exercise,price));
}