// cap.cc // // Fixed-income binomial option pricing engine // #include #include #include "cap.h" #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b)) f_i_bin::f_i_bin(double ttm,int npers,double sigma,double rrbar,double k) { nper=npers; tinc = ttm/(double) nper; sig = sigma; up = sigma*sqrt(tinc); rbar = rrbar; kappa = k; prfact = kappa*sqrt(tinc)/(2.0*sig);} double f_i_bin::bprice(double r0) { int i,j; double prup; //initialize terminal payoffs //i is the number of up moves for(i=0;i<=nper;i++) { // r[i] = r0 + up * (double)(2*i-nper); not needed for this claim val[i] = 1.0;} //compute prices back through the tree //j is the number of periods from the end //i is the number of up moves from the start for(j=1;j<=nper;j++) {for(i=0;i<=nper-j;i++) { r[i] = r0 + up * (double) (2*i-nper + j); prup = 0.5 + prfact*(rbar-r[i]); prup = MIN(1.0,MAX(0.0,prup)); val[i] = (prup*val[i+1]+(1.0-prup)*val[i])*exp(-r[i]*tinc);}} return(val[0]);} double f_i_bin::cap(double level,double r0) { int i,j; double prup; //initialize terminal payoffs //i is the number of up moves for(i=0;i<=nper;i++) { // r[i] = r0 + up * (double)(2*i-nper); not needed for this claim val[i] = 0.0;} //compute prices back through the tree //j is the number of periods from the end //i is the number of up moves from the start for(j=1;j<=nper;j++) {for(i=0;i<=nper-j;i++) { r[i] = r0 + up * (double) (2*i-nper + j); prup = 0.5 + prfact*(rbar-r[i]); prup = MIN(1.0,MAX(0.0,prup)); val[i] = (prup*val[i+1]+(1.0-prup)*val[i])*exp(-r[i]*tinc) + MAX(0.0,(r[i]-level)*tinc);}} return(val[0]);}