JoL
JoL

Reputation: 31

Get last added value of boost accumulator_set

I am using boost::accumulator_set to compute the min, max and mean of some values.

Is there a way (perhaps a boost::accumulators::tag) to retrieve the last value that was fed to the accumulator? Something like boost::accumulators::last(my_acc)?

Upvotes: 1

Views: 214

Answers (2)

JoL
JoL

Reputation: 31

I've found an easy way to implement this feature: Simply write a new feature by copying the existing 'sum' feature (link to original) and changing '+=' to '='. This does the job.

Upvotes: 1

sehe
sehe

Reputation: 393134

You could hack it using a rolling window size==1:

Live On Coliru

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_sum.hpp>
#include <boost/accumulators/statistics/rolling_window.hpp>
#include <fmt/ranges.h>
namespace ba  = boost::accumulators;
namespace bat = boost::accumulators::tag;

int main() {
    ba::accumulator_set<double, ba::stats<bat::mean, bat::rolling_window, bat::rolling_sum>> accu(
        bat::rolling_window::window_size = 1);

    for (int i : {100, 10, 50, -70}) {
        accu(i);
        fmt::print("mean {:>6.2f} last {:>6.2f} (total #{} samples)\n", ba::mean(accu), ba::rolling_sum(accu),
                   ba::count(accu));
    }
}

Print

mean 100.00 last 100.00 (total #1 samples)
mean  55.00 last  10.00 (total #2 samples)
mean  53.33 last  50.00 (total #3 samples)
mean  22.50 last -70.00 (total #4 samples)

Upvotes: 1

Related Questions