LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
getTranProbAggr.m
1function [Pi_t, SSnode_a] = getTranProbAggr(self, node)
2% [PI_T, SSNODE_A] = GETTRANPROBAGGR(NODE)
3%
4% Empirical transient probability of per-class aggregate states at a station,
5% estimated from iter_max independent JMT replications.
6%
7% Output:
8% Pi_t - [t, pi_t] where pi_t(k,j) = empirical fraction of replications
9% whose aggregate state at the requested node at time t(k) equals
10% SSnode_a(j,:).
11% SSnode_a - Unique aggregate state vectors observed at NODE across all
12% replications and time points (sorted, one row per state, one
13% column per class).
14if GlobalConstants.DummyMode
15 Pi_t = NaN;
16 SSnode_a = NaN;
17 return
18end
19options = self.getOptions;
20initSeed = self.options.seed;
21if isfield(options,'timespan') && isfinite(options.timespan(2))
22 sn = self.getStruct;
23 ist = sn.nodeToStation(node.index);
24 if ist < 1
25 line_error(mfilename,'getTranProbAggr in SolverJMT requires the input node to be a station.');
26 end
27 if sn.nodetype(node.index) == NodeType.Source
28 line_error(mfilename,'getTranProbAggr in SolverJMT does not apply to Source nodes.');
29 end
30
31 tu = [];
32 TranSysStateAggr = cell(options.iter_max,1);
33 for it=1:options.iter_max
34 self.options.seed = initSeed + it - 1;
35 TranSysStateAggr{it} = self.sampleSysAggr;
36 tu = union(tu, TranSysStateAggr{it}.t);
37 end
38 self.options.seed = initSeed; % restore in case of interruption
39 tu = tu(:);
40 nT = length(tu);
41 iterMax = options.iter_max;
42 nclasses = sn.nclasses;
43
44 % interpolate per-station state onto union grid for the requested station
45 nodeStates = NaN(nT, nclasses, iterMax);
46 for it=1:iterMax
47 tit = TranSysStateAggr{it}.t;
48 block = TranSysStateAggr{it}.state{ist};
49 if isempty(block) || isempty(tit) || any(isinf(block(:)))
50 continue
51 end
52 nodeStates(:,:,it) = interp1(tit, block, tu, 'previous');
53 end
54
55 % flatten to (nT*iterMax) x nclasses, drop rows with any NaN, take unique
56 flat = reshape(permute(nodeStates,[1,3,2]), nT*iterMax, nclasses);
57 validMask = ~any(isnan(flat),2);
58 SSnode_a = unique(flat(validMask,:), 'rows');
59 nStates = size(SSnode_a,1);
60
61 % empirical fraction at each time point
62 pi_t = zeros(nT, nStates);
63 if nStates > 0
64 for it=1:iterMax
65 block = nodeStates(:,:,it);
66 [~, idx] = ismember(block, SSnode_a, 'rows');
67 for t_idx = 1:nT
68 if idx(t_idx) > 0
69 pi_t(t_idx, idx(t_idx)) = pi_t(t_idx, idx(t_idx)) + 1;
70 end
71 end
72 end
73 pi_t = pi_t / iterMax;
74 end
75
76 Pi_t = [tu, pi_t];
77else
78 line_error(mfilename,'getTranProbAggr in SolverJMT requires to specify a finite timespan T, e.g., SolverJMT(model,''timespan'',[0,T]).');
79end
80end
Definition mmt.m:124