void correctIQ (CXB sigbuf, IQ iq, BOOLEAN isTX, int subchan) { int i; REAL doit; if (IQdoit == 0) return; if (subchan == 0) doit = iq->mu; else doit = 0; if(!isTX) { // if (subchan == 0) // removed so that sub rx's will get IQ correction for (i = 0; i < CXBhave (sigbuf); i++) { iq->del[iq->index] = CXBdata(sigbuf,i); iq->y[iq->index] = Cadd(iq->del[iq->index],Cmul(iq->w[0],Conjg(iq->del[iq->index]))); iq->y[iq->index] = Cadd(iq->y[iq->index],Cmul(iq->w[1],Conjg(iq->y[iq->index]))); iq->w[1] = Csub(iq->w[1], Cscl(Cmul(iq->y[iq->index],iq->y[iq->index]), doit)); // this is where the adaption happens CXBdata(sigbuf,i)=iq->y[iq->index]; iq->index = (iq->index+iq->MASK)&iq->MASK; } //fprintf(stderr, "w1 real: %g, w1 imag: %g\n", iq->w[1].re, iq->w[1].im); fflush(stderr); } else { for (i = 0; i < CXBhave (sigbuf); i++) { CXBimag (sigbuf, i) += iq->phase * CXBreal (sigbuf, i); CXBreal (sigbuf, i) *= iq->gain; } } }
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"); }
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; }
void zroots(fcomplex a[], int m, fcomplex roots[], int polish) { void zroots(fcomplex a[], int m, fcomplex *x, int *its); int i, its, j, jj; fcomplex x, b, c, ad[MAXM]; for (j = 0; j <= m; j++) ad[j] = a[j]; for (j = m; j >= 1; j--) { x Complex(0.0, 0.0); laguer(ad, j, &x, &its); if (fabs(x.i) <= 2.0*EPS*fabs(x.r)) x.i = 0.0; roots[j] = x; b = ad[j]; for (jj = j - 1; jj >= 0; jj--) { c = ad[jj]; ad[jj] = b; b = Cadd(Cmul(x, b), c); } } if (polish) for (j = 1; j <= m; j++) laguer(a, m, &roots[j], &its); for (j = 2; j <= m; j++) { x = roots[j]; for (i = j - 1; i >= 1; i--) { if (rootes.r <= x.r) break; roots[i + 1] = roots[i]; } roots[i + 1] = x; } }
/*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)); }
/*Use the Abate-Whitt for numerical inversion of the Laplace transform*/ static double SumAW(double expiry, double sg, double r, double aa, int terms, int totterms, int nummoment) { int k; double h=sg*sg*expiry/4.0; double Eulero; dcomplex term; dcomplex sum; double *sum_r; sum_r = malloc((totterms-terms+2)*sizeof(double)); sum =Complex(0.0, 0.0); Eulero = 0.0; sum =RCmul(1.0/2.0,dermellin(Complex(aa/(2.0*h),0), sg, r,nummoment)); for (k=1;k<=totterms;k++) { term = RCmul(PNL_ALTERNATE(k) ,dermellin(Complex(aa/(2.0*h) , k*M_PI/h),sg, r,nummoment )); sum = Cadd(term, sum); if(terms<= k) sum_r[k-terms+1]= sum.r; } for (k=0;k<=totterms-terms;k++) { Eulero = Eulero + bico(totterms-terms,k) * pow( 2.0, -(totterms-terms) ) * sum_r[k+1]; } free(sum_r); return exp(aa/2.0)*Eulero/h; }
void SDROMnoiseblanker (NB nb) { int i; for (i = 0; i < CXBsize (nb->sigbuf); i++) { REAL cmag = Cmag (CXBdata (nb->sigbuf, i)); nb->average_sig = Cadd (Cscl (nb->average_sig, 0.75), Cscl (CXBdata (nb->sigbuf, i), 0.25)); nb->average_mag = (REAL) (0.999 * (nb->average_mag) + 0.001 * cmag); if (cmag > (nb->threshold * nb->average_mag)) CXBdata (nb->sigbuf, i) = nb->average_sig; } }
void blms_adapt (BLMS blms) { int sigsize = CXBhave (blms->signal); int sigidx = 0; // fputs("Inside\n",stderr),fflush(stderr); do { int j; memcpy (blms->delay_line, &blms->delay_line[128], sizeof (COMPLEX) * 128); // do overlap move memcpy (&blms->delay_line[128], &CXBdata (blms->signal, sigidx), sizeof (COMPLEX) * 128); // copy in new data fftwf_execute (blms->Xplan); // compute transform of input data for (j = 0; j < 256; j++) { blms->Yhat[j] = Cmul (blms->What[j], blms->Xhat[j]); // Filter new signal in freq. domain blms->Xhat[j] = Conjg (blms->Xhat[j]); // take input data's complex conjugate } fftwf_execute (blms->Yplan); //compute output signal transform for (j = 128; j < 256; j++) blms->y[j] = Cscl (blms->y[j], BLKSCL); memset (blms->y, 0, 128 * sizeof (COMPLEX)); for (j = 128; j < 256; j++) blms->error[j] = Csub (blms->delay_line[j], blms->y[j]); // compute error signal if (blms->filter_type) memcpy (&CXBdata (blms->signal, sigidx), &blms->y[128], 128 * sizeof (COMPLEX)); // if noise filter, output y else memcpy (&CXBdata (blms->signal, sigidx), &blms->error[128], 128 * sizeof (COMPLEX)); // if notch filter, output error fftwf_execute (blms->Errhatplan); // compute transform of the error signal for (j = 0; j < 256; j++) blms->Errhat[j] = Cmul (blms->Errhat[j], blms->Xhat[j]); // compute cross correlation transform fftwf_execute (blms->UPDplan); // compute inverse transform of cross correlation transform for (j = 0; j < 128; j++) blms->update[j] = Cscl (blms->update[j], BLKSCL); memset (&blms->update[128], 0, sizeof (COMPLEX) * 128); // zero the last block of the update, so we get // filter coefficients only at front of buffer fftwf_execute (blms->Wplan); for (j = 0; j < 256; j++) { blms->What[j] = Cadd (Cscl (blms->What[j], blms->leak_rate), // leak the W away Cscl (blms->Update[j], blms->adaptation_rate)); // update at adaptation rate } sigidx += 128; // move to next block in the signal buffer } while (sigidx < sigsize); // done? }
/** * Complex Modified Bessel function of the first kind * divided by exp(|Creal(z)|) * * @param z a complex number * @param v a real number, the order of the Bessel function * */ dcomplex pnl_complex_bessel_i_scaled( double v, dcomplex z ) { int nz, ierr; dcomplex cy; int n = 1; int kode = 2; if (v >= 0) { pnl_zbesi(CADDR(z), &v, &kode, &n, CADDR(cy), &nz, &ierr); DO_MTHERR("ive:"); } else { dcomplex aux1, aux2; aux1 = pnl_complex_bessel_i_scaled (-v, z); aux2 = pnl_complex_bessel_k (-v, z); aux2 = CRmul (aux2, M_2_PI * sin(M_PI * -v) * exp ( -fabs ( Creal(z) ) )); cy = Cadd (aux1, aux2); } return cy; }
/** * Complex Bessel function of the second kind * divided by exp(|Imag(z)|) * * @param z a complex number * @param v a real number, the order of the Bessel function * */ dcomplex pnl_complex_bessel_y_scaled( double v, dcomplex z ) { int nz, ierr; dcomplex cy, cwork; int n = 1; int kode = 2; if (v >= 0) { pnl_zbesy(CADDR(z), &v, &kode, &n, CADDR(cy), &nz, CADDR(cwork), &ierr); DO_MTHERR("yve:"); } else { dcomplex aux1, aux2; aux1 = pnl_complex_bessel_y_scaled (-v, z); aux1 = CRmul (aux1, cos(M_PI * v)); aux2 = pnl_complex_bessel_j_scaled (-v, z); aux2 = CRmul (aux2, sin(M_PI * -v)); cy = Cadd (aux1, aux2); } return cy; }
/** * Complex Bessel function of the first kind * * @param z a complex number * @param v a real number, the order of the Bessel function * */ dcomplex pnl_complex_bessel_j( double v, dcomplex z ) { int nz, ierr; dcomplex cy; int n = 1; int kode = 1; if (v >= 0) { pnl_zbesj(CADDR(z), &v, &kode, &n, CADDR(cy), &nz, &ierr); DO_MTHERR("jv:"); } else { dcomplex aux1, aux2; aux1 = pnl_complex_bessel_j (-v, z); aux1 = CRmul (aux1, cos(M_PI * v)); aux2 = pnl_complex_bessel_y (-v, z); aux2 = CRmul (aux2, sin(M_PI * v)); cy = Cadd (aux1, aux2); } return cy; }
static int compute_price(double tt, double H, double K, double r_premia, double v0, double kappa, double theta, double sigma, double rho, double L, int M, int Nt ) { /*Variables*/ int j, n, k; double r; /*continuous rate*/ double min_log_price, max_log_price; double ds, dt; /*price and time discretization steps*/ double rho_hat; /*parameter after substitution*/ double q, factor, discount_factor; /*pde parameters*/ double treshold = 1e-9; /* when we assume probability to be zero and switch to a different equation*/ int k_d, k_u; /*n+1 vertice numbers, depending on [n][k]*/ double sigma_local, gamma; /*wh factors parameters*/ double beta_minus, beta_plus; /*wh-factors coefficients*/ double local_barrier; /*a barrier depending on [n][k], to check crossing on each step*/ //if (2.0 * kappa * theta < pow(sigma, 2)) // return 1; /*Novikov condition not satisfied, probability values could be incorrect*/ /*Body*/ r = log(1 + r_premia / 100); /*building voltree*/ tree_v(tt, v0, kappa, theta, sigma, Nt); /*spacial variable. Price space construction*/ min_log_price = L*log(0.5) - (rho / sigma)* V[Nt][Nt]; max_log_price = L*log(2); ds = (max_log_price - min_log_price) / double(M); for (j = 0; j < M; j++) { ba_log_prices[j] = min_log_price + j*ds; ba_prices[j] = H*exp(ba_log_prices[j] + (rho / sigma)* V[0][0]); } dt = tt / double(Nt); /*fft frequences we'll need in every vertice of a tree*/ fftfreq(M, ds); rho_hat = sqrt(1.0 - pow(rho, 2.0)); q = 1.0 / dt + r; factor = pow(q*dt, -1.0); //discount_factor = exp(r*dt); discount_factor = r - rho / sigma * kappa * theta; /*filling F_next matrice by initial (in time T) conditions*/ for (j = 0; j < M; j++) for (k = 0; k < Nt + 1; k++) { F_next[j][k] = Complex(G(H*exp(ba_log_prices[j] + (rho / sigma)* V[Nt][k]), K), 0); } /*here the main cycle starts - the backward induction procedure*/ for (n = Nt - 1; n >= 0; n--) { printf("Processing: %d of %d\n", n, Nt-1); for (k = 0; k <= n; k++) { /*to calculate the binomial expectation we should use matrices from the tree method. After (n,k) vertice one could either get to (n+1,k_u) or (n+1, k_d). The numbers k_u and k_d could be read from f_up and f_down matrices, by the rule of addition, for example: f_down[i][j] = -z; Rd = V[i + 1][j - z] f_up[i][j] = z; Ru = V[i + 1][j + z]; */ k_u = k + f_up[n][k]; k_d = k + f_down[n][k]; local_barrier = - (rho / sigma) * V[n][k]; /*initial conditions of a step*/ for (j = 0; j < M; j++) { //f_n_plus_1_k_u[j] = F[j][n+1][k_u]; //f_n_plus_1_k_d[j] = F[j][n+1][k_d]; f_n_plus_1_k_u[j] = F_next[j][k_u]; f_n_plus_1_k_d[j] = F_next[j][k_d]; } /*applying indicator function*/ for (j = 0; j < M; j++) { if (ba_log_prices[j] < local_barrier) { f_n_plus_1_k_u[j].r = 0.0; f_n_plus_1_k_u[j].i = 0.0; f_n_plus_1_k_d[j].r = 0.0; f_n_plus_1_k_d[j].i = 0.0; } } if (V[n][k] >= treshold) { /*set up variance - dependent parameters for a given step*/ sigma_local = rho_hat * sqrt(V[n][k]); gamma = r - 0.5 * V[n][k] - rho / sigma * kappa * (theta - V[n][k]); /*also local*/ /* beta_plus and beta_minus*/ /*beta_minus = -(gamma + sqrt(gamma^2 + 2 * sigma^2 * q)) / sigma^2 beta_plus = -(gamma - sqrt(gamma^2 + 2 * sigma^2 * q)) / sigma^2*/ beta_minus = -(gamma + sqrt(pow(gamma,2) + 2 * pow(sigma_local,2) * q)) / pow(sigma_local,2); beta_plus = -(gamma - sqrt(pow(gamma,2) + 2 * pow(sigma_local,2) * q)) / pow(sigma_local,2); for (j = 0; j < M; j++) { /* factor functions phi_plus_array = ([beta_plus / (beta_plus - i * 2 * pi*xi) for xi in xi_space]) phi_minus_array = ([-beta_minus / (-beta_minus + i * 2 * pi*xi) for xi in xi_space]) */ phi_plus_array[j] = RCdiv(beta_plus, RCsub(beta_plus, RCmul((2.0 * PI * fftfreqs[j]), CI))); phi_minus_array[j] = RCdiv(-beta_minus, RCadd(-beta_minus, RCmul((2.0 * PI * fftfreqs[j]), CI))); } /*factorization calculation*/ /*f_n_k_u = factor * fft.ifft(phi_minus_array * fft.fft( indicator(original_prices_array, 0) * fft.ifft(phi_plus_array * fft.fft(f_n_plus_1_k_u))))*/ for (int j = 0; j < M; j++) { f_n_plus_1_k_u_re[j] = f_n_plus_1_k_u[j].r; f_n_plus_1_k_u_im[j] = f_n_plus_1_k_u[j].i; } pnl_fft2(f_n_plus_1_k_u_re, f_n_plus_1_k_u_im, M); for (j = 0; j < M; j++) { /*putting complex and imaginary part together again*/ f_n_plus_1_k_u_fft_results[j] = Complex(f_n_plus_1_k_u_re[j], f_n_plus_1_k_u_im[j]); /*multiplying by phi_plus*/ f_n_plus_1_k_u_fft_results[j] = Cmul(phi_plus_array[j], f_n_plus_1_k_u_fft_results[j]); /*extracting imaginary and complex parts to use in further fft*/ f_n_plus_1_k_u_fft_results_re[j] = f_n_plus_1_k_u_fft_results[j].r; f_n_plus_1_k_u_fft_results_im[j] = f_n_plus_1_k_u_fft_results[j].i; } pnl_ifft2(f_n_plus_1_k_u_fft_results_re, f_n_plus_1_k_u_fft_results_im, M); /*applying indicator function, after ifft*/ for (j = 0; j < M; j++) { if (ba_log_prices[j] < local_barrier) { f_n_plus_1_k_u_fft_results_re[j] = 0.0; f_n_plus_1_k_u_fft_results_im[j] = 0.0; } } /*performing second fft */ pnl_fft2(f_n_plus_1_k_u_fft_results_re, f_n_plus_1_k_u_fft_results_im, M); for (j = 0; j < M; j++) { /*putting complex and imaginary part together again*/ f_n_plus_1_k_u_fft_results[j] = Complex(f_n_plus_1_k_u_fft_results_re[j], f_n_plus_1_k_u_fft_results_im[j]); /*multiplying by phi_minus*/ f_n_plus_1_k_u_fft_results[j] = Cmul(phi_minus_array[j], f_n_plus_1_k_u_fft_results[j]); /*extracting imaginary and complex parts to use in further fft*/ f_n_plus_1_k_u_fft_results_re[j] = f_n_plus_1_k_u_fft_results[j].r; f_n_plus_1_k_u_fft_results_im[j] = f_n_plus_1_k_u_fft_results[j].i; } /*the very last ifft*/ pnl_ifft2(f_n_plus_1_k_u_fft_results_re, f_n_plus_1_k_u_fft_results_im, M); /*multiplying by factor*/ for (j = 0; j < M; j++) { f_n_k_u[j].r = factor * f_n_plus_1_k_u_fft_results_re[j]; f_n_k_u[j].i = factor * f_n_plus_1_k_u_fft_results_im[j]; } /*f_n_k_d = factor * fft.ifft(phi_minus_array * fft.fft( indicator(original_prices_array, 0) * fft.ifft(phi_plus_array * fft.fft(f_n_plus_1_k_d))))*/ for (int j = 0; j < M; j++) { f_n_plus_1_k_d_re[j] = f_n_plus_1_k_d[j].r; f_n_plus_1_k_d_im[j] = f_n_plus_1_k_d[j].i; } pnl_fft2(f_n_plus_1_k_d_re, f_n_plus_1_k_d_im, M); for (j = 0; j < M; j++) { /*putting complex and imaginary part together again*/ f_n_plus_1_k_d_fft_results[j] = Complex(f_n_plus_1_k_d_re[j], f_n_plus_1_k_d_im[j]); /*multiplying by phi_plus*/ f_n_plus_1_k_d_fft_results[j] = Cmul(phi_plus_array[j], f_n_plus_1_k_d_fft_results[j]); /*extracting imaginary and complex parts to use in further fft*/ f_n_plus_1_k_d_fft_results_re[j] = f_n_plus_1_k_d_fft_results[j].r; f_n_plus_1_k_d_fft_results_im[j] = f_n_plus_1_k_d_fft_results[j].i; } pnl_ifft2(f_n_plus_1_k_d_fft_results_re, f_n_plus_1_k_d_fft_results_im, M); /*applying indicator function, after ifft*/ for (j = 0; j < M; j++) { if (ba_log_prices[j] < local_barrier) { f_n_plus_1_k_d_fft_results_re[j] = 0.0; f_n_plus_1_k_d_fft_results_im[j] = 0.0; } } /*performing second fft */ pnl_fft2(f_n_plus_1_k_d_fft_results_re, f_n_plus_1_k_d_fft_results_im, M); for (j = 0; j < M; j++) { /*putting complex and imaginary part together again*/ f_n_plus_1_k_d_fft_results[j] = Complex(f_n_plus_1_k_d_fft_results_re[j], f_n_plus_1_k_d_fft_results_im[j]); /*multiplying by phi_minus*/ f_n_plus_1_k_d_fft_results[j] = Cmul(phi_minus_array[j], f_n_plus_1_k_d_fft_results[j]); /*extracting imaginary and complex parts to use in further fft*/ f_n_plus_1_k_d_fft_results_re[j] = f_n_plus_1_k_d_fft_results[j].r; f_n_plus_1_k_d_fft_results_im[j] = f_n_plus_1_k_d_fft_results[j].i; } /*the very last ifft*/ pnl_ifft2(f_n_plus_1_k_d_fft_results_re, f_n_plus_1_k_d_fft_results_im, M); /*multiplying by factor*/ for (j = 0; j < M; j++) { f_n_k_d[j].r = factor * f_n_plus_1_k_d_fft_results_re[j]; f_n_k_d[j].i = factor * f_n_plus_1_k_d_fft_results_im[j]; } } else if (V[n][k] < treshold) { /*applying indicator function*/ for (j = 0; j < M; j++) { if (ba_log_prices[j] < local_barrier) { f_n_plus_1_k_u[j].r = 0.0; f_n_plus_1_k_u[j].i = 0.0; f_n_plus_1_k_d[j].r = 0.0; f_n_plus_1_k_d[j].i = 0.0; } } for (j = 0; j < M; j++) { //f_n_plus_1_k_u[j] = F[j][n + 1][k_u]; f_n_plus_1_k_u[j] = F_next[j][k_u]; f_n_k_u[j] = CRsub(f_n_plus_1_k_u[j], discount_factor * dt); f_n_k_d[j] = f_n_k_u[j]; } } /* f_n_k = pd_f[n, k] * f_n_k_d + pu_f[n, k] * f_n_k_u */ for (j = 0; j < M; j++) { f_n_k[j] = Cadd(RCmul(pd_f[n][k], f_n_k_d[j]), RCmul(pu_f[n][k], f_n_k_u[j])); F_prev[j][k] = f_n_k[j]; } } for (j = 0; j < M; j++) { for (int state = 0; state < Nt; state++) { F_next[j][state] = F_prev[j][state]; F_prev[j][state] = Complex(0,0); } } } /*Preprocessing F before showing out*/ for (j = 0; j < M; j++) { if (ba_prices[j] <= H) { F_next[j][0].r = 0; } if (F_next[j][0].r < 0.) { F_next[j][0].r = 0; } } return OK; }
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); } }
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; }
void estdop(char file[],int nDopLines, float *a, float *b,float *c) { #define MULTILOOK 100 complexFloat *signal, *signalNext, *dop; float *x_vec, *y_vec,*phase; int x,line,firstLine; getRec *r; float t1, t2, t3; long double sum; float lastPhase; r=fillOutGetRec(file); firstLine=(r->nLines-nDopLines)/2; signal=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); signalNext=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); dop=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); x_vec=(float *)MALLOC(sizeof(float)*r->nSamples); y_vec=(float *)MALLOC(sizeof(float)*r->nSamples); phase=(float *)MALLOC(sizeof(float)*r->nSamples); for (x = 0; x<r->nSamples; x++) dop[x] = Czero(); getSignalLine(r,firstLine,signalNext,0,r->nSamples); for (line = firstLine+1; line < firstLine+nDopLines; line++) { complexFloat *ptr = signal; signal = signalNext; signalNext = ptr; getSignalLine(r,line,signalNext,0,r->nSamples); for (x = 0; x<r->nSamples; x++) dop[x] = Cadd(dop[x],Cmul(signalNext[x],Cconj(signal[x]))); } /*Multilook the phase data along range.*/ for (x=0;x<r->nSamples/MULTILOOK;x++) { int i; double out_r=0.0,out_i=0.0; for (i=0;i<MULTILOOK;i++) { out_r+=dop[x*MULTILOOK+i].real; out_i+=dop[x*MULTILOOK+i].imag; } dop[x].real = out_r; dop[x].imag = out_i; } /*Phase-unwrap the doppler values into the "phase" array.*/ lastPhase=0; for (x=0;x<r->nSamples/MULTILOOK;x++) { float nextPhase=atan2(dop[x].imag, dop[x].real)*(1.0/(2.0*pi)); while ((nextPhase-lastPhase)<-0.5) nextPhase+=1.0; while ((nextPhase-lastPhase)>0.5) nextPhase-=1.0; phase[x]=nextPhase; lastPhase=nextPhase; } sum = 0.0; for (x = 0; x<r->nSamples/MULTILOOK; x++) { sum += phase[x]; x_vec[x] = x*MULTILOOK; y_vec[x] = phase[x]; } // Don't output this file for now since none of our other software uses it // This file causes the windows uninstaller not to fully uninstall // if (1) /*Output doppler vs. range*/ // { // FILE *f=FOPEN("dop_vs_rng","w"); // for (x=0;x<r->nSamples/MULTILOOK;x++) // fprintf(f,"%.0f\t%f\n",x_vec[x],y_vec[x]); // fclose(f); // } sum = sum / (r->nSamples/MULTILOOK); if (!quietflag) printf(" Constant Average : y = %f\n",(float)sum); if (logflag) { sprintf(logbuf," Constant Average : y = %f\n",(float)sum); printLog(logbuf); } yaxb(x_vec, y_vec, r->nSamples/MULTILOOK, &t1, &t2); if (!quietflag) printf(" Linear Regression : y = %f x + %f\n",t1,t2); if (logflag) { sprintf(logbuf," Linear Regression : y = %f x + %f\n",t1,t2); printLog(logbuf); } yax2bxc(x_vec, y_vec, r->nSamples/MULTILOOK, &t1, &t2, &t3); if (!quietflag) printf(" Quadratic Regression: y = %f x^2 + %f x + %f\n",t1,t2,t3); if (logflag) { sprintf(logbuf," Quadratic Regression: y = %f x^2 + %f x + %f\n",t1,t2,t3); printLog(logbuf); } *a = t3; *b = t2; *c = t1; free((void *)signal); free((void *)signalNext); free((void *)dop); free((void *)x_vec); free((void *)y_vec); free((void *)phase); freeGetRec(r); return; }
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); }
DttSP_EXP void Audio_Callback2 (float **input, float **output, unsigned int nframes) { unsigned int thread; BOOLEAN b = reset_em; BOOLEAN return_empty=FALSE; unsigned int i; for(thread=0;thread<threadno;thread++) { if (top[thread].susp) return_empty = TRUE; } if (return_empty) { for(thread=0;thread<threadno;thread++) { memset (output[2*thread], 0, nframes * sizeof (float)); memset (output[2*thread+1], 0, nframes * sizeof (float)); } return; } if (b) { //fprintf(stderr, "reset_em!\n"); fflush(stderr); //fprintf(stdout,"Audio_Callback2: reset_em = TRUE\n"), fflush(stdout); reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[2*thread], 0, nframes * sizeof (float)); memset (output[2*thread+1], 0, nframes * sizeof (float)); } return; } #if 0 if (diversity.flag) { // Deal with the transmitter first if ((ringb_float_read_space (top[1].jack.ring.o.l) >= nframes) && (ringb_float_read_space (top[1].jack.ring.o.r) >= nframes)) { ringb_float_read (top[1].jack.ring.o.l, output[2], nframes); ringb_float_read (top[1].jack.ring.o.r, output[3], nframes); } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // input: copy from port to ring if ((ringb_float_write_space (top[1].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[1].jack.ring.i.r) >= nframes)) { ringb_float_write (top[1].jack.ring.i.l, input[2], nframes); ringb_float_write (top[1].jack.ring.i.r, input[3], nframes); } else { // rb pathology for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // if enough accumulated in ring, fire dsp if ((ringb_float_read_space (top[1].jack.ring.i.l) >= top[1].hold.size.frames) && (ringb_float_read_space (top[1].jack.ring.i.r) >= top[1].hold.size.frames)) sem_post (&top[1].sync.buf.sem); // // Deal with the diversity channel next // if ((ringb_float_read_space (top[0].jack.ring.o.l) >= nframes) && (ringb_float_read_space (top[0].jack.ring.o.r) >= nframes)) { /*ringb_float_read (top[thread].jack.auxr.o.l, output[l], nframes); ringb_float_read (top[thread].jack.auxr.o.r, output[r], nframes);*/ ringb_float_read (top[0].jack.ring.o.l, output[2], nframes); ringb_float_read (top[0].jack.ring.o.r, output[3], nframes); } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // Deal with the diversity/phased array channel next // input: copy from port to ring if ((ringb_float_write_space (top[0].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[0].jack.ring.i.r) >= nframes) && (ringb_float_write_space (top[2].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[2].jack.ring.i.r) >= nframes)) { REAL *l0 = input[0]; REAL *r0 = input[1]; REAL *l2 = input[4]; REAL *r2 = input[5]; for (i=0;i<nframes;i++) { COMPLEX A = Cmplx(l0[i],r0[i]); COMPLEX B = Cmplx(l2[i],r2[i]); A = Cscl(Cadd(A,Cmul(B,diversity.scalar)),diversity.gain); ringb_float_write (top[0].jack.ring.i.l, &A.re, 1); ringb_float_write (top[0].jack.ring.i.r, &A.im, 1); } /*ringb_float_write (top[thread].jack.auxr.i.l, input[l], nframes); ringb_float_write (top[thread].jack.auxr.i.r, input[r], nframes);*/ } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // if enough accumulated in ring, fire dsp if ((ringb_float_read_space (top[0].jack.ring.i.l) >= top[0].hold.size.frames) && (ringb_float_read_space (top[0].jack.ring.i.r) >= top[0].hold.size.frames)) sem_post (&top[0].sync.buf.sem); // // Deal with 2nd receiver channel now // if ((ringb_float_read_space (top[2].jack.ring.o.l) >= nframes) && (ringb_float_read_space (top[2].jack.ring.o.r) >= nframes)) { /*ringb_float_read (top[thread].jack.auxr.o.l, output[l], nframes); ringb_float_read (top[thread].jack.auxr.o.r, output[r], nframes);*/ ringb_float_read (top[2].jack.ring.o.l, output[4], nframes); ringb_float_read (top[2].jack.ring.o.r, output[5], nframes); } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // input: copy from port to ring if ((ringb_float_write_space (top[2].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[2].jack.ring.i.r) >= nframes)) { ringb_float_write (top[2].jack.ring.i.l, input[4], nframes); ringb_float_write (top[2].jack.ring.i.r, input[5], nframes); } else { // rb pathology for(thread=0;thread<threadno;thread++) { memset (output[thread], 0, nframes * sizeof (float)); memset (output[thread], 0, nframes * sizeof (float)); } return; } // if enough accumulated in ring, fire dsp if ((ringb_float_read_space (top[2].jack.ring.i.l) >= top[2].hold.size.frames) && (ringb_float_read_space (top[2].jack.ring.i.r) >= top[2].hold.size.frames)) sem_post (&top[2].sync.buf.sem); } else #endif for(thread=0; thread<threadno; thread++) { int l=2*thread, r = 2*thread+1; if ((ringb_float_read_space (top[thread].jack.ring.o.l) >= nframes) && (ringb_float_read_space (top[thread].jack.ring.o.r) >= nframes)) { /*ringb_float_read (top[thread].jack.auxr.o.l, output[l], nframes); ringb_float_read (top[thread].jack.auxr.o.r, output[r], nframes);*/ ringb_float_read (top[thread].jack.ring.o.l, output[l], nframes); ringb_float_read (top[thread].jack.ring.o.r, output[r], nframes); } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[2*thread ], 0, nframes * sizeof (float)); memset (output[2*thread+1], 0, nframes * sizeof (float)); } return; } // input: copy from port to ring if ((ringb_float_write_space (top[thread].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[thread].jack.ring.i.r) >= nframes)) { if (diversity.flag && (thread == 0)) { if ((ringb_float_write_space (top[2].jack.ring.i.l) >= nframes) && (ringb_float_write_space (top[2].jack.ring.i.r) >= nframes)) { REAL *l0 = input[0]; REAL *r0 = input[1]; REAL *l2 = input[4]; REAL *r2 = input[5]; for (i=0;i<nframes;i++) { COMPLEX A = Cmplx(l0[i],r0[i]); COMPLEX B = Cmplx(l2[i],r2[i]); A = Cscl(Cadd(A,Cmul(B,diversity.scalar)),diversity.gain); ringb_float_write (top[0].jack.ring.i.l, &A.re, 1); ringb_float_write (top[0].jack.ring.i.r, &A.im, 1); } /*ringb_float_write (top[thread].jack.auxr.i.l, input[l], nframes); ringb_float_write (top[thread].jack.auxr.i.r, input[r], nframes);*/ } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[2*thread ], 0, nframes * sizeof (float)); memset (output[2*thread+1], 0, nframes * sizeof (float)); } return; } } else { ringb_float_write (top[thread].jack.ring.i.l, input[l], nframes); ringb_float_write (top[thread].jack.ring.i.r, input[r], nframes); /*ringb_float_write (top[thread].jack.auxr.i.l, input[l], nframes); ringb_float_write (top[thread].jack.auxr.i.r, input[r], nframes);*/ } } else { // rb pathology //reset_system_audio(nframes); for(thread=0;thread<threadno;thread++) { memset (output[2*thread ], 0, nframes * sizeof (float)); memset (output[2*thread+1], 0, nframes * sizeof (float)); } return; } // if enough accumulated in ring, fire dsp if ((ringb_float_read_space (top[thread].jack.ring.i.l) >= top[thread].hold.size.frames) && (ringb_float_read_space (top[thread].jack.ring.i.r) >= top[thread].hold.size.frames)) sem_post (&top[thread].sync.buf.sem); } }
DttSP_EXP void PolyPhaseFIR (ResSt resst) /****************************************************************************** * CALLING PARAMETERS: * Name Use Description * ____ ___ ___________ * *input pointer to input COMPLEX data array * *output pointer to output COMPLEX data array * RealFIR filter pointer to filter coefficients array * *filterMemoryBuff pointer to buffer used as filter memory. Initialized * all data to 0 before 1st call. length is calculated * from numFiltTaps * filterMemoryBuffLength length of filterMemoryBuff * inputArrayLength length of input array :note that "output" may differ * in length * numFiltTaps number of filter taps in array "filtcoeff": <filterMemoryBuffLength * indexfiltMemBuf index to where next input sample is to be stored in * "filterMemoryBuff",initalized 0 to before first call * interpFactor interpolation factor: output rate = input rate * * "interpFactor" / "deciFactor". * filterPhaseNum filter phase number (index), initialized to 0 before * first call * deciFactor decimation factor: * output rate = (input rate * "interpFactor"/"deciFactor") * numOutputSamples number of output samples placed in array "output" * * CALLED BY: * * RETURN VALUE: * Name Description * ____ ___________ * none * * DESCRIPTION: This function is used to change the sampling rate of the data. * The input is first upsampled to a multiple of the desired * sampling rate and then down sampled to the desired sampling rate. * * Ex. If we desire a 7200 Hz sampling rate for a signal that has * been sampled at 8000 Hz the signal can first be upsampled * by a factor of 9 which brings it up to 72000 Hz and then * down sampled by a factor of 10 which results in a sampling * rate of 7200 Hz. * * NOTES: * Also, the "*filterMemoryBuff" MUST be 2^N REALs long. This * routine uses circular addressing on this buffer assuming that * it is 2^N REALs in length. * ******************************************************************************/ { /****************************************************************************** * LOCAL VARIABLE DECLARATIONS ******************************************************************************* * Type Name Description * ____ ____ ___________ */ int i, j, k, jj; /* counter variables */ COMPLEX *outptr; resst->numOutputSamples = 0; for (i = 0; i < resst->inputArrayLength; i++) { /* * save data in circular buffer */ resst->filterMemoryBuff[resst->indexfiltMemBuf] = resst->input[i]; j = resst->indexfiltMemBuf; jj = j; /* * circular addressing */ resst->indexfiltMemBuf = (resst->indexfiltMemBuf + 1) & resst->MASK; /* * loop through each filter phase: interpolate then decimate */ while (resst->filterPhaseNum < resst->interpFactor) { j = jj; /* output[*numOutputSamples] = 0.0; */ outptr = resst->output + resst->numOutputSamples; *outptr = cxzero; /* * perform convolution */ for (k = resst->filterPhaseNum; k < resst->numFiltTaps; k += resst->interpFactor) { /* output[*numOutputSamples] += filtcoeff[k]*filterMemoryBuff[j]; */ //*outptr += resst->filtcoeff[k]* resst->filterMemoryBuff[j]; *outptr = Cadd (*outptr, Cscl (resst->filterMemoryBuff[j], FIRtap (resst->filter, k))); /* * circular adressing */ j = (j + resst->MASK) & resst->MASK; } /* * scale the data */ /* output[*numOutputSamples]*=(REAL)interpFactor; */ *outptr = Cscl (*outptr, (REAL) resst->interpFactor); resst->numOutputSamples += 1; /* * increment interpolation phase # by decimation factor */ resst->filterPhaseNum += (resst->deciFactor); } /* end while *filterPhaseNum < interpFactor */ resst->filterPhaseNum -= resst->interpFactor; } /* end for inputArrayLength */ } /* end PolyPhaseFir */
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; }
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; }
/*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); }
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; }
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; }