mcpdf.cpp
Go to the documentation of this file.
1 // $Id$
2 // Copyright (C) 2003 Klaas Gadeyne <first dot last at gmail dot com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation; either version 2.1 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
19 // This file only contains template specialisation code
20 #include "mcpdf.h"
21 
22 namespace BFL
23 {
24  using namespace MatrixWrapper;
25 
26 
27  // Template Specialisation for T =ColumnVector
28 
29  // Make sure no memory is allocated @runtime
30  // Constructor
31  template <> inline MCPdf<ColumnVector>::MCPdf(unsigned int num_samples, unsigned int dimension) :
32  Pdf<ColumnVector>(dimension)
33  , _CumSum(dimension)
34  , _mean(dimension)
35  , _diff(dimension)
36  , _covariance(dimension)
37  , _diffsum(dimension,dimension)
38  {
39  _SumWeights = 0;
40  WeightedSample<ColumnVector> my_sample(dimension);
41  _listOfSamples.insert(_listOfSamples.begin(),num_samples,my_sample);
42  _CumPDF.insert(_CumPDF.begin(),num_samples+1,0.0);
43 
44  _los.assign(num_samples,WeightedSample<ColumnVector>(dimension));
45  _it_los = _los.begin();
46  #ifdef __CONSTRUCTOR__
47  // if (num_samples > 0)
48  cout << "MCPDF Constructor: NumSamples = " << _listOfSamples.size()
49  << ", CumPDF Samples = " << _CumPDF.size()
50  << ", _SumWeights = " << _SumWeights << endl;
51  #endif // __CONSTRUCTOR__
52  }
53 
54  // Make sure no memory is allocated @runtime
55  // Copy constructor
56  template <> inline
57  MCPdf<ColumnVector>::MCPdf(const MCPdf & pdf) : Pdf<ColumnVector>(pdf)
58  , _CumSum(pdf.DimensionGet())
59  , _mean(pdf.DimensionGet())
60  , _diff(pdf.DimensionGet())
61  , _covariance(pdf.DimensionGet())
62  , _diffsum(pdf.DimensionGet(),pdf.DimensionGet())
63  {
64  this->_listOfSamples = pdf._listOfSamples;
65  this->_CumPDF = pdf._CumPDF;
67  this->_los = pdf._listOfSamples;
68  _it_los = _los.begin();
69 #ifdef __CONSTRUCTOR__
70  cout << "MCPDF Copy Constructor: NumSamples = " << _listOfSamples.size()
71  << ", CumPDF Samples = " << _CumPDF.size()
72  << ", SumWeights = " << _SumWeights << endl;
73 #endif // __CONSTRUCTOR__
74  }
75 
76  template <> inline
78  {
79  _CumSum = 0.0;
81  for ( _it_los = _los.begin() ; _it_los != _los.end() ; _it_los++ )
82  _CumSum += ( _it_los->ValueGet() * _it_los->WeightGet() );
83  return _CumSum/_SumWeights;
84  }
85 
86 
87  template <> inline
88  SymmetricMatrix MCPdf<ColumnVector>::CovarianceGet ( ) const
89  {
90  _mean = (this->ExpectedValueGet());
92  _diffsum = 0.0;
93  // Actual calculation
94  for (_it_los = _los.begin(); _it_los != _los.end(); _it_los++)
95  {
96  _diff = ( _it_los->ValueGet() - _mean);
97  _diffsum += _diff * (_diff.transpose() * _it_los->WeightGet());
98  }
99  // Biased estimator!! (unbiased possible with weighted samples??)
100  (_diffsum/_SumWeights).convertToSymmetricMatrix(_covariance);
101  return _covariance;
102  }
103 
104 
105 
106 
107 
108 
109  // Template Specialisation for T =unsigned int
110  template <> inline
112  {
113  unsigned int result;
114  double CumSum = 0;
116  double current_weight;
117  for ( _it_los = _los.begin() ; _it_los != _los.end() ; _it_los++ )
118  {
119  current_weight = _it_los->WeightGet();
120  CumSum += ( ((double)_it_los->ValueGet()) * current_weight );
121  }
122  result = (unsigned int)((CumSum/_SumWeights) + 0.5);
123  return result;
124  }
125 
126 
127 
128 
129  template <> inline
130  SymmetricMatrix MCPdf<unsigned int>::CovarianceGet ( ) const
131  {
132  unsigned int mean = this->ExpectedValueGet();
133  unsigned int diff;
134  double diffsum, current_weight;
136  diffsum = 0.0;
137  // Actual calculation
138  for (_it_los = _los.begin(); _it_los != _los.end(); _it_los++)
139  {
140  current_weight = _it_los->WeightGet();
141  diff = (_it_los->ValueGet() - mean);
142  diffsum += (((double)(diff * diff)) * current_weight);
143  }
144 
145  // Biased estimator!! (unbiased possible with weighted samples??)
146  _covariance(1,1) = (diffsum / _SumWeights);
147  return _covariance;
148  }
149 
150 }
Class PDF: Virtual Base class representing Probability Density Functions.
Definition: pdf.h:53
vector< WeightedSample< T > >::iterator _it_los
Definition: mcpdf.h:81
vector< WeightedSample< T > > _los
Definition: mcpdf.h:76
vector< WeightedSample< T > > _listOfSamples
STL-list containing the list of samples.
Definition: mcpdf.h:56
T _diff
Definition: mcpdf.h:78
Monte Carlo Pdf: Sample based implementation of Pdf.
Definition: mcpdf.h:49
MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
unsigned int DimensionGet() const
Get the dimension of the argument.
Matrix _diffsum
Definition: mcpdf.h:80
vector< double > _CumPDF
STL-iterator.
Definition: mcpdf.h:60
T _mean
Definition: mcpdf.h:77
T ExpectedValueGet() const
Get the expected value E[x] of the pdf.
double _SumWeights
Sum of all weights: used for normalising purposes.
Definition: mcpdf.h:54
T _CumSum
Definition: mcpdf.h:75
MCPdf(unsigned int num_samples=0, unsigned int dimension=0)
Constructor.
SymmetricMatrix _covariance
Definition: mcpdf.h:79


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 Mon Feb 28 2022 21:56:33