LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fj_xmax_het.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_FJ_XMAX_HET_H
6#define LINE_API_FJ_XMAX_HET_H
7
8/**
9 * @file
10 * @ingroup api_fj
11 * Exact moments of the maximum of heterogeneous exponentials.
12 *
13 * Templated port of matlab/src/api/fj/fj_xmax_het.m.
14 *
15 * E[Y^n] = sum over the nonempty subsets S of {1..K} of
16 * (-1)^(|S|+1) n! / ( sum_{i in S} lambda_i )^n
17 *
18 * Exact, at a cost of 2^K - 1 terms. At n = 1 and K = 2 it collapses to
19 * 1/l1 + 1/l2 - 1/(l1+l2), and for equal rates to H_K/lambda.
20 */
21
22#include <cstddef>
23#include <vector>
24
26#include "line/num/number.h"
27#include "line/util/error.h"
28
29namespace line {
30namespace fj {
31
32/**
33 * @brief Exact moments of the maximum of heterogeneous exponentials.
34 *
35 * @param lambda the K positive exponential rates
36 * @param n moment order, n >= 1
37 * @return the n-th moment of the maximum
38 */
39template <class T>
40T fj_xmax_het(const std::vector<T>& lambda, unsigned n = 1) {
41 const std::size_t K = lambda.size();
42 if (K < 1) throw InputError("fj_xmax_het: at least one rate is required");
43 if (n < 1) throw InputError("fj_xmax_het: the moment order must be a positive integer");
44 if (K > 24)
45 throw InputError(
46 "fj_xmax_het: inclusion-exclusion needs 2^K terms; use fj_xmax_moments_het instead");
47 const T zero = num_traits<T>::from_int(0);
48 for (std::size_t i = 0; i < K; ++i)
49 if (!(lambda[i] > zero))
50 throw InputError("fj_xmax_het: all exponential rates must be positive");
51
52 T nfact = num_traits<T>::from_int(1);
53 for (unsigned i = 2; i <= n; ++i) nfact *= num_traits<T>::from_int(static_cast<long>(i));
54
55 T acc = zero;
56 const std::size_t nmask = static_cast<std::size_t>(1) << K;
57 for (std::size_t mask = 1; mask < nmask; ++mask) {
58 T rate = zero;
59 unsigned card = 0;
60 for (std::size_t i = 0; i < K; ++i)
61 if (mask & (static_cast<std::size_t>(1) << i)) {
62 rate += lambda[i];
63 ++card;
64 }
65 T den = num_traits<T>::from_int(1);
66 for (unsigned e = 0; e < n; ++e) den *= rate;
67 const T term = nfact / den;
68 if (card % 2 == 1) acc += term; else acc -= term;
69 }
70 return acc;
71}
72
73} // namespace fj
74} // namespace line
75
76#endif // LINE_API_FJ_XMAX_HET_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Shared return types and arithmetic helpers for the templated fork-join port.
T fj_xmax_het(const std::vector< T > &lambda, unsigned n=1)
Exact moments of the maximum of heterogeneous exponentials.
Definition fj_xmax_het.h:40
Number-type abstraction for the templated API port.