Shark Machine Learning Library
  • About Shark
  • Sourceforge
    • Project Summary
    • Downloads
    • Subversion Repository
  • Getting Started
  • Tutorials
  • FAQ
  • Main Modules
    • ReClaM
    • EALib
    • MOO-EALib
    • Fuzzy
  • Tools
    • Mixture
    • Array
    • Rng
    • LinAlg
    • FileUtil
  • Main Page
  • Classes

RandomVar.h

Go to the documentation of this file.
00001 //===========================================================================
00044 //===========================================================================
00045 
00046 #ifndef __RANDOMVAR_H
00047 #define __RANDOMVAR_H
00048 
00049 #ifdef _WIN32
00050 // disable warning C4804: suspicious bool comparison
00051 // occurs during instantiation of vector< bool >
00052 #pragma warning(disable: 4804)
00053 #endif
00054 
00055 #include <cmath>
00056 #include <vector>
00057 #include "Rng/RNG.h"
00058 
00059 
00060 //===========================================================================
00084 template < class T >
00085 class RandomVar
00086 {
00087 public:
00088 
00089     virtual ~RandomVar()
00090     { }
00091 
00099     virtual T operator()() = 0;
00100 
00101 
00108     virtual double p(const T&) const = 0;
00109 
00110 
00111     //========================================================================
00133     virtual void   seed(long s)
00134     {
00135         rng.seed(s);
00136     }
00137 
00138 
00139     //========================================================================
00159     RandomVar& operator = (const RandomVar& r)
00160     {
00161         rng = r.rng; return *this;
00162     }
00163 
00164 
00165 
00166     //========================================================================
00203     virtual double entropy()
00204     {
00205         double   t;
00206         unsigned i;
00207 
00208         for (t = 0, i = monteCarloTrials; i--;) {
00209             t += log(p((*this)()));
00210         }
00211 
00212         return -t / monteCarloTrials;
00213     }
00214 
00215 
00216     //========================================================================
00252     virtual double KullbackLeibler(const RandomVar< T >& rv)
00253     {
00254         double   t;
00255         unsigned i;
00256         T        x;
00257 
00258         for (t = 0, i = monteCarloTrials; i--;) {
00259             x  = (*this)();
00260             t += log(p(x) / rv.p(x));
00261         }
00262 
00263         return t / monteCarloTrials;
00264     }
00265 
00266     //========================================================================
00293     virtual double logLikelihood(const std::vector< T >& x) const
00294     {
00295         double l = 0;
00296 
00297         for (unsigned k = x.size(); k--;) {
00298             l += log(p(x[ k ]));
00299         }
00300 
00301         return l;
00302     }
00303 
00304     //========================================================================
00349     void setMonteCarlo(unsigned N = 10000)
00350     {
00351         monteCarloTrials = N;
00352     }
00353 
00354 protected:
00355 
00360     RNG&     rng;
00361 
00365     unsigned monteCarloTrials;
00366 
00367 
00368     //========================================================================
00397     RandomVar(RNG& r = RNG::globalRng)
00398             : rng(r),
00399             monteCarloTrials(10000)
00400     {}
00401 };
00402 
00403 #endif  /* !__RANDOMVAR_H */
00404 
00405 
00406 
00407