28#include <itertools/itertools.hpp>
38namespace triqs::stat {
67 enum class error_tag { sum, var_data, var_mean, err_data, err_mean, jk_err };
80 template <mean_tag mtag, AccCompatible T>
void apply_mean_tag(T &m, [[maybe_unused]]
long nsamples) {
81 if constexpr (mtag == mean_tag::sum) m *= nsamples;
96 template <error_tag etag, AccCompatible T>
void apply_error_tag(T &sum_sq_devs, [[maybe_unused]]
long nsamples) {
97 if constexpr (etag == error_tag::sum)
return;
98 auto const nd =
static_cast<double>(nsamples);
99 if constexpr (etag == error_tag::err_data || etag == error_tag::var_data)
100 sum_sq_devs /= (nd - 1);
101 else if constexpr (etag == error_tag::err_mean || etag == error_tag::var_mean)
102 sum_sq_devs /= (nd * (nd - 1));
103 else if constexpr (etag == error_tag::jk_err)
104 sum_sq_devs *= (nd - 1) / nd;
105 if constexpr (etag == error_tag::err_data || etag == error_tag::err_mean || etag == error_tag::jk_err) sum_sq_devs = nda::sqrt(sum_sq_devs);
120 template <mean_tag mtag = mean_tag::mean, StatCompatibleRange R>
auto mean(R &&rg) {
121 if constexpr (mtag == mean_tag::mean) {
124 for (
auto const &[n, x] : itertools::enumerate(rg)) res += (x - res) / (n + 1);
128 return std::accumulate(std::ranges::begin(rg), std::ranges::end(rg),
zeroed_sample(*std::ranges::begin(rg)));
160 template <mean_tag mtag = mean_tag::mean, StatCompatibleRange R>
161 auto mean_mpi(std::optional<mpi::communicator> c, R &&rg) {
168 if constexpr (mtag == mean_tag::mean) {
170 auto const n_i = std::ranges::size(rg);
171 auto const n = mpi::all_reduce(n_i, *c);
172 res *=
static_cast<double>(n_i) /
static_cast<double>(n);
174 mpi::all_reduce_in_place(res, *c);
195 template <error_tag etag = error_tag::err_mean, mean_tag mtag = mean_tag::mean, StatCompatibleRange R>
200 for (
auto const &[n, x] : itertools::enumerate(rg)) {
201 auto const nd =
static_cast<double>(n);
202 res_s +=
abs_square(x - res_m) * nd / (nd + 1);
203 res_m += (x - res_m) / (nd + 1);
208 return std::make_pair(res_m, res_s);
235 template <error_tag etag = error_tag::err_mean, mean_tag mtag = mean_tag::mean, StatCompatibleRange R>
246 auto const n = std::ranges::size(rg);
247 auto const n_red = mpi::all_reduce(n, *c);
250 value_t res_m = m * (
static_cast<double>(n) /
static_cast<double>(n_red));
251 mpi::all_reduce_in_place(res_m, *c);
255 mpi::all_reduce_in_place(ssqdev, *c);
260 return std::make_pair(res_m, ssqdev);
278 return nda::make_regular(nda::map([](
auto v,
auto v0) {
return (v0 == 0.0) ?
nan_sample(v0) : 0.5 * (v / v0 - 1.0); })(var, var0));
auto mean_and_err_mpi(std::optional< mpi::communicator > c, R &&rg)
Calculate the arithmetic mean or the simple sum as well as a corresponding error estimate of some ran...
error_tag
Tag to indicate what to calculate when computing the error of a range of values.
void apply_error_tag(T &sum_sq_devs, long nsamples)
Given the sum of squared deviations from the mean, , and the number of samples , apply a transformat...
auto tau_estimate_from_errors(T const &s_n, T const &s_0)
Compute an estimate for the integrated auto-correlation time from standard errors.
auto mean_and_err(R &&rg)
Calculate the arithmetic mean or the simple sum as well as a corresponding error estimate of some ran...
auto mean_mpi(std::optional< mpi::communicator > c, R &&rg)
Calculate the arithmetic mean or the simple sum of some range of values spread across multiple MPI pr...
mean_tag
Tag to indicate what to calculate when computing the mean of a range of values.
auto tau_estimate_from_vars(auto const &var, auto const &var0)
Compute an estimate for the integrated auto-correlation time from variances.
auto mean(R &&rg)
Calculate the arithmetic mean or the simple sum of some range of values.
void apply_mean_tag(T &m, long nsamples)
Given the mean and the number of samples , apply a transformation to get the result specified by the...
std::remove_cvref_t< decltype(nda::make_regular(std::declval< T >()))> get_regular_t
Type trait to get the type that would be returned by nda::make_regular.
auto nan_sample(T const &sample)
Get a sample with all elements set to NaN.
auto zeroed_sample(T const &sample)
Get a sample with all elements set to zero.
auto abs_square(auto const &x)
Calculate the (elementwise) absolute square of an array/view/scalar.
auto make_real(T &&t)
Make a given object real and regular.
Provides various concepts for the Utilities.
Provides various utilities for the Utilities.