LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
m3a_fit_from.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_MAM_M3A_FIT_FROM_H
6#define LINE_API_MAM_M3A_FIT_FROM_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * The m3a fitters driven from a process or from a trace rather than from
12 * moments.
13 *
14 * Templated port of matlab/lib/m3a/m3a/aph2/{aph2_fit_map,aph2_fit_trace}.m and
15 * amap2/{amap2_fit_gamma_map,amap2_fit_gamma_trace}.m. Each is a four-line
16 * wrapper that measures the descriptors and hands them to the moment fitter, so
17 * what they really contribute is WHICH descriptors a given fitter consumes and
18 * how they are measured -- and that is worth having in one place, because the
19 * two families disagree:
20 *
21 * aph2_* (M1, M2, M3), no autocorrelation: an APH(2) is renewal.
22 * amap2_gamma_* (M1, M2, M3, gamma), where gamma is the autocorrelation decay
23 * rate, measured by `map_gamma` on a process and by
24 * `trace_gamma` on a trace.
25 *
26 * THE TWO GAMMAS ARE NOT THE SAME MEASUREMENT. `map_gamma` reads the decay rate
27 * off the process's own autocorrelation; `trace_gamma` FITS one on a grid and
28 * returns the residual of that fit alongside it. The trace entry point here
29 * discards the residual, exactly as the reference does, but a caller that wants
30 * to know how well the geometric decay described the trace should call
31 * `trace_gamma` directly rather than infer it from the fit.
32 *
33 * The trace moments are the RAW sample moments mean(T), mean(T^2), mean(T^3),
34 * not the unbiased estimators; that is the reference's choice and it matters at
35 * small sample sizes.
36 *
37 * ARITHMETIC: transcendental, inherited from the fitters.
38 */
39
40#include <cstddef>
41#include <vector>
42
48#include "line/num/number.h"
49#include "line/util/error.h"
50
51namespace line {
52namespace mam {
53
54namespace m3adetail {
55
56/** The raw sample moments 1..3 of a trace, as the reference measures them. */
57template <class T>
58void trace_moments3(const std::vector<T>& S, T* m1, T* m2, T* m3) {
59 if (S.empty()) throw InputError("m3a fit: the trace is empty");
60 const T zero = num_traits<T>::from_int(0);
61 T a = zero, b = zero, c = zero;
62 for (std::size_t i = 0; i < S.size(); ++i) {
63 const T x = S[i];
64 a += x;
65 b += x * x;
66 c += x * x * x;
67 }
68 const T n = num_traits<T>::from_int(static_cast<long>(S.size()));
69 *m1 = T(a / n);
70 *m2 = T(b / n);
71 *m3 = T(c / n);
72}
73
74} // namespace m3adetail
75
76/** Fit an APH(2) to the first three moments of a MAP. */
77template <class T>
79 return aph2_fit(map_mean(m), map_moment(m, 2), map_moment(m, 3));
80}
81
82/** Fit an APH(2) to the first three sample moments of a trace. */
83template <class T>
84Aph2FitResult<T> aph2_fit_trace(const std::vector<T>& S) {
85 T m1, m2, m3;
86 m3adetail::trace_moments3(S, &m1, &m2, &m3);
87 return aph2_fit(m1, m2, m3);
88}
89
90/** Fit an AMAP(2) to the three moments and the decay rate of a MAP. */
91template <class T>
95
96/**
97 * Fit an AMAP(2) to the three sample moments and the fitted decay rate of a
98 * trace. The residual of the gamma fit is discarded, as in the reference.
99 */
100template <class T>
102 T m1, m2, m3;
103 m3adetail::trace_moments3(S, &m1, &m2, &m3);
104 return amap2_fit_gamma(m1, m2, m3, line::trace::trace_gamma(S).gamma);
105}
106
107} // namespace mam
108} // namespace line
109
110#endif // LINE_API_MAM_M3A_FIT_FROM_H
AMAP(2) fit of three moments and the autocorrelation decay rate (matlab/lib/m3a/m3a/amap2/amap2_fit_g...
APH(2) fit of three moments, with a fallback to adjusted moments (matlab/lib/m3a/m3a/aph2/aph2_fit....
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Autocorrelation decay rate of a MAP: the gamma of the geometric model rho(k) = rho0 * gamma^k with rh...
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Amap2FitGammaResult< T > amap2_fit_gamma_map(const Map< T > &m)
Fit an AMAP(2) to the three moments and the decay rate of a MAP.
Aph2FitResult< T > aph2_fit(const T &M1, const T &M2, const T &M3)
Fit an APH(2) to (M1, M2, M3), relaxing the moments if necessary.
Definition aph2_fit.h:42
Amap2FitGammaResult< T > amap2_fit_gamma(const T &M1, const T &M2, const T &M3, const T &GAMMA, const T &cvtol)
Fit an AMAP(2) to (M1, M2, M3, GAMMA).
Aph2FitResult< T > aph2_fit_trace(const std::vector< T > &S)
Fit an APH(2) to the first three sample moments of a trace.
T map_mean(const Map< T > &m)
Mean inter-arrival time, 1/lambda.
Definition map_moment.h:101
Aph2FitResult< T > aph2_fit_map(const Map< T > &m)
Fit an APH(2) to the first three moments of a MAP.
T map_moment(const Map< T > &m, unsigned k)
Raw moment of order k of the inter-arrival time: k!
Definition map_moment.h:118
Amap2FitGammaResult< T > amap2_fit_gamma_trace(const std::vector< T > &S)
Fit an AMAP(2) to the three sample moments and the fitted decay rate of a trace.
T map_gamma(const Map< T > &m, long limit=1000)
Autocorrelation decay rate of a MAP (map_gamma.m).
Definition map_gamma.h:192
TraceGammaResult< T > trace_gamma(const std::vector< T > &S, long limit=1000, const std::vector< T > &grid=std::vector< T >())
Autocorrelation decay rate of a trace: the gamma of the geometric model rho(k) = rho0 * gamma^k,...
Definition trace_gamma.h:73
Number-type abstraction for the templated API port.
Result of amap2_fit_gamma.
Result of aph2_fit.
Definition aph2_fit.h:34
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Autocorrelation decay rate of a trace: the gamma of the geometric model rho(k) = rho0 * gamma^k,...