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

Pricing.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_pricing_h    
00024 #define martingale_pricing_h
00025 
00026 
00027 // function templates, fully definded in the header
00028 // thus the need for the includes
00029 #include "TypedefsMacros.h"
00030 #include "Array.h"
00031 #include <vector>               
00032 
00033 
00034 MTGL_BEGIN_NAMESPACE(Martingale)
00035 
00036 
00064 /**********************************************************************************
00065  *
00066  *                       PRICING
00067  *
00068  *********************************************************************************/
00069  
00070 
00071 
00072 
00074 namespace Pricing {
00075         
00076 using std::vector;
00077         
00078 
00094 template<typename LatticeType, typename OptionType>
00095 Real latticeForwardPrice(LatticeType* theLattice, OptionType* theOption)
00096 {
00097         Real  expiration=theOption->getExpiration(),
00098               dt = theLattice->getTimeStep();             // size of time step  
00099     int   timeSteps=theLattice->getTimeSteps(),       // number of time steps in lattice
00100               expiry = (int)(expiration/dt);              // time steps to expiry
00101         
00102         // is the lattice built far enough:
00103         if(timeSteps*dt<expiration-0.00000001){
00104            
00105                 cout << "\n\nPricing::latticeForwardPrice:"
00106                 << "\nLattice not built all the way to expiration."
00107                 << "\nTerminating.";
00108                 exit(1);
00109         }
00110          
00111         typedef typename LatticeType::NodeType NodeType;
00112         
00113         // run through the list of nodes at time T=expiration and set the known value pi_T=h_T
00114         vector<NodeType*>* nodes_at_expiry=theLattice->getNodeList(expiry);
00115         // *theNode is pointer to Node
00116     vector<NodeType*>::iterator theNode=nodes_at_expiry->begin();
00117         while(theNode!=nodes_at_expiry->end()) {
00118         
00119                 NodeType* node=*theNode;
00120                 node->setValue(theOption->forwardPayoff(node,theLattice,expiry));
00121                 theNode++;
00122         }
00123         
00124         // backward computation through earlier nodes
00125         for(int t=expiry-1; t>=0;t--){
00126                 
00127             // can we exercise at time t (continuous time!)
00128                 bool exercise_t = theOption->isExercisable(t*dt);
00129                 
00130                 vector<NodeType*>* nodes_t=theLattice->getNodeList(t);
00131             theNode=nodes_t->begin();
00132         while(theNode!=nodes_t->end()) {
00133             
00134                         NodeType* node = *theNode; 
00135                         const Array1D<NodeType*> edges=node->getEdges();    // list of edges
00136                         int nEdge = edges.getDimension();                   // number of edges
00137                                 
00138                         Real V=0.0;           // option forward price at the node
00139                         for(int i=0;i<nEdge;i++) {
00140                 
00141                                  Real V_i=edges[i]->getValue();                   // value at node *edges[i]
00142                                  Real p_i=theLattice->transitionProbability(i);   // transition probability along edge_i
00143                                  V+=p_i*V_i;
00144                  }      
00145                          
00146                          if(!exercise_t) node->setValue(V);     
00147                          else  node->setValue( max( V, theOption->forwardPayoff(node,theLattice,t) ) ); 
00148                          ++theNode;
00149             } // end while(theNode)
00150                         
00151         } // end for t
00152         
00153         // report the price at the root node
00154     NodeType* root=theLattice->getRoot();
00155         return root->getValue();
00156         
00157 } // end 
00158 
00159 
00160 
00164 template<typename OptionType>
00165 Real monteCarloForwardPrice(OptionType* theOption, int N)
00166 {
00167         Real sum_payoff=0.0;
00168         for(int i=0;i<N;++i) sum_payoff+=theOption->nextForwardPayoff();
00169         return sum_payoff/N;    
00170 } 
00171 
00172 
00177 template<typename OptionType>
00178 Real betaCoefficient(OptionType* theOption, int N)
00179 {
00180         // Recall that Cov(X,Y)=E(XY)-E(X)E(Y) and Var(X)=E(X^2)-E(X)^2.
00181         // X = Payoff, Y = ControlVariate
00182         Real X,Y,
00183              sum_X=0.0, sum_Y=0.0,
00184              sum_XX=0.0, sum_XY=0.0;
00185         
00186     for(int i=0;i<N;i++){
00187                 
00188            // next forwardPayoff - controlVariate pair
00189            const RealArray1D& Z = theOption->nextControlledForwardPayoff();
00190            X = Z[0];       // the payoff
00191            Y = Z[1];       // the control variate
00192             
00193        sum_X+=X; sum_Y+=Y;
00194        sum_XX+=X*X; sum_XY+=X*Y;
00195     }
00196         
00197     // divide numerator and denominator by N^2
00198         return (N*sum_XY-sum_X*sum_Y)/(N*sum_XX-sum_X*sum_X);
00199         
00200 }
00201 
00202 
00207 template<typename OptionType>
00208 Real controlledMonteCarloForwardPrice(OptionType* theOption, int nPaths)
00209 {
00210         Real controlVariateMean=theOption->controlVariateMean();
00211         int M=1024;          // complete cycle for the Sobol generator
00212         Real beta=betaCoefficient(theOption,M);
00213         Real sum_payoff=0.0, 
00214              payoff, 
00215              controlVariate;
00216         
00217         for(int i=0;i<nPaths;++i){
00218                 
00219             // next forwardPayoff - controlVariate pair
00220             const RealArray1D& Z = theOption->nextControlledForwardPayoff();
00221             payoff = Z[0];       
00222             controlVariate = Z[1];       
00223                 sum_payoff+=payoff+beta*(controlVariateMean-controlVariate);
00224         }
00225         return sum_payoff/nPaths;       
00226 } 
00227 
00228 
00229 
00233 template<class OptionType>
00234 Real correlationWithControlVariate(OptionType* theOption, int N)
00235 {
00236         Real x,         // forward payoff
00237              y,         // corresponding control variate
00238              sum_x=0.0, sum_xx=0.0, 
00239              sum_y=0.0, sum_yy=0.0, sum_xy=0.0;
00240         for(int n=0;n<N;n++)
00241     {
00242             // next forwardPayoff - controlVariate pair
00243             const RealArray1D& Z = theOption->nextControlledForwardPayoff();
00244             x = Z[0];       // the payoff
00245             y = Z[1];       // the control variate    
00246                 sum_x+=x; sum_y+=y;
00247         sum_xx+=x*x; sum_yy+=y*y; sum_xy+=x*y;
00248     }
00249     
00250         //when divided by N^4: 
00251     Real Var_xVar_y=((N*sum_xx-sum_x*sum_x)*(N*sum_yy-sum_y*sum_y));
00252     return (N*sum_xy-sum_x*sum_y)/sqrt(Var_xVar_y);
00253 }
00254 
00255 
00256 }; // end Pricing
00257 
00258 
00259 
00260 
00261 MTGL_END_NAMESPACE(Martingale)
00262 
00263 #endif
00264  

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