mcpdf.cpp
Go to the documentation of this file.
00001 // $Id: mcpdf.cpp 30030 2009-03-11 12:41:13Z tdelaet $
00002 // Copyright (C) 2003 Klaas Gadeyne <first dot last at gmail dot com>
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published by
00006 // the Free Software Foundation; either version 2.1 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 
00019 // This file only contains template specialisation code
00020 #include "mcpdf.h"
00021 
00022 namespace BFL
00023 {
00024   using namespace MatrixWrapper;
00025 
00026 
00027   // Template Specialisation for T =ColumnVector
00028 
00029   // Make sure no memory is allocated @runtime
00030   // Constructor
00031   template <> inline MCPdf<ColumnVector>::MCPdf(unsigned int num_samples, unsigned int dimension) :
00032       Pdf<ColumnVector>(dimension)
00033       , _CumSum(dimension)
00034       , _mean(dimension)
00035       , _diff(dimension)
00036       , _covariance(dimension)
00037       , _diffsum(dimension,dimension)
00038       {
00039         _SumWeights = 0;
00040         WeightedSample<ColumnVector> my_sample(dimension);
00041         _listOfSamples.insert(_listOfSamples.begin(),num_samples,my_sample);
00042         _CumPDF.insert(_CumPDF.begin(),num_samples+1,0.0);
00043 
00044        _los.assign(num_samples,WeightedSample<ColumnVector>(dimension));
00045        _it_los = _los.begin();
00046   #ifdef __CONSTRUCTOR__
00047         // if (num_samples > 0)
00048         cout << "MCPDF Constructor: NumSamples = " << _listOfSamples.size()
00049              << ", CumPDF Samples = " << _CumPDF.size()
00050              << ", _SumWeights = " << _SumWeights << endl;
00051   #endif // __CONSTRUCTOR__
00052       }
00053 
00054   // Make sure no memory is allocated @runtime
00055   // Copy constructor
00056   template <> inline
00057     MCPdf<ColumnVector>::MCPdf(const MCPdf & pdf) : Pdf<ColumnVector>(pdf)
00058     , _CumSum(pdf.DimensionGet())
00059     , _mean(pdf.DimensionGet())
00060     , _diff(pdf.DimensionGet())
00061     , _covariance(pdf.DimensionGet())
00062     , _diffsum(pdf.DimensionGet(),pdf.DimensionGet())
00063     {
00064       this->_listOfSamples = pdf._listOfSamples;
00065       this->_CumPDF = pdf._CumPDF;
00066       _SumWeights = pdf._SumWeights;
00067       this->_los = pdf._listOfSamples;
00068      _it_los = _los.begin();
00069 #ifdef __CONSTRUCTOR__
00070       cout << "MCPDF Copy Constructor: NumSamples = " << _listOfSamples.size()
00071            << ", CumPDF Samples = " << _CumPDF.size()
00072            << ", SumWeights = " << _SumWeights << endl;
00073 #endif // __CONSTRUCTOR__
00074     }
00075 
00076   template <> inline
00077   ColumnVector MCPdf<ColumnVector>::ExpectedValueGet (  ) const
00078   {
00079     _CumSum = 0.0;
00080     _los = _listOfSamples;
00081     for ( _it_los = _los.begin() ; _it_los != _los.end() ; _it_los++ )
00082          _CumSum += ( _it_los->ValueGet() * _it_los->WeightGet() );
00083     return _CumSum/_SumWeights;
00084   }
00085 
00086 
00087   template <> inline
00088   SymmetricMatrix MCPdf<ColumnVector>::CovarianceGet (  ) const
00089   {
00090     _mean = (this->ExpectedValueGet());
00091     _los = _listOfSamples;
00092     _diffsum = 0.0;
00093     // Actual calculation
00094     for (_it_los = _los.begin(); _it_los != _los.end(); _it_los++)
00095       {
00096         _diff = ( _it_los->ValueGet() - _mean);
00097         _diffsum += _diff * (_diff.transpose() * _it_los->WeightGet());
00098       }
00099     // Biased estimator!! (unbiased possible with weighted samples??)
00100     (_diffsum/_SumWeights).convertToSymmetricMatrix(_covariance);
00101     return _covariance;
00102   }
00103 
00104 
00105 
00106 
00107 
00108 
00109   // Template Specialisation for T =unsigned int
00110   template <> inline
00111   unsigned int MCPdf<unsigned int>::ExpectedValueGet (  ) const
00112   {
00113     unsigned int result;
00114     double CumSum = 0;
00115     _los = _listOfSamples;
00116     double current_weight;
00117     for ( _it_los = _los.begin() ; _it_los != _los.end() ; _it_los++ )
00118       {
00119         current_weight = _it_los->WeightGet();
00120         CumSum += ( ((double)_it_los->ValueGet()) * current_weight );
00121       }
00122     result = (unsigned int)((CumSum/_SumWeights) + 0.5);
00123     return result;
00124   }
00125 
00126 
00127 
00128 
00129   template <> inline
00130   SymmetricMatrix MCPdf<unsigned int>::CovarianceGet (  ) const
00131   {
00132     unsigned int mean = this->ExpectedValueGet();
00133     unsigned int diff;
00134     double diffsum, current_weight;
00135     _los = _listOfSamples;
00136     diffsum = 0.0;
00137     // Actual calculation
00138     for (_it_los = _los.begin(); _it_los != _los.end(); _it_los++)
00139       {
00140         current_weight = _it_los->WeightGet();
00141         diff = (_it_los->ValueGet() - mean);
00142         diffsum += (((double)(diff * diff)) * current_weight);
00143       }
00144 
00145     // Biased estimator!! (unbiased possible with weighted samples??)
00146     _covariance(1,1) = (diffsum / _SumWeights);
00147     return _covariance;
00148   }
00149 
00150 }


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Fri Aug 28 2015 10:10:21