⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 triggerswap.java

📁 金融资产定价,随机过程,MONTE CARLO 模拟 JAVA 程序和文档资料
💻 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*//* * TriggerSwap.java * * Created on September 10, 2002, 10:15 PM */package Libor.LiborDerivatives;import Libor.LiborProcess.*;import Statistics.*;import Exceptions.*;import LinAlg.*;/** <p>The trigger swap on <code>[T_p,T_q]</code> with trigger level  *  <code>K</code> and strike rate <code>kappa</code> is triggered at the first  *  time <code>T_j</code> such that <code>L_j(T_j)>K</code> and then  *  initiates a swap <code>swap([T_j,T_q],kappa)</code>.</p> * * <p>This is a nasty derivative for which Monte Carlo converges very slowly. * You might want to think about writing pricing routines that generate  * Libor paths until a desired precision is reached with desired confidence. * This is a trivial task since the forward payoff is available as a  * (controlled) {@link Statistics.RandomVariable} and this class offers such * methods.</p> *  * <p>The payoff of the cap <code>cap([T_p,T_q],kappa)</code> * shows excellent correlation with the trigger swap payoff  * (run <code>main()</code> for an example) and is thus used as a control  * variate for the trigger swap.</p> * * @author  Michael J. Meyer */public class TriggerSwap extends LiborDerivative {         int p,q;                   //swap interval [T_p,T_q]      double kappa,              //strike level            K;                  //trigger level           Swap[] swap;               // the array of possible swaps                                // shift to zero based indices j->j-p     Cap cap;                   // the cap used as control variate     //LiborDerivative::T_max = min(T_q,T_{n-1})     //Liborderivative::k == p    /******************************************************************************* * *                           CONSTRUCTOR * ******************************************************************************/           /** <p>Libors <code>L_j</code> needed for <code>j&gt;=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&lt;p&lt;q&lt;=n</code>.     * @param kappa strike level.     * @param K trigger level.     *      */    public TriggerSwap    (LiborProcess LP, int p, int q, double kappa, double K)     {        super(LP,p,Math.min(q,LP.dimension()-1));        this.p=p;        this.q=q;        this.kappa=kappa;        this.K=K;        super.hasControlVariate=true;        super.controlVariateNeedsX0=false;            super.controlVariateNeedsX1=false;                 swap=new Swap[q-p];        for(int j=p;j<q;j++)swap[j-p]=new Swap(LP,j,q,kappa);                cap=new Cap(LP,p,q,kappa);            } // end constructor    /******************************************************************************* * *                        FORWARD TRANSPORTED PAYOFF * ******************************************************************************/                   /** The payoff transported forward to time     *  <code>T_n</code>, value in current Libor path.     */    public double currentForwardPayoff()    {                  for(int j=p;j<q;j++)         if(LP.L(j,j)>K)return swap[j-p].currentForwardPayoff();                 return 0;      }             /******************************************************************************* * *                            CONTROL VARIATE * ******************************************************************************/                 /** Control variate is the cap <code>cap([T_p,T_q],kappa)</code>      *  forward payoff and so its mean is the cap forward price.      *      * @param t current discrete time <code>t&lt;=p</code>.      */     public double controlVariateMean(int t)     {             return cap.analyticForwardPrice(t);     }              /** Control variate is the forward payoff of the cap<br>      *  <code>cap([T_p,T_q],kappa)</code>.      */     public double[] currentControlledForwardPayoff()     {         double h=currentForwardPayoff(),                cv=cap.currentForwardPayoff();            return new double[] {h,cv};       }               /******************************************************************************* * *                              TEST PROGRAM * ******************************************************************************/        /** <p>Test program. Allocates a Libor process of dimension       *  <code>n=15</code> and prices the trigger swap      *  <code>TRGSWP(p,q,kappa,K)</code> with       *  <code>p=5, q=15, kappa=0.06, K=0.10</code>. All Libors are intialized      *  at <code>L_j(0)=0.04</code>.</p>      *      * <p>The correlation of the trigger swap payoff with the control      * variate is computed also.</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>.</p>      */     public static void main(String[] args)     {         // Libor process setup         int n=15,                    // dimension of Libor process             p=5, q=15,               // accrual period [T_p,T_q]             nPath=40000;             // number of Libor paths                  double kappa=0.06, K=0.10;                // Libor parameter sample          final LMM_Parameters lmmParams=new LMM_Parameters(n,LMM_Parameters.CS);         final LiborProcess LP=new LiborProcess(lmmParams);                final LiborDerivative trgswp=new TriggerSwap(LP,p,q,kappa,K);                System.out.print         ("\nTRIGGER SWAP: \n"+          "Correlation of payoff with control variate, "+nPath/4+" paths: ");         double cvcorr=trgswp.controlledForwardPayoff().                              correlationWithControlVariate(0,nPath/4);         cvcorr=FinMath.round(cvcorr,4);         System.out.print(cvcorr+"\n\nPrice, "+nPath+" paths:\n\n");                  // all prices forward prices at time T_n         double mcprice,              // Monte carlo price                cvmcprice;            // Monte carlo price with control variate                mcprice=trgswp.monteCarloForwardPrice(0,nPath);         cvmcprice=trgswp.controlledMonteCarloForwardPrice(0,nPath);                  mcprice=FinMath.round(mcprice,8);         cvmcprice=FinMath.round(cvmcprice,8);                String report=         "Monte Carlo: "+mcprice+"\n"+         "Controlled Monte Carlo: "+cvmcprice;         System.out.println(report);                    } // end main } // end TriggerSwap

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -