📄 cap.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * Cap.java * * Created on September 10, 2002, 11:20 PM */package Libor.LiborDerivatives;import Libor.LiborProcess.*;import Statistics.*;import Exceptions.*;/** <p>The cap on <code>[T_p,T_q]</code> with strike rate <code>kappa</code> * implemented as the sum of individual caplets.</p> * * @author Michael J. Meyer */public class Cap extends LiborDerivative { int p,q; // caps Libor on [T_p,T_q] double kappa; //strike rate Caplet[] cplts; // the array of individual cpalets // shift to zero based indics i->i-p /******************************************************************************* * * CONSTRUCTOR * ******************************************************************************/ /** Libors <code>L_j</code> needed for <code>j>=p</code> and until time * <code>min(T_q,T_{n-1})</code> (for forward transporting the payoff). * * @param LP underlying Libor process. * @param p caps Libor on <code>[T_p,T_q]</code>. * @param q caps Libor on <code>[T_p,T_q]</code>. * @param kappa strike rate. */ public Cap(LiborProcess LP, int p, int q, double kappa) { super(LP,p,Math.min(q,LP.dimension()-1)); this.p=p; this.q=q; this.kappa=kappa; hasControlVariate=true; controlVariateNeedsX0=false; controlVariateNeedsX1=false; cplts=new Caplet[q-p]; for(int i=p;i<q;i++)cplts[i-p]=new Caplet(LP,i,kappa); } /******************************************************************************* * * FORWARD TRANSPORTED PAYOFF * ******************************************************************************/ /** Payoff from current Libor path transported forward to time * <code>T_n</code>. The sum of the individual caplet payoffs. */ public double currentForwardPayoff() { double S=0; for(int i=p;i<q;i++)S+=cplts[i-p].currentForwardPayoff(); return S; } /** Mean of the control variate conditioned on the state of the * Libor path at time <code>t</code>. The sum of the individual * caplet control variate means. * * @param t current discrete time (continuous time <code>T_t</code>). */ public double controlVariateMean(int t) { double S=0; for(int i=p;i<q;i++)S+=cplts[i-p].controlVariateMean(t); return S; } /** Control variate is the sum of forward transported Libors * <code>L_i(T_i)*B_{i+1}(T_i)/B_n(T_i), j=p,...,q-1</code>. * See <i>LiborProcess.ps</i> */ public double[] currentControlledForwardPayoff() { double[] S={0,0}; for(int i=p;i<q;i++) Vector.add(S,cplts[i-p].currentControlledForwardPayoff()); return S; } /******************************************************************************* * * ANALYTIC PRICE * ******************************************************************************/ /** <p>The sum of the caplet prices.</p> * * @param t current discrete time. */ public double analyticForwardPrice(int t) { double S=0; for(int i=p;i<q;i++)S+=cplts[i-p].analyticForwardPrice(t); return S; } // end analyticForwardPrice /******************************************************************************* * * TEST PROGRAM * ******************************************************************************/ /** <p>Test program. Allocates a Libor process of dimension * <code>n=15</code> and prices the (at the money) cap * <code>cap([T_5,T_15],0.04)</code>. * * <p>Compares the Monte Carlo forward price to the analytic price and * computes the correlation of the payoff with the control variate.</p> */ public static void main(String[] args) { // Libor process setup int n=15, // dimension of Libor process p=5,q=15; // caps Libor on [T_p,T_q] double kappa=0.04; // strike rate // Libor parameter sample final LMM_Parameters lmmParams=new LMM_Parameters(n,LMM_Parameters.CS); final LiborProcess LP=new LiborProcess(lmmParams); final LiborDerivative cap=new Cap(LP,p,q,kappa); // all prices forward prices at time T_n double aprice, // analytic price mcprice, // Monte carlo price cvmcprice; // Monte carlo price with control variate int nPath=40000; // number of Libor paths System.out.print ("\nCAP: \n"+ "Correlation of payoff with control variate, "+nPath/4+" paths: "); double cvcorr=cap.controlledForwardPayoff(). correlationWithControlVariate(0,nPath/4); cvcorr=FinMath.round(cvcorr,4); System.out.print(cvcorr+"\n\nPrice, "+nPath+" paths:\n\n"); aprice=cap.analyticForwardPrice(0); mcprice=cap.monteCarloForwardPrice(0,nPath); cvmcprice=cap.controlledMonteCarloForwardPrice(0,nPath); aprice=FinMath.round(aprice,8); mcprice=FinMath.round(mcprice,8); cvmcprice=FinMath.round(cvmcprice,8); String report= "analytic: "+aprice+"\n"+ "Monte Carlo: "+mcprice+"\n"+ "Monte Carlo with control variate: "+cvmcprice; System.out.println(report); } // end main } // end Cap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -