]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | // Copyright 2019 Hans Dembinski |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt | |
5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | //[ guide_fill_weighted_profile | |
8 | ||
9 | #include <boost/format.hpp> | |
10 | #include <boost/histogram.hpp> | |
11 | #include <cassert> | |
12 | #include <iostream> | |
13 | #include <sstream> | |
14 | ||
15 | int main() { | |
16 | using namespace boost::histogram; | |
17 | using namespace boost::histogram::literals; // _c suffix creates compile-time numbers | |
18 | ||
19 | // make 2D weighted profile | |
20 | auto h = make_weighted_profile(axis::integer<>(0, 2), axis::integer<>(0, 2)); | |
21 | ||
22 | // The mean is computed from the values marked with the sample() helper function. | |
23 | // Weights can be passed as well. The `sample` and `weight` arguments can appear in any | |
24 | // order, but they must be the first or last arguments. | |
25 | h(0, 0, sample(1)); // sample goes to cell (0, 0); weight is 1 | |
26 | h(0, 0, sample(2), weight(3)); // sample goes to cell (0, 0); weight is 3 | |
27 | h(1, 0, sample(3)); // sample goes to cell (1, 0); weight is 1 | |
28 | h(1, 0, sample(4)); // sample goes to cell (1, 0); weight is 1 | |
29 | h(0, 1, sample(5)); // sample goes to cell (1, 0); weight is 1 | |
30 | h(0, 1, sample(6)); // sample goes to cell (1, 0); weight is 1 | |
31 | h(1, 1, weight(4), sample(7)); // sample goes to cell (1, 1); weight is 4 | |
32 | h(weight(5), sample(8), 1, 1); // sample goes to cell (1, 1); weight is 5 | |
33 | ||
34 | std::ostringstream os; | |
35 | for (auto&& x : indexed(h)) { | |
36 | const auto i = x.index(0_c); | |
37 | const auto j = x.index(1_c); | |
38 | const auto m = x->value(); // weighted mean | |
39 | const auto v = x->variance(); // estimated variance of weighted mean | |
40 | os << boost::format("index %i,%i mean %.1f variance %.1f\n") % i % j % m % v; | |
41 | } | |
42 | ||
43 | std::cout << os.str() << std::flush; | |
44 | ||
45 | assert(os.str() == "index 0,0 mean 1.8 variance 0.5\n" | |
46 | "index 1,0 mean 3.5 variance 0.5\n" | |
47 | "index 0,1 mean 5.5 variance 0.5\n" | |
48 | "index 1,1 mean 7.6 variance 0.5\n"); | |
49 | } | |
50 | ||
51 | //] |