24 std::size_t min_samples) noexcept
25 : window_(window < 1 ? 1 : window)
26 , min_samples_(min_samples == 0
27 ? std::max(window_, DEFAULT_MIN_SAMPLES)
29 assert(window > 0 &&
"CoordinateNormalizer: window_size must be > 0");
34void CoordinateNormalizer::push(std::deque<double>& buf,
36 std::size_t max_size)
noexcept {
38 if (buf.size() > max_size) {
45double CoordinateNormalizer::rolling_mean(
const std::deque<double>& buf)
noexcept {
48 for (
double v : buf) {
51 return sum /
static_cast<double>(buf.size());
56double CoordinateNormalizer::rolling_stddev(
const std::deque<double>& buf,
57 double mean)
noexcept {
63 for (
double v : buf) {
64 const double d = v - mean;
68 return std::sqrt(sq_sum /
static_cast<double>(buf.size() - 1));
73double CoordinateNormalizer::zscore(
double value,
74 const std::deque<double>& buf)
noexcept {
78 const double m = rolling_mean(buf);
79 const double s = rolling_stddev(buf, m);
80 if (s < FLAT_STDDEV_THRESHOLD) {
84 return (value - m) / s;
89std::optional<manifold::SpacetimeEvent>
92 push(price_buf_, raw.price, window_);
93 push(volume_buf_, raw.volume, window_);
94 push(momentum_buf_, raw.momentum, window_);
108 .price = zscore(raw.price, price_buf_),
109 .volume = zscore(raw.volume, volume_buf_),
110 .momentum = zscore(raw.momentum, momentum_buf_),
118 return price_buf_.size();
130 return total_ >= min_samples_;
136 momentum_buf_.clear();
std::size_t total_samples() const noexcept
Total number of samples seen so far (does not saturate at window_size()).
CoordinateNormalizer(std::size_t window=20, std::size_t min_samples=0) noexcept
bool warmed_up() const noexcept
std::size_t size() const noexcept
Number of samples currently in the rolling window (≤ window_size()).
void reset() noexcept
Reset the normalizer, clearing all buffered observations.
std::size_t window_size() const noexcept
Configured maximum window size.
std::optional< manifold::SpacetimeEvent > normalize(const manifold::SpacetimeEvent &raw) noexcept
CoordinateNormalizer — rolling z-score normalizer for SpacetimeEvent.
A point in 4D spacetime (t, x, y, z).
double time
Market time coordinate (bar index or timestamp)