mixture.cpp
Go to the documentation of this file.
00001 // $Id: mixture.cpp 2009-01-22 tdelaet $
00002 // Copyright (C) 2009 Tinne De Laet <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 "mixture.h"
00021 
00022 namespace BFL
00023 {
00024   using namespace MatrixWrapper;
00025 
00026 
00027 //TODO: this still contains allocation
00028   // Template Specialisation for T =ColumnVector
00029   template <> inline
00030   ColumnVector Mixture<ColumnVector>::ExpectedValueGet (  ) const
00031   {
00032     TestNotInit();
00033     ColumnVector expectedValue(DimensionGet());
00034     expectedValue = 0.0;
00035     for (int i=0; i<NumComponentsGet();i++)
00036         expectedValue = expectedValue + ( (*_componentPdfs)[i]->ExpectedValueGet() * (double)((*_componentWeights)[i]) );
00037     return expectedValue;
00038   }
00039 
00040 
00041   // Template Specialisation for T =unsigned int
00042   // THIS RETURNS THE MOST PROBABLE STATE
00043   // TODO: only works for DiscretePdf
00044   template <> inline
00045   unsigned int Mixture<unsigned int>::ExpectedValueGet (  ) const
00046   {
00047     TestNotInit();
00048     unsigned int expectedValue =0;
00049     double probState = 0.0;
00050     double mostProbState = -1.0;
00051     int numStates = ((DiscretePdf*)((*_componentPdfs)[0]))->NumStatesGet();
00052     for (int i=0; i<NumComponentsGet();i++) //loop over all components
00053     {
00054         if(numStates != ((DiscretePdf*)((*_componentPdfs)[i]))->NumStatesGet())
00055         {
00056             cerr << "Mixture::ExpectedValueGet failed since the different components in the mixture don't have the same number of states" << endl; 
00057             assert(0);
00058         }
00059     }
00060     for (int j=0; j<numStates ;j++ ) //loop over all states
00061     {
00062         probState = 0.0;
00063         for (int i=0; i<NumComponentsGet();i++) //loop over all components
00064         {
00065             probState  +=   ( ((double)(*_componentPdfs)[i]->ProbabilityGet(j)) * (double)((*_componentWeights)[i]) );
00066         }
00067         if (probState > mostProbState)
00068         {
00069             expectedValue = (unsigned int)j;
00070             mostProbState = probState;
00071         }
00072     }
00073     return expectedValue;
00074   }
00075 
00076 
00077   // Template Specialisation for T =int
00078   // THIS RETURNS THE MOST PROBABLE STATE
00079   // TODO: only works for DiscretePdf
00080   template <> inline
00081   int Mixture<int>::ExpectedValueGet (  ) const
00082   {
00083     TestNotInit();
00084     int expectedValue =0;
00085     double probState = 0.0;
00086     double mostProbState = -1.0;
00087     int numStates = ((DiscretePdf*)((*_componentPdfs)[0]))->NumStatesGet();
00088     for (int i=0; i<NumComponentsGet();i++) //loop over all components
00089     {
00090         if(numStates != ((DiscretePdf*)((*_componentPdfs)[i]))->NumStatesGet())
00091         {
00092             cerr << "Mixture::ExpectedValueGet failed since the different components in the mixture don't have the same number of states" << endl; 
00093             assert(0);
00094         }
00095     }
00096     for (int j=0; j< numStates;j++ ) //loop over all states
00097     {
00098         probState = 0.0;
00099         for (int i=0; i<NumComponentsGet();i++) //loop over all components
00100         {
00101             probState  +=   ( ((double)(*_componentPdfs)[i]->ProbabilityGet(j)) * (double)((*_componentWeights)[i]) );
00102         }
00103         if (probState > mostProbState)
00104         {
00105             expectedValue = j;
00106             mostProbState = probState;
00107         }
00108     }
00109     return expectedValue;
00110   }
00111 
00112 
00113   // Template Specialisation for T =double
00114   template <> inline
00115   double Mixture<double>::ExpectedValueGet (  ) const
00116   {
00117     TestNotInit();
00118     double expectedValue;
00119     expectedValue = 0.0;
00120     for (int i=0; i<NumComponentsGet();i++)
00121         expectedValue = expectedValue + ( (*_componentPdfs)[i]->ExpectedValueGet() * (double)((*_componentWeights)[i]) );
00122     return expectedValue;
00123   }
00124 }
00125 


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