LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
CheckProbVector.m
1% r = CheckProbVector(pi, sub, prec)
2%
3% Checks if the vector is a valid probability vector: the
4% vector has only non-negative elements, the sum of the
5% vector elements is 1.
6%
7% If parameter "sub" is set to true, it checks if the
8% vector is a valid substochastic vector: the vector has
9% only non-negative elements, the sum of the elements are
10% less than 1.
11%
12% Parameters
13% ----------
14% pi : vector, shape (1, M) or (M, 1)
15% The matrix to check.
16% sub : bool, optional
17% If false, the procedure checks for stochastic, if
18% true, it checks for sub-stochastic property. The
19% default value is false.
20% prec : double, optional
21% Numerical precision. Entries with absolute value
22% less than prec are considered to be zeros. The
23% default value is 1e-14.
24%
25% Returns
26% -------
27% r : bool
28% The result of the check.
29
30function r = CheckProbVector (pi,sub,prec)
31
32 global BuToolsVerbose;
33 global BuToolsCheckInput;
34 if isempty(BuToolsCheckInput)
35 BuToolsCheckInput = true;
36 end
37 global BuToolsCheckPrecision;
38 if isempty(BuToolsCheckPrecision)
39 BuToolsCheckPrecision = 1e-14;
40 end
41
42 if ~exist('prec','var')
43 prec = BuToolsCheckPrecision;
44 end
45
46 if ~exist('sub','var')
47 sub = 0;
48 end
49
50 r = false;
51
52 if min(pi)<-prec
53 if BuToolsVerbose
54 fprintf ('CheckProbVector: The vector has negative element (precision: %g)!\n', prec);
55 end
56 return;
57 end
58
59 if sub
60 if sum(pi)>1+prec*numel(pi)
61 if BuToolsVerbose
62 fprintf ('CheckProbVector: The sum of the substochastic vector is not less than 1 (precision: %g)!\n', prec);
63 end
64 return;
65 end
66 else
67 if abs(sum(pi)-1)>prec*numel(pi)
68 if BuToolsVerbose
69 fprintf ('CheckProbVector: The sum of the vector is not 1 (precision: %g)!\n', prec);
70 end
71 return;
72 end
73 end
74
75 r = true;
76end