quant-pricer-cpp
Loading...
Searching...
No Matches
stats.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <algorithm>
5#include <cstdint>
6
7namespace quant::stats {
8
9struct Welford {
10 std::uint64_t count{0};
11 double mean{0.0};
12 double m2{0.0};
13
14 inline void add(double value) {
15 ++count;
16 const double delta = value - mean;
17 mean += delta / static_cast<double>(count);
18 const double delta2 = value - mean;
19 m2 += delta * delta2;
20 }
21
22 inline void merge(const Welford& other) {
23 if (other.count == 0)
24 return;
25 if (count == 0) {
26 *this = other;
27 return;
28 }
29 const double total = static_cast<double>(count + other.count);
30 const double delta = other.mean - mean;
31 mean += delta * static_cast<double>(other.count) / total;
32 m2 += other.m2 + delta * delta * (static_cast<double>(count) * other.count / total);
33 count += other.count;
34 }
35
36 inline double variance() const { return (count > 1) ? m2 / static_cast<double>(count - 1) : 0.0; }
37};
38
39} // namespace quant::stats
quant::stats::Welford delta
Definition mc.cpp:53
Streaming statistics utilities.
Definition stats.hpp:7
double value
Definition pde.cpp:91
Definition stats.hpp:9
double variance() const
Definition stats.hpp:36
void merge(const Welford &other)
Definition stats.hpp:22
std::uint64_t count
Definition stats.hpp:10
double m2
Definition stats.hpp:12
double mean
Definition stats.hpp:11
void add(double value)
Definition stats.hpp:14