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

RandomObject.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_randomobject_h    
00024 #define martingale_randomobject_h
00025 
00026 // template class fully defined in header
00027 #include "TypedefsMacros.h"
00028 #include "Matrix.h"
00029 #include "Utils.h"
00030 #include "Random.h"  
00031 
00032 
00033 
00038 // The whole implementation is in the header!
00039 
00096 MTGL_BEGIN_NAMESPACE(Martingale)        
00097 
00098 
00099 
00100 template<typename RangeType=Real, typename ScalarType=Real>
00101 class RandomObject {
00102 
00103 
00104     int dim;        // the range dimension
00105 
00106         
00107  public:
00108         
00109     int getDimension() const { return dim; }
00110 
00111 
00112 /*******************************************************************************    
00113     
00114                            CONSTRUCTOR
00115  
00116 *******************************************************************************/      
00117 
00118 
00123   RandomObject(int d=1): dim(d) { }
00124           
00125  
00126     
00127 /*******************************************************************************    
00128     
00129                            SAMPLING 
00130  
00131 *******************************************************************************/    
00132     
00144     virtual RangeType nextValue() = 0;
00145     
00146 
00147 
00148 
00149     
00150 
00151 /*******************************************************************************    
00152     
00153     MEAN 
00154     Fixed sample size.
00155  
00156 *******************************************************************************/
00157     
00158     
00163     RangeType expectation(int N)
00164     {
00165                 // initialization n=0 (RangeType need not have a zero element)
00166         RangeType EX=nextValue();
00167         for(int n=1;n<N;n++) EX+=nextValue(); EX/=N;
00168                         
00169         return EX;
00170 
00171     } // end conditionalExpectation
00172 
00173     
00174  
00175     
00182     RangeType expectation(int N, string message)
00183     {
00184         int m=N/100;
00185         LoopStatus LS(message);
00186         RangeType EX=nextValue();
00187         for(int n=1;n<N;n++){
00188 
00189                         if(n%m==0)LS.consoleReport(n,N);
00190             EX+=nextValue();
00191         }
00192         
00193         EX/=N;
00194         return EX;
00195 
00196     } // end conditionalExpectation
00197 
00198  
00199 
00200 /*******************************************************************************    
00201     
00202     MEAN AND STANDARD DEVIATION
00203     Fixed sample size, all samples independent
00204  
00205 *******************************************************************************/
00206 
00207 
00208     
00215     RangeType* meanAndVariance(int N)
00216     {
00217         RangeType x=nextValue(),
00218                   EX=x,            // X_1+X_2+...+X_n
00219                   EXX=x*x,         // X_1^2+X_2^2+...+X_n^2
00220                       Var_X;
00221 
00222         for(int n=1;n<N;n++){
00223             
00224             x=nextValue();
00225             EX+=x;
00226             EXX+=x*x;
00227         }
00228         
00229         EX/=N;
00230         EXX/=N;
00231                 Var_X=EXX;
00232         Var_X-=EX*EX;
00233         
00234         RangeType* v = new RangeType[2]; v[0]=EX; v[1]=Var_X;
00235         return v;
00236 
00237     } // end meanAndVariance
00238         
00239         
00248     RangeType* meanAndVariance(int N, string message)
00249     {
00250         RangeType x=nextValue(),
00251                   EX=x,            // X_1+X_2+...+X_n
00252                   EXX=x*x,         // X_1^2+X_2^2+...+X_n^2
00253                       Var_X;
00254                 
00255                 int m=N/100;
00256                 LoopStatus LS(message);
00257         for(int n=1;n<N;n++){
00258             
00259             if(n%m==0)LS.consoleReport(n,N);
00260                         x=nextValue();
00261             EX+=x;
00262             EXX+=x*x;
00263         }
00264         
00265         EX/=N;
00266         EXX/=N;
00267                 Var_X=EXX;
00268         Var_X-=EX*EX;
00269         
00270         RangeType* v = new RangeType[2]; v[0]=EX; v[1]=Var_X;
00271         return v;
00272 
00273     } // end meanAndVariance
00274     
00275     
00276 
00277 
00278 /*******************************************************************************    
00279     
00280               VARIANCE, COVARIANCE and CORRELATION
00281  
00282 *******************************************************************************/
00283 
00284 
00285     
00286     
00292    RangeType variance(int N)
00293    {
00294         RangeType x=nextValue(),
00295                   EX=x,            // X_1+X_2+...+X_n
00296                   EXX=x*x;         // X_1^2+X_2^2+...+X_n^2
00297 
00298         for(int n=1;n<N;n++){
00299             
00300             x=nextValue();
00301             EX+=x;
00302             EXX+=x*x;
00303         }
00304         
00305         EX/=N;
00306         EXX/=N;
00307                 EXX-=(EX*EX);
00308         return EXX;
00309 
00310    } // end Variance
00311     
00312 
00313     
00314 
00320    ScalarType covariance(int i, int j, int N)
00321    {
00322         RangeType  x=nextValue();
00323         ScalarType xi=x[i], xj=x[j],
00324                        EXi=xi, EXj=xj, EXiXj=xi*xj;         
00325 
00326         for(int n=1;n<N;n++){
00327             
00328                x=nextValue(); xi=x[i]; xj=x[j];
00329                EXi+=xi; EXj+=xj, EXiXj+=xi*xj;
00330         }
00331         
00332         EXi/=N;
00333         EXj/=N;
00334         EXiXj/=N;
00335                 EXiXj -= EXi*EXj;
00336         return EXiXj;
00337 
00338    } // end covariance
00339    
00340    
00341    
00353     ScalarType correlation(int i, int j, int N)
00354     {
00355         RangeType  x=nextValue();
00356                 ScalarType sum_Xi=x[i], sum_Xj=x[j],
00357                    sum_XiXi=x[i]*x[i], sum_XjXj=x[j]*x[j], sum_XiXj=x[i]*x[j];
00358         
00359         for(int n=0;n<N;n++)
00360         {
00361             x=nextValue();
00362             sum_Xi+=x[i]; sum_Xj+=x[j];
00363             sum_XiXi+=x[i]*x[i]; sum_XjXj+=x[j]*x[j]; sum_XiXj+=x[i]*x[j];
00364         }
00365                 
00366         //when divided by N^4: 
00367         ScalarType 
00368                 Var_XiVar_Xj=((N*sum_XiXi-sum_Xi*sum_Xi)*(N*sum_XjXj-sum_Xj*sum_Xj));
00369         
00370         return (N*sum_XiXj-sum_Xi*sum_Xj)/sqrt(Var_XiVar_Xj);
00371         
00372     } // end correlation
00373   
00374     
00375 
00376 
00377 /*******************************************************************************    
00378     
00379                       COVARIANCE MATRIX
00380  
00381 *******************************************************************************/
00382 
00383 
00384 
00390    UTRMatrix<ScalarType> covarianceMatrix(int N)
00391    {
00392        ScalarType E[dim];                       // E[i]=EX_i
00393        UTRMatrix<ScalarType> C(dim);            // C(i,j)=Cov(X_i,X_j)
00394 
00395        // initialization n=0 (the type S need not have a zero element)
00396        RangeType x=nextValue();
00397        for(int i=0;i<dim;i++){
00398 
00399                E[i]=x[i];
00400            for(int j=i;j<dim;j++) C(i,j)=x[i]*x[j];
00401        }
00402 
00403        // accumulate samples for the component means, averaging later
00404        for(int n=1;n<N;n++){
00405 
00406                x=nextValue();
00407            for(int i=0;i<dim;i++){
00408 
00409                    E[i]+=x[i];
00410                for(int j=i;j<dim;j++) C(i,j)+=x[i]*x[j];
00411            }
00412        }
00413    
00414        // average
00415        for(int i=0;i<dim;i++){
00416 
00417                E[i]/=N;
00418            for(int j=i;j<dim;j++) C(i,j)/=N;
00419        }
00420 
00421        // now C(i,j)=E(X_iX_j) change this to C(i,j)=E(X_iX_j)-EX_i*EX_j
00422        for(int i=0;i<dim;i++)
00423        for(int j=i;j<dim;j++) C(i,j)-=E[i]*E[j];
00424 
00425        return C;
00426 
00427    } // end conditionalCovarianceMatrix
00428 
00429         
00430  
00438    UTRMatrix<ScalarType> covarianceMatrix(int N, string message)
00439    {
00440        ScalarType E[dim];                  // E[i]=EX_i
00441        UTRMatrix<ScalarType> C(dim);       // C(i,j)=Cov(X_i,X_j)
00442        RangeType x=nextValue();
00443 
00444        LoopStatus LS(message); 
00445 
00446        // accumulate samples for the component means, averaging later
00447        // initialization n=0 (the type S need not have a zero element)
00448        for(int i=0;i<dim;i++){
00449 
00450                E[i]=x[i];
00451            for(int j=i;j<dim;j++) C(i,j)=x[i]*x[j];
00452        }
00453 
00454        int m=N/100;
00455        for(int n=1;n<N;n++){
00456 
00457            x=nextValue();
00458                if(n%m==0)LS.consoleReport(n,N);
00459            for(int i=0;i<dim;i++){
00460 
00461                    E[i]+=x[i];
00462                for(int j=i;j<dim;j++) C(i,j)+=x[i]*x[j];
00463            }
00464        }
00465    
00466        // average
00467        for(int i=0;i<dim;i++){
00468 
00469                E[i]/=N;
00470            for(int j=i;j<dim;j++) C(i,j)/=N;
00471        }
00472 
00473        // now C(i,j)=E(X_iX_j) change this to C(i,j)=E(X_iX_j)-EX_i*EX_j
00474        for(int i=0;i<dim;i++)
00475        for(int j=i;j<dim;j++) C(i,j)-=E[i]*E[j];
00476 
00477        return C;
00478 
00479     } // end conditionalCovarianceMatrix
00480 
00481 
00482 
00483 
00484 }; // end RandomObject
00485 
00486 
00487 
00488 /*******************************************************************************    
00489     
00490                   RANDOMVECTORS and RANDOMVARIABLES
00491  
00492 *******************************************************************************/
00493 
00494 
00495 
00498 typedef RandomObject<> RandomVariable;
00499   
00500 
00503 typedef RandomObject< RealVector > RandomVector;
00504   
00505 
00506 
00507 
00508 
00509 
00510 
00511 
00512 MTGL_END_NAMESPACE(Martingale)
00513 
00514 
00515 #endif  

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