/* nag_rand_bb_inc (g05xdc) Example Program. * * Copyright 2013 Numerical Algorithms Group. * * Mark 24, 2013. */ #include #include #include #include #include #include int get_z(Nag_OrderType order, Integer ntimes, Integer d, Integer a, Integer npaths, double * z, Integer pdz); void display_results(Integer npaths, Integer ntimes, double *St, double *analytic); #define CHECK_FAIL(name,fail) if(fail.code != NE_NOERROR) { \ printf("Error calling %s\n%s\n",name,fail.message); exit_status=-1; goto END;} int main(void) { Integer exit_status = 0; NagError fail; /* Scalars */ double t0, tend, r, S0, sigma; Integer a, d, pdb, pdc, pdz, nmove, npaths, ntimesteps, i,p ; /* Arrays */ double *b = 0, c[1], *t = 0, *rcomm = 0, *diff = 0, *times = 0, *z = 0, *analytic = 0, *St = 0; Integer *move = 0; INIT_FAIL(fail); /* We wish to solve the stochastic differential equation (SDE) * dSt = r * St * dt + sigma * St * dXt * where X is a one dimensional Wiener process. This means we have * a = 0 * d = 1 * c = 1 * We now set the other parameters of the SDE and the Euler-Maruyama scheme * * Initial value of the process */ S0 = 1.0; r = 0.05; sigma = 0.12; /* Number of paths to simulate */ npaths = 3; /* The time interval [t0,T] on which to solve the SDE */ t0 = 0.0; tend = 1.0; /* The time steps in the discretization of [t0,T] */ ntimesteps = 20; /* Other bridge parameters */ c[0] = 1.0; a = 0; nmove = 0; d = 1; pdz = d*(ntimesteps+1-a); pdb = d*(ntimesteps+1); pdc = d; /* Allocate memory */ if ( !( t = NAG_ALLOC((ntimesteps), double)) || !( times = NAG_ALLOC((ntimesteps), double)) || !( rcomm = NAG_ALLOC((12 * (ntimesteps + 1)), double)) || !( diff = NAG_ALLOC(d, double)) || !( z = NAG_ALLOC(pdz * npaths, double)) || !( b = NAG_ALLOC(pdb * npaths, double)) || !( move = NAG_ALLOC(nmove, Integer)) || !( St = NAG_ALLOC(npaths * (ntimesteps+1), double)) || !( analytic = NAG_ALLOC(npaths, double)) ) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Fix the time points at which the bridge is required */ for (i=0; i