LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
CheckProbMatrix.m
1% r = CheckProbMatrix(P, transient, prec)
2%
3% Checks if the matrix is a valid probability matrix: the
4% matrix is a square matrix, the matrix has positive or
5% zero off-diagonal elements, the rowsum of the matrix is 1.
6%
7% If "transient" is true, it checks if the matrix is a
8% valid transient probability matrix: the matrix is a square
9% matrix, the matrix has positive or zero off-diagonal
10% elements, the rowsum of the matrix is less than or equal
11% to 1, the maximum absolute eigenvalue is less than 1.
12%
13% Parameters
14% ----------
15% P : matrix, shape (M,M)
16% The matrix to check.
17% transient : bool, optional
18% If true, the procedure checks if P is a transient
19% probability matrix, otherwise it checks if it is
20% a valid probability matrix. The default value is
21% false.
22% prec : double, optional
23% Entries with absolute value less than prec are
24% considered to be zeros. The default value is 1e-14.
25%
26% Returns
27% -------
28% r : bool
29% The result of the check.
30
31function r = CheckProbMatrix (P,transient,prec)
32
33 global BuToolsVerbose;
34 global BuToolsCheckInput;
35 if isempty(BuToolsCheckInput)
36 BuToolsCheckInput = true;
37 end
38 global BuToolsCheckPrecision;
39 if isempty(BuToolsCheckPrecision)
40 BuToolsCheckPrecision = 1e-14;
41 end
42
43 if ~exist('prec','var')
44 prec = BuToolsCheckPrecision;
45 end
46
47 if ~exist('transient','var')
48 transient = 0;
49 end
50
51 r = false;
52
53 if size(P,1)~=size(P,2)
54 if BuToolsVerbose
55 fprintf ('CheckProbMatrix: the matrix is not a square matrix!\n');
56 end
57 return;
58 end
59
60 if min(min(P))<-prec
61 if BuToolsVerbose
62 fprintf ('CheckProbMatrix: the matrix has negative element (precision: %g)!\n', prec);
63 end
64 return;
65 end
66
67 if transient
68 if any(sum(P,2)-1>size(P,2)*prec)
69 if BuToolsVerbose
70 fprintf ('CheckProbMatrix: The rowsum of the matrix (transient) is not less or equal than 1 (precision: %g)!\n', prec);
71 end
72 return;
73 end
74
75 if max(real(eig(P)))>=1-prec
76 if BuToolsVerbose
77 fprintf ('CheckProbMatrix: The real part of the largest eigenvalue of the transient matrix is not less than 1 (precision: %g)!\n', prec);
78 end
79 return;
80 end
81 else
82 if any(abs(sum(P,2)-1)>size(P,2)*prec)
83 if BuToolsVerbose
84 fprintf ('CheckProbMatrix: The rowsum of the matrix is not 1 (precision: %g)!\n', prec);
85 end
86 return;
87 end
88 end
89 r = true;
90end