JaiaBot 3.0.0
JaiaBot micro-AUV software
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1#ifndef JAIABOT_UTILS_STATS_H
2#define JAIABOT_UTILS_STATS_H
3
4#include <boost/circular_buffer.hpp>
5
6#include <boost/accumulators/accumulators.hpp>
7#include <boost/accumulators/statistics/mean.hpp>
8#include <boost/accumulators/statistics/median.hpp>
9#include <boost/accumulators/statistics/variance.hpp>
10
11#include <boost/units/cmath.hpp>
12#include <boost/units/io.hpp>
13
14namespace jaiabot
15{
16namespace utils
17{
18
19enum class VarianceNorm
20{
23};
24
25template <typename Quantity> class RollingStatsAccumulator;
26
27template <typename Quantity>
28inline std::ostream& operator<<(std::ostream& os, const RollingStatsAccumulator<Quantity>& acc)
29{
30 os << "RollingStatsAccumulator {\n";
31
32 os << " size : " << acc.size() << "\n";
33 os << " capacity : " << acc.buf_.capacity() << "\n";
34
35 os << " values : [";
36 for (std::size_t i = 0; i < acc.buf_.size(); ++i)
37 {
38 if (i > 0)
39 os << ", ";
40 os << Quantity::from_value(acc.buf_[i]);
41 }
42 os << "]\n";
43
44 if (acc.buf_.empty())
45 {
46 os << " stats : <empty>\n";
47 }
48 else
49 {
50 os << " stats :\n";
51 os << " μ = " << acc.mean() << "\n";
52 os << " med = " << acc.median() << "\n";
53 os << " min = " << acc.min() << "\n";
54 os << " max = " << acc.max() << "\n";
55 os << " σ (N-1) = " << acc.stddev(VarianceNorm::UNBIASED) << "\n";
56 os << " σ (N) = " << acc.stddev(VarianceNorm::BIASED) << "\n";
57 os << " σ² (N-1) = " << acc.variance(VarianceNorm::UNBIASED) << "\n";
58 os << " σ² (N) = " << acc.variance(VarianceNorm::BIASED) << "\n";
59 }
60
61 os << "}";
62
63 return os;
64}
65
67template <typename Quantity> class RollingStatsAccumulator
68{
69 public:
73 RollingStatsAccumulator(std::size_t n) : buf_(n) {}
74
75 void push_back(const Quantity& v) { buf_.push_back(v.value()); }
76 void operator()(const Quantity& v) { push_back(v); }
77
78 using value_type = typename Quantity::value_type;
79 using unit_type = typename Quantity::unit_type;
80 using unit2_type = typename boost::units::multiply_typeof_helper<unit_type, unit_type>::type;
81 using QuantitySquared = boost::units::quantity<unit2_type, value_type>;
82
83 std::size_t size() const { return buf_.size(); }
84 std::size_t n() const { return size(); }
85
86 Quantity mean() const { return extract<boost::accumulators::tag::mean, Quantity>(); }
87 Quantity μ() const { return mean(); }
88
89 Quantity median() const
90 {
91 if (buf_.empty())
92 throw std::runtime_error("No values in RollingStatsAccumulator");
93 std::vector<typename Quantity::value_type> v(buf_.begin(), buf_.end());
94 std::sort(v.begin(), v.end());
95 const std::size_t n = v.size();
96 if (n % 2 == 1)
97 return Quantity::from_value(v[n / 2]);
98 else
99 return Quantity::from_value((v[n / 2 - 1] + v[n / 2]) / 2);
100 }
101
106 {
107 auto biased_var = extract<boost::accumulators::tag::variance, QuantitySquared>();
108 value_type n = size();
109 if (norm == VarianceNorm::UNBIASED && n > 1)
110 {
111 return (n / (n - 1)) * biased_var;
112 }
113 else
114 {
115 return biased_var;
116 }
117 }
119
124 {
125 return boost::units::sqrt(variance(norm));
126 }
127 Quantity σ(VarianceNorm norm = VarianceNorm::UNBIASED) const { return stddev(norm); }
128
129 Quantity min() const { return extract<boost::accumulators::tag::min, Quantity>(); }
130 Quantity max() const { return extract<boost::accumulators::tag::max, Quantity>(); }
131
132 friend std::ostream& operator<< <>(std::ostream&, const RollingStatsAccumulator&);
133
134 private:
135 template <typename Tag, typename ReturnQuantity> ReturnQuantity extract() const
136 {
137 if (buf_.empty())
138 throw(std::runtime_error("No values in RollingStatsAccumulator"));
139
140 namespace ba = boost::accumulators;
141 ba::accumulator_set<typename Quantity::value_type, ba::features<Tag>> a;
142 std::for_each(buf_.begin(), buf_.end(), std::bind(std::ref(a), std::placeholders::_1));
143 auto result = ba::extract_result<Tag>(a);
144 return ReturnQuantity::from_value(result);
145 }
146
147 private:
148 boost::circular_buffer<typename Quantity::value_type> buf_;
149};
150
151} // namespace utils
152} // namespace jaiabot
153
154#endif
Maintains a circular buffer of n samples of some boost::units::quantity and allows for computation of...
Definition stats.h:68
void push_back(const Quantity &v)
Definition stats.h:75
void operator()(const Quantity &v)
Definition stats.h:76
boost::units::quantity< unit2_type, value_type > QuantitySquared
Definition stats.h:81
typename boost::units::multiply_typeof_helper< unit_type, unit_type >::type unit2_type
Definition stats.h:80
typename Quantity::value_type value_type
Definition stats.h:78
Quantity σ(VarianceNorm norm=VarianceNorm::UNBIASED) const
Definition stats.h:127
Quantity stddev(VarianceNorm norm=VarianceNorm::UNBIASED) const
Standard deviation.
Definition stats.h:123
RollingStatsAccumulator(std::size_t n)
Constructor.
Definition stats.h:73
typename Quantity::unit_type unit_type
Definition stats.h:79
QuantitySquared σ2(VarianceNorm norm=VarianceNorm::UNBIASED) const
Definition stats.h:118
QuantitySquared variance(VarianceNorm norm=VarianceNorm::UNBIASED) const
Variance.
Definition stats.h:105
std::ostream & operator<<(std::ostream &os, const RollingStatsAccumulator< Quantity > &acc)
Definition stats.h:28