MRPT  1.9.9
CRejectionSamplingCapable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/random.h>
13 
14 namespace mrpt
15 {
16 /// \ingroup mrpt_bayes_grp
17 namespace bayes
18 {
19 /** A base class for implementing rejection sampling in a generic state space.
20  * See the main method CRejectionSamplingCapable::rejectionSampling
21  * To use this class, create your own class as a child of this one and
22  * implement the desired
23  * virtual methods, and add any required internal data.
24  * \ingroup mrpt_bayes_grp
25  */
26 template <
27  class TStateSpace, mrpt::bayes::particle_storage_mode STORAGE =
30 {
31  public:
33 
34  /** Virtual destructor
35  */
36  virtual ~CRejectionSamplingCapable() = default;
37  /** Generates a set of N independent samples via rejection sampling.
38  * \param desiredSamples The number of desired samples to generate
39  * \param outSamples The output samples.
40  * \param timeoutTrials The maximum number of rejection trials for each
41  * generated sample (i.e. the maximum number of iterations). This can be
42  * used to set a limit to the time complexity of the algorithm for difficult
43  * probability densities.
44  * All will have equal importance weights (a property of rejection
45  * sampling), although those samples
46  * generated at timeout will have a different importance weights.
47  */
49  size_t desiredSamples, std::vector<TParticle>& outSamples,
50  size_t timeoutTrials = 1000)
51  {
53 
54  TStateSpace x;
55  typename std::vector<TParticle>::iterator it;
56 
57  // Set output size:
58  if (outSamples.size() != desiredSamples)
59  {
60  // Free old memory:
61  outSamples.clear();
62 
63  // Reserve new memory:
64  outSamples.resize(desiredSamples);
65  for (it = outSamples.begin(); it != outSamples.end(); ++it)
66  it->d.reset(new TStateSpace);
67  }
68 
69  // Rejection sampling loop:
70  double acceptanceProb;
71  for (it = outSamples.begin(); it != outSamples.end(); ++it)
72  {
73  size_t timeoutCount = 0;
74  double bestLik = -1e250;
75  TStateSpace bestVal;
76  do
77  {
78  RS_drawFromProposal(*it->d);
79  acceptanceProb = RS_observationLikelihood(*it->d);
80  ASSERT_(acceptanceProb >= 0 && acceptanceProb <= 1);
81  if (acceptanceProb > bestLik)
82  {
83  bestLik = acceptanceProb;
84  bestVal = *it->d;
85  }
86  } while (acceptanceProb <
88  0.0, 0.999) &&
89  (++timeoutCount) < timeoutTrials);
90 
91  // Save weights:
92  if (timeoutCount >= timeoutTrials)
93  {
94  it->log_w = log(bestLik);
95  *it->d = bestVal;
96  }
97  else
98  {
99  it->log_w = 0; // log(1.0);
100  }
101  } // end for it
102 
103  MRPT_END
104  }
105 
106  protected:
107  /** Generates one sample, drawing from some proposal distribution.
108  */
109  virtual void RS_drawFromProposal(TStateSpace& outSample) = 0;
110 
111  /** Returns the NORMALIZED observation likelihood (linear, not
112  * exponential!!!) at a given point of the state space (values in the range
113  * [0,1]).
114  */
115  virtual double RS_observationLikelihood(const TStateSpace& x) = 0;
116 
117 }; // End of class def.
118 
119 } // namespace bayes
120 } // namespace mrpt
virtual ~CRejectionSamplingCapable()=default
Virtual destructor.
#define MRPT_START
Definition: exceptions.h:241
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
virtual double RS_observationLikelihood(const TStateSpace &x)=0
Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the st...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A template class for holding a the data and the weight of a particle.
#define MRPT_END
Definition: exceptions.h:245
A base class for implementing rejection sampling in a generic state space.
particle_storage_mode
use for CProbabilityParticle
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
virtual void RS_drawFromProposal(TStateSpace &outSample)=0
Generates one sample, drawing from some proposal distribution.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020