44namespace triqs::stat {
101 if (
max_n_bins != -1 &&
max_n_bins < 1)
throw std::runtime_error(
"log_binning: max_n_bins must be -1 (unbounded) or >= 1.");
115 bare_counts_.push_back(0);
120 [[nodiscard]]
long max_n_bins()
const {
return max_n_bins_; }
126 [[nodiscard]]
long n_bins()
const {
return var_bins_.size(); }
129 [[nodiscard]]
long count()
const {
return count_; }
133 std::vector<long> res(
n_bins());
134 for (
int i = 0; i <
n_bins(); ++i) res[i] = count_ >> i;
139 [[nodiscard]]
auto const &
mean_bins()
const {
return mean_bins_; }
142 [[nodiscard]]
auto const &
var_bins()
const {
return var_bins_; }
145 [[nodiscard]]
auto const &
bare_bins()
const {
return bare_bins_; }
148 [[nodiscard]]
auto const &
bare_counts()
const {
return bare_counts_; }
199 if (mean_bins_.empty()) {
203 if (max_n_bins_ != 1) {
205 bare_counts_.push_back(0);
209 T x_m = (x - mean_bins_[0]);
210 var_bins_[0] +=
abs_square(x_m) * (
static_cast<double>(count_ - 1) / count_);
211 mean_bins_[0] += x_m / count_;
213 if (max_n_bins_ == 1)
return *
this;
215 if (count_ == (1L << bare_bins_.size()) && (
is_unbounded() ||
n_bins() < max_n_bins_)) {
222 bare_counts_.push_back(0);
229 for (
int i = 0; i < bare_bins_.size() && bare_counts_[i] == 2; ++i) {
230 if (i + 1 < bare_bins_.size()) {
231 bare_bins_[i + 1] += bare_bins_[i];
232 ++bare_counts_[i + 1];
235 auto const bc =
static_cast<double>(1ul << (i + 1));
236 auto const k = count_ / bc;
237 x_m = (bare_bins_[i] / bc - mean_bins_[i + 1]);
238 var_bins_[i + 1] +=
abs_square(x_m) * ((k - 1) / k);
239 mean_bins_[i + 1] += x_m / k;
275 return calculate_mean_errors_and_taus(mean_bins_, var_bins_,
effective_counts(), min_samples);
299 return calculate_mean_errors_and_taus(mean_red, var_red, nsamples_red, min_samples);
315 auto mean_red = mean_bins_;
316 auto var_red = var_bins_;
320 auto const nbins = mpi::all_reduce(
n_bins(), c, MPI_MAX);
323 nsamples_red.resize(nbins, 0);
325 for (
int i = 0; i < nbins; ++i) {
326 auto const ns = nsamples_red[i];
327 mpi::all_reduce_in_place(nsamples_red[i], c);
330 if (nsamples_red[i] > 0) mean_red[i] *=
static_cast<double>(ns) /
static_cast<double>(nsamples_red[i]);
331 mpi::all_reduce_in_place(mean_red[i], c);
333 if (i < mean_bins_.size()) var_red[i] += ns *
abs_square(mean_bins_[i] - mean_red[i]);
334 mpi::all_reduce_in_place(var_red[i], c);
337 return std::make_tuple(mean_red, var_red, nsamples_red);
341 [[nodiscard]]
static std::string
hdf5_format() {
return "log_binning"; }
351 auto gr = g.create_group(name);
352 h5::write_hdf5_format(gr, acc);
353 h5::write(gr,
"max_n_bins", acc.max_n_bins_);
354 h5::write(gr,
"count", acc.count_);
355 h5::write(gr,
"mean_bins", acc.mean_bins_);
356 h5::write(gr,
"var_bins", acc.var_bins_);
357 h5::write(gr,
"bare_bins", acc.bare_bins_);
358 h5::write(gr,
"bare_counts", acc.bare_counts_);
369 auto gr = g.open_group(name);
370 h5::assert_hdf5_format(gr, acc);
371 h5::read(gr,
"max_n_bins", acc.max_n_bins_);
372 h5::read(gr,
"count", acc.count_);
373 h5::read(gr,
"mean_bins", acc.mean_bins_);
374 h5::read(gr,
"var_bins", acc.var_bins_);
375 h5::read(gr,
"bare_bins", acc.bare_bins_);
376 h5::read(gr,
"bare_counts", acc.bare_counts_);
378 if (acc.max_n_bins_ != -1 && acc.max_n_bins_ < 1)
379 throw std::runtime_error(
"h5_read: invalid log_binning state (max_n_bins must be -1 or >= 1).");
385 [[nodiscard]]
auto calculate_mean_errors_and_taus(std::vector<value_t>
const &mk, std::vector<real_t>
const &qk,
386 std::vector<long>
const &nsamples,
int min_samples)
const {
388 if (mk.empty())
return std::make_tuple(
value_t{}, std::vector<real_t>{}, std::vector<real_t>{}, std::vector<long>{});
391 auto const size = std::distance(nsamples.begin(), std::ranges::upper_bound(nsamples, min_samples, std::greater{}));
392 auto effs = std::vector(nsamples.begin(), nsamples.begin() +
size);
393 auto errs = std::vector(qk.begin(), qk.begin() +
size);
394 auto taus = std::vector<real_t>(
size);
398 real_t var0 = qk[0] / (
static_cast<double>(nsamples[0]) *
static_cast<double>(nsamples[0] - 1));
399 for (
int i = 0; i <
size; ++i) {
400 real_t var = errs[i] / (
static_cast<double>(effs[i]) *
static_cast<double>(effs[i] - 1));
401 errs[i] = nda::sqrt(var);
405 return std::make_tuple(mk[0], errs, taus, effs);
408 long max_n_bins_{-1};
410 std::vector<value_t> mean_bins_{};
411 std::vector<real_t> var_bins_{};
412 std::vector<value_t> bare_bins_{};
413 std::vector<int> bare_counts_{};
int size() const
Get the total number of blocks.
Logarithmic binning accumulator.
auto const & var_bins() const
Get the bins that store the sum of the squared deviations from the mean.
friend void h5_write(h5::group g, std::string const &name, log_binning const &acc)
Write a triqs::stat::log_binning accumulator to HDF5.
long max_n_bins() const
Get the maximum number of bins.
auto const & bare_counts() const
Get the number of samples currently in the bare bins.
get_real_t< T > real_t
Real type of the accumulated data type.
auto const & bare_bins() const
Get the bins that are used for accumulating the bare samples.
long n_bins() const
Get the current number of bins.
log_binning(T const &sample, int max_n_bins)
Construct a logarithmic binning accumulator with a dummy sample and the maximum number of bins.
auto effective_counts() const
Get the number of effective samples accumulated in each bin.
bool is_unbounded() const
True if the accumulator was constructed with an unbounded number of bins.
T value_t
Type of the accumulated data.
auto mean_errors_and_taus(int min_samples=2) const
Get the overall mean of the data as well as the estimated standard error, integrated autocorrelation ...
friend void h5_read(h5::group g, std::string const &name, log_binning &acc)
Read a triqs::stat::log_binning accumulator from HDF5.
log_binning< T > & operator<<(U const &x)
Accumulate a new sample.
long count() const
Get the total number of accumulated samples.
static std::string hdf5_format()
Get the HDF5 format string.
auto mpi_all_reduce(mpi::communicator c) const
Allreduce the accumulated data over multiple MPI processes.
auto const & mean_bins() const
Get the bins that store the mean data.
auto mean_errors_and_taus(mpi::communicator c, int min_samples=2) const
Get the overall mean of the data as well as the estimated standard error, integrated autocorrelation ...
log_binning()=default
Default construct a logarithmic binning accumulator.
auto tau_estimate_from_vars(auto const &var, auto const &var0)
Compute an estimate for the integrated auto-correlation time from variances.
std::remove_cvref_t< decltype(make_real(std::declval< T >()))> get_real_t
Type trait to get the type that would be returned by triqs::stat::make_real.
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 functions to calculate the arithmetic mean and standard error of a range of values.
Provides various concepts for the Utilities.
Provides various utilities for the Utilities.