include
beluga
algorithm
effective_sample_size.hpp
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
#ifndef BELUGA_ALGORITHM_EFFECTIVE_SAMPLE_SIZE_HPP
16
#define BELUGA_ALGORITHM_EFFECTIVE_SAMPLE_SIZE_HPP
17
18
#include <
beluga/type_traits/particle_traits.hpp
>
19
#include <
beluga/views/particles.hpp
>
20
21
#include <range/v3/numeric/accumulate.hpp>
22
28
namespace
beluga
{
29
31
45
template
<
class
Range, std::enable_if_t<!is_particle_range_v<Range>,
int
> = 0>
46
auto
effective_sample_size
(Range&& range) {
47
const
auto
total_weight = ranges::accumulate(range, 0.0);
48
49
if
(total_weight == 0.0) {
50
return
0.0;
51
}
52
53
auto
normalize_and_square = [total_weight](
auto
weight
) {
54
const
auto
normalized_weight =
weight
/ total_weight;
55
return
normalized_weight * normalized_weight;
56
};
57
58
return
1.0 / ranges::accumulate(range, 0.0, std::plus<>{}, std::move(normalize_and_square));
59
}
60
62
template
<
class
Range, std::enable_if_t<is_particle_range_v<Range>,
int
> = 0>
63
auto
effective_sample_size
(Range&& range) {
64
return
effective_sample_size
(range |
beluga::views::weights
);
65
}
66
67
}
// namespace beluga
68
69
#endif
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition:
primitives.hpp:264
particles.hpp
Implementation of views related to particle ranges.
particle_traits.hpp
Implementation of traits for particle types, see the Particle named requirements.
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
The main Beluga namespace.
Definition:
3d_embedding.hpp:21
beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53