📄 reversefloater.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*//* * ReverseFloater.java * * Created on September 10, 2002, 2:59 PM */package Libor.LiborDerivatives;import Libor.LiborProcess.*;import Statistics.*;import Exceptions.*;import LinAlg.*;/** The <a name="rf">reverse floater</a> <code>RF(p,q,K1,K2)</code> receives * Libor <code>delta_jL_j(T_j)</code> and pays * <code>delta_j*max{K1-L_j(T_j),K2}</code> at time * <code>T_{j+1}, j=p,p+1,...,q-1</code>. Here <code>K1>K2</code>. * * @author Michael J. Meyer */public class ReverseFloater extends LiborDerivative { int p,q; //interval [T_p,T_q] double K1,K2; /******************************************************************************* * * CONSTRUCTOR * ******************************************************************************/ /** <p>Libors <code>L_j</code> needed for <code>j>=p</code> and until * time <code>min(T_q,T_{n-1})</code>.</p> * * @param LP underlying Libor process. * @param p accrual starts <code>T_p</code>. * @param q accrual stops <code>T_q</code>, with * <code>0<p<q<=n</code>. * @param K1 see contract definition <i>LiborProcess.ps</i>. * @param K2 see contract definition <i>LiborProcess.ps</i>. * */ public ReverseFloater (LiborProcess LP, int p, int q, double K1, double K2) { // Libors needed to transport payoff 1 forward from time T_i // to time T_n are L_j, j>=i and they are needed at time t=T_i. super(LP,p,Math.min(q,LP.dimension()-1)); this.p=p; this.q=q; this.K1=K1; this.K2=K2; super.hasControlVariate=true; super.controlVariateNeedsX0=false; // no control variate implemented super.controlVariateNeedsX1=false; } /******************************************************************************* * * FORWARD TRANSPORTED PAYOFF * ******************************************************************************/ /** The <a href=#rf>payoff</a> transported forward to time * <code>T_n</code>, value in current Libor path. */ public double currentForwardPayoff() { double S=0; for(int j=p;j<q;j++){ double XjTj=LP.X(j,j), delta_j=delta[j]; S+=(XjTj-Math.max(delta_j*K1-XjTj,delta_j*K2))*LP.forwardTransport(j+1); } return S; } /******************************************************************************* * * ANALYTIC PRICE * ******************************************************************************/ /** <p>Analytic <code>T_n</code>-forward price at time <code>t</code>. * See <i>LiborProcess.ps</i></p>. * * @param t current discrete time. */ public double analyticForwardPrice(int t) { double S=(2*(LP.B(p,t)-LP.B(q,t))-K1*LP.B_pq(p,q,t))* LP.forwardTransport(t); for(int j=p;j<q;j++) { Caplet cpl_j=new Caplet(LP,j,K1-K2); S-=cpl_j.analyticForwardPrice(t); } return S; } // end analyticForwardPrice /******************************************************************************* * * CONTROL VARIATE * ******************************************************************************/ /** <p>Mean of the control variate conditioned on the state of the * Libor path at time <code>t</code>. This is simply * <code>(B_p(t)-B_q(t))/B_n(t)</code>. See <i>LiborProcess.ps</i>.</p> * * @param t current discrete time <code>t<=p</code>. */ public double controlVariateMean(int t) { return (LP.B(p,t)-LP.B(q,t))/LP.B(n,t); } /** <p>Control variate is sum of forward transported Libors * <code>delta_jL_j(T_j)*B_{j+1}(T_j)/B_n(T_j)= * (B_j(T_j)-B_{j+1}(T_j))/B_n(T_j)</code> * for <code>j=p,p+1,...,q-1</code>. Note that the conditional expectation * <code>E^{P_n}_t(.)</code> of this is the expression * <code>(B_j(t)-B_{j+1}(t))/B_n(t)</code> which collapse under * summation. See <i>LiborProcess.ps</i> .</p> */ public double[] currentControlledForwardPayoff() { double h=currentForwardPayoff(), cv=0; for(int j=p;j<q;j++) cv+=(1-LP.B(j+1,j))/LP.B(n,j); // note 1=LP.B(j,j) for the control variate mean computation return new double[] {h,cv}; } /******************************************************************************* * * TEST PROGRAM * ******************************************************************************/ /** <p>Test program. Allocates a Libor process of dimension * <code>n=15</code> and prices <a href=#rf>reverse floater</a> * <code>RF(p,q,K1,K2)</code> with * <code>p=8, q=15, K1=0.09, K2=0.03</code>. All Libors are intialized * at <code>L_j(0)=0.04</code>.</p> * * <p>Moves the Libor path forward to time <code>t=T_3</code> and computes * the Monte Carlo forward price of the floater * and compares it to the analytic price computed directly from the * Libors at time <code>t=T_3</code>. The correlation of the payoff with * the control variate is computed also.</p> */ public static void main(String[] args) { // Libor process setup int n=15, // dimension of Libor process p=8, q=15; // accrual period [T_p,T_q] double K1=0.09, K2=0.03; // Libor parameter sample final LMM_Parameters lmmParams=new LMM_Parameters(n,LMM_Parameters.CS); final LiborProcess LP=new LiborProcess(lmmParams); final LiborDerivative rf=new ReverseFloater(LP,p,q,K1,K2); // 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 ("\nREVERSE FLOATER: \n"+ "Correlation of payoff with control variate, "+nPath/4+" paths: "); double cvcorr=rf.controlledForwardPayoff(). correlationWithControlVariate(0,nPath/4); cvcorr=FinMath.round(cvcorr,4); System.out.print(cvcorr+"\n\nPrice, "+nPath+" paths:\n\n"); // move full Libor path to time t=T_3 LP.newPath(3,0); // all prices computed at time t=T_3 aprice=rf.analyticForwardPrice(3); mcprice=rf.monteCarloForwardPrice(3,nPath); cvmcprice=rf.controlledMonteCarloForwardPrice(3,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"+ "Controlled Monte Carlo: "+cvmcprice; System.out.println(report); } // end main } // end ReverseFloater
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -