LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
fj_xmax_erlang.m
1%{ @file fj_xmax_erlang.m
2 % @brief Expected maximum of K i.i.d. Erlang-k random variables
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Expected maximum of K i.i.d. Erlang-k random variables
9 %
10 % @details
11 % Computes the expected value of the maximum of K independent and
12 % identically distributed Erlang random variables with k stages
13 % and rate mu per stage.
14 %
15 % For k=2 (Erlang-2):
16 % X_K^max = (1/mu) * sum_{n=1}^{K} C(K,n) * (-1)^{n-1} * sum_{m=1}^{n} C(n,m) * m! / (2*n^{m+1})
17 %
18 % For general k-stage Erlang, the formula involves k-fold convolution
19 % of exponential distributions.
20 %
21 % The Erlang distribution has mean k/mu and variance k/mu^2, giving
22 % CV = 1/sqrt(k) < 1.
23 %
24 % @par Syntax:
25 % @code
26 % Xmax = fj_xmax_erlang(K, k, mu)
27 % @endcode
28 %
29 % @par Parameters:
30 % <table>
31 % <tr><th>Name<th>Description
32 % <tr><td>K<td>Number of parallel servers (positive integer)
33 % <tr><td>k<td>Number of Erlang stages (positive integer, default=2)
34 % <tr><td>mu<td>Rate parameter per stage (mean service time = k/mu)
35 % </table>
36 %
37 % @par Returns:
38 % <table>
39 % <tr><th>Name<th>Description
40 % <tr><td>Xmax<td>Expected maximum service time X_K^max
41 % </table>
42 %
43 % @par Reference:
44 % A. Thomasian, "Analysis of Fork/Join and Related Queueing Systems",
45 % ACM Computing Surveys, Vol. 47, No. 2, Article 17, July 2014.
46 % Page 17:13.
47%}
48function Xmax = fj_xmax_erlang(K, k, mu)
49
50if nargin < 2
51 k = 2; % Default to Erlang-2
52end
53
54if K < 1
55 line_error(mfilename, 'K must be a positive integer. Got K=%d.', K);
56end
57
58if k < 1
59 line_error(mfilename, 'k (number of stages) must be a positive integer. Got k=%d.', k);
60end
61
62if mu <= 0
63 line_error(mfilename, 'Rate mu must be positive. Got mu=%.4f.', mu);
64end
65
66% For Erlang-2 (k=2), use the closed-form formula from the paper
67if k == 2
68 % X_K^max = (1/mu) * sum_{n=1}^{K} C(K,n) * (-1)^{n-1} * sum_{m=1}^{n} C(n,m) * m! / (2*n^{m+1})
69 outer_sum = 0;
70 for n = 1:K
71 inner_sum = 0;
72 for m = 1:n
73 inner_sum = inner_sum + nchoosek(n, m) * factorial(m) / (2 * n^(m + 1));
74 end
75 outer_sum = outer_sum + nchoosek(K, n) * ((-1)^(n - 1)) * inner_sum;
76 end
77 Xmax = outer_sum / mu;
78else
79 % For general k-stage Erlang, use numerical integration
80 % The CDF of Erlang-k is: F(t) = 1 - exp(-mu*t) * sum_{j=0}^{k-1} (mu*t)^j / j!
81 % The expected max is: X_K^max = integral_0^inf [1 - F(t)^K] dt
82
83 % Erlang CDF function
84 erlang_cdf = @(t) 1 - exp(-mu * t) .* sum_erlang_terms(mu * t, k);
85
86 % Integrand: 1 - F(t)^K
87 integrand = @(t) 1 - erlang_cdf(t).^K;
88
89 % Numerical integration (upper limit chosen based on Erlang mean and variance)
90 mean_erlang = k / mu;
91 upper_limit = mean_erlang * 10 + 10 * sqrt(k) / mu; % Well beyond the tail
92 Xmax = integral(integrand, 0, upper_limit, 'RelTol', 1e-10, 'AbsTol', 1e-12);
93end
94
95end
96
97function S = sum_erlang_terms(x, k)
98% Helper function to compute sum_{j=0}^{k-1} x^j / j!
99% Handles both scalar and vector inputs
100S = zeros(size(x));
101for j = 0:(k-1)
102 S = S + (x.^j) / factorial(j);
103end
104end