37 #include <gtest/gtest.h>
61 size_t size()
const override
78 TEST(Pf, BayesianEstimation)
81 const float center_list[] =
88 const float abs_error = 2e-1;
89 const float sigma = 1.0;
90 const float sigma2 = 2.0;
92 for (
auto center : center_list)
94 for (
auto center2 : center_list)
100 ASSERT_NEAR(center, pf.
expectation()[0], abs_error);
101 ASSERT_NEAR(sigma, pf.
covariance()[0][0], abs_error);
103 auto likelihood = [center2, sigma2](
const State&
s) ->
float
105 return std::exp(-std::pow(
s[0] - center2, 2) / (2.0 * std::pow(sigma2, 2)));
110 const int HISTOGRAM_SIZE = 4000;
111 const float HISTOGRAM_RESOLUTION = 0.02;
113 float dist[HISTOGRAM_SIZE];
118 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
120 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION;
121 dist[i] = nd1(x - center) * nd2(x - center2);
128 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
130 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION - avg;
131 var += std::pow(x, 2) * dist[i];
137 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
142 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
145 ASSERT_NEAR(var, pf.
covariance(1.0, 0.5)[0][0], abs_error * 2);
152 const size_t size_num = 3;
153 const size_t size[size_num] =
161 const float center = 12.3;
162 const float sigma = 0.45;
167 for (
size_t i = 0; i < size_num; ++i)
173 ASSERT_LT(fabs(e[0] - center), 1e-1);
174 ASSERT_LT(fabs(std::sqrt(v[0][0]) - sigma), 1e-1);
179 const std::vector<State> v_r = pf.
covariance();
180 ASSERT_LT(fabs(e_r[0] - center), 1e-1);
181 ASSERT_LT(fabs(std::sqrt(v_r[0][0]) - sigma), 1e-1);
183 if (i + 1 != size_num)
190 TEST(Pf, ResampleFlatLikelihood)
193 const float center = 12.3;
194 const float sigma = 0.45;
199 std::vector<float> orig;
210 void testResample(
const std::vector<float>& probs,
const std::vector<float>& states,
211 const std::vector<float>& expected_resampled_states)
213 const size_t particle_num = probs.size();
215 auto it = pf.
begin();
216 for (
size_t i = 0; i < particle_num; ++i, ++it)
218 it->state_.x = states.at(i);
219 it->probability_ = probs.at(i);
226 EXPECT_FLOAT_EQ(expected_resampled_states.at(i), pf.
getParticle(i)[0]);
230 TEST(Pf, ResampleFirstAndLastParticle)
232 const float small_prob = 1.0e-06
f;
234 SCOPED_TRACE(
"ResampleFirstParticle");
235 const std::vector<float> probs =
243 const std::vector<float> states =
251 const std::vector<float> expected_resampled_states =
262 SCOPED_TRACE(
"ResampleLastParticle");
263 const std::vector<float> probs =
271 const std::vector<float> states =
279 const std::vector<float> expected_resampled_states =
294 const float val0 = 12.3;
295 const float val1 = 45.6;
300 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
302 ASSERT_EQ(it->state_[0], val0);
303 it->state_[0] = val1;
305 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
306 ASSERT_EQ(it->state_[0], val1);
312 const float val0 = 12.3;
313 const float val1 = 45.6;
320 it->state_[0] = val1;
324 for (
size_t i = 0; i < 10; ++i)
326 for (
size_t i = 10; i < 20; ++i)
336 unsigned int idx = 0;
337 auto likelihood = [&idx](
const State&
s) ->
float
339 return idx++ == 0 ? 1.0 : 0.0;
348 auto likelihood = [](
const State&
s) ->
float
360 unsigned int idx = 0;
361 auto likelihood1 = [&idx](
const State&
s) ->
float
364 if (idx >= 4 && idx < 6)
376 auto likelihood2 = [&idx](
const State&
s) ->
float
379 if (idx >= 2 && idx < 8)
389 ASSERT_GT(entropy2, entropy1);
393 int main(
int argc,
char** argv)
395 testing::InitGoogleTest(&argc, argv);
397 return RUN_ALL_TESTS();