test_effective_sample_size.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <vector>
18 
20 
21 namespace {
22 
23 TEST(EffectiveSampleSize, Empty) {
24  auto weights = std::vector<double>{};
25  ASSERT_EQ(beluga::effective_sample_size(weights), 0.0);
26 }
27 
28 TEST(EffectiveSampleSize, Zeros) {
29  auto weights = std::vector{0.0, 0.0, 0.0, 0.0, 0.0};
30  ASSERT_EQ(beluga::effective_sample_size(weights), 0.0);
31 }
32 
33 TEST(EffectiveSampleSize, Ones) {
34  auto weights = std::vector{1.0, 1.0, 1.0, 1.0, 1.0};
35  ASSERT_NEAR(beluga::effective_sample_size(weights), 5.0, 0.01);
36 }
37 
38 TEST(EffectiveSampleSize, HomogeneousLow) {
39  auto weights = std::vector{0.1, 0.1, 0.1, 0.1, 0.1};
40  ASSERT_NEAR(beluga::effective_sample_size(weights), 5.0, 0.01);
41 }
42 
43 TEST(EffectiveSampleSize, HomogeneousHigh) {
44  auto weights = std::vector{100, 100, 100, 100, 100};
45  ASSERT_NEAR(beluga::effective_sample_size(weights), 5.0, 0.01);
46 }
47 
48 TEST(EffectiveSampleSize, OneZero) {
49  auto weights = std::vector{1.0, 0.0};
50  ASSERT_NEAR(beluga::effective_sample_size(weights), 1.0, 0.01);
51 }
52 
53 TEST(EffectiveSampleSize, OneZeroZero) {
54  auto weights = std::vector{1.0, 0.0, 0.0};
55  ASSERT_NEAR(beluga::effective_sample_size(weights), 1.0, 0.01);
56 }
57 
58 TEST(EffectiveSampleSize, OneOneZero) {
59  auto weights = std::vector{1.0, 1.0, 0.0};
60  ASSERT_NEAR(beluga::effective_sample_size(weights), 2.0, 0.01);
61 }
62 
63 TEST(EffectiveSampleSize, Heterogeneous1) {
64  auto weights = std::vector{1.0, 0.5, 0.0};
65  ASSERT_NEAR(beluga::effective_sample_size(weights), 1.8, 0.01);
66 }
67 
68 TEST(EffectiveSampleSize, Heterogeneous2) {
69  auto weights = std::vector{1.0, 0.5, 0.5};
70  ASSERT_NEAR(beluga::effective_sample_size(weights), 2.66, 0.01);
71 }
72 
73 TEST(EffectiveSampleSize, Particles) {
74  struct Particle {
75  int state;
76  double weight;
77  };
78  auto particles = std::vector{Particle{1, 1.0}, Particle{1, 0.5}, Particle{1, 0.5}};
79  ASSERT_NEAR(beluga::effective_sample_size(particles), 2.66, 0.01);
80 }
81 
82 } // namespace
effective_sample_size.hpp
Implementation of an algorithm to calculate the effective sample size (ESS).
beluga::state
constexpr state_detail::state_fn state
Customization point object for accessing the state of a particle.
Definition: primitives.hpp:163
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition: primitives.hpp:264
beluga::effective_sample_size
auto effective_sample_size(Range &&range)
Calculate the ESS of a given a range of weights.
Definition: effective_sample_size.hpp:46
beluga::views::weights
constexpr auto weights
Definition: particles.hpp:34
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53