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
7
namespace
quant::stats
{
8
9
struct
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
delta
quant::stats::Welford delta
Definition
mc.cpp:53
quant::stats
Streaming statistics utilities.
Definition
stats.hpp:7
value
double value
Definition
pde.cpp:91
quant::stats::Welford
Definition
stats.hpp:9
quant::stats::Welford::variance
double variance() const
Definition
stats.hpp:36
quant::stats::Welford::merge
void merge(const Welford &other)
Definition
stats.hpp:22
quant::stats::Welford::count
std::uint64_t count
Definition
stats.hpp:10
quant::stats::Welford::m2
double m2
Definition
stats.hpp:12
quant::stats::Welford::mean
double mean
Definition
stats.hpp:11
quant::stats::Welford::add
void add(double value)
Definition
stats.hpp:14
include
quant
stats.hpp
Generated by
1.9.8