📄 syntheticdata.java
字号:
/* * SyntheticData.java * * Created on September 20, 2002, 12:21 AM */package Libor.LiborProcess;import Libor.LiborDerivatives.*;import java.io.*;/** <p>This class has static methods to produce a set of caplet and swaption * prices from the analytic pricing formulas in two separate files * <code>CapletFile</code> and <code>SwaptionFile</code> in the precise format * in which a {@link Calibrator} object expects these files for reading in the * data.</p> * * <p>Caplets with indices divisible by 3 are omitted to test the caplet * interpolation routine. Only swaptions where the underlying swap has a * period <code>q-p</code> of at least <code>5</code> accrual intervals are * written. Consequently the dimension <code>n</code> of the underlying * Libor process must be at least <code>5</code>. * * <p>The prices are derived from a Libor process based on an * {@link EP_FactorLoading} and are intended for calibration of a * {@link CS_FactorLoading}.</p> * * @author Michael J. Meyer */public class SyntheticData { String capletFile, swaptionFile; FileWriter capletWriter, swaptionWriter; /** * @param capletFile name of file containing caplet data. * @param swaptionFile name of file containing swaption data. */ public SyntheticData(String capletFile, String swaptionFile) { this.capletFile=capletFile; this.swaptionFile=swaptionFile; try{ capletWriter=new FileWriter(capletFile); swaptionWriter=new FileWriter(swaptionFile); } catch(IOException e) { System.out.println ("SyntheticData(): output files could not be allocated\n"+ "Aborting."); } } // end constructor /** <p>Allocates and writes the files of caplet and swaption prices into * the current directory. * A Libor process based on an {@link EP_FactorLoading} and analytic * pricing formula are used. The files are written in the form in which * a {@link Calibrator} object expects them to read in the data.</p> * * <p>All initial Libors are set to <code>L_j(0)=0.04</code> and at the * money caplets and swaptions are written to the files.</p> * * @param n dimension of the underlying Libor process. */ public void writeData(int n) throws IOException { FactorLoading fl=EP_FactorLoading.sample(n); // strike rate and initial Libors double kappa=0.04; // intial term structure of forward Libors L_j(0) double[] l=new double[n]; for(int i=0;i<n;i++)l[i]=kappa; // the Libor process LiborProcess LP=new LiborProcess(l,fl); // write caplets, leave out every third one for(int i=0;i<n;i++){ if(i%3!=0){ Caplet cplt_i=new Caplet(LP,i,kappa); double price=LP.B0(n)*cplt_i.analyticForwardPrice(0); capletWriter.write(i+" "); capletWriter.write(kappa+" "); capletWriter.write(price+" "); capletWriter.write("\n"); } } // end caplets capletWriter.close(); // write swaptions with q-p>4, p>=1 sine at the money swaptions which // exercised immediately are worthless. for(int p=1;p<n-5;p++) for(int q=p+5;q<n;q++){ Swaption swpn_pq=new Swaption(LP,p,p,q,kappa); double price=LP.B0(n)*swpn_pq.analyticForwardPrice(0); swaptionWriter.write(p+" "); swaptionWriter.write(q+" "); swaptionWriter.write(kappa+" "); swaptionWriter.write(price+" "); // don't write a "\n" at the end, eof must follow // the last number! if((p<n-6)||(q<n-1))swaptionWriter.write("\n"); } // end caplets swaptionWriter.close(); System.out.println ("Finished. Results are in the files "+capletFile+ " and "+swaptionFile+"."); } // end writeData /******************************************************************************* * * TEST PROGRAM * ******************************************************************************/ /** <p>Test program. * Writes a data set in dimension <code>n=15</code>.</p> */ public static void main(String[] args) throws IOException { new SyntheticData("Caplets","Swaptions").writeData(15); } } // end synthetic data
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -