double Potential_interpolation_2d::operator()(double rho, double z) const
{
    Point p(rho, z);
    vector<pair<Point, double> > ncoords;
    double norm = natural_neighbor_coordinates_2(
                      _dt, p, back_inserter(ncoords)).second;
    pair<double, bool> res = quadratic_interpolation(
                                 ncoords.begin(), ncoords.end(), norm, p,
                                 Data_access_value(_value_map),
                                 Data_access_grad(_grad_map),
                                 GradTraits());
    if (res.second)
    {
        return res.first;
    }
    else
    {
        return linear_interpolation(
                   ncoords.begin(), ncoords.end(), norm,
                   Data_access_value(_value_map));
    }
}
int main()
{
	/*Option parameters*/
	double tt = 1;
	double H = 90.0;
	double K = 100.0;
	double r_premia = 10;
	double spot = 95.0;
	double spot_step = 5;
	uint spot_iterations = 21;

	/*Heston model parameters*/
	double v0 = 0.1; /* initial volatility */
	double kappa = 2.0; /*heston parameter, mean reversion*/
	double theta = 0.2; /*heston parameter, long-run variance*/
	double sigma = 0.2; /*heston parameter, volatility of variance*/
	double omega = sigma; /*sigma is used everywhere, omega - in the variance tree*/
	double rho = 0.5; /*heston parameter, correlation*/

	/*method parameters*/
	uint Nt = 100; /*number of time steps*/
	uint M = uint(pow(2, 12)); /*space grid. should be a power of 2*/
	double L = 3; /*scaling coefficient*/

	int allocation = memory_allocation(Nt, M, M);
	if (allocation == MEMORY_ALLOCATION_FAILURE)
	{
		return MEMORY_ALLOCATION_FAILURE;
	}
	else
	{
		double start_time = clock() / double(CLOCKS_PER_SEC);
		compute_price(tt, H, K, r_premia, v0, kappa, theta, sigma, rho, L, M, Nt);
		double end_time = clock() / double(CLOCKS_PER_SEC);
		for (int j = find_nearest_right_price_position(1.5*K,M); j >= find_nearest_left_price_position(H, M); j--)
		{
			printf("ba_price %f Price %f + %f i\n", ba_prices[j], F_next[j][0].r, F_next[j][0].i);
		}
		for (uint i = 0; i < spot_iterations; i++)
		{
			printf("interp ba_price %f Price %f\n", spot + i*spot_step, quadratic_interpolation(spot + i*spot_step, M));
		}
		printf("Time elapsed (in sedonds): %f\n", end_time - start_time);

		free_memory(Nt, M, M);
		getchar();
		return OK;
	}
	/*
	double mc_price = 0;
	int trajectories = 100000;
	for (int trajectory = 0; trajectory < trajectories; trajectory++)
	{
		if(trajectory % 1000 == 0)
			printf("trajectories left:  %d \n", trajectories - trajectory);
		mc_price += generate_heston_trajectory_return(tt, spot, H, K, r_premia, v0, kappa, theta, sigma, rho, 10000);
	}
	mc_price = mc_price / double(trajectories);
	printf("avg_price %f", mc_price);
	getchar();*/
}
Exemplo n.º 3
0
int CarrMethod_onStrikeList(PnlVect *K,
                            PnlVect * Price,
                            double S0,
                            double T,
                            double CallPut,
                            double r,
                            double divid,
                            double sigma,
                            Levy_diffusion * Model)
  
{
  PnlVect * StrikeFFT,*PriceFFT;
  int n,error,ancestor,current,next;
  double delta;
  double strike_min = GET(K, 0);
  double strike_max = GET(K,K->size-1);
  //double nbr_strike = K->size;
  double strike_bnd = 2*MAX(log(strike_max/S0),fabs(log(strike_min/S0)));//0.25*log(strike_max/strike_min)/nbr_strike;
  // 2 adjust heuristic parameter, to find four points
  // in fft in which all real strike value from K

  // Stored data for homogen grid in strike
  StrikeFFT=pnl_vect_create(0);
  PriceFFT=pnl_vect_create(0);
  
  error=CarrMethod_VectStrike(StrikeFFT,PriceFFT,
                              S0,T,strike_bnd,CallPut,r,divid,sigma,
                              Model,
                              &Levy_diffusion_ln_characteristic_function_with_cast);
  ancestor=0;
  current=0;
  next=1;
  n=0;
  while(n<K->size)
    {
      if((GET(StrikeFFT,current)<=GET(K,n))&&(GET(StrikeFFT,next)>GET(K,n)))
        {
          quadratic_interpolation(GET(PriceFFT,ancestor),
                                  GET(PriceFFT,current),
                                  GET(PriceFFT,next),
                                  GET(StrikeFFT,ancestor),
                                  GET(StrikeFFT,current),
                                  GET(StrikeFFT,next),
                                  GET(K,n),
                                  &LET(Price,n),
                                  &delta);
          n++;
        }
      else
        {
          ancestor=current;//not ++ for the first step
          current++;
          next++;
          if(next>StrikeFFT->size)
            PNL_ERROR(" Carr method domain size is too small for interpolation after FFT !","carr.c ");
 
        }
    }
  LET(Price,n)=GET(PriceFFT,PriceFFT->size);
  return error;
}