示例#1
0
void CSpeechEst::UpdateGain()
{
	int i;
	float prisnr,g,p;
	for (i=m_nStartBin;i<m_nEndBin;i++)
	{
		p=m_pfP[i];
		prisnr=m_pfPrioriSNR[i];
		g=prisnr/(prisnr+1.f);       		
		g*=expf(0.5f*expint(m_pfV[i]));
		g=min(1.f,g);
		g=max(m_fGainMin,g);
		m_pfGain[i]=powf(g,p);
		m_pfGain[i]*=powf(m_fGainMin,1.f-p);
	}
}
示例#2
0
int 
main_test(long int x)
{
	return expint(50, x) & 1023;
}
示例#3
0
BOOL gotcha(void)
{ /* use new factorisation */
    int r,j,i,k,n,rb,had,hp;
    unsigned int t;
    BOOL found;
    found=TRUE;
    if (partial)
    { /* check partial factorisation for usefulness */
        had=lp%hmod;
        forever
        { /* hash search for matching large prime */
            hp=hash[had];
            if (hp<0)
            { /* failed to find match */
                found=FALSE;
                break;
            }
            if (pr[hp]==lp) break; /* hash hit! */
            had=(had+(hmod2-lp%hmod2))%hmod;
        }
        if (!found && nlp>=mlf) return FALSE;
    }
    copy(PP,XX);
    convert(1,YY);
    for (k=1;k<=mm;k++)
    { /* build up square part in YY  *
       * reducing e[k] to 0s and 1s */
        if (e[k]<2) continue;
        r=e[k]/2;
        e[k]%=2;
        expint(epr[k],r,TT);
        multiply(TT,YY,YY);
    }
/* debug only
    cotnum(XX,stdout);
    cotnum(YY,stdout);
    if (e[0]==1) printf("-1");
    else printf("1");
    for (k=1;k<=mm;k++)
    {
        if (e[k]==0) continue;
        printf(".%d",epr[k]);
    }
    if (partial) printf(".%d\n",lp);
    else printf("\n");
*/
    if (partial)
    { /* factored with large prime */
        if (!found)
        { /* store new partial factorization */
            hash[had]=nlp;
            pr[nlp]=lp;
            copy(XX,z[nlp]);
            copy(YY,w[nlp]);
            for (n=0,rb=0,j=0;j<=mm;j++)
            {
                G[nlp][n]|=((e[j]&1)<<rb);
                if (++rb==nbts) n++,rb=0;
            }
            nlp++;
        }
        if (found)
        { /* match found so use as factorization */
            printf("\b\b\b\b\b\b*");
            fflush(stdout);
            mad(XX,z[hp],XX,NN,NN,XX);
            mad(YY,w[hp],YY,NN,NN,YY);
            for (n=0,rb=0,j=0;j<=mm;j++)
            {
                t=(G[hp][n]>>rb);
                e[j]+=(t&1);
                if (e[j]==2)
                {
                    premult(YY,epr[j],YY);
                    divide(YY,NN,NN);
                    e[j]=0;
                }
                if (++rb==nbts) n++,rb=0;
            }
            premult(YY,lp,YY);
            divide(YY,NN,NN);
        }
    }
    else 
    {
示例#4
0
void main(void)
{
  expint(50,1);
  // with  expint(50,21) as argument, runs the short path
  // in expint.   expint(50,1)  gives the longest execution time
}
示例#5
0
scprog()
{ short i,j,k,l,m,n,stconj,np,pm1,ncr,crct,ndc,dcct,
        lo,intexsk,stpt,lpt,stint,olen,pt,ad,*p1,*ip1,*p2,*ip2,
        *pint,*ptr,*g,*ig,ino,olo,**vsvptr;
  int quot;
  char  ingp,igth;

  if ((ip=fopen(inf1,"r"))==0)
  { fprintf(stderr,"Cannot open %s.\n",inf1); return(-1); }
  fscanf(ip,"%hd%hd%hd%hd",&npt,&exp,&nb,&l);
  if (npt>mpt)
  { fprintf(stderr,"npt too big. Increase MPT.\n"); return(-1); }
  if (nb>=mb)
  { fprintf(stderr,"nb too big. Increase MB.\n"); return(-1); }
  if (nb*npt*2>svsp)
  { fprintf(stderr,"svsp too small. Increase SVSP.\n"); return(-1); }
  if (exp>=mexp)
  { fprintf(stderr,"exp too big. Increase MEXP.\n"); return(-1); }
  if (l!=2) {fprintf(stderr,"Wrong input format.\n"); return(-1);}
  readbaselo(nb,base,lorb); npt1=npt+1;
  quot=psp/npt1; if (quot>mp) quot=mp; mxp=quot; mxp=2*(mxp/2);
  if (2*exp>mxp)
  { fprintf(stderr,"Out of perm space. Increase PSP (or MP).\n"); return(-1); }
  for (i=0;i<mxp;i++) pptr[i]=perm+i*npt1-1;
  for (i=1;i<=nb;i++) svptr[i]=sv+(i-1)*npt-1;
  readpsv(0,nb,exp,svptr);
  for (i=exp;i>=1;i--) fscanf(ip,"%hd",pwt+i);
  fscanf(ip,"%hd%hd",&prime,&ngads);
  if (2*exp+ngads>mxp)
  { fprintf(stderr,"Out of perm space. Increase PSP (or MP).\n"); return(-1); }
  for (i=1;i<=exp;i++) fscanf(ip,"%hd",power+i);
  for (i=1;i<=exp;i++) { j=2*(i-1); ngno[i]=j; igno[j+1]=i; }
  k=2*exp-1;
  for (i=1;i<=ngads;i++)
  { l=i+k; readvec(pptr[l],1); m=pptr[l][npt1]; ngno[m]=l; }
  fclose(ip);

  stconj=2*exp+ngads-1; itp=tp+npt1;
  if (4*exp+ngads>mxp)
  { fprintf(stderr,"Out of perm space. Increase PSP (or MP).\n"); return(-1); }
  if (subgp)
  { stig=stconj+2*exp;
    for (i=1;i<=nb;i++) sv2ptr[i]=sv+(nb+i-1)*npt-1;
    if (subgp>1)
    {strcpy(inf3,inf0); strcat(inf3,"sg"); sgstr[0]=sgc; strcat(inf3,sgstr);}
    if (rsgp()== -1) return(-1);
    igth=1;
  }
  else vsvptr=svptr;

  if ((ip=fopen(inf2,"r"))==0)
  { printf("Cannot open %s.\n",inf2); return(-1); }
  fscanf(ip,"%hd%hd",&i,&ndc); while (getc(ip)!='\n');
  if (i!=npt) {printf("dcr has npt false.\n"); return(-1); }

/* If subgp is true, then we will have to update the dcr file, so we print
   it out on to a temporary file as we go along.
*/
  if (subgp)
  { opy=fopen(outft,"w"); fprintf(opy,"%4d%4d%4d%4d\n",npt,ndc,0,0);}
  op=fopen(outf,"w"); pm1=prime-1; setpinv();

  for (dcct=1;dcct<=ndc;dcct++)
  { if (subgp>1 && igth==0)
    { strcpy(inf3,inf0); strcat(inf3,"sg"); sgstr[0]=sgc;
      strcat(inf3,sgstr); ipkp=ip;
      if (rsgp()== -1) return(-1);
      ip=ipkp; vsvptr=sv2ptr; igth=1;
    }
    else if (subgp) vsvptr=sv2ptr;
    readvec(dcrep,0); invert(dcrep,dcrepinv);
    fscanf(ip,"%hd",&lo); intexp=0; olo=lo;
    while (lo%prime==0) {intexp++; lo/=prime; }
    if (lo!=1) printf("Warning. lo was not a power of p.\n");
    intexp=exp-intexp; printf("dcct,intexp=%d,%d.\n",dcct,intexp);
/* intexp is the exponent of Q = P ^ gPg(-1), for the current dcrep g */
    if (intexp==0 || (intexp==1 && mult))
    { if (subgp)
      { if (npt>=1000) for (n=1;n<=npt;n++) fprintf(opy,"%5d",dcrep[n]);
        else for (n=1;n<=npt;n++) fprintf(opy,"%4d",dcrep[n]);
        fprintf(opy,"   %4d\n",olo);
      }
      if (mult==0) fprintf(op,"%4d\n",0);
      continue;
    }
/* Now we compute the gens g(-1)h(i)g, where h(i) are the PCP gens of P */
    for (i=1;i<=exp;i++)
    { p1=pptr[ngno[i]]; p2=pptr[stconj+i];
      for (n=1;n<=npt;n++) p2[n]=dcrep[p1[dcrepinv[n]]];
    }
    stint=stconj+exp; pint=pptr[stint]; intexsk=0;
    for (i=1;i<=exp;i++) intno[i]=0;
    lpt=1; stpt=1; pm1=prime-1;
/* Now we search through the elements of g(-1)Pg, testing for membership of
   P, until we have found the intexp gens of g(-1)Qg.
*/
    for (i=1;i<=exp;i++)
    { olen=1; cp[1]=stconj+i;
      for (j=1;j<=exp;j++) co[j]=0;
      while(1)
      { *cp=olen; ingp=1;
        for (k=1;k<=nb;k++)
        { pt=image(base[k]); ptr=vsvptr[k];
          if (ptr[pt]==0) {ingp=0; ad=stpt; break;}
          if (k<nb) addsv(pt,ptr);
        }
        if (ingp) break;
        while (co[ad]==pm1) {olen-=pm1; co[ad]=0; ad=fpt[ad]; }
        if (ad==i) { lpt=i; fpt[i]=i+1; break; }
        olen++; co[ad]++; cp[olen]=stconj+ad;
      }
      if (ingp)
      { intexsk++; pint+=npt1; *cp=olen;
        for (n=1;n<=npt;n++) pint[n]=image(n);
        pint[npt1]=intexsk; intno[i]=stconj+2*intexsk-1;
        if (intexsk==intexp) break;
        if (stpt==i) {lpt=i+1; stpt=lpt; } else fpt[lpt]=i+1;
      }
    }
/* Search is complete */
    if (intexsk<intexp) {fprintf(stderr,"Intersection error.\n"); return(-1); }
    printf("Found intersection.\n");

/* If subgp, then we will now modify g, until H ^ gHg- contains Q */
    if (subgp) for (m=subgp;m>=1;m--)
    { if (m==subgp) for (i=1;i<=npt;i++) {tp[i]=i; itp[i]=i;}
      if (subgp>1)
      { strcpy(inf4,inf0); strcat(inf4,"cr"); strcat(inf4,sgstr);
        if (m>1)
        { sgstr[0]--; strcpy(inf3,inf0); strcat(inf3,"sg"); strcat(inf3,sgstr);
          ipkp=ip;
          if (rsgp()== -1) return(-1);
          ip=ipkp; igth=0; vsvptr=sv2ptr;
        }
        else vsvptr=svptr;
      }
      else vsvptr=svptr;
      if ((ipcr=fopen(inf4,"r"))==0)
      { fprintf(stderr,"Cannot open %s.\n",inf4); return(-1); }
      ingp=0;
      fscanf(ipcr,"%hd%hd",&n,&ncr); while (getc(ipcr)!='\n');
      if (n!=npt) { fprintf(stderr,"inf4 has npt wrong.\n"); return(-1); }
      for (crct=0;crct<=ncr;crct++)
      { if (crct==0) for (n=1;n<=npt;n++) crep[n]=n;
        else for (n=1;n<=npt;n++) fscanf(ipcr,"%hd",crep+n);
        invert(crep,crepinv);
        for (i=1;i<=intexp;i++)
        { p1=pptr[stint+i]; *cp=0;
          for (j=1;j<=nb;j++)
          { pt=image(crep[tp[p1[itp[crepinv[base[j]]]]]]); ptr=vsvptr[j];
            if (ptr[pt]==0) goto nextcr;
            if (j<nb) addsv(pt,ptr);
          }
        }
        ingp=1; printf("Got intersection in subgp %d. crct=%d.\n",m-1,crct);
        for (n=1;n<=npt;n++) tp[n]=crep[tp[n]]; invert(tp,itp);
        fclose(ipcr); break;
nextcr:;
      }
      if (ingp==0)
      { fprintf(stderr,"Cannot get intersection in subgp %d.\n",m-1);
        return(-1);
      }
    }

/* Now we reconjugate the gens of g(-1)Qg by g(-1) to get gens of Q */
    for (i=1;i<=intexp;i++)
    { p1=pptr[stconj+2*i-1]; p2=pptr[stint+i];
      for (n=1;n<=npt;n++) p1[n]=dcrepinv[p2[dcrep[n]]]; p1[npt1]=p2[npt1];
      invert(p1,p1+npt1);
    }
/* If subgp, then update dcr */
    if (subgp)
    { if (npt>=1000) for (n=1;n<=npt;n++)
      { dcrep[n]=tp[dcrep[n]]; fprintf(opy,"%5d",dcrep[n]); }
      else for (n=1;n<=npt;n++)
      { dcrep[n]=tp[dcrep[n]]; fprintf(opy,"%4d",dcrep[n]); }
      fprintf(opy,"   %4d\n",olo); invert(dcrep,dcrepinv);
    }
    norm= intexp==exp;
/* If norm, then we do not need to compute the PCP for Q */
    if (norm)
    { fprintf(op,"%4d\n",intexp);
      if (mult==0)
      { for (i=exp;i>=1;i--) { wt[i]=pwt[i]; fprintf(op,"%4d",wt[i]); }
        fprintf(op,"\n");
      }
      goto outconj;
    }

/* Now we compute the PCP for Q. This is similar to the algorithm in pcrun */
    for (i=1;i<=intexp;i++) { wt[i]=1; d1[i]=0; d2[i]=0; }
restart:
    if (mult) nwt=2;
    for (i=intexp;i>=2;i--)
    { p1=pptr[stconj+2*i-1]; ip1=p1+npt1;
      for (j=intexp;j>i;j--)
      { if (mult==0) nwt=wt[i]+wt[j];
        p2=pptr[stconj+2*j-1]; ip2=p2+npt1;
        for (n=1;n<=npt;n++) {pt=p2[p1[ip2[ip1[n]]]]; tp[n]=pt; itp[pt]=n; }
        if ((n=expint(intexp+1-i,intexp+1-j,nwt))>0) goto restart;
        if (n== -1) return(-1);
      }
    }
    if (mult==0) for (i=intexp;i>=2;i--)
    { nwt=wt[i]+1; p1=pptr[stconj+2*i-1]; ip1=p1+npt1;
      for (n=1;n<=npt;n++)
      { pt=n; for (m=1;m<=prime;m++) pt=p1[pt]; tp[n]=pt; itp[pt]=n; }
      if ((n=expint(intexp+1-i,intexp+1-i,nwt))>0) goto restart;
      if (n== -1) return(-1);
    }
    fprintf(op,"%4d\n",intexp);
    if (mult==0)
    { for (i=intexp;i>=1;i--) fprintf(op,"%3d",wt[i]); fprintf(op,"\n"); }
/* We output the PCP gens of Q followed by those of g(-1)Qg */
    for (i=intexp;i>=1;i--)
    { p1=pptr[stconj+2*i-1]; p2=tp; ptr=p1+2*npt1;
      while (p1<ptr) *(++p2)= *(++p1);
      express(tp,rel,0); l= *rel;
      for (n=0;n<=l;n++) fprintf(op,"%4d",rel[n]); fprintf(op,"\n");
    }
outconj:
    for (i=intexp;i>=1;i--)
    { p1= pptr[stconj+2*i-1];
      for (n=1;n<=npt;n++) {pt=dcrep[p1[dcrepinv[n]]]; tp[n]=pt; itp[pt]=n; }
      express(tp,rel,0); l= *rel;
      for (n=0;n<=l;n++) fprintf(op,"%4d",rel[n]); fprintf(op,"\n");
    }
    if (norm) continue;
    if (mult==0)
    { for (i=intexp;i>=1;i--) fprintf(op,"%3d",d1[i]); fprintf(op,"\n");
      for (i=intexp;i>=1;i--) fprintf(op,"%3d",d2[i]); fprintf(op,"\n");
    }
    for (i=intexp;i>=2;i--)
    { p1=pptr[stconj+2*i-1]; ip1=p1+npt1;
      for (j=intexp;j>i;j--)
      { p2=pptr[stconj+2*j-1]; ip2=p2+npt1;
        for (n=1;n<=npt;n++) {pt=p2[p1[ip2[ip1[n]]]]; tp[n]=pt; itp[pt]=n; }
        expint(intexp+1-i,intexp+1-j,0);
      }
    }
    if (mult==0) for (i=intexp;i>=2;i--)
    { p1=pptr[stconj+2*i-1]; ip1=p1+npt1;
      for (n=1;n<=npt;n++)
      { pt=n; for (m=1;m<=prime;m++) pt=p1[pt]; tp[n]=pt; itp[pt]=n; }
      expint(intexp+1-i,intexp+1-i,0);
    }
  }
  fprintf(op,"%d\n",-1);
  if (subgp)
  { fclose(opy); fclose(op); fclose(ip);
    ip=fopen(outft,"r"); op=fopen(inf2,"w");
    while ((i=getc(ip))!= -1) putc(i,op); fclose(ip); unlink(outft);
  }
  return(0);
}
示例#6
0
int main(int argc,char **argv){
	struct timeval startTime, endTime; 
	long seconds, useconds; 
	double mtime;
	double   *L, *D, *U, *W, *x;
	double   aA, bB;
	double   ps0;
	double   Dx, Dt;
	int      i, j,k, ir=0;  

	double   sig=0.2;
	double   T=0.5;
	double   S0=1800.0, K=1650.0; 
	double   rfr=0.005;
	double   div=0.0135;
	double 	 theta=-0.15;
	double 	 v=0.17;
	
	double   sMin = 1000.0, sMax = 2000.0;
	int      N=4000;
	int      M=1000;

	double 		g[6][N+1];

	Dx = (log(sMax)-log(sMin))/N;
	Dt = T/M;
	double lp = sqrt(pow(theta,2)/pow(sig,4)+2/(v*pow(sig,2)))-theta/pow(sig,2);
	double ln = sqrt(pow(theta,2)/pow(sig,4)+2/(v*pow(sig,2)))+theta/pow(sig,2);
	double sigma_squared = 1/v * ((1/pow(lp,2)*(1-exp(-lp*Dx)*(1+lp*Dx)))+(1/pow(ln,2)*(1-exp(-ln*Dx)*(1+ln*Dx))));
	double omega = 1/v *((expint(lp*Dx)-expint(Dx*(lp-1)))+(expint(ln*Dx)-expint(Dx*(ln-1))));

	L = new double[N];
	D = new double[N];
	U = new double[N];
	W = new double[N+1];
	x = new double[N+1];	
	double *w_adjusted = new double[N+1];

	aA = sigma_squared * Dt/(2 * pow(Dx,2)); //first part of Bl or Bu
	bB = (rfr-div+omega-0.5*sigma_squared)*Dt/(2.0*Dx); //second part of Bl or Bu

	gettimeofday(&startTime, NULL); 
	// populate payoff and 6 precalculated vectors

	for ( i = 0; i <= N; ++i){
		x[i] = log(sMin)+i*Dx;

		if(exp(x[i])<K)
			W[i] = K-exp(x[i]);
		else
			W[i] = 0.0;

		g[0][i]=exp(k*Dx*ln);
		g[1][i]=exp(k*Dx*lp);
		g[2][i]=expint(ln*k*Dx);
		g[3][i]=expint(lp*k*Dx);
		g[4][i]=expint((ln+1)*k*Dx);
		g[5][i]=expint((lp+1)*k*Dx);
	}

for (i = 1; i <=N-1; ++i){
		if (i==1){
			D[i] = 1 + 2.0*aA + rfr*Dt - (aA-bB)*       2.0/(1.0+Dx/2.0);
			U[i] = -(aA+bB)            + (aA-bB)*(1.0-Dx/2)/(1.0+Dx/2.0);
			
		}
		else if (i==N-1){
			L[i] = -(aA-bB)              + (aA+bB)*(1+Dx/2.0)/(1.0-Dx/2.0);
			D[i] = 1.0 + 2.0*aA + rfr*Dt - (aA+bB)*       2.0/(1.0-Dx/2.0);
			
		}
		else{
			L[i] = -(aA-bB);
			D[i] =  1 + 2.0*aA + rfr*Dt + Dt/v*(g[2][i]+g[3][N-i]);
			U[i] = -(aA+bB);
		}
	}


    for (j = M-1; j >= 0; --j) {
    	
    	for (i = 1; i <=N-1; ++i)
    	{
    		double rhs=0;
    		for (k = 1; k <= i-1; ++k)
    			{
    				rhs+=(W[i-k]-W[i]-k*(W[i-k-1]-W[i-k]))*(g[2][k]-g[4][k+1]);
    				rhs+=1/(ln*Dx)*(W[i-k-1]-W[i-k])*(g[0][k]-g[0][k+1]);
    			}
			for (k = 1; k <= N-i-1; ++k)
    			{
    				rhs+=(W[i+k]-W[i]-k*(W[i+k+1]-W[i+k]))*(g[3][k]-g[5][k+1]);
    				rhs+=1/(lp*Dx)*(W[i+k+1]-W[i+k])*(g[1][k]-g[1][k+1]);
    			}
			rhs+=K*g[2][i]-exp(x[i])*g[4][i];
			W[i]+=rhs;
    	}
		tridiagSolver(L, D, U, W, N-1);

	}

	//
	for(i = 0; i <= N-1; ++i){
		if(x[i]>log(S0)){
			ir=i;
			break;
		}
	}
	
	ps0 = W[ir-1] + ((W[ir]-W[ir-1])/Dx)*(log(S0)-x[ir-1]);

	cout << "Stock Price " << S0  << endl;
	cout << "Put Value   " << ps0 << "\n" << endl;

	delete [] L;
	delete [] D;
	delete [] U;
	delete [] W;
	delete [] x;

	gettimeofday(&endTime, NULL); 
	seconds = endTime.tv_sec - startTime.tv_sec; 
	useconds = endTime.tv_usec - startTime.tv_usec; 
	mtime = ((seconds) * 1000 + useconds/1000.0); 
	cout << "Time elapsed was: " << mtime << " (milliseconds)" << endl;

	return 0;
}
示例#7
0
int main(int argc,char **argv){

	/*struct timeval startTime, endTime; 
	long seconds, useconds; 
	double mtime;*/
	clock_t  t1;
	t1 = clock();
	double   *L, *D, *U, *W,*Wadj, *x;
	double   aA, bB;
	double   ps0;
	double   tau_j;	
	double 	 rhs=0;
	int      i, j, k, ir;

	double   sig=0.2;
	double   T=0.5;
	double   S0=1800.0, K=1650.0; 
	double   rfr=0.005;
	double   div=0.0135;
	double   sMin = 100.0, sMax = 4100.0;
	double   mu=0.17,theta=-0.15;

	int      N=1600;
	int      M=400;

	double   Dx = (log(sMax)-log(sMin))/N;
	double   Dt = T/M;
	double   lp = sqrt((pow(theta, 2) / pow(sig, 4) + 2 / (pow(sig, 2)*mu))) - theta / pow(sig, 2);//lambda p
	double   ln = sqrt((pow(theta, 2) / pow(sig, 4) + 2 / (pow(sig, 2)*mu))) + theta / pow(sig, 2);//lambda n

	L = new double[N];
	D = new double[N];
	U = new double[N];
	W = new double[N+1];
	Wadj = new double[N+1];
	x = new double[N+1];
	double** g = new double*[6];
	for (int i = 0; i < 6; ++i)
	{
		g[i]= new double[N+1];
	}

	//double   sig2_Dx = 1/(mu*lp*lp) *(1-exp(-lp*Dx)*(1+lp*Dx))+1/(mu*ln*ln) *(1-exp(-ln*Dx)*(1+ln*Dx));
	double Y = 0;
	double   sig2_Dx = (1 / mu)* pow(lp, Y - 2)* (-pow(lp*Dx, 1 - Y)*exp(-lp*Dx) + (1 - Y)*(exp(0.0) - exp(-lp*Dx)))
		+ (1 / mu)* pow(ln, Y - 2)* (-pow(ln*Dx, 1 - Y)*exp(-ln*Dx) + (1 - Y)*(exp(0.0) - exp(-ln*Dx)));
	//double   omega_Dx = 1/mu*(expint(lp*Dx)-expint(Dx*(lp-1)))+1/mu*(expint(ln*Dx)-expint(Dx*(ln+1)));
	double   omega_Dx = (expint(lp*Dx) - expint((lp - 1)*Dx) + expint(ln*Dx) - expint((ln + 1)*Dx)) / mu;
	aA = sig2_Dx*Dt/(2.0*Dx*Dx);
	bB = (rfr-div+ omega_Dx-0.5*sig2_Dx)*Dt/(2.0*Dx);

	//gettimeofday(&startTime, NULL); 

	// payoff
	for ( i = 0; i <= N; ++i){

		x[i] = log(sMin)+i*Dx;

		if(exp(x[i])<K)
			Wadj[i]=W[i] = K-exp(x[i]);
		else
			Wadj[i]=W[i] = 0.0;
		if (i>=1)
		{
			g[0][i] = exp(-i*ln*Dx);
			g[1][i] = exp(-i*lp*Dx);
			g[2][i] = expint(i*ln*Dx);
			g[3][i] = expint(i*lp*Dx);
			g[4][i] = expint(i*(ln+1)*Dx);
			g[5][i] = expint(i*(lp-1)*Dx);
		}		

	}

	double Bl = (aA - bB);
	double Bu = (aA + bB);
	double r = rfr;
	for (i = 1; i <=N-1; ++i){
		if (i==1){
			//L[i] = 0;
			D[i] = 1 + 2.0*aA + rfr*Dt - (aA-bB)*       2.0/(1.0+Dx/2.0);
			U[i] = -(aA+bB)            + (aA-bB)*(1.0-Dx/2)/(1.0+Dx/2.0);
			// Boundary condition
			//W[1] = W[1]+(aA-bB)*(K-exp(x[0]));
			//W[1] = W[1]+(aA-bB)*(K*exp(-rfr*tau_j)-exp(x[0])*exp(-div*tau_j));

		}
		else if (i==N-1){
			L[i] = -(aA-bB)              + (aA+bB)*(1+Dx/2.0)/(1.0-Dx/2.0);
			D[i] = 1.0 + 2.0*aA + rfr*Dt - (aA+bB)*       2.0/(1.0-Dx/2.0);
			//U[i] = 0;
			// Boundary condition
			//W[N-1] = W[N-1]+0.0;
		}
		else{
			L[i] = -(aA-bB);
			D[i] =  1 + 2.0*aA + rfr*Dt + Dt/mu*(g[2][i]-g[3][N-i]);
			U[i] = -(aA+bB);
		}

		//here
		//if (i == 1) {//Boundary Condition
		//	L[i] = 0;
		//	D[i] = -2 * Bl / (1 + Dx / 2) + 1 + r * Dt + Bl + Bu + (Dt / mu)*(g[2][i] + g[2][N - i]);
		//	U[i] = -Bu + (1 - Dx / 2) / (1 + Dx / 2)* Bl;
		//}
		//else if (i == N - 1) {//Boundary Condition
		//	L[i] = -Bl + (1 + Dx / 2) / (1 - Dx / 2)* Bu;
		//	D[i] = -2 * Bu / (1 - Dx / 2) + 1 + r * Dt + Bl + Bu + (Dt / mu)*(g[2][i] + g[2][N - i]);
		//	U[i] = 0;
		//}
		//else {
		//	L[i] = -Bl;
		//	D[i] = 1 + r * Dt + Bl + Bu + (Dt / mu) *(g[2][i] + g[2][N - i]);
		//	U[i] = -Bu;
		//}
	}


	for (j = M-1; j >= 0; --j) {
		
		for (int i = 1; i < N; ++i)
		{
			rhs=0;
			for (int k = 1; k <= i-1; ++k)
			{
				rhs+=(W[i-k]-W[i]-k*(W[i-k-1]-W[i-k]))*(g[2][k]-g[2][k+1])+(W[i-k-1]-W[i-k])/(ln*Dx)*(g[0][k]-g[0][k+1]);
			}
			for (int k = 1; k <= N-i-1; ++k)
			{
				rhs+=(W[i+k]-W[i]-k*(W[i+k+1]-W[i+k]))*(g[3][k]-g[3][k+1])+(W[i+k+1]-W[i+k])/(lp*Dx)*(g[1][k]-g[1][k+1]);
			}
			rhs+=K*g[2][i]-exp(x[i])*g[4][i];
			Wadj[i]=W[i]+Dt/mu*rhs;
		}

		tridiagSolver(L, D, U, Wadj, N-1);
		for (int i = 0; i <=N; ++i)
		{
			W[i]=Wadj[i];
		}

	}

	//
	for(i = 0; i <= N-1; ++i){
		if(x[i]>log(S0)){
			ir=i;
			break;
		}
	}

	ps0 = Wadj[ir-1] + ((Wadj[ir]-Wadj[ir-1])/Dx)*(log(S0)-x[ir-1]);


	cout << "Stock Price " << S0  << endl;
	cout << "Put Value   " << ps0 << "\n" << endl;

	delete [] L;
	delete [] D;
	delete [] U;
	delete [] W;
	delete [] x;	
	for (int i = 0; i < 6; ++i)
	{
		delete [] g[i];
	}
	delete [] g;

	/*gettimeofday(&endTime, NULL); 
	seconds = endTime.tv_sec - startTime.tv_sec; 
	useconds = endTime.tv_usec - startTime.tv_usec; 
	mtime = ((seconds) * 1000 + useconds/1000.0); 
	cout << "Time elapsed was: " << mtime << " (milliseconds)" << endl;*/
	printf("%f",(float(clock()) - float(t1))/ CLOCKS_PER_SEC);
	return 0;
}
示例#8
0
void LogMmse(LogMMSE* logmmse,float* Y,float* Xest)
{

		int i=nFFT-1,j;
		int psiNTdB,gammadB;
		float temp,vk,eivk;
		float gamma[nFFT],psi[nFFT],psiNT[nFFT],SafetyNetPmin[nFFT],NoiseEst[nFFT],smoothpost[nFFT];
		double Speechpresenceprob[nFFT];

		//*******Initial Noise variance Estimation assuming 1st 6 frames are noise********//
		if(framecounter <= 6)
		{
			do
			{
				logmmse->noiseMu[i]+= sqrt(Y[i]);          // Noise mean
				i--;
			}while(i>=0);

			i=nFFT-1;
			do
			{
				logmmse->noiseVar[i] = (float)logmmse->noiseMu[i]*logmmse->noiseMu[i]*2.777777777777778e-02; 	//(abs(fft)/6)^2
				i--;
			}while(i>=0);
		}

		i=nFFT-1;
		do
		{
			gamma[i] = min((Y[i]/logmmse->noiseVar[i]),40);

			if(framecounter == 1)
			{
				psi[i] = 0.98 + 0.02*max((gamma[i]-1),0);
				psiNT[i] = max((0.98 + 0.02*gamma[i]),0.003162277660168);
				logmmse->SafetyNetP[i][SafetyNetInd] = 0.9*Y[i];
			}
			else
			{
				psi[i] = 0.98*(double)logmmse->X[i]/logmmse->noiseVar[i] + 0.02*max((gamma[i]-1),0);
				psi[i] = max(psi[i],0.003162277660168);  														// limiting psi to -25dB
				psiNT[i] = max((0.98*logmmse->Yprev[i]/logmmse->noiseVar[i]+0.02*gamma[i]),0.003162277660168);
				if(SafetyNetInd > 0) logmmse->SafetyNetP[i][SafetyNetInd] = 0.1*logmmse->SafetyNetP[i][SafetyNetInd-1]+0.9*Y[i];
				else logmmse->SafetyNetP[i][SafetyNetInd] = 0.1*logmmse->SafetyNetP[i][numWinPmin]+0.9*Y[i];
			}

			//*******Setting SafetynetPmin********//
			temp = logmmse->SafetyNetP[i][0];
			j=numWinPmin-1;
			do{
				if(temp>logmmse->SafetyNetP[i][j]) temp = logmmse->SafetyNetP[i][j];
				j--;
			}while(j>=0);
			SafetyNetPmin[i] = temp;

			//*******Fetching Noise Estimate********//
			logmmse->Yprev[i] = Y[i];
			psiNTdB = round(max(min(10.0*log10f(psiNT[i]),40),-19))+19;
			gammadB = round(max(min(10.0*log10f(gamma[i]),40),-30))+30;
			NoiseEst[i] = GainTable[classdecision][71*psiNTdB+gammadB]*Y[i];

			i--;
		}while(i >= 0);

		//*******Frequency Smoothing********//
		smoothpost[0] = (gamma[0]+gamma[1])/2;
		i = nFFT-2;
		do
		{
			smoothpost[i] = (gamma[i-1]+gamma[i]+gamma[i+1])/3;
			i--;
		}while(i>0);
		smoothpost[nFFT-1] = (gamma[nFFT-1]+gamma[nFFT-2])/2;

		i=nFFT-1;
		do
		{
			//*******Updating NoiseVariance for next frame********//
			if(framecounter == 1) Speechpresenceprob[i] = (double)0.9;
			else Speechpresenceprob[i] =  (double)0.1*Speechpresenceprob[i]+(double)0.9*(smoothpost[i]>4);

			alphas = 0.85 + 0.15 * Speechpresenceprob[i];
			logmmse->noiseVar[i] = alphas*logmmse->noiseVar[i] + (1-alphas)*NoiseEst[i];

			if((1.5*SafetyNetPmin[i]) > logmmse->noiseVar[i])
			{
				Speechpresenceprob[i] = 0;
				logmmse->noiseVar[i] = 1.5*SafetyNetPmin[i];
			}

			//*******log MMSE Estimator********//
			temp = psi[i]/(1+psi[i]);
			vk = gamma[i]*temp;
			expint(&vk,&eivk);                                  //Evaluating the exponential integral part
			Xest[i] = temp*exp(eivk);
			logmmse->X[i] = Y[i]*Xest[i]*Xest[i];

			i--;
		}while(i>=0);

		SafetyNetInd = SafetyNetInd+1;
		if(SafetyNetInd>=numWinPmin) SafetyNetInd = 0;
		framecounter++;
}