mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-03-12 14:46:49 +01:00
MathUtil: Add RunningMean/RunningVariance classes.
This commit is contained in:
parent
82a3aa5ff6
commit
9efcd08ea3
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
@ -93,6 +94,45 @@ struct Rectangle
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class RunningMean
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
constexpr void Clear() { *this = {}; }
|
||||||
|
|
||||||
|
constexpr void Push(T x) { m_mean = m_mean + (x - m_mean) / ++m_count; }
|
||||||
|
|
||||||
|
constexpr size_t Count() const { return m_count; }
|
||||||
|
constexpr T Mean() const { return m_mean; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t m_count = 0;
|
||||||
|
T m_mean{};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class RunningVariance
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
constexpr void Clear() { *this = {}; }
|
||||||
|
|
||||||
|
constexpr void Push(T x)
|
||||||
|
{
|
||||||
|
const auto old_mean = m_running_mean.Mean();
|
||||||
|
m_running_mean.Push(x);
|
||||||
|
m_variance += (x - old_mean) * (x - m_running_mean.Mean());
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t Count() const { return m_running_mean.Count(); }
|
||||||
|
constexpr T Mean() const { return m_running_mean.Mean(); }
|
||||||
|
constexpr T Variance() const { return m_variance / (Count() - 1); }
|
||||||
|
constexpr T StandardDeviation() const { return std::sqrt(Variance()); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
RunningMean<T> m_running_mean;
|
||||||
|
T m_variance{};
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace MathUtil
|
} // namespace MathUtil
|
||||||
|
|
||||||
float MathFloatVectorSum(const std::vector<float>&);
|
float MathFloatVectorSum(const std::vector<float>&);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user