📄 cs_factorloading.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*//* * CS_FactorLoading.java * * Created on August 25, 2002, 3:11 PM */package Libor.LiborProcess;import LinAlg.*;import Statistics.Random;/** Implements the correlation and volatility structure from the document * <i>LiborProcess.ps</i> which follows ideas of B. Coffey and J. * Schoenmakers (CS). * * @author Michael J. Meyer */public class CS_FactorLoading extends FactorLoading { /** Parameters describing the volatility and correlation structure * as described in <i>LiborProcess.ps</i>. * */ double A,D,alpha,beta,rho00; /** Array of factors <code>c_j</code> defining the volatilities * <code>sigma_j</code> as <code>sigma_j=c_jg(1-t/T_j)</code>. * See <i>LiborProcess.ps</i>. */ double[] c; /** The matrix of instantaneous log-Libor correlations. * Warning <code>rho[i][j]=rho_{i+1,j+1}</code> since the Libors * <code>L_i(t)</code> start at i=1,2,... but array indexing starts at * zero. */ double[][] rho; /******************************************************************************* * * THE CORRELATION MATRIX * ******************************************************************************/ /** Accessor to correlation matrix <code>rho[][]</code>. * WARNING: <code>rho[i][j]=rho_{i+1,j+1}</code> because we do not * store the correlations <code>rho_{0,j}</code>. */ public double[][] getRho(){ return rho; } /** The correlation matrix <code>rho_ij</code> as a <code>ColtMatrix</code>. * See <i>LiborProcess.ps</i>. Used for testing purposes only. */ public ColtMatrix correlationMatrix(){ return new ColtMatrix(rho); } /******************************************************************************* * * AUXILLIARY FUNCTIONS * *******************************************************************************/ //EXPONENTIAL INTEGRALS: // int exp(s/D)ds double F(double D, double s){ return D*Math.exp(s/D); } // int s*exp(s/D)ds double G(double D, double s){ return D*Math.exp(s/D)*(s-D); } // int s^2*exp(s/D)ds double H(double D, double s){ return D*Math.exp(s/D)*((s-D)*(s-D)+D*D); } /** The convex function f(x) from which the log-Libor correlations * are derived. See i>LiborProcess.ps</i>. */ private double f(double x) { double a=alpha/2, b=(beta-alpha)/6, c=Math.log(rho00)-(a+b); return x*(c+x*(a+b*x)); //cx+ax^2-bx^3 } /** The function g(x) defining the volatilities <code>sigma_j</code> * as <code>sigma_j=c_jg(1-t/T_j)</code>. See <i>LiborProcess.ps</i>. */ double g(double x){ return 1+A*x*Math.exp(-x/D); } //The integral int_0^T g(s)^2ds //TEST THIS !!!!! double integral_g_squared(double T) { double R=T; R+=2*A*(G(-D,T)+D*D); R+=A*A*(H(-D/2,T)+D*D*D/4); //now R=int_0^T g^2(s)ds, see Tex document LiborSimulation II.1.equation(4) return R; } //end Integral_g_squared // int_t^T g(1-s/a)g(1-s/b)ds, TeX document LiborSimulation II.4 // TEST THIS!!!! private double integral_gg(double t, double T, double a, double b) { double R,d,da,db,Da,Db,Dab; d=A*Math.exp(-1/D); da=d/a; db=d/b; Da=D*a; Db=D*b; Dab=Da*Db/(Da+Db); R=T-t; R+=d*(F(Da,T)-F(Da,t)); R+=d*(F(Db,T)-F(Db,t)); R+=d*d*(F(Dab,T)-F(Dab,t)); R-=da*(G(Da,T)-G(Da,t)); R-=db*(G(Db,T)-G(Db,t)); R-=d*(da+db)*(G(Dab,T)-G(Dab,t)); R+=da*db*(H(Dab,T)-H(Dab,t)); //now R=int_t^Tg(1-u/a)g(1-u/b)du return R; } //end Integral_gg /******************************************************************************* * * CONSTRUCTOR * *******************************************************************************/ /** For the meaning of the parameters A,D,alpha,beta,rho00 see * <i>LiborProcess.ps</i>. Parameter array <code>c[ ]</code> starts with * <code>c[0]=c_0</code> even though <code>L_0(t)</code> is constant and * so superfluous. * * @param n number of forward Libors including <code>L_0</code>. * @param c array of <code>c_j</code> factors calibrating volatilities to * caplet prices. * @param Tc continuous time tenor structure * <code>(0=T_0,T_1,...,T_n)</code>. */ public CS_FactorLoading (int n, double A, double D, double alpha, double beta, double rho00, double[] c, double[] Tc) { super(n,Tc); //System.out.print //("\n\nAllocating volatility and correlation structure, time: "); //double before, after, time; //before=System.currentTimeMillis(); this.n=n; this.A=A; this.D=D; this.alpha=alpha; this.beta=beta; this.rho00=rho00; this.c=c; // allocate and initialize the correlation base b[]=(b_1,...,b_{n-1}) // WARNING: <code>b[i]=b_{i+1}. double[] b=new double[n-1]; for(int i=0;i<n-1;i++) { double x=((double)i)/(n-2); b[i]=Math.exp(-f(x)); } // allocate and initialize the correlation matrix // rho=(rho_ij)_{i.j=1}^{n-1} // WARNING: rho[i][j]=rho_{i+1,j+1} rho=new double[n-1][n-1]; for(int i=0;i<n-1;i++) for(int j=0;j<=i;j++){ double q=b[j]/b[i]; rho[i][j]=q; rho[j][i]=q; } // initialize the matrix arrays super.covariationMatrices and // super.choleskyRoots. for(int t=0;t<n-1;t++) { ColtMatrix cv_t=logCovariationMatrix(t), cr_t=cv_t.choleskyRoot(); covariationMatrices.initialize(t,cv_t); choleskyRoots.initialize(t,cr_t); } //after=System.currentTimeMillis(); //time=after-before; //System.out.print(time+"\n"); } // end constructor /******************************************************************************* * * VOLATILITIES, CORRELATIONS, COVARIATION INTEGRALS * *******************************************************************************/ /** <p>Instantaneous log-Libor correlations <code>rho_ij</code> * for <code>i,j>=1</code>.</p> * *@param i index of L_i(t), must satisfy <code>i>=1</code>. *@param j index of L_j(t), must satisfy <code>j>=1</code>. */ public double rho(int i, int j){ return rho[i-1][j-1]; } /** Volatility <code>sigma_i(t)</code> of <code>log(L_i(t))</code>, * defined on <code>[0,T_i]</code>. * *@param i index of forward Libor <code>L_i(t)</code>. *@param t current <i>continuous</i> time. */ public double sigma(int i, double t){ return c[i]*g(1-t/Tc[i]); } /** The integral<br> * <code> * integral_t^T sigma_i(s)sigma_j(s)rho_ijds =<log(L_i),log(L_j)>_t^T * </code><br> * neeeded for the distribution of time step increments. * See document LiborProcess.ps * *@param i index of forward Libor <code>L_i(t)</code>. *@param j index of forward Libor <code>L_j(t)</code>. */ public double integral_sgi_sgj_rhoij(int i, int j, double t, double T) { return c[i]*c[j]*rho(i,j)*integral_gg(t,T,Tc[i],Tc[j]); }/******************************************************************************* * * SAMPLE FACTOR LOADING * *******************************************************************************/ /** <p>Provides a sample <code>CS_FactorLoading</code> object of dimension * <code>n</code>. Parameter values are as follows:</p> * <p> * <center> * <code>delta_j=0.25, c_j=0.25, A=1.3, D=2, alpha=0.9, beta=0.01, * rho00=0.9*exp(-0.02*n). * </code> * </center> * </p> * <p>This choice implies annualized Libor volatilities of about 33% * and a humped volatility peaking halfway out to the reset date.</p> * * @param n dimension of the Libor process */ public static CS_FactorLoading sample(int n) { double delta=0.25, A=1.5, D=2, alpha=1.8, beta=0.1, rho00=0.4; double[] c=new double[n], Tc=new double[n+1]; for(int i=0;i<n;i++) c[i]=0.25; for(int i=0;i<n+1;i++) Tc[i]=i*delta; return new CS_FactorLoading(n,A,D,alpha,beta,rho00,c,Tc); } // end sample /******************************************************************************* * * toString() * *******************************************************************************/ /** A message what type of factor loading it is, all the parameter values. */ public String toString() { String stringRep= "\nCS_FactorLoading:"+ "\nn="+n+", A="+A+", D="+D+", alpha="+alpha+", beta="+beta+ ", rho00="+rho00; return stringRep; } /******************************************************************************* * * TEST PROGRAM * *******************************************************************************/ /** Small test program. Allocates a factor loading of size * <code>n=50</code> to time the computation of the matrix arrays * (happens in the constructor). There is a separate class of * <code>junit</code> tests which tests the class more thoroughly. */ public static void main(String[] args) { sample(50); } // end main} // end CS_FactorLoading
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -