MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ update_trend()

double MayaFlux::Kinesis::Stochastic::Estimate::update_trend ( double  sample)
private

Definition at line 151 of file Estimate.cpp.

152{
153 m_history.push(sample);
154
155 if (m_state.sample_count < 2) {
156 m_state.trend_slope = 0.0;
158 m_state.floor = 0.0;
159 m_state.filtered_value = sample;
160 return m_state.floor;
161 }
162
163 const auto view = m_history.linearized_view();
164
165 std::vector<double> chronological(view.rbegin(), view.rend());
166 std::span<const double> ordered(chronological);
167
168 const double slope = trend_slope(ordered);
169 m_state.trend_slope = slope;
171
172 const double mean = std::accumulate(chronological.begin(), chronological.end(), 0.0) / static_cast<double>(chronological.size());
174
175 const auto n = static_cast<double>(chronological.size());
176 double sum_x = 0.0;
177 for (size_t i = 0; i < chronological.size(); ++i)
178 sum_x += static_cast<double>(i);
179 const double mean_x = sum_x / n;
180 const double intercept = mean - slope * mean_x;
181 const auto newest_x = static_cast<double>(chronological.size() - 1);
182 m_state.filtered_value = intercept + slope * newest_x;
183
184 const double first = chronological.front();
185 const double last = chronological.back();
186 const auto span_n = static_cast<double>(chronological.size() - 1);
187
188 double residual_sq_sum = 0.0;
189 for (size_t i = 0; i < chronological.size(); ++i) {
190 const double t = static_cast<double>(i) / span_n;
191 const double trend_value = first + t * (last - first);
192 const double residual = chronological[i] - trend_value;
193 residual_sq_sum += residual * residual;
194 }
195
196 const double residual_var = (chronological.size() > 2)
197 ? residual_sq_sum / static_cast<double>(chronological.size() - 2)
198 : 0.0;
199
200 m_state.running_variance = residual_var;
201 m_state.floor = std::sqrt(residual_var);
202
203 return m_state.floor;
204}
static double trend_slope(std::span< const double > samples) noexcept
Linear trend slope across a span.
Definition Estimate.cpp:301
static double trend_explained_ratio(std::span< const double > samples) noexcept
Fraction of a span's variance attributable to its linear trend.
Definition Estimate.cpp:270
Memory::HistoryBuffer< double > m_history
Definition Estimate.hpp:400
std::span< T > linearized_view()
Get mutable linearized view of entire history.
void push(const T &value)
Push new value to front of history.
double mean(const std::vector< double > &data)
Calculate mean of single-channel data.
Definition Yantra.cpp:55

References MayaFlux::Kinesis::Stochastic::EstimateState::filtered_value, MayaFlux::Kinesis::Stochastic::EstimateState::floor, MayaFlux::Memory::HistoryBuffer< T >::linearized_view(), m_history, m_state, MayaFlux::mean(), MayaFlux::Memory::HistoryBuffer< T >::push(), MayaFlux::Kinesis::Stochastic::EstimateState::running_mean, MayaFlux::Kinesis::Stochastic::EstimateState::running_variance, MayaFlux::Kinesis::Stochastic::EstimateState::sample_count, MayaFlux::Kinesis::Stochastic::EstimateState::trend_explained_ratio, trend_explained_ratio(), MayaFlux::Kinesis::Stochastic::EstimateState::trend_slope, and trend_slope().

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: