MRPT
2.0.3
mrpt
bayes
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
11
#include <
mrpt/bayes/CProbabilityParticle.h
>
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 =
28
mrpt::bayes::particle_storage_mode::POINTER
>
29
class
CRejectionSamplingCapable
30
{
31
public
:
32
using
TParticle
=
CProbabilityParticle<TStateSpace, STORAGE>
;
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
*/
48
void
rejectionSampling
(
49
size_t
desiredSamples, std::vector<TParticle>& outSamples,
50
size_t
timeoutTrials = 1000)
51
{
52
MRPT_START
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 <
87
mrpt::random::getRandomGenerator
().drawUniform(
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
CProbabilityParticle.h
mrpt::bayes::CRejectionSamplingCapable
A base class for implementing rejection sampling in a generic state space.
Definition:
CRejectionSamplingCapable.h:29
mrpt::bayes::particle_storage_mode
particle_storage_mode
use for CProbabilityParticle
Definition:
CProbabilityParticle.h:18
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition:
BaseAppDataSource.h:15
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition:
exceptions.h:120
random.h
mrpt::bayes::CProbabilityParticle
A template class for holding a the data and the weight of a particle.
Definition:
CProbabilityParticle.h:36
mrpt::bayes::CRejectionSamplingCapable::RS_drawFromProposal
virtual void RS_drawFromProposal(TStateSpace &outSample)=0
Generates one sample, drawing from some proposal distribution.
MRPT_START
#define MRPT_START
Definition:
exceptions.h:241
mrpt::bayes::CRejectionSamplingCapable::~CRejectionSamplingCapable
virtual ~CRejectionSamplingCapable()=default
Virtual destructor.
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition:
RandomGenerator.cpp:89
MRPT_END
#define MRPT_END
Definition:
exceptions.h:245
mrpt::bayes::particle_storage_mode::POINTER
@ POINTER
mrpt::bayes::CRejectionSamplingCapable::rejectionSampling
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
Definition:
CRejectionSamplingCapable.h:48
mrpt::bayes::CRejectionSamplingCapable::RS_observationLikelihood
virtual double RS_observationLikelihood(const TStateSpace &x)=0
Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the st...
Page generated by
Doxygen 1.8.17
for MRPT 2.0.3 at Fri May 15 15:49:54 UTC 2020