Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

LiborCalibrator.h

Go to the documentation of this file.
00001 /* WARANTY NOTICE AND COPYRIGHT
00002 This program is free software; you can redistribute it and/or
00003 modify it under the terms of the GNU General Public License
00004 as published by the Free Software Foundation; either version 2
00005 of the License, or (at your option) any later version.
00006 
00007 This program is distributed in the hope that it will be useful,
00008 but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 GNU General Public License for more details.
00011 
00012 You should have received a copy of the GNU General Public License
00013 along with this program; if not, write to the Free Software
00014 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00015 
00016 Copyright (C) Michael J. Meyer
00017 
00018 matmjm@mindspring.com
00019 spyqqqdia@yahoo.com
00020 
00021 */
00022 
00023 #ifndef martingale_lmm_calibrator_h    
00024 #define martingale_lmm_calibrator_h
00025 
00026 #include "TypedefsMacros.h"
00027 #include "Utils.h"
00028 #include "FinMath.h"
00029 #include "Matrix.h"                         // direct members 
00030 #include "Optimizer.h"                      // base class
00031 #include "VolatilityAndCorrelation.h"       // reference to VolSurface::JR, Correlations::CS
00032 #include <fstream>                          // direct members ifstream, ofstream
00033 
00034 
00035 MTGL_BEGIN_NAMESPACE(Martingale)
00036 
00037 // dependencies
00038 class std::ostream;
00039 class LiborFactorLoading;
00040 // class RealArray1D;
00041 // class RealVector;
00042 // class UTRRealMatrix;
00043 // template<class> class Array1D;
00044 // class VolSurface;
00045 // extern int VolSurface::JR;
00046 // class Correlations;
00047 // extern int Correlations::CS;
00048 
00049 
00050 
00058 /**********************************************************************************
00059  *
00060  *               LmmOptimizer
00061  *
00062  *********************************************************************************/
00063  
00064 
00065 // forward declaration
00066 class LmmCalibrator;
00067 
00068 
00071 class SobolLiborCalibrationOptimizer : public SobolSearch {
00072         
00073 protected:
00074         
00075         LmmCalibrator* cal;
00076         
00077 public:
00078         
00086         SobolLiborCalibrationOptimizer
00087         (LmmCalibrator* clbr, const RealArray1D& x0, int nVals, const RealArray1D& delta) : 
00088         SobolSearch(x0,nVals,delta,true),
00089         cal(clbr)
00090         {    }
00091         
00092         bool isInDomain(const RealArray1D& x) const;
00093         
00094         Real f(const RealArray1D& x);
00095         
00096 }; // end SobolLiborCalibrationOptimizer
00097                                 
00098 
00099 
00100 
00101 
00102 /**********************************************************************************
00103  *
00104  *               Swaptions and Caplets
00105  *
00106  *********************************************************************************/
00107  
00111 struct SwaptionData {
00112         
00113         int p,q;
00114         Real strike, forwardPrice, calibratedForwardPrice, error;
00115         
00117         SwaptionData() :
00118         p(0), q(0), strike(0.0), forwardPrice(0.0), 
00119         calibratedForwardPrice(0.0), error(0.0)
00120     {   }
00121         
00122 };
00123 
00124 
00125 
00131 std::ostream& operator << (std::ostream& os, const SwaptionData& swpn);
00132 
00133 
00135 std::istream& operator >> (std::istream& is, SwaptionData& swpn);
00136 
00137 
00138 
00139 
00143 struct CapletData {
00144         
00145         int i;
00146         Real strike, forwardPrice, calibratedForwardPrice, error;
00147         
00149         CapletData() :
00150         i(0), strike(0.0), forwardPrice(0.0), 
00151         calibratedForwardPrice(0.0), error(0.0)
00152     {   }
00153         
00154 };
00155  
00156  
00157 
00160 std::ostream& operator << (std::ostream& os, const CapletData& cplt);
00161 
00164 std::istream& operator >> (std::istream& is, CapletData& cplt);
00165 
00166 
00167 
00168 /**********************************************************************************
00169  *
00170  *               LiborCalibrator
00171  *
00172  *********************************************************************************/
00173         
00174 
00195 class LmmCalibrator {
00196         
00197         
00198 protected:
00199         
00200         LiborFactorLoading* factorLoading;
00201         int n;                                   // dimension of Libor process
00202         // from the factorloading:
00203         const RealArray1D& X0;                   // X0[j]=X_j(0)
00204         const RealArray1D& delta;                // delta[j]=delta_j, accrual periods
00205          
00206         // caching some quantities
00207         RealVector U0;                           // U0[j]=U_j(0)
00208         RealVector H0;                           // H0[j]=H_j(0) 
00209         Real Bn0;                                // B_n(0)
00210         UTRRealMatrix S_pq0;                     // swaprates S_pq(0) 
00211         
00212         // reading data
00213         ifstream capletsIn;
00214         ifstream swaptionsIn;
00215         
00216         // writing synthetic data, calibration results
00217         ofstream logFile;
00218         ofstream capletsOut;    
00219         ofstream swaptionsOut;
00220         
00221         Array1D<CapletData*> caplets;
00222         Array1D<SwaptionData*> swaptions;
00223                 
00224         
00225 public:
00226         
00227 // CONSTRUCTOR
00228         
00229         LmmCalibrator
00230         (LiborFactorLoading* fl,
00231          const char* capletsInFile="CapletsIn.txt",  const char* swaptionsInFile="SwaptionsIn.txt",
00232          const char* capletsOutFile="CapletsOut.txt", const char* swaptionsOutFile="SwaptionsOut.txt");
00233          
00234         virtual ~LmmCalibrator(){ }
00235         
00236 // ACCESSORS
00237         
00238         LiborFactorLoading* getFactorLoading(){ return factorLoading; }
00239         Array1D<CapletData*>& getCaplets(){ return caplets; }
00240         Array1D<SwaptionData*>& getSwaptions(){ return swaptions; }
00241         
00242 // READING AND WRITING CAPLETS AND SWAPTIONS
00243 // Instrument = CapletData, SwaptionData  
00244 
00247         template<typename Instrument>
00248         void read(istream& is, Array1D<Instrument*>& instruments)
00249     {
00250                 for(int i=1;i<n;i++){
00251                 
00252                         instruments[i] = new Instrument();
00253                         is >> *(instruments[i]);
00254                 }       
00255     } // end read
00256 
00257         
00261         template<typename Instrument>
00262         void write(ostream& os, const Array1D<Instrument*>& instruments)
00263     {
00264                 for(int i=1;i<n;i++){
00265                         
00266                         Instrument* currentInstrument=instruments[i];
00267                         os << *currentInstrument;
00268                         os << "  " << currentInstrument->calibratedForwardPrice
00269                            << "  " << currentInstrument->error << endl;
00270                 }
00271     } // end write
00272         
00273         
00274         void readCaplets();
00275         void readSwaptions();
00276         
00279         void writeCaplets();
00280         
00283         void writeSwaptions();
00284         
00285         
00286 // CORRELATIONS
00287         
00289         Real rho(int i, int j);
00290         
00291 // ACCRUAL FACTORS, BONDS, SWAP RATES, .. AT TIME ZERO
00292         
00294     Real L_i0(int i);
00295          
00297     Real H_i0(int i);
00298      
00300     Real B_i0(int i);
00301         
00303         Real H_pq0(int p, int q);
00304         
00306         Real B_pq0(int p, int q);
00307         
00309     Real swapRate(int p, int q);
00310      
00311         
00312 // CAPLET AND SWAPTION PRICES
00313 
00318     Real capletForwardPrice(int i, Real strike, Real Sigma);
00319         
00320         
00325     Real swaptionForwardPrice(int i, Real strike, Real Sigma);
00326 
00327 
00336    virtual Real capletForwardPrice(int i, Real strike){ return 0.0; }
00337 
00338    
00347    virtual Real swaptionForwardPrice(int i, Real strike){ return 0.0; }
00348    
00349    
00350 // WRITING SYNTHETIC DATA
00351    
00363    void writeSyntheticData();
00364    
00365    
00366 // OBJECTIVE FUNCTION
00367         
00368         
00372    virtual Real objectiveFunction() = 0;
00373    
00374    
00375                 
00379    virtual Real objectiveFunction(const RealArray1D& x) = 0;
00380    
00381 
00382         
00383 // CALIBRATION 
00384 
00385    
00390    Real meanRelativeCalibrationError();
00391    
00392 
00393      
00400    virtual LiborFactorLoading* calibrate(int nVals) = 0;
00401    
00402    
00403 private:
00404 
00409         void recordCapletCalibration();
00410    
00411         
00412 }; // end LiborCalibrator
00413 
00414 
00415 
00416 
00417 /**********************************************************************************
00418  *
00419  *              StandardLMM-Calibrator
00420  *
00421  *********************************************************************************/
00422 
00423 
00443 class StandardLmmCalibrator : public LmmCalibrator {
00444         
00445 protected:
00446         
00447         RealVector capletImpliedSigma;           // implied caplet volatility to expiration Sigma_i(0,T_i)
00448         
00449         UTRRealMatrix cvMatrix;                  // workspace for covariation matrices
00450         
00451         Real capletVol, swaptionVol;             // current aggregate volatities (cache)
00452         RealVector x;                            // cache, vector x for caplet and swaption aggregate vols
00453                                                  // see book, 6.8.3, 6.8.2 (DriftlessLMM) and 6.7 (PredictorCorrectorLMM).
00454         
00455 public:
00456                 
00457         StandardLmmCalibrator
00458         (LiborFactorLoading* fl,
00459          const char* capletsInFile="CapletsIn.txt", 
00460          const char* swaptionsInFile="SwaptionsIn.txt", 
00461          const char* capletsOutFile="CapletsOut.txt",    
00462          const char* swaptionsOutFile="SwaptionsOut.txt"
00463         );
00464 
00465         
00466         
00467 // THE OBJECTIVE FUNCTION
00468         
00469                 
00473      virtual Real objectiveFunction(const RealArray1D& x);
00474         
00475         
00476          Real objectiveFunction();
00477         
00478         
00483      virtual void setScalingFactors() = 0;
00484         
00485         
00486         
00487 
00488 // CAPLET AND SWAPTION PRICES
00489 
00494    virtual Real capletForwardPrice(int i, Real strike) = 0;
00495    
00500    virtual Real swaptionForwardPrice(int i, Real strike) = 0;
00501 
00502 
00503         
00504 // SYNTHETIC DATA GENERATION
00505          
00514          static void writeSyntheticDataSample
00515          (int n, int volType=VolSurface::JR, int corrType=Correlations::CS);
00516                  
00517 
00521          static void writeSyntheticDataSample();
00522          
00523         
00541         static void testCalibration
00542         (int nVals, int dim, 
00543          int dataLmmType, int dataVolSurfaceType, int dataCorrelationType,
00544          int lmmType, int volSurfaceType, int correlationType
00545          );
00546         
00547    
00552    void writeCovariationMatrix(int p, int q);
00553    
00554    
00561    LiborFactorLoading* calibrate(int nVals);
00562    
00563   
00564 protected:
00565 
00566 
00575      Real root_xCx(int i);
00576 
00577 
00578 private:
00579 
00580       
00585          void writeCapletImpliedSigmas();
00586          
00587         
00588 
00589 }; // end StandardLmmCalibrator
00590 
00591 
00592 
00593 
00594 
00595 
00596 /**********************************************************************************
00597  *
00598  *              DriftLessLMM-Calibrator
00599  *
00600  *********************************************************************************/
00601 
00602 
00605 class DriftlessLmmCalibrator : public StandardLmmCalibrator {
00606         
00607 public:
00608                 
00609          DriftlessLmmCalibrator
00610          (LiborFactorLoading* fl,
00611           const char* capletsInFile="CapletsIn.txt", 
00612           const char* swaptionsInFile="SwaptionsIn.txt", 
00613           const char* capletsOutFile="CapletsOut.txt",   
00614           const char* swaptionsOutFile="SwaptionsOut.txt"
00615          ) :    
00616      StandardLmmCalibrator(fl,capletsInFile,swaptionsInFile,capletsOutFile,swaptionsOutFile)
00617      {    }
00618         
00619 
00620 // CAPLET AND SWAPTION PRICES
00621 
00626      Real capletForwardPrice(int i, Real strike);
00627    
00632      Real swaptionForwardPrice(int i, Real strike);
00633 
00634          
00639      void setScalingFactors();
00640         
00641    
00646      static void testIO();
00647          
00648         
00654         static void testCalibration(int nVals);
00655 
00656 
00657 }; // end DriftlessLmmCalibrator
00658 
00659 
00660 
00661 
00662 /**********************************************************************************
00663  *
00664  *              PredictorCorrectorLMM-Calibrator
00665  *
00666  *********************************************************************************/
00667 
00668 
00669 
00672 class PredictorCorrectorLmmCalibrator : public StandardLmmCalibrator {
00673         
00674 public:
00675         
00676         
00677         PredictorCorrectorLmmCalibrator
00678         (LiborFactorLoading* fl,
00679          const char* capletsInFile="CapletsIn.txt", 
00680          const char* swaptionsInFile="SwaptionsIn.txt", 
00681          const char* capletsOutFile="CapletsOut.txt",    
00682          const char* swaptionsOutFile="SwaptionsOut.txt"
00683         ) :     
00684     StandardLmmCalibrator(fl,capletsInFile,swaptionsInFile,capletsOutFile,swaptionsOutFile)
00685     {    }
00686 
00687                 
00692      Real capletForwardPrice(int i, Real strike);
00693    
00698      Real swaptionForwardPrice(int i, Real strike);
00699 
00700         
00705      void setScalingFactors();
00706         
00707         
00713         static void testCalibration(int nVals);
00714 
00715          
00716 }; // end PredictorCorrectorLmmCalibrator
00717 
00718 
00719 
00720                         
00721 
00722 
00723 
00724 MTGL_END_NAMESPACE(Martingale)
00725 
00726 #endif

Generated on Mon Sep 22 02:16:32 2003 for Libor-Library by doxygen1.3-rc3