LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
me_sample.m
1function sample = me_sample(ME, n, xs)
2% SAMPLE = ME_SAMPLE(ME, N, XS) - Generate random samples from ME distribution
3%
4% Generate samples from a Matrix Exponential (ME) distribution using
5% inverse CDF interpolation method.
6%
7% Input:
8% ME: ME distribution as either:
9% - ME object
10% - Process cell array {D0, D1}
11% N: Number of samples to generate (default: 1)
12% XS: Optional pre-computed grid for CDF evaluation
13% If not provided, auto-generates grid based on mean and variance
14%
15% Output:
16% SAMPLE: Column vector of N samples from the ME distribution
17%
18% Algorithm:
19% Uses inverse CDF interpolation with binary search:
20% 1. Compute CDF on a fine grid (default: 1000 points)
21% 2. For each sample, generate u ~ Uniform(0,1)
22% 3. Use interpolation to find x such that CDF(x) = u
23%
24% Examples:
25% me = ME([0.3, 0.7], [-2, 1; 0.5, -1.5]);
26% samples = me_sample(me, 10000);
27%
28% % Or use process representation directly
29% ME_proc = {[-2, 1; 0.5, -1.5], [0.4, 0.6; 0.3, 0.7]};
30% samples = me_sample(ME_proc, 10000);
31%
32% Copyright (c) 2012-2026, Imperial College London
33% All rights reserved.
34
35% Handle input arguments
36if nargin < 2
37 n = 1;
38end
39
40% Extract process representation
41if isa(ME, 'ME')
42 ME_proc = ME.getProcess();
43elseif iscell(ME)
44 ME_proc = ME;
45else
46 error('ME must be either an ME object or a cell array {D0, D1}');
47end
48
49% Auto-generate grid if not provided
50if nargin < 3 || isempty(xs)
51 mean_val = map_mean(ME_proc);
52 var_val = map_var(ME_proc);
53 std_val = sqrt(var_val);
54
55 % Create grid from 0 to mean + 10*sigma with 1000 points
56 xs = linspace(0, mean_val + 10*std_val, 1000);
57end
58
59% Compute CDF at grid points
60Fxs = map_cdf(ME_proc, xs);
61
62% Ensure CDF is strictly increasing for interpolation
63% (handle numerical precision issues)
64for i = 2:length(Fxs)
65 if Fxs(i) <= Fxs(i-1)
66 Fxs(i) = Fxs(i-1) + eps;
67 end
68end
69
70% Generate samples via inverse CDF interpolation
71sample = zeros(n, 1);
72for i = 1:n
73 u = rand();
74
75 % Handle edge cases
76 if u <= Fxs(1)
77 sample(i) = xs(1);
78 elseif u >= Fxs(end)
79 % Extrapolate beyond grid if needed
80 % Use exponential tail approximation
81 sample(i) = xs(end) + log(1 / (1 - u + eps));
82 else
83 % Linear interpolation between grid points
84 sample(i) = interp1(Fxs, xs, u, 'linear');
85 end
86end
87
88end