CKTIS / C++
Reductions and statistics
A reduction combines a range into one result. Learn how std::accumulate, std::reduce, and std::transform_reduce differ, and calculate a mean and variance while accounting for accumulator types and operation order.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <numeric>
std::accumulate(first, last, init)
std::accumulate(first, last, init, binary_op)
std::reduce(first, last, init)
std::transform_reduce(first, last, init, binary_op, unary_op)A reduction combines the elements of [first, last) into one result, such as a sum. Algorithms in <numeric> provide building blocks for statistics. std::accumulate starts with init and processes elements in order from beginning to end. The type of init determines the accumulator type: 0 means int, whereas 0.0 means double. An unsuitable type can discard fractional parts when the accumulator is updated.
std::reduce may change operation order and grouping even without an execution policy. It is therefore not a replacement for std::accumulate when order matters. For an order-independent result, the operation should be associative and commutative; floating-point addition is not associative. std::transform_reduce transforms elements and reduces the results without requiring a separate intermediate container; it also permits reordering. Its single-range overload does not transform init.
For n observations, the mean is the sum divided by n. Population variance is the sum of squared deviations from the mean divided by n; sample variance with Bessel's correction uses n - 1. Before dividing, require n > 0 or n > 1, respectively. The example makes two passes: first it calculates the mean, then the squared deviations. For empty input, it exits without printing a result.
The example uses std::accumulate to preserve calculation order; this does not guarantee exact arithmetic for arbitrary floating-point data. The APIs shown are suitable for C++23. The linked document is an evolving working draft, not a frozen C++23 publication; the repository compiler flag does not attest to the production runtime.
Examples
#include <array>
#include <iomanip>
#include <iostream>
#include <numeric>
int main() {
const std::array<double, 4> values{1.0, 2.0, 3.0, 4.0};
if (values.empty()) {
return 0;
}
const double n = static_cast<double>(values.size());
const double sum = std::accumulate(values.begin(), values.end(), 0.0);
const double mean = sum / n;
const double squared_deviations = std::accumulate(
values.begin(), values.end(), 0.0,
[mean](double acc, double x) {
const double d = x - mean;
return acc + d * d;
});
const double variance = squared_deviations / n;
std::cout << std::fixed << std::setprecision(2)
<< "sum=" << sum << '\n'
<< "mean=" << mean << '\n'
<< "population_variance=" << variance << '\n';
}
Result
sum=10.00 mean=2.50 population_variance=1.25
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.