📄 swap.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*//* * Swap.java * * Created on September 10, 2002, 12:30 PM */package Libor.LiborDerivatives;import Libor.LiborProcess.*;import Statistics.*;import Exceptions.*;import LinAlg.*;/** <p>Payer swap <code>swp([T_p,T_q],kappa)</code> settled in arrears pays off * <code>delta_k*(L_k(T_k)-kappa)</code> at time <code>T_{k+1}</code> for * <code>k=p,p+1,...,q-1</code>. Here <code>kappa</code> is the strike rate * and the notional amount of the swap is <code>1</code>.</p> * * <p>At any time <code>t<=T_p</code> the value of receiving Libor * on the interval <code>[T_p,T_q]</code> is the same as receiving the * swap rate <code>S_pq(t)</code> and consequently the price <code>c(t)</code> * of the swap becomes</p> * <p> * <center> * <code>c(t)=B_pq(t)(S_pq(t)-kappa)</code> * </center> * </p> * <p>where <code>B_pq(t)=sum_{k=p}^{q-1}delta_kB_{k+1}(t)</code> is the annuity * on <code>[T_p,T_q]</code> as usual.</p> * * <p>No Libor vector implemented for fast valuation. No control variate * implemented. Why would we want to compute a Monte Carlo price for this swap * at all when the price can be computed directly from the Libors? The answer is * verification of model correctness.</p> * * @author Michael J. Meyer */public class Swap extends LiborDerivative { int p,q; // swap along [T_p,T_q] double kappa; //strike rate /******************************************************************************* * * CONSTRUCTOR * ******************************************************************************/ /** Libors needed for forward payoff are <code>L_j, j>=p</code> up to * time <code>min{T_q,T_{n-1}}</code>. Note: if <code>q=n</code> * nothing happens when transporting forward from time * <code>T_q</code>. * * @param LP underlying Libor process. * @param p swap begins at <code>T_p</code>. * @param q swap ends at <code>T_q</code>. * @param kappa strike rate. */ public Swap (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; } /******************************************************************************* * * FORWARD TRANSPORTED PAYOFF * ******************************************************************************/ /** The payoffs * <code>delta_k*(L_k(T_k)-kappa)=X_k(T_k)-delta_k*kappa</code> * at times <code>T_{k+1}, k=p,p+1,...,q-1</code> transported forward * and aggregated at the horizon <code>T_n</code>, value computed * from current Libor path. */ public double currentForwardPayoff() { double S=0; for(int j=p;j<q;j++) S+=(LP.X(j,j)-delta[k]*kappa)*LP.forwardTransport(j+1); return S; } //end currentForwardPayoff /******************************************************************************* * * ANALYTIC PRICE * ******************************************************************************/ /** <p>Analytic time <code>T_n</code>-forward swap price * <code>c(t)=B_pq(t)(S_pq(t)-kappa)/B_n(t)</code> at time <code>t</code>. * * @param t current discrete time. */ public double analyticForwardPrice(int t) { if(t>p)return 0; // needed for exercise triggers double S_pqt=LP.swapRate(p,q,t); return LP.B_pq(p,q,t)*(S_pqt-kappa)/LP.B(n,t); } //end analyticForwardPrice() /******************************************************************************* * * TEST PROGRAM * ******************************************************************************/ /** <p>Test program. Allocates a Libor process of dimension * <code>n=15</code> and prices the swap * <code>swp([T_5,T_15],0.06)</code>. * * <p>The Monte Carlo forward price is compared to analytic price. * 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=5, q=15; // [T_p,T_q]-swap double kappa=0.06; // strike rate // Libor parameter sample final LMM_Parameters lmmParams=new LMM_Parameters(n,LMM_Parameters.CS); final LiborProcess LP=new LiborProcess(lmmParams); final LiborDerivative swpn=new Swap(LP,p,q,kappa); // all prices forward prices at time T_n double aprice, // approximate analytic price mcprice; // Monte carlo price int nPath=50000; // number of Libor paths System.out.println ("\nSwap forward price, "+nPath+" paths:"); aprice=swpn.analyticForwardPrice(0); mcprice=swpn.monteCarloForwardPrice(0,nPath); aprice=FinMath.round(aprice,8); mcprice=FinMath.round(mcprice,8); String report= "analytic: "+aprice+"\n"+ "Monte Carlo: "+mcprice; System.out.println(report); } // end main } // end Swap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -