GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
get_option_payoffs.cu
Go to the documentation of this file.
1 
4 #include "rng.c"
5 
6 const int CALL = 0;
7 const int PUT = 1;
8 const int JMAX = 10;
9 const double SQRT2PI = 2.506628274631; // sqrt(2*pi)
10 
11 
13 __device__
14 double get_payoff( double S,
15  double K,
16  int option_type
17  ) {
18  switch (option_type) {
19  case CALL:
20  return ( S>K ? S-K : 0.0 );
21  case PUT:
22  return ( S>K ? 0.0 : K-S );
23  }
24  return 0.0;
25 }
26 
27 
28 
29 __device__
30 double normal_pdf( double x, double mu, double sd )
31 {
32  double z = (x-mu)/sd;
33  return exp( -0.5*z*z ) / ( sd * SQRT2PI );
34 }
35 
36 
37 
47 
48 
49 
50 
51 
52 __global__
53 void get_option_payoffs( double * payoff_d,
54  double * state_d,
55  unsigned int * seed_d,
56  const double * parms,
57  const double * strike,
58  const int * option_type,
59  int noption,
60  double T,
61  int nsim
62  )
63 
64 {
65  int tid = blockIdx.x*blockDim.x + threadIdx.x;
66  int nthreads = blockDim.x*gridDim.x;
67 
68  double r = parms[ 0];
69  double lamz = parms[ 1];
70  double az = parms[ 2];
71  double bz = parms[ 3];
72  double cz = parms[ 4];
73  double dz = parms[ 5];
74  double ez = parms[ 6];
75  double wz = parms[ 7];
76  double lamy = parms[ 8];
77  double ay = parms[ 9];
78  double by = parms[10];
79  double cy = parms[11];
80  double dy = parms[12];
81  double ey = parms[13];
82  double wy = parms[14];
83  double muJ = parms[15];
84  double sigJ = parms[16];
85  double xi = exp( muJ + 0.5* sigJ*sigJ ) - 1.0;
86 
87  for ( int i=tid; i<nsim; i+=nthreads) {
88 
89  unsigned int seed[4];
90  get_seed( seed, seed_d, i, nsim );
91 
92  double logS = state_d[0*nsim+i];
93  double hz = state_d[1*nsim+i];
94  double hy = state_d[2*nsim+i];
95 
96  for (int t=0; t<T; t++) {
97  double R, m, z, y, z1, z2, u, nj, pj;
98  m = r + (lamz - 0.5)*hz + (lamy - xi)*hy;
99 
100  random_normal( &z1, &z2, seed );
101  z = sqrt(hz)*z1;
102 
103  u = random_uniform( seed );
104  nj = JMAX;
105  for ( int j=0; j<JMAX; j++) {
106  u -= pj = (j==0 ? exp(-hy) : pj*hy/j );
107  if (u<=0) {
108  nj = j;
109  break;
110  }
111  }
112  y = nj*muJ + sqrt(nj*sigJ*sigJ)*z2;
113  R = m + y + z; // log return
114  logS += R; // log price
115 
116  hy = wy + by*hy + ay/hz*(z-cy*hz)*(z-cy*hz) + dy*(y - ey)*(y - ey );
117  hz = wz + bz*hz + az/hz*(z-cz*hz)*(z-cz*hz) + dz*(y - ez)*(y - ez );
118 
119  if ( hz < 0.000004 ) { hz = 0.000004; }
120  if ( hy < 0.00 ) { hy = 0.00; }
121  if ( hy > 1.00 ) { hy = 1.00; }
122  }
123 
124  for (int j=0; j<noption; j++) {
125  payoff_d[j*nsim+i] = get_payoff( exp(logS), strike[j], option_type[j] );
126  }
127  state_d[0*nsim+i] = logS;
128  state_d[1*nsim+i] = hz;
129  state_d[2*nsim+i] = hy;
130  put_seed( seed, seed_d, i, nsim );
131  }
132 }