mixture.cpp
Go to the documentation of this file.
1 // $Id: mixture.cpp 2009-01-22 tdelaet $
2 // Copyright (C) 2009 Tinne De Laet <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 "mixture.h"
21 
22 namespace BFL
23 {
24  using namespace MatrixWrapper;
25 
26 
27 //TODO: this still contains allocation
28  // Template Specialisation for T =ColumnVector
29  template <> inline
31  {
32  TestNotInit();
33  ColumnVector expectedValue(DimensionGet());
34  expectedValue = 0.0;
35  for (int i=0; i<NumComponentsGet();i++)
36  expectedValue = expectedValue + ( (*_componentPdfs)[i]->ExpectedValueGet() * (double)((*_componentWeights)[i]) );
37  return expectedValue;
38  }
39 
40 
41  // Template Specialisation for T =unsigned int
42  // THIS RETURNS THE MOST PROBABLE STATE
43  // TODO: only works for DiscretePdf
44  template <> inline
46  {
47  TestNotInit();
48  unsigned int expectedValue =0;
49  double probState = 0.0;
50  double mostProbState = -1.0;
51  int numStates = ((DiscretePdf*)((*_componentPdfs)[0]))->NumStatesGet();
52  for (int i=0; i<NumComponentsGet();i++) //loop over all components
53  {
54  if(numStates != ((DiscretePdf*)((*_componentPdfs)[i]))->NumStatesGet())
55  {
56  cerr << "Mixture::ExpectedValueGet failed since the different components in the mixture don't have the same number of states" << endl;
57  assert(0);
58  }
59  }
60  for (int j=0; j<numStates ;j++ ) //loop over all states
61  {
62  probState = 0.0;
63  for (int i=0; i<NumComponentsGet();i++) //loop over all components
64  {
65  probState += ( ((double)(*_componentPdfs)[i]->ProbabilityGet(j)) * (double)((*_componentWeights)[i]) );
66  }
67  if (probState > mostProbState)
68  {
69  expectedValue = (unsigned int)j;
70  mostProbState = probState;
71  }
72  }
73  return expectedValue;
74  }
75 
76 
77  // Template Specialisation for T =int
78  // THIS RETURNS THE MOST PROBABLE STATE
79  // TODO: only works for DiscretePdf
80  template <> inline
82  {
83  TestNotInit();
84  int expectedValue =0;
85  double probState = 0.0;
86  double mostProbState = -1.0;
87  int numStates = ((DiscretePdf*)((*_componentPdfs)[0]))->NumStatesGet();
88  for (int i=0; i<NumComponentsGet();i++) //loop over all components
89  {
90  if(numStates != ((DiscretePdf*)((*_componentPdfs)[i]))->NumStatesGet())
91  {
92  cerr << "Mixture::ExpectedValueGet failed since the different components in the mixture don't have the same number of states" << endl;
93  assert(0);
94  }
95  }
96  for (int j=0; j< numStates;j++ ) //loop over all states
97  {
98  probState = 0.0;
99  for (int i=0; i<NumComponentsGet();i++) //loop over all components
100  {
101  probState += ( ((double)(*_componentPdfs)[i]->ProbabilityGet(j)) * (double)((*_componentWeights)[i]) );
102  }
103  if (probState > mostProbState)
104  {
105  expectedValue = j;
106  mostProbState = probState;
107  }
108  }
109  return expectedValue;
110  }
111 
112 
113  // Template Specialisation for T =double
114  template <> inline
116  {
117  TestNotInit();
118  double expectedValue;
119  expectedValue = 0.0;
120  for (int i=0; i<NumComponentsGet();i++)
121  expectedValue = expectedValue + ( (*_componentPdfs)[i]->ExpectedValueGet() * (double)((*_componentWeights)[i]) );
122  return expectedValue;
123  }
124 }
125 
Class representing a PDF on a discrete variable.
Definition: discretepdf.h:34
T ExpectedValueGet() const
Get the expected value E[x] of the pdf.


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