33 #include <gtest/gtest.h> 60 size_t size()
const override 77 TEST(Pf, BayesianEstimation)
80 const float center_list[] =
85 const float abs_error = 2e-1;
86 const float sigma = 1.0;
87 const float sigma2 = 2.0;
89 for (
auto center : center_list)
91 for (
auto center2 : center_list)
97 ASSERT_NEAR(center, pf.
expectation()[0], abs_error);
98 ASSERT_NEAR(sigma, pf.
covariance()[0][0], abs_error);
100 auto likelihood = [center2, sigma2](
const State&
s) ->
float 102 return exp(-powf(
s[0] - center2, 2.0) / (2.0 * powf(sigma2, 2.0)));
107 const int HISTOGRAM_SIZE = 4000;
108 const float HISTOGRAM_RESOLUTION = 0.02;
110 float dist[HISTOGRAM_SIZE];
115 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
117 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION;
118 dist[i] = nd1(x - center) * nd2(x - center2);
125 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
127 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION - avg;
128 var += powf(x, 2.0) * dist[i];
134 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
139 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
146 const size_t size_num = 3;
147 const size_t size[size_num] =
153 const float center = 12.3;
154 const float sigma = 0.45;
159 for (
size_t i = 0; i < size_num; ++i)
165 ASSERT_LT(fabs(e[0] - center), 1e-1);
166 ASSERT_LT(fabs(sqrtf(v[0][0]) - sigma), 1e-1);
171 const std::vector<State> v_r = pf.
covariance();
172 ASSERT_LT(fabs(e_r[0] - center), 1e-1);
173 ASSERT_LT(fabs(sqrtf(v_r[0][0]) - sigma), 1e-1);
175 if (i + 1 != size_num)
182 TEST(Pf, ResampleFlatLikelihood)
185 const float center = 12.3;
186 const float sigma = 0.45;
191 std::vector<float> orig;
202 TEST(Pf, ResampleFirstParticle)
204 const std::vector<float> probs =
206 0.0001f, 0.2f, 0.2f, 0.2f, 0.3999f
208 const std::vector<float> states =
210 0.0f, 1.0f, 2.0f, 3.0f, 4.0f
212 const std::vector<float> expected_resampled_states =
214 1.0f, 2.0f, 3.0f, 4.0f, 4.0f
216 const size_t particle_num = probs.size();
219 auto it = pf.
begin();
220 for (
size_t i = 0; i < particle_num; ++i, ++it)
222 it->state_.x = states.at(i);
223 it->probability_ = probs.at(i);
230 EXPECT_FLOAT_EQ(expected_resampled_states.at(i), pf.
getParticle(i)[0]);
237 const float val0 = 12.3;
238 const float val1 = 45.6;
243 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
245 ASSERT_EQ(it->state_[0], val0);
246 it->state_[0] = val1;
248 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
249 ASSERT_EQ(it->state_[0], val1);
255 const float val0 = 12.3;
256 const float val1 = 45.6;
263 it->state_[0] = val1;
267 for (
size_t i = 0; i < 10; ++i)
269 for (
size_t i = 10; i < 20; ++i)
273 int main(
int argc,
char** argv)
275 testing::InitGoogleTest(&argc, argv);
277 return RUN_ALL_TESTS();
T expectation(const FLT_TYPE pass_ratio=1.0)
void measure(std::function< FLT_TYPE(const T &)> likelihood)
std::vector< Particle< T, FLT_TYPE > >::iterator appendParticle(const size_t num)
std::vector< T > covariance(const FLT_TYPE pass_ratio=1.0)
size_t getParticleSize() const
void init(T mean, T sigma)
int main(int argc, char **argv)
std::vector< Particle< T, FLT_TYPE > >::iterator end()
float & operator[](const size_t i) override
T getParticle(const size_t i) const
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
void resizeParticle(const size_t num)
size_t size() const override
std::vector< Particle< T, FLT_TYPE > >::iterator begin()
TEST(Pf, BayesianEstimation)
const float & operator[](const size_t i) const