/*We use the Cauchy Gourat theorem to compute the derivatives of the double(Mellin+Laplace) transform */
static dcomplex dermellin(dcomplex l, double sg, double r, int nummom)
{
  dcomplex term, cv, mu;
  int i;
  double r0,sumr, sumi/*,x[NPOINTS_FUSAITAGL+1],w[NPOINTS_FUSAITAGL+1]*/;
  double v;
  double *x,*w;

  x=malloc((NPOINTS_FUSAITAGL+1)*sizeof(double));
  w=malloc((NPOINTS_FUSAITAGL+1)*sizeof(double));

  sumr=0.0;
  sumi=0.0;
  
  gauleg(0, 2*M_PI, x, w,NPOINTS_FUSAITAGL);
	
  v   = 2*r/(sg*sg)-1.0;
  cv = Complex(v,0.0);
  mu = Csqrt(Cadd(Complex(v*v,0), RCmul(2.0,l)));
  r0 = Creal(RCmul(0.5,Csub(mu,cv)));	
  if(r0>1.0) r0=0.25;


  for (i=1;i<=NPOINTS_FUSAITAGL;i++) 
    {
    term = 	RCmul(pow(r0,nummom), Cexp(Complex(0.0, nummom*x[i])));
    sumr += w[i]*Creal(Cdiv(mellintransform(l, RCmul(r0, Cexp(Complex(0.0, x[i]))),  sg, r), term));
    sumi += w[i]*Cimag(Cdiv(mellintransform(l, RCmul(r0, Cexp(Complex(0.0, x[i]))),  sg, r), term));
    }

  free(x);
  free(w);
  
  return Complex(exp(factln(nummom))*sumr/(2.0*M_PI),exp(factln(nummom))*sumi/(2.0*M_PI));
}
Exemplo n.º 2
0
static double charact_funct1(double uu)
{
  double a,b,rs,rsp,sig,tau,tpf1,tpf2, f10, c0, d0;
  dcomplex g,z,w,tp1,tp2,DD,CN,ans,d,expo;


  tau=T;
  a=k*teta;
  rs=rho*sigma;
  rsp=rs*uu;
  sig=sigma*sigma;

  b=k+lambda-rs;
  if(uu==0)
    {
      if(b==0)
	{
	  c0=a*T*T/4.0;
          d0=T/2.0;
	}
      else
        {
          c0=0.5*a*(exp(-b*T)+b*T - 1.0)/b/b;
          d0=0.5*(1.0-exp(-b*T))/b;
	} 
      f10=log(S/K)+(r-divid)*T+c0+d0*v;

      return f10; 
    }
  z=Complex(-b,rsp);
  z=Cmul(z,z);
  w=RCmul(sig,Complex(-uu*uu,uu));
  d=Csqrt(Csub(z,w));
  tp1=Complex(d.r+b,d.i-rsp);
  tp2=Complex(-d.r+b,-d.i-rsp);
  g=Cdiv(tp2,tp1);

 expo=Cexp(RCmul(-tau,d));
     DD=Csub(Complex(1,0),expo);
     DD=Cdiv(DD,Csub(Complex(1,0),Cmul(g,expo)));
     DD=Cmul(DD,RCmul(1.0/sig,tp2));

  CN=Csub(Cmul(g,expo), Complex(1,0));
  CN=Cdiv(CN,Csub(g, Complex(1,0) ));
  tpf1=a*(tau*tp2.r-2.0*Clog(CN).r)/sig;
  tpf2=a*(tau*tp2.i-2.0*Clog(CN).i)/sig;

  tpf2+=(r-divid)*uu*tau;
  ans=Complex(tpf1+v*DD.r,tpf2+v*DD.i+uu*log(S));
  ans=Cmul(Cexp(ans),Cexp(Complex(0,-uu*log(K))));
  ans=Cdiv(ans,Complex(0,uu));
  
  return ans.r;
}
Exemplo n.º 3
0
void ComplexLUBackSubst(pcomplex **a, int n, int *indx, pcomplex *b)
{
	int			i, ip, j,
				ii = -1;
	pcomplex		sum;

	for (i = 0; i < n; i++) {
		ip = indx[i];
		sum = b[ip];
		b[ip] = b[i];
		if (ii >= 0) {
			for (j = ii; j <= i - 1; j++)
				sum = Csub(sum, Cmul(a[i][j], b[j]));
		} else if ((sum.re != 0.0) || (sum.im != 0.0))
			ii = i;
		b[i] = sum;
	}
	
	for (i = n - 1; i >= 0; i--) {
		sum = b[i];
		for (j = i + 1; j < n; j++)
			sum = Csub(sum, Cmul(a[i][j], b[j]));
		b[i] = Cdiv(sum, a[i][i]);
	}
}
/*Computation the double(Mellin+Laplace) transform of the density of arithmetic average */
static dcomplex mellintransform(dcomplex l, dcomplex n,  double sg, double r)
{

  dcomplex   mu,nterm1, nterm2, nterm3, dterm1, dterm2;
  dcomplex num, den, cv,cost;
  double v;

  v= 2*r/(sg*sg)-1.0;
  cv =Complex(v,0.0);
  mu = Csqrt(Cadd(Complex(v*v,0), RCmul(2.0,l)));

  cost=RCmul(log(2.0/(sg*sg)), n);
  
  nterm1 =Clgamma(Cadd(n,CONE));
  nterm2 =Clgamma(Cadd(RCmul(0.5, Cadd(mu,cv)),CONE));
  nterm3 =Clgamma(Csub(RCmul(0.5, Csub(mu,cv)),n));
  num = Cadd(Cadd( nterm1,nterm2),nterm3);

  dterm1 =Clgamma(RCmul(0.5, Csub(mu,cv)));
  dterm2 =Clgamma(Cadd(Cadd(RCmul(0.5, Cadd(mu,cv)),CONE),n));

  den = Cadd( dterm1,dterm2);

  return Cdiv(Cexp(Cadd(Csub(num,den),cost)),l);
}
Exemplo n.º 5
0
void fixrts(float d[], int m)
{
	void zroots(fcomplex a[], int m, fcomplex roots[], int polish);
	int i,j,polish;
	fcomplex a[NMAX],roots[NMAX];

	a[m]=ONE;
	for (j=m-1;j>=0;j--)
		a[j]=Complex(-d[m-j],0.0);
	polish=1;
	zroots(a,m,roots,polish);
	for (j=1;j<=m;j++)
		if (Cabs(roots[j]) > 1.0)
			roots[j]=Cdiv(ONE,Conjg(roots[j]));
	a[0]=Csub(ZERO,roots[1]);
	a[1]=ONE;
	for (j=2;j<=m;j++) {
		a[j]=ONE;
		for (i=j;i>=2;i--)
			a[i-1]=Csub(a[i-2],Cmul(roots[j],a[i-1]));
		a[0]=Csub(ZERO,Cmul(roots[j],a[0]));
	}
	for (j=0;j<=m-1;j++)
		d[m-j] = -a[j].r;
}
Exemplo n.º 6
0
Arquivo: hypser.c Projeto: gnovak/bin
void hypser(fcomplex a, fcomplex b, fcomplex c, fcomplex z, fcomplex *series,
	fcomplex *deriv)
{
	void nrerror(char error_text[]);
	int n;
	fcomplex aa,bb,cc,fac,temp;

	deriv->r=0.0;
	deriv->i=0.0;
	fac=Complex(1.0,0.0);
	temp=fac;
	aa=a;
	bb=b;
	cc=c;
	for (n=1;n<=1000;n++) {
		fac=Cmul(fac,Cmul(aa,Cdiv(bb,cc)));
		deriv->r+=fac.r;
		deriv->i+=fac.i;
		fac=Cmul(fac,RCmul(1.0/n,z));
		*series=Cadd(temp,fac);
		if (series->r == temp.r && series->i == temp.i) return;
		temp= *series;
		aa=Cadd(aa,ONE);
		bb=Cadd(bb,ONE);
		cc=Cadd(cc,ONE);

	}
	nrerror("convergence failure in hypser");
}
Exemplo n.º 7
0
void hypdrv(float s, float yy[], float dyyds[])
{
	fcomplex z,y[3],dyds[3];

	y[1]=Complex(yy[1],yy[2]);
	y[2]=Complex(yy[3],yy[4]);
	z=Cadd(z0,RCmul(s,dz));
	dyds[1]=Cmul(y[2],dz);
	dyds[2]=Cmul(Csub(Cmul(Cmul(aa,bb),y[1]),Cmul(Csub(cc,
		Cmul(Cadd(Cadd(aa,bb),ONE),z)),y[2])),
		Cdiv(dz,Cmul(z,Csub(ONE,z))));
	dyyds[1]=dyds[1].r;
	dyyds[2]=dyds[1].i;
	dyyds[3]=dyds[2].r;
	dyyds[4]=dyds[2].i;
}
Exemplo n.º 8
0
///******************* Gamma-OU 1d Model*******************///
void phi_psi_gou1d(PnlVect *ModelParams, double t, dcomplex u, dcomplex *phi_i, dcomplex *psi_i)
{
    double lambda, alpha, beta;
    double a_t;
    dcomplex z0, z1, z2, z3;

    lambda = GET(ModelParams, 1);
    alpha  = GET(ModelParams, 2);
    beta    = GET(ModelParams, 3);

    a_t = exp(-lambda*t);

    z0 = RCmul(a_t, u);
    z1 = RCsub(alpha, z0);
    z2 = RCsub(alpha, u);
    z3 = RCmul(beta, Clog(Cdiv(z1, z2)));

    *phi_i = z3;
    *psi_i = z0;
}
Exemplo n.º 9
0
void phi_psi_cir1d(PnlVect *ModelParams, double t, dcomplex u, dcomplex *phi_i, dcomplex *psi_i)
{
    double lambda, theta, eta, SQR_eta;
    dcomplex z1, z2;
    double b_t, a_t;

    //x0     = GET(ModelParams, 0);
    lambda = GET(ModelParams, 1);
    theta  = GET(ModelParams, 2);
    eta    = GET(ModelParams, 3);
    SQR_eta = SQR(eta);

    a_t = exp(-lambda*t);

    if (lambda == 0.) b_t = t;
    else b_t = (1.-a_t)/lambda;

    z1 = RCsub(1., RCmul(2*SQR_eta*b_t, u));
    *phi_i = RCmul(-lambda*theta/(2*SQR_eta), Clog(z1));

    z1 = RCmul(a_t, u);
    z2 = RCsub(1., RCmul(2*SQR_eta*b_t, u));
    *psi_i = Cdiv(z1, z2);
}
Exemplo n.º 10
0
void frenel(float x, float *s, float *c)
{
	void nrerror(char error_text[]);
	int k,n,odd;
	float a,ax,fact,pix2,sign,sum,sumc,sums,term,test;
	fcomplex b,cc,d,h,del,cs;

	ax=fabs(x);
	if (ax < sqrt(FPMIN)) {
		*s=0.0;
		*c=ax;
	} else if (ax <= XMIN) {
		sum=sums=0.0;
		sumc=ax;
		sign=1.0;
		fact=PIBY2*ax*ax;
		odd=TRUE;
		term=ax;
		n=3;
		for (k=1;k<=MAXIT;k++) {
			term *= fact/k;
			sum += sign*term/n;
			test=fabs(sum)*EPS;
			if (odd) {
				sign = -sign;
				sums=sum;
				sum=sumc;
			} else {
				sumc=sum;
				sum=sums;
			}
			if (term < test) break;
			odd=!odd;
			n += 2;
		}
		if (k > MAXIT) nrerror("series failed in frenel");
		*s=sums;
		*c=sumc;
	} else {
		pix2=PI*ax*ax;
		b=Complex(1.0,-pix2);
		cc=Complex(1.0/FPMIN,0.0);
		d=h=Cdiv(ONE,b);
		n = -1;
		for (k=2;k<=MAXIT;k++) {
			n += 2;
			a = -n*(n+1);
			b=Cadd(b,Complex(4.0,0.0));
			d=Cdiv(ONE,Cadd(RCmul(a,d),b));
			cc=Cadd(b,Cdiv(Complex(a,0.0),cc));
			del=Cmul(cc,d);
			h=Cmul(h,del);
			if (fabs(del.r-1.0)+fabs(del.i) < EPS) break;
		}
		if (k > MAXIT) nrerror("cf failed in frenel");
		h=Cmul(Complex(ax,-ax),h);
		cs=Cmul(Complex(0.5,0.5),
			Csub(ONE,Cmul(Complex(cos(0.5*pix2),sin(0.5*pix2)),h)));
		*c=cs.r;
		*s=cs.i;
	}
	if (x < 0.0) {
		*c = -(*c);
		*s = -(*s);
	}
}
Exemplo n.º 11
0
int CarrMethod_VectStrike(PnlVect *K,
                          PnlVect * Price,
                          double S0,
                          double T,
                          double B,
                          double CallPut,
                          double r,
                          double divid,
                          double sigma,
                          void * Model,
                          dcomplex (*ln_phi)(dcomplex u,double t,void * model))
    
{
  int n;
  dcomplex dzeta,dzetaBS;
  double alpha=0.75;
  int Nlimit = 4*2048;//2048;
  //>> Should be even => use of real_fft
  //number of integral discretization steps
  double mone;//0.010;
  double Kstep=B*2/(Nlimit); // strike domain is (-B,B)
  double h  = M_2PI/(Nlimit*Kstep);
  //double B  = 0.5*(Nlimit)*Kstep; // strike domain is (-B,B)
  double vn = 0;
  dcomplex vn_minus_alpha_plus_uno = Complex(0,-(alpha+1));
  dcomplex i_vn_plus_alpha         = Complex(alpha,0);
  dcomplex uno_plus_alpha_plus_ivn =Complex(1+alpha,vn);
  PnlVectComplex * y = pnl_vect_complex_create(Nlimit);
 
  // Should become output
  pnl_vect_resize(K,Nlimit);
  pnl_vect_resize(Price,Nlimit);
  
  //delta
  mone=1;
  //printf("limit integration %7.4f \n",A); 
  for(n=0; n<Nlimit; n++)
    {
      dzeta   = Cadd(ln_phi(vn_minus_alpha_plus_uno,T,Model),Complex(0,vn*B));
      dzetaBS = Cadd(ln_phi_BS(vn_minus_alpha_plus_uno,T,sigma),Complex(0,vn*B));
      dzeta   = Csub(Cexp(dzeta),Cexp(dzetaBS));
      dzeta   = Cdiv(dzeta,i_vn_plus_alpha);
      dzeta   = Cdiv(dzeta,uno_plus_alpha_plus_ivn);
      //>> With Simson rules
      pnl_vect_complex_set(y,n,RCmul(3+mone-((n==0)?1:0),Conj(dzeta)));
      //>> Update value 
      vn += h;
      vn_minus_alpha_plus_uno.r+=h;
      i_vn_plus_alpha.i+=h;
      uno_plus_alpha_plus_ivn.i+=h;
      mone*=-1;
    }
  pnl_ifft_inplace(y);
  for(n=0;n<Nlimit;n++)
    {
      LET(K,n)=exp(-B+n*Kstep+(r-divid)*T)*(S0);
      pnl_cf_call_bs(S0,GET(K,n),T,r,divid,sigma,&LET(Price,n),&vn);
      LET(Price,n)+=2./3* S0/(Kstep)*exp(alpha*(B-n*Kstep)-divid*T)*GET_REAL(y,n);
    }
  if (CallPut==2)
    for(n=0;n<Nlimit;n++)
      LET(Price,n)-=S0*exp(-divid*T)+GET(K,n)*exp(-r*T);
  /*
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2-5),GET(Price,Nlimit/2-5));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2-4),GET(Price,Nlimit/2-4));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2-3),GET(Price,Nlimit/2-3));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2-2),GET(Price,Nlimit/2-2));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2-1),GET(Price,Nlimit/2-1));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+0),GET(Price,Nlimit/2+0));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+1),GET(Price,Nlimit/2+1));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+2),GET(Price,Nlimit/2+2));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+3),GET(Price,Nlimit/2+3));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+4),GET(Price,Nlimit/2+4));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+5),GET(Price,Nlimit/2+5));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+6),GET(Price,Nlimit/2+6));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+7),GET(Price,Nlimit/2+7));
  printf("Price K=  %7.4f  P= %7.4f \n",GET(K,Nlimit/2+8),GET(Price,Nlimit/2+8));
  pnl_vect_free(&K);
  pnl_vect_free(&Price);
  */
  return OK;
}
Exemplo n.º 12
0
int CarrMethod_old_verison(double S0,
                           double T,
                           double K,
                           double CallPut,
                           double r,
                           double divid,
                           double sigma,
                           void * Model,
                           dcomplex (*ln_phi)(dcomplex u,double t,void * model),
                           double *ptprice,
                           double *ptdelta)
{       
  int n;
  dcomplex dzeta,dzetaBS;
  double alpha=0.0;
  //taking account of dividends
  int Nlimit =  2048;
  //number of integral discretization steps
  double logstrikestep = 0.01;
  double k0 = log(K/(S0*exp(-divid*T)));
  double h = M_2PI/Nlimit/logstrikestep; //integral discretization step
  double A = (Nlimit-1)*h; // integration domain is (-A/2,A/2)
  PnlVectComplex * z =pnl_vect_complex_create(Nlimit);
  PnlVectComplex * y =pnl_vect_complex_create(Nlimit);
  double vn = -A/2;
  dcomplex vn_minus_alpha_plus_uno = Complex(-A/2,-(alpha+1));
  dcomplex i_vn_plus_alpha = Complex(alpha,-A/2);
  double weight = 1./3; //Simpson's rule weights
  dcomplex uno_plus_alpha_plus_ivn=Complex(1+alpha,vn);
  //delta
  for(n=0; n<Nlimit; n++)
    {
      dzeta=   Cadd(ln_phi(vn_minus_alpha_plus_uno,T,Model),Complex(0,vn*(r*T-k0)));
      dzetaBS= Cadd(ln_phi_BS(vn_minus_alpha_plus_uno,T,sigma),Complex(0,vn*(r*T-k0)));
      dzeta  = Csub(Cexp(dzeta),Cexp(dzetaBS));
      dzeta  = Cdiv(dzeta,i_vn_plus_alpha);
      dzeta  = RCmul(weight,dzeta);
      pnl_vect_complex_set(z,n,dzeta);
      dzeta=Cdiv(dzeta,uno_plus_alpha_plus_ivn);
      pnl_vect_complex_set(y,n,dzeta);
      //>> Update value 
      vn += h;
      vn_minus_alpha_plus_uno.r+=h;
      i_vn_plus_alpha.i+=h;
      uno_plus_alpha_plus_ivn.i+=h;
      weight = (weight<1) ? 4./3 : 2./3; //Simpson's rule weights
      weight = (n==(Nlimit-2)) ?2./3. :weight;
    }
  //pnl_vect_complex_print(z);
  pnl_fft_inplace(z);
  pnl_fft_inplace(y);
  //pnl_vect_complex_print(z);
  
  //Black-Scholes formula
  pnl_cf_call_bs(S0,K,T,r,divid,sigma,ptprice,ptdelta);
  S0 *= exp(-divid*T);
  /*Call Case*/
  *ptprice += S0*A/M_2PI/(Nlimit-1)*exp(-alpha*k0)*GET_REAL(y,0);
  *ptdelta += exp(-divid*T)*(A/M_2PI/(Nlimit-1)*exp(-alpha*k0)*GET_REAL(z,0));
                             
  //Put Case via parity*/
  if (CallPut==2)
    {
      *ptprice =*ptprice-S0+K*exp(-r*T);
      *ptdelta =*ptdelta-exp(-divid*T);
    } 
  //memory desallocation
  pnl_vect_complex_free(&z);
  pnl_vect_complex_free(&y);
  return OK;
}
Exemplo n.º 13
0
static double charact_func(double k)
{
  double X,tau,roeps,u,b,I,eps,eps2;
  dcomplex Ak,Bk,Ck,Dk,Lambdak,z1,z2,z3,zeta,psi_moins,psi_plus,expo,ans;
  dcomplex dlk;
 
  tau   = T;
  eps   = sigma;
  roeps = rho*eps;
  X     = log(S/K) + (r - divid)*tau; 
  eps2  = eps*eps;
 
  if(func_type==1)
    {
      u = 1.;
      b = kappa - roeps;
      I = 1.;
    }
  else if(func_type==2)
    {
      u = -1.;
      b = kappa;
      I =  0.;
    }
  else
    {
      printf("erreur : dans charact_func il faut initialiser func_type a 1 ou 2.\n");
      exit(-1);
    }
  
  if(heston==1)
    {
	
      z1   = Complex(k*k,-u*k);
      z2   = Complex(b,-roeps*k);
      z2   = Cmul(z2,z2);
	
      zeta = Cadd(z2,RCmul(eps2,z1));
      zeta = Csqrt(zeta);
	  
      psi_moins = Complex(b,-roeps*k);
      psi_plus  = RCmul(-1.,psi_moins);
      psi_moins = Cadd(psi_moins,zeta);
      psi_plus  = Cadd(psi_plus,zeta);
	
      expo = Cexp( RCmul(-tau,zeta) );
      z3   = Cadd( psi_moins , Cmul(psi_plus,expo) );
	  
      Bk = RCmul(-1.,z1);
      Bk = Cmul( Bk , Csub(Complex(1.,0),expo) );
      Bk = Cdiv(Bk,z3);
	  
      Ak = Cdiv( z3 , RCmul(2.,zeta) );
      Ak = Clog(Ak);
	  
      if(initlog>0)
	{
	  dlk = Csub(Ak,lk_1);
	  if(dlk.i < -M_PI)
	    {
	      bk = bk + 1;
	    }
	  else if(dlk.i > M_PI)
	    {
	      bk = bk - 1;
	    }
	  initlog++;
	  lk_1 = Ak;
	} else {
	initlog++;
	lk_1 = Ak;
      }
	  
      Ak = Cadd(Ak, Complex(0.,2*M_PI*bk)); 
	 
      Ak = RCmul( 2. , Ak );
      Ak = Cadd( RCmul(tau,psi_plus) , Ak);
      Ak = RCmul( -kappa*teta/eps2 , Ak);
	  
    }
  else
    {
      Ak = Complex(0.,0.);
      Bk = Complex( -0.5*tau*k*k , 0.5*tau*u*k );
    }
  
  if(merton==1)
    {
      z1 = Complex( -0.5*v*v*k*k + I*(m0+0.5*v*v) , (m0+I*v*v)*k );
      z1 = Cexp(z1); 
      z2 = Complex(I,k);
      z2 = RCmul( exp(m0+0.5*v*v)  -1, z2);
      z2 = Cadd( Complex(1.,0.) , z2 );
      Lambdak = Csub(z1,z2);
	  
      Ck = Complex(0.,0.);
      Dk = RCmul(tau,Lambdak);
      
    }
  else
    {
      Ck = Complex(0.,0.);
      Dk = Complex(0.,0.);
    }
  
  ans = Cadd( Ak , RCmul(V0,Bk) );
  ans = Cadd( ans , Ck );
  ans = Cadd( ans , RCmul(lambda0,Dk) );  
  ans = Cadd( ans , Complex(0.,k*X) );
  ans = Cexp(ans);
  ans = Cdiv(ans,Complex(0.,k));
  
  return ans.r;
}
Exemplo n.º 14
0
void cisi(float x, float *ci, float *si)
{
	void nrerror(char error_text[]);
	int i,k,odd;
	float a,err,fact,sign,sum,sumc,sums,t,term;
	fcomplex h,b,c,d,del;

	t=fabs(x);
	if (t == 0.0) {
		*si=0.0;
		*ci = -1.0/FPMIN;
		return;
	}
	if (t > TMIN) {
		b=Complex(1.0,t);
		c=Complex(1.0/FPMIN,0.0);
		d=h=Cdiv(ONE,b);
		for (i=2;i<=MAXIT;i++) {
			a = -(i-1)*(i-1);
			b=Cadd(b,Complex(2.0,0.0));
			d=Cdiv(ONE,Cadd(RCmul(a,d),b));
			c=Cadd(b,Cdiv(Complex(a,0.0),c));
			del=Cmul(c,d);
			h=Cmul(h,del);
			if (fabs(del.r-1.0)+fabs(del.i) < EPS) break;
		}
		if (i > MAXIT) nrerror("cf failed in cisi");
		h=Cmul(Complex(cos(t),-sin(t)),h);
		*ci = -h.r;
		*si=PIBY2+h.i;
	} else {
		if (t < sqrt(FPMIN)) {
			sumc=0.0;
			sums=t;
		} else {
			sum=sums=sumc=0.0;
			sign=fact=1.0;
			odd=TRUE;
			for (k=1;k<=MAXIT;k++) {
				fact *= t/k;
				term=fact/k;
				sum += sign*term;
				err=term/fabs(sum);
				if (odd) {
					sign = -sign;
					sums=sum;
					sum=sumc;
				} else {
					sumc=sum;
					sum=sums;
				}
				if (err < EPS) break;
				odd=!odd;
			}
			if (k > MAXIT) nrerror("maxits exceeded in cisi");
		}
		*si=sums;
		*ci=sumc+log(t)+EULER;
	}
	if (x < 0.0) *si = -(*si);
}
Exemplo n.º 15
0
int ComplexLUDecompose(pcomplex **a, int n, double *vv, int *indx, double *pd)
//	pcomplex		**a;	the matrix whose LU-decomposition is wanted
//	int			n;		order of a
//	double		*vv;	work vector of size n (stores implicit 
//							scaling of each row)
//	int			*indx;	=> row permutation according to partial 
//							pivoting sequence
//	double		*pd;	=> 1 if number of row interchanges was even, 
//							-1 if odd (NULL OK)
{
	int			i, imax, j, k;
	double		big, dum, temp, d;
	pcomplex		sum, cdum;

	d = 1.0;
	imax = 0; // only to shut the compiler up.

	for (i = 0; i < n; i++) {
		big = 0.0;
		for (j = 0; j < n; j++) {
			if ((temp = Cabs(a[i][j])) > big)
				big = temp;
		}
		if (big == 0.0) {
                    printf("singular matrix in routine ComplexLUDecompose\n");
                    return 1;
		}
		vv[i] = 1.0 / big;
	}
	
	for (j = 0; j < n; j++) {
		for (i = 0; i < j; i++) {
			sum = a[i][j];
			for (k = 0; k < i; k++) 
				sum = Csub(sum, Cmul(a[i][k], a[k][j]));
			a[i][j] = sum;
		}
		big = 0.0;
		for (i = j; i < n; i++) {
			sum = a[i][j];
			for (k = 0; k < j; k++)
				sum = Csub(sum, Cmul(a[i][k], a[k][j]));
			a[i][j] = sum;
			dum = vv[i] * Cabs(sum);
			if (dum >= big) {
				big = dum;
				imax = i;
			}
		}
		if (j != imax) {
			for (k = 0; k < n; k++) {
				cdum = a[imax][k];
				a[imax][k] = a[j][k];
				a[j][k] = cdum;
			}	
			d = -d;
			vv[imax] = vv[j];
		}
		indx[j] = imax;
		if (a[j][j].re == 0.0 && a[j][j].im == 0.0)
			a[j][j] = Complex(1.0e-20, 1.0e-20);
		if (j != n - 1){
			cdum = Cdiv(Complex(1.0, 0.0), a[j][j]);
			for (i = j + 1; i < n; i++)
				a[i][j] = Cmul(a[i][j], cdum);
		}
	}

	if (pd != NULL)
		*pd = d;
	return 0;
}
Exemplo n.º 16
0
int CarrMethod(double S0,
               double T,
               double K,
               double CallPut,
               double r,
               double divid,
               double sigma,
               void * Model,
               dcomplex (*ln_phi)(dcomplex u,double t,void * model),
               double *ptprice,
               double *ptdelta)
{       
  int n;
  dcomplex dzeta,dzetaBS;
  double alpha=0.75;
  //taking account of dividends
  int Nlimit =  2048;//2048;
  //number of integral discretization steps
  double logstrikestep = 0.01;
  double k0 = log(K/S0)-(r-divid)*T;
  double h = M_PI/Nlimit/logstrikestep; //integral discretization step
  double z,y;
  double vn = 0;
  dcomplex vn_minus_alpha_plus_uno = Complex(0,-(alpha+1));
  dcomplex i_vn_plus_alpha = Complex(alpha,0);
  double weight = 1./3; //Simpson's rule weights
  dcomplex uno_plus_alpha_plus_ivn=Complex(1+alpha,vn);
  //delta
  z=0;y=0;
  for(n=0; n<Nlimit; n++)
    {
      dzeta=Cadd(ln_phi(vn_minus_alpha_plus_uno,T,Model),Complex(0,-vn*k0));
      // printf("%7.4f + i %7.4f \n",dzeta.r,dzeta.i);
      dzetaBS= Cadd(ln_phi_BS(vn_minus_alpha_plus_uno,T,sigma),Complex(0,-vn*k0));
      dzeta  = Csub(Cexp(dzeta),Cexp(dzetaBS));
      dzeta  = Cdiv(dzeta,i_vn_plus_alpha);
      dzeta  = RCmul(weight,dzeta);
      //printf(">>%7.4f + i %7.4f \n",dzeta.r,dzeta.i);
      z+=dzeta.r;
      dzeta=Cdiv(dzeta,uno_plus_alpha_plus_ivn);
      y+=dzeta.r;
      //>> Update value 
      vn += h;
      vn_minus_alpha_plus_uno.r+=h;
      i_vn_plus_alpha.i+=h;
      uno_plus_alpha_plus_ivn.i+=h;
      weight = (weight<1) ? 4./3 : 2./3; //Simpson's rule weights
      weight = (n==(Nlimit-2)) ?2./3. :weight;
    }
  //Black-Scholes formula
  pnl_cf_call_bs(S0,K,T,r,divid,sigma,ptprice,ptdelta);
  S0 *= exp(-divid*T);
  /*Call Case*/
  *ptprice += S0/(Nlimit*logstrikestep)*exp(-alpha*k0)*y;
  //*ptprice = y;
  *ptdelta += exp(-divid*T)/(Nlimit*logstrikestep)*exp(-alpha*k0)*z;
  //Put Case via parity*/
  if (CallPut==2)
    {
      *ptprice =*ptprice-S0+K*exp(-r*T);
      *ptdelta =*ptdelta-exp(-divid*T);
    }
  //memory desallocation
  return OK;
}
Exemplo n.º 17
0
static double charact_func0(double k)
{
  double X,tau,roeps,u,eps,eps2;
  dcomplex Ak,Bk,Ck,Dk,Lambdak,z1,z2,z3,zeta,psi_moins,psi_plus,expo,ans;
  dcomplex dlk;
  
  tau   = T;
  eps   = sigma;
  roeps = rho*eps;
  X     = log(S/K) + (r - divid)*tau;
 
  u = kappa - roeps/2.;
  
  eps2 = eps*eps;
  
  if(heston==1)
    {
      zeta.r = k*k*eps2*(1.-rho*rho) + u*u + eps2/4.;
      zeta.i = 2.*k*roeps*u;
      zeta   = Csqrt(zeta);
	 
      psi_moins = Complex(u,roeps*k);
      psi_plus  = RCmul(-1.,psi_moins);
      psi_moins = Cadd(psi_moins,zeta);
      psi_plus  = Cadd(psi_plus,zeta);
	
      expo = Cexp( RCmul(-tau,zeta) );
      z3   = Cadd( psi_moins , Cmul(psi_plus,expo) );
	 
      Bk = RCmul( -(k*k+0.25) , Csub(Complex(1.,0),expo) );
      Bk = Cdiv(Bk,z3);
	 
      Ak = Cdiv( z3 , RCmul(2.,zeta) );
      Ak = Clog(Ak);
	
      if(initlog>0)
	{
	  dlk = Csub(Ak,lk_1);
	  if(dlk.i < -M_PI)
	    {
	      bk = bk + 1;
	    }
	  else if(dlk.i > M_PI)
	    {
	      bk = bk - 1;
	    }
	  initlog++;
	  lk_1 = Ak;
	} else {
	initlog++;
	lk_1 = Ak;
      }
	  
      Ak = Cadd(Ak, Complex(0.,2*M_PI*bk));
	
      Ak = RCmul( 2. , Ak );
      Ak = Cadd( RCmul(tau,psi_plus) , Ak);
      Ak = RCmul( -kappa*teta/eps2 , Ak);
    }
  else
    {
      Ak = Complex(0.,0.);
      Bk = Complex( -0.5*tau*(k*k+0.25) ,0.);
    }
  
  if(merton==1)
    {
      z1 = Complex( 0.5*m0-0.5*v*v*(k*k-0.25) , -k*(m0+0.5*v*v) );
      z1 = Cexp(z1); 
      z2 = Complex(0.5,-k);
      z2 = RCmul( exp(m0+0.5*v*v) - 1. , z2);
      z2 = Cadd( Complex(1.,0.) , z2 );
      Lambdak = Csub(z1,z2);
	
      Ck = Complex(0.,0.);
      Dk = RCmul(tau,Lambdak);
    }
  else
    {
      Ck = Complex(0.,0.);
      Dk = Complex(0.,0.);
    }
 
  ans = Cadd( Ak , RCmul(V0,Bk) );
  ans = Cadd( ans , Ck );
  ans = Cadd( ans , RCmul(lambda0,Dk) );
  ans = Cadd( ans , RCmul(X,Complex(0.5,-k) ) );
  ans = Cexp(ans);
  ans = Cdiv(ans,Complex(k*k+0.25,0.));
 
  if(probadelta == 1)
    {
      ans = Cmul( ans , Complex(0.5,-k) );
      ans = RCmul( 1./S , ans );
    }
  
  return ans.r;
}