Ejemplo n.º 1
0
/* European Call/Put price with Bates model */ 
int MCAlfonsiBates(double S0, NumFunc_1  *p, double t, double r, double divid, double V0,double k,double theta,double sigma,double rho,double mu_jump,double gamma2,double lambda, long nb, int M,int generator, double confidence,int flag_cir,double *ptprice, double *ptdelta, double *pterror_price, double *pterror_delta , double *inf_price, double *sup_price, double *inf_delta, double *sup_delta)
{
  long i,ipath;
  double price_sample, delta_sample, mean_price, mean_delta, var_price, var_delta;
  int init_mc;
  int simulation_dim;
  double alpha, z_alpha;
  double  S_T, g1,g2;
  double h = t /(double)M;
  double sqrt_h = sqrt(h);
  double *X1a,*X2a,*X3a,*X4a;
  double w_t_1,w_t_2;
  double aaa=k*theta;
  double Kseuil,aux; 
  double mu=r-divid;
  double prev_jump=0;
  double next_jump;
  double h2,sqrt_h2,jump;
  double correction_mg;
  double mu2,sg_jump;

  sg_jump=sqrt(gamma2);
  correction_mg=lambda*(exp(mu_jump+0.5*gamma2)-1);
  mu2=mu-correction_mg;
  if(flag_cir==1)
    Kseuil=MAX((0.25*SQR(sigma)-aaa)*psik(h*0.5,k),0.);
  else
    {
      if (k==0)
        Kseuil=1;
      else Kseuil=(exp(k*h)-1)/(h*k);
      if (sigma*sigma <= 4*k*theta/3) {
    
        Kseuil=Kseuil*sigma*sqrt(k*theta-sigma*sigma/4)/sqrt(2);
      }
      if (sigma*sigma > 4*k*theta/3 && sigma*sigma <= 4*k*theta){
        aux=(0.5*sigma*sqrt(3+sqrt(6))+sqrt(sigma*sigma/4 - k*theta+sigma*sqrt(-sigma*sigma/4+ k*theta)/sqrt(2)));
        Kseuil=Kseuil*SQR(aux);
      }
      if (sigma*sigma > 4*k*theta){
        aux=0.5*sigma*sqrt(3+sqrt(6))+ sqrt(sigma*sqrt(sigma*sigma/4- k*theta)/sqrt(2));
        Kseuil=Kseuil*(sigma*sigma/4 - k*theta + SQR(aux));
      }
      if (sigma*sigma == 4*k*theta) Kseuil=0;
    }
 
  /*Memory allocation*/
  X1a = malloc(sizeof(double)*(M+1));
  X2a = malloc(sizeof(double)*(M+1));
  X3a = malloc(sizeof(double)*(M+1));
  X4a = malloc(sizeof(double)*(M+1));
 
  /* Value to construct the confidence interval */
  alpha= (1.- confidence)/2.;
  z_alpha= pnl_inv_cdfnor(1.- alpha);

  /*Initialisation*/
  mean_price= 0.0;
  mean_delta= 0.0;
  var_price= 0.0;
  var_delta= 0.0;

  /* Size of the random vector we need in the simulation */
  simulation_dim= M;

  /* MC sampling */
  init_mc= pnl_rand_init(generator, simulation_dim,nb);
  /* Test after initialization for the generator */
  if(init_mc == OK)
    {
      for(ipath= 1;ipath<= nb;ipath++)
        {
          /* Begin of the N iterations */
          X1a[0]=V0; X2a[0]=0; X3a[0]=S0;  X4a[0]=0;
          next_jump=-log(pnl_rand_uni(generator))/lambda;
          for(i=1 ; i<=M ; i++) 
            {
              /*Discrete law obtained by matching of first
                five moments of a gaussian r.v.*/
              if (next_jump > (double)i*h)
                {
                  if(flag_cir==1)
                    g1=DiscLawMatch5(generator);
                  else
                    g1=DiscLawMatch7(generator);
                  w_t_1=sqrt_h*g1;
              
                  g2= pnl_rand_normal(generator);
                  w_t_2=sqrt_h*g2;
              
                  X1a[i]=X1a[i-1];
                  X2a[i]=X2a[i-1];
                  X3a[i]=X3a[i-1];
                  X4a[i]=X4a[i-1];
                  fct_Heston(&X1a[i],&X2a[i],&X3a[i],&X4a[i],
                             h,w_t_1,w_t_2,aaa,k,sigma,mu2,rho,Kseuil,generator,flag_cir);
                }
              else
                {
                  h2=next_jump-(i-1)*h;
                  sqrt_h2=sqrt(h2);
                  X1a[i]=X1a[i-1];
                  X2a[i]=X2a[i-1];
                  X3a[i]=X3a[i-1];
                  X4a[i]=X4a[i-1];
                  while (next_jump <= (double)i*h)
                    {

                      if(flag_cir==1)
                        g1=DiscLawMatch5(generator);
                      else
                        g1=DiscLawMatch7(generator);
                      w_t_1=sqrt_h2*g1;
              
                      g2= pnl_rand_normal(generator);
                      w_t_2=sqrt_h2*g2;
                      fct_Heston(&X1a[i],&X2a[i],&X3a[i],&X4a[i],
                                 h2,w_t_1,w_t_2,aaa,k,sigma,mu2,rho,Kseuil,generator,flag_cir);
                      prev_jump=next_jump;
                      next_jump=next_jump-log(pnl_rand_uni(generator))/lambda;
                      h2=next_jump-prev_jump;
                      sqrt_h2=sqrt(h2);
                      jump= exp(mu_jump+sg_jump*pnl_rand_normal(generator));
                      X3a[i]=   X3a[i]*jump;
                    }

                  h2=i*h-prev_jump;
                  sqrt_h2=sqrt(h2);
                   

                  if(flag_cir==1)
                    g1=DiscLawMatch5(generator);
                  else
                    g1=DiscLawMatch7(generator);
                  w_t_1=sqrt_h2*g1;
              
                  g2= pnl_rand_normal(generator);
                  w_t_2=sqrt_h2*g2;
                  fct_Heston(&X1a[i],&X2a[i],&X3a[i],&X4a[i],
                             h2,w_t_1,w_t_2,aaa,k,sigma,mu2,rho,Kseuil,generator,flag_cir);
                }
            }
          
          /*Price*/
          S_T=X3a[M];
          price_sample=(p->Compute)(p->Par,S_T);
     
          /* Delta */
          if(price_sample >0.0)
            delta_sample=(S_T/S0);
          else  delta_sample=0.;

          /* Sum */
          mean_price+= price_sample;
          mean_delta+= delta_sample;

          /* Sum of squares */
          var_price+= SQR(price_sample);
          var_delta+= SQR(delta_sample);
        }
      /* End of the N iterations */
      
      /* Price estimator */
      *ptprice=(mean_price/(double)nb);
      *pterror_price= exp(-r*t)*sqrt(var_price/(double)nb-SQR(*ptprice))/sqrt((double)nb-1);
      *ptprice= exp(-r*t)*(*ptprice);
      
      /* Price Confidence Interval */
      *inf_price= *ptprice - z_alpha*(*pterror_price);
      *sup_price= *ptprice + z_alpha*(*pterror_price);
      
       
      /* Delta estimator */
      *ptdelta=exp(-r*t)*(mean_delta/(double)nb);
      if((p->Compute) == &Put) 
        *ptdelta *= (-1); 
      *pterror_delta= sqrt(exp(-2.0*r*t)*(var_delta/(double)nb-SQR(*ptdelta)))/sqrt((double)nb-1);
      
      /* Delta Confidence Interval */
      *inf_delta= *ptdelta - z_alpha*(*pterror_delta);
      *sup_delta= *ptdelta + z_alpha*(*pterror_delta);
    }
  
  /*Memory desallocation*/
  free(X1a);
  free(X2a);
  free(X3a);
  free(X4a);
  
  return init_mc;
}
Ejemplo n.º 2
0
void run_serial(  int nx, int ny, int nz, int ndiag, const vector<diag_c>& diag,
                  int ndimen, int id, int jd, int kd,
                  int nang, int nmom, int noct, int ng,
                  int ich, int ichunk,
                  int jlo, int klo, int jhi, int khi, int jst, int kst,
                  bool firsty, bool lasty, bool firstz, bool lastz,
                  int nnested, int src_opt, int fixup,
                  double hi, double vdelt )
{

	typedef Kokkos::Serial device_type;
	typedef Kokkos::View<double*, Kokkos::LayoutLeft, device_type> serial_view_t_1d;
	typedef Kokkos::View<double**, Kokkos::LayoutLeft, device_type> serial_view_t_2d;
	typedef Kokkos::View<double***, Kokkos::LayoutLeft, device_type> serial_view_t_3d;
	typedef Kokkos::View<double****, Kokkos::LayoutLeft, device_type> serial_view_t_4d;
	typedef Kokkos::View<double*****, Kokkos::LayoutLeft, device_type> serial_view_t_5d;
	typedef Kokkos::View<double******, Kokkos::LayoutLeft, device_type> serial_view_t_6d;
	typedef Kokkos::View<double*, Kokkos::LayoutStride> serial_view_t_1d_s;

//   cout << " a " << endl;
  
	int cmom = nmom * nmom;
	int d1 = nang; // TEST, (timedep == 1 => 		d1 = nang; d2 = nx; d3 = ny; d4 = nz )
	int d2 = nx; // TEST
	int d3 = ny; // TEST
	int d4 = nz; // TEST

//   cout << " b " << endl;
  
  serial_view_t_1d hj( "hj", nang );
  serial_view_t_1d hk( "hk", nang );
  serial_view_t_1d mu( "mu", nang );
	serial_view_t_1d w( "w", nang );
  serial_view_t_6d qim( "qim", nang, nx, ny, nz, noct, ng );
	serial_view_t_3d psii( "psii", nang, ny, nz );
	serial_view_t_3d psij( "psij", nang, ichunk, nz );
	serial_view_t_3d psik( "psik", nang, ichunk, ny );
  serial_view_t_4d qtot( "qtot", cmom, nx, ny, nz );
	serial_view_t_2d ec( "ec", nang, cmom );
  serial_view_t_4d ptr_in( "ptr_in", d1, d2, d3, d4 );
  serial_view_t_4d ptr_out( "ptr_out", d1, d2, d3, d4 );
  serial_view_t_4d dinv( "dinv", nang, nx, ny, nz );
	serial_view_t_3d flux( "flux", nx, ny, nz );
  serial_view_t_4d fluxm( "fluxm", cmom-1, nx, ny, nz );
 	serial_view_t_3d jb_in( "jb_in", nang, ichunk, nz );
  serial_view_t_3d jb_out( "jb_out", nang, ichunk, nz );
	serial_view_t_3d kb_in( "kb_in", nang, ichunk, ny );
	serial_view_t_3d kb_out( "kb_out", nang, ichunk, ny );
	serial_view_t_1d wmu( "wmu", nang );
	serial_view_t_1d weta( "weta", nang );
	serial_view_t_1d wxi( "wxi", nang );
  serial_view_t_3d flkx( "flkx", nx+1, ny, nz );
  serial_view_t_3d flky( "flky", nx, ny+1, nz );
  serial_view_t_3d flkz( "flkz", nx, ny, nz+1 );
  serial_view_t_3d t_xs( "t_xs", nx, ny, nz );

//   cout << " c " << endl;
  
  for (int ii = 0; ii < n_test_iter; ii++) {
    time(&timer_start);
    
    for (int oct = 0; oct < noct; oct++) {
      for (int g = 0; g < ng; g++) {	
        dim3_sweep< device_type, serial_view_t_1d, serial_view_t_2d,
                  serial_view_t_3d, serial_view_t_4d, serial_view_t_5d,
                  serial_view_t_6d, serial_view_t_1d_s >
                ( ichunk, firsty, lasty, firstz, lastz, nnested,
                  nx, hi, hj, hk, ndimen, ny, nz, ndiag, diag,
                  cmom, nang, mu, w, noct,
                  src_opt, ng, qim,
                  fixup,
                  ich, id, d1, d2, d3, d4, jd, kd, jlo, klo, oct, g,
                  jhi, khi, jst, kst, psii, psij, psik, qtot, ec, vdelt,
                  ptr_in, ptr_out, dinv, flux, fluxm, jb_in, jb_out,
                  kb_in, kb_out, wmu, weta, wxi, flkx, flky, flkz, t_xs );
      }
    }
    
    time(&timer_end);
    std::cout << " ii " << ii << " elapsed time " << difftime(timer_end, timer_start) << std::endl;
  }
}
Ejemplo n.º 3
0
void run( const int N, const int M, const int L, const int hyper_threads, const int vector_lanes,
                  const int nx, const int ny, const int nz, const int ichunk,
                  const int nang, const int noct, const int ng, const int nmom, const int cmom,
                  const vector<diag_c>& diag )
{
  
	typedef typename Kokkos::DefaultExecutionSpace device_t;
	typedef TeamPolicy<device_t> team_policy_t;
	typedef View<double*, device_t> view_1d_t;
	typedef View<double**, Kokkos::LayoutLeft, device_t> view_2d_t;
	typedef View<double***, Kokkos::LayoutLeft, device_t> view_3d_t;
	typedef View<double****, Kokkos::LayoutLeft, device_t> view_4d_t;
	typedef View<double*****, Kokkos::LayoutLeft, device_t> view_5d_t;
	typedef View<double******, Kokkos::LayoutLeft, device_t> view_6d_t;
	typedef View<double*******, Kokkos::LayoutLeft, device_t> view_7d_t;


	int id = 1;
	int ich = 1;
	int jlo = 0;
	int jhi = ny-1;
	int jst = 1;
	int jd = 2;
	int klo = 0;
	int khi = nz-1;
	int kst = 1;
	int kd = 2;
	double hi = c1;
	
	Kokkos::initialize();
	Kokkos::DefaultExecutionSpace::print_configuration(cout);
	
	view_4d_t psii( "psii", nang, ny, nz, ng );
	view_4d_t psij( "psij", nang, ichunk, nz, ng );
	view_4d_t psik( "psik", nang, ichunk, ny, ng );
	view_4d_t jb_in( "jb_in", nang, ichunk, ny, ng ); // jb_in(nang,ichunk,nz,ng)
	view_4d_t kb_in( "kb_in", nang, ichunk, ny, ng ); // kb_in(nang,ichunk,nz,ng)
	view_6d_t qim( "qim", nang, nx, ny, nz, noct, ng ); // qim(nang,nx,ny,nz,noct,ng)
  view_5d_t qtot( "qtot", cmom, nx, ny, nx, ng ); // qtot(cmom,nx,ny,nz,ng)
	view_2d_t ec( "ec", nang, cmom ); // ec(nang,cmom)
	view_1d_t mu( "mu", nang ); // mu(nang)
	view_1d_t w( "w", nang ); // w(nang)
	view_1d_t wmu( "wmu", nang ); // wmu(nang)
	view_1d_t weta( "weta", nang ); // weta(nang)
	view_1d_t wxi( "wxi", nang ); // wxi(nang)
	view_1d_t hj( "hj", nang ); // hj(nang)
	view_1d_t hk( "hk", nang ); // hk(nang)
	view_1d_t vdelt( "vdelt", ng ); // vdelt(ng)
	view_6d_t ptr_in( "ptr_in", nang, nx, ny, nz, noct, ng ); // ptr_in(nang,nx,ny,nz,noct,ng)
	view_6d_t ptr_out( "ptr_out", nang, nx, ny, nz, noct, ng ); // ptr_out(nang,nx,ny,nz,noct,ng)
	view_4d_t flux( "flux", nx, ny, nz, ng ); // flux(nx,ny,nz,ng)
	view_5d_t fluxm( "fluxm", cmom-1, nx, ny, nz, ng ); //fluxm(cmom-1,nx,ny,nz,ng)
	view_2d_t psi( "psi", nang, M );
	view_2d_t pc( "pc", nang, M );
	view_4d_t jb_out( "jb_out", nang, ichunk, nz, ng );
	view_4d_t kb_out( "kb_out", nang, ichunk, ny, ng );
	view_4d_t flkx( "flkx", nx+1, ny, nz, ng );
	view_4d_t flky( "flky", nx, ny+1, nz, ng );
	view_4d_t flkz( "flkz", nx, ny, nz+1, ng );
  view_3d_t hv( "hv", nang, 4, M ); // hv(nang,4,M)
  view_3d_t fxhv( "fxhv", nang, 4, M ); // fxhv(nang,4,M)
  view_5d_t dinv( "dinv", nang, nx, ny, nz, ng ); // dinv(nang,nx,ny,nz,ng)
  view_2d_t den( "den", nang, M ); // den(nang,M)
  view_4d_t t_xs( "t_xs", nx, ny, nz, ng ); // t_xs(nx,ny,nz,ng)
   	  
  const team_policy_t policy( N, hyper_threads, vector_lanes );
  
  for (int ii = 0; ii < n_test_iter; ii++) {
    Kokkos::Impl::Timer timer;
    
    for (int oct = 0; oct < noct; oct++) {
      parallel_for( policy, dim3_sweep2<  team_policy_t,
                                          view_1d_t, view_2d_t, view_3d_t, view_4d_t,
                                          view_5d_t, view_6d_t, view_7d_t >
                                        ( M, L,
                                          ng, cmom, noct,
                                          nx, ny, nz, ichunk,
                                          diag,
                                          id, ich, oct,
                                          jlo, jhi, jst, jd,
                                          klo, khi, kst, kd,
                                          psii, psij, psik,
                                          jb_in, kb_in,
                                          qim, qtot, ec,
                                          mu, w,
                                          wmu, weta, wxi,
                                          hi, hj, hk,
                                          vdelt, ptr_in, ptr_out,
                                          flux, fluxm, psi, pc,
                                          jb_out, kb_out,
                                          flkx, flky, flkz,
                                          hv, fxhv, dinv,
                                          den, t_xs ) );
    }// end noct
    
    std::cout << " ii " << ii << " elapsed time " << timer.seconds() << std::endl;
  } // end n_test_iter	
	
	Kokkos::finalize();
}
Ejemplo n.º 4
0
//gets the parameters that the user entered
void get_parameters(void) {
  int i, index = 0, isSet = FALSE, parInt, *tempInts;
  char *par;
  c = '\0';
  parameters.first = NULL;
  parameters.second = NULL;
  parameters.result = NULL;
  notMinus1 = TRUE;
  setOK = TRUE;
  sleep(10);
  while (c != '\n' && c != EOF) {
    par = psik();
    //pulls the parameters that are sets
    for (i = 0; i < 6; i++) {
      if (*par == sets[i].name)
      {
        if (parameters.result != NULL)
        {
          fprintf(stderr, "Too many Sets\n");
          setOK = FALSE;
          return;
        }
        if (*(par + 1) != '\0')
        {
          fprintf(stderr, "Wrong set name!\n");
          setOK = FALSE;
          return;
        }

        if (parameters.first == NULL)
          parameters.first = (set *)(sets[i].s);
        else if (parameters.second == NULL)
          parameters.second = (set *)(sets[i].s);
        else if (parameters.result == NULL)
          parameters.result = (set *)(sets[i].s);

        isSet = TRUE;
      }
    }
    if ((i == 12) && (isSet != TRUE) && (isalpha(*par)))
    {
      fprintf(stderr, "Wrong set name!\n");
      setOK = FALSE;
      while (c != '\n')
        c= getchar();
      return;
    }
    //pulls the parameters that are numbers
    if(!isSet)
    {
      tempInts = realloc(ints, (index + 1) * sizeof(int));
      if (tempInts != NULL)
        ints = tempInts;
      parInt = atof(par);
      *(ints + index) = parInt;
      index++;
      if (parInt == -1)
        notMinus1 = FALSE;
    }
    isSet = FALSE;
  }
}