5#ifndef LINE_API_MAM_M3PP2M_FITC_TRACE_H
6#define LINE_API_MAM_M3PP2M_FITC_TRACE_H
75 const std::size_t n = C.
rows();
76 if (n < 2)
throw InputError(
"m3pp2m_fitc_trace: fewer than two counting windows");
78 for (std::size_t i = 0; i < n; ++i) {
80 for (std::size_t j = 0; j < C.
cols(); ++j) acc += C(i, j);
84 for (std::size_t i = 0; i < n; ++i) mu += s[i];
87 for (std::size_t i = 0; i < n; ++i) acc += (s[i] - mu) * (s[i] - mu);
94 const std::size_t n = C.
rows();
96 for (std::size_t i = 0; i < n; ++i) {
98 for (std::size_t j = 0; j < C.
cols(); ++j) s += C(i, j);
100 for (
unsigned k = 0; k < power; ++k) p *= num_traits<T>::from_int(s);
103 return acc / num_traits<T>::from_int(
static_cast<long>(n));
108void split_var(
const Matrix<long>& C, std::size_t c, T& vc, T& vrest) {
109 const std::size_t n = C.rows();
110 if (n < 2)
throw InputError(
"m3pp2m_fitc_trace: fewer than two counting windows");
111 std::vector<T> x(n), y(n);
112 for (std::size_t i = 0; i < n; ++i) {
114 for (std::size_t j = 0; j < C.cols(); ++j) tot += C(i, j);
115 x[i] = num_traits<T>::from_int(C(i, c));
116 y[i] = num_traits<T>::from_int(tot - C(i, c));
118 T mx = num_traits<T>::from_int(0), my = num_traits<T>::from_int(0);
119 for (std::size_t i = 0; i < n; ++i) {
123 const T nn = num_traits<T>::from_int(
static_cast<long>(n));
126 T ax = num_traits<T>::from_int(0), ay = num_traits<T>::from_int(0);
127 for (std::size_t i = 0; i < n; ++i) {
128 ax += (x[i] - mx) * (x[i] - mx);
129 ay += (y[i] - my) * (y[i] - my);
131 const T nm1 = num_traits<T>::from_int(
static_cast<long>(n - 1));
138T split_cov(
const Matrix<long>& C, std::size_t c) {
139 const std::size_t n = C.rows();
140 if (n < 2)
throw InputError(
"m3pp2m_fitc_trace: fewer than two counting windows");
141 std::vector<T> x(n), y(n);
142 for (std::size_t i = 0; i < n; ++i) {
144 for (std::size_t j = 0; j < C.cols(); ++j) tot += C(i, j);
145 x[i] = num_traits<T>::from_int(C(i, c));
146 y[i] = num_traits<T>::from_int(tot - C(i, c));
148 T mx = num_traits<T>::from_int(0), my = num_traits<T>::from_int(0);
149 for (std::size_t i = 0; i < n; ++i) {
153 const T nn = num_traits<T>::from_int(
static_cast<long>(n));
156 T acc = num_traits<T>::from_int(0);
157 for (std::size_t i = 0; i < n; ++i) acc += (x[i] - mx) * (y[i] - my);
158 return acc / num_traits<T>::from_int(
static_cast<long>(n - 1));
174 const std::string& method,
const T& t1,
const T& tinf) {
176 "m3pp2m_fitc_trace requires transcendental arithmetic");
177 if (Tv.empty())
throw InputError(
"m3pp2m_fitc_trace: empty trace");
178 if (A.size() != Tv.size())
179 throw InputError(
"m3pp2m_fitc_trace: labels and inter-arrival times disagree");
183 const std::size_t m = N1.
labels.size();
184 if (method ==
"approx_cov" && m > 2)
185 throw InputError(
"m3pp2m_fitc_trace: approximate covariance fitting only supports two "
189 for (std::size_t i = 0; i < Tv.size(); ++i) sumT += Tv[i];
192 const T t2 = t1 + mean;
195 std::vector<T> ai(m);
196 for (std::size_t i = 0; i < m; ++i) {
198 for (std::size_t k = 0; k < A.size(); ++k)
199 if (A[k] == N1.
labels[i]) ++cnt;
204 const T bt1 = fitdetail::rowsum_var<T>(N1.
counts) / (a * t1);
206 const T binf = fitdetail::rowsum_var<T>(Ninf.
counts) / (a * tinf);
207 const T m3t2 = fitdetail::m3_from_raw(fitdetail::rowsum_moment<T>(N1.
counts, 1),
208 fitdetail::rowsum_moment<T>(N1.
counts, 2),
209 fitdetail::rowsum_moment<T>(N1.
counts, 3));
218 if (method ==
"exact_delta" || method ==
"approx_delta") {
219 std::vector<T> dvt3(m);
220 for (std::size_t i = 0; i < m; ++i) {
222 fitdetail::split_var<T>(N1.
counts, i, vc, vr);
225 if (method ==
"exact_delta")
226 out.
mmap =
m3pp2m_fitc(a, bt1, bt2, binf, m3t2, t1, t2, ai, dvt3, t3).mmap;
231 if (method ==
"approx_cov") {
232 const T V = fitdetail::rowsum_var<T>(N1.
counts);
235 fitdetail::split_var<T>(N1.
counts, 0, v0, dummy);
236 fitdetail::split_var<T>(N1.
counts, 1, v1, dummy);
241 if (method ==
"approx_ag") {
242 std::vector<T> gt3(m);
243 for (std::size_t i = 0; i < m; ++i) {
245 fitdetail::split_var<T>(N1.
counts, i, vc, vr);
246 gt3[i] = vc + fitdetail::split_cov<T>(N1.
counts, i);
251 throw InputError(
"m3pp2m_fitc_trace: unknown method '" + method +
"'");
257 const std::string& method) {
258 if (Tv.empty())
throw InputError(
"m3pp2m_fitc_trace: empty trace");
260 for (std::size_t i = 0; i < Tv.size(); ++i) sumT += Tv[i];
278 "mmpp2_fitc_theoretical requires transcendental arithmetic");
286 const T a = mu[0] / t1;
287 const T bt1 = vr[0] / (a * t1);
288 const T bt2 = vr[1] / (a * t2);
289 const T binf = vr[2] / (a * tinf);
291 std::vector<unsigned> orders;
296 const T m3t2 = fitdetail::m3_from_raw(mt2[0], mt2[1], mt2[2]);
298 return mmpp2_fitc(a, bt1, bt2, binf, m3t2, t1, t2);
The exception types the port throws.
M3PP(2, 2) fitted to the count COVARIANCE between its two classes (matlab/lib/m3a/m3a/m3pp/m3pp22_fit...
Marked MMPP(2) with m classes, fitted to counting-process characteristics (matlab/lib/m3a/m3a/m3pp/m3...
M3PP(2, m) fitted to counting-process characteristics with an optimized per-class split (matlab/lib/m...
M3PP obtained by SUPERPOSING one second-order process per class (matlab/lib/m3a/m3a/m3pp/m3pp_superpo...
Mean of the counting process of a MAP at resolution t.
Power moments of the counts of a MAP in a window of length t.
Variance of the counting process of a MAP at resolution t.
Dense matrix and non-owning view.
Per-class counting processes of a marked trace: for each arrival, how many events of each class fall ...
M3pp2mFitcApproxResult< T > m3pp2m_fitc_approx_ag(const T &a, const T &bt1, const T &bt2, const T &binf, const T &m3t2, const T &t1, const T &t2, const std::vector< T > &ai, const std::vector< T > >3, const T &t3, const std::vector< Bound< T > > &bounds, const AugLagOptions< T > &opt)
m3pp2m_fitc_approx_ag: fit the underlying MMPP(2) by optimization, then apply the 'ag' per-class spli...
Mmpp2FitcResult< T > mmpp2_fitc(const T &mu, const T &bt1, const T &bt2, const T &binf, const T &m3t2, const T &t1, const T &t2)
MMPP(2) from the arrival rate, the IDC at t1, t2 and infinity, and the third central moment of the co...
M3pp2mFitcResult< T > m3pp2m_fitc(const T &a, const T &bt1, const T &bt2, const T &binf, const T &m3t2, const T &t1, const T &t2, const std::vector< T > &ai, const std::vector< T > &dvt3, const T &t3)
Fit an M3PP(2, m).
std::vector< T > map_count_var(const Map< T > &m, const std::vector< T > &t)
Variance of the counting process of a MAP at resolution t.
Mmpp2FitcResult< T > mmpp2_fitc_theoretical(const Map< T > &mp, const T &t1, const T &t2, const T &tinf)
MMPP(2) fitted to the counting characteristics of a GIVEN MAP (matlab/lib/kpctoolbox/mmpp/mmpp2_fitc_...
std::vector< T > map_count_mean(const Map< T > &m, const std::vector< T > &t)
Mean of the counting process of a MAP at resolution t.
M3pp22FitcCovResult< T > m3pp22_fitc_approx_cov(const T &a, const T &bt1, const T &bt2, const T &binf, const T &m3t2, const T &t1, const T &t2, const std::vector< T > &ai, const T &st3, const T &t3, const AugLagOptions< T > &opt)
Fit the underlying MMPP(2) by optimization, then apply the covariance split.
M3pp2mFitcApproxResult< T > m3pp2m_fitc_approx(const T &a, const T &bt1, const T &bt2, const T &binf, const T &m3t2, const T &t1, const T &t2, const std::vector< T > &ai, const std::vector< T > &dvt3, const T &t3, const std::vector< Bound< T > > &bounds, const AugLagOptions< T > &opt)
m3pp2m_fitc_approx: fit the underlying MMPP(2) by optimization, then split the classes on their varia...
M3pp2mFitcTraceResult< T > m3pp2m_fitc_trace(const std::vector< T > &Tv, const std::vector< int > &A, const std::string &method, const T &t1, const T &tinf)
Fit a multi-class trace with an M3PP(2, m) on its counting process.
std::vector< T > map_count_moment(const Map< T > &m, const T &t, const std::vector< unsigned > &orders)
Power moments of the counts of a MAP in a window of length t.
MtraceCountsResult< T > mtrace_iat2counts(const std::vector< T > &Tv, const std::vector< int > &A, const T &scale)
Per-class counting processes of a marked trace: for each arrival, how many events of each class fall ...
Number-type abstraction for the templated API port.
Result of m3pp2m_fitc_trace.
std::string method
the method actually run
Mmap< T > mmap
the fitted M3PP(2, m)
T m3t2
third central moment of counts at t2
T a
the aggregate rate read off the trace
A MAP as the pair of matrices (D0, D1).
An MMAP: the underlying MAP plus the per-class arrival matrices.
Return value of mtrace_iat2counts.
Matrix< long > counts
(rows x C), column c for labels[c]
std::vector< int > labels
the distinct labels, increasing