/* nag_pde_bs_1d_analytic (d03ndc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include #include #define F(I,J) f[ns*((J)-1)+(I)-1] #define THETA(I,J) theta[ns*((J)-1)+(I)-1] #define DELTA(I,J) delta[ns*((J)-1)+(I)-1] #define GAMMA(I,J) gamma[ns*((J)-1)+(I)-1] #define LAMBDA(I,J) lambda[ns*((J)-1)+(I)-1] #define RHO(I,J) rho[ns*((J)-1)+(I)-1] int main(void) { double ds, dt, tmat, x; Integer i, igreek, j, ns, nt, exit_status; double *delta, *f, *gamma, *lambda, q[3], r[3], *rho, *s, sigma[3], *t, *theta, smin, smax, tmin, tmax; Boolean gprnt[5]={TRUE, TRUE, TRUE, TRUE, TRUE}, tdpar[3]; const char *gname[5]={"Theta", "Delta", "Gamma", "Lambda", "Rho"}; NagError fail; /* Skip heading in data file */ Vscanf("%*[^\n] "); exit_status = 0; /* Read problem parameters */ Vscanf("%lf", &x); Vscanf("%lf", &tmat); Vscanf("%lf", &r[0]); Vscanf("%lf", &q[0]); Vscanf("%lf", &sigma[0]); Vscanf("%ld%ld", &ns, &nt); Vscanf("%lf%lf", &smin, &smax); Vscanf("%lf%lf", &tmin, &tmax); /* Allocate memory */ if ( !(s = NAG_ALLOC(ns, double)) || !(t = NAG_ALLOC(nt, double)) || !(f = NAG_ALLOC(ns*nt, double)) || !(theta = NAG_ALLOC(ns*nt, double)) || !(delta = NAG_ALLOC(ns*nt, double)) || !(gamma = NAG_ALLOC(ns*nt, double)) || !(lambda = NAG_ALLOC(ns*nt, double)) || !(rho = NAG_ALLOC(ns*nt, double)) ) { Vprintf("Allocation failure\n"); exit_status = 1; goto END; } INIT_FAIL(fail); Vprintf("d03ndc Example Program Results\n\n"); /* Set up input parameters for d03ncc */ s[0] = smin; s[ns-1] = smax; t[0] = tmin; t[nt-1] = tmax; tdpar[0] = FALSE; tdpar[1] = FALSE; tdpar[2] = FALSE; ds = (s[ns-1]-s[0])/(ns-1.0); dt = (t[nt-1]-t[0])/(nt-1.0); /* Loop over times */ for (j=1; j<=nt; j++) { t[j-1] = t[0] + (j-1)*dt; /* Loop over stock prices */ for (i=1; i<=ns; i++) { s[i-1] = s[0] + (i-1)*ds; /* Evaluate analytic solution of Black-Scholes equation */ d03ndc(Nag_AmericanCall, x, s[i-1], t[j-1], tmat, tdpar, r, q, sigma, &F(i,j), &THETA(i,j), &DELTA(i,j), &GAMMA(i,j), &LAMBDA(i,j), &RHO(i,j), &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from d03ndc.\n%s\n", fail.message); exit_status = 1; goto END; } } } /* Output option values */ Vprintf("\n"); Vprintf("Option Values\n"); Vprintf("-------------\n"); Vprintf(" Stock Price | Time to Maturity (months)\n"); Vprintf(" | "); for (i=0; i