22 #ifndef EDSP_STATISTICAL_MOMENT_H 23 #define EDSP_STATISTICAL_MOMENT_H 26 #include <edsp/meta/iterator.hpp> 29 namespace edsp {
namespace statistics {
34 template <
int N,
class T>
35 constexpr T nthPower(T x) {
37 for (
int i = 1; i < N; ++i) {
43 template <
class T,
int N>
44 struct SumDiffNthPower {
45 explicit SumDiffNthPower(T x) : mean_(x) {}
46 constexpr T operator()(T sum, T current) {
47 return sum + nthPower<N>(current - mean_);
52 template <
class T,
int N,
class Iter_T>
53 T nthMoment(Iter_T first, Iter_T last, T
mean) {
55 return std::accumulate(first, last, T(), SumDiffNthPower<T, N>(mean)) /
56 static_cast<meta::value_type_t<Iter_T>
>(cnt);
69 template <std::
size_t N,
typename ForwardIt>
70 constexpr meta::value_type_t<ForwardIt>
moment(ForwardIt first, ForwardIt last) {
71 using input_t = meta::value_type_t<ForwardIt>;
72 const auto m =
mean(first, last);
73 return internal::nthMoment<input_t, N>(first, last, m);
86 template <std::
size_t N,
typename ForwardIt>
87 constexpr meta::value_type_t<ForwardIt>
moment(ForwardIt first, ForwardIt last,
88 const meta::value_type_t<ForwardIt> mean) {
89 using input_t = meta::value_type_t<ForwardIt>;
90 return internal::nthMoment<input_t, N>(first, last,
mean);
95 #endif // EDSP_STATISTICAL_MOMMENT_H constexpr meta::value_type_t< ForwardIt > moment(ForwardIt first, ForwardIt last)
Computes the n-th moment of the range [first, last)
Definition: moment.hpp:70
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29
constexpr meta::value_type_t< ForwardIt > mean(ForwardIt first, ForwardIt last)
Computes the average or mean value of the range [first, last)
Definition: mean.hpp:44