#include "LiveAgg.hpp"

#include <cmath>
LiveAgg::LiveAgg() : num_of_data(0), mean(0.0), S(0.0) {}

double LiveAgg::getSD() const noexcept { return S / (num_of_data - 1); }

double LiveAgg::getSEM() const noexcept {
    return S / (num_of_data - 1) / std::sqrt(num_of_data);
}

double LiveAgg::getMean() const noexcept { return mean; }

void LiveAgg::feed(double value) noexcept {
    if (value != value) {
        return;
    }
    num_of_data += 1;
    auto delta = value - mean;
    mean += delta / num_of_data;
    auto delta2 = value - mean;
    S += delta * delta2;
}
int LiveAgg::getNumPoints() const noexcept { return num_of_data; }

std::ostream &operator<<(std::ostream &os, const LiveAgg &agg) {
    os << "num_of_data: " << agg.num_of_data << " mean: " << agg.mean
       << " S: " << agg.S;
    return os;
}
