-
Notifications
You must be signed in to change notification settings - Fork 2
/
SatisfiesVODCondition.m
90 lines (75 loc) · 2.48 KB
/
SatisfiesVODCondition.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function satisfiesVOD = SatisfiesVODCondition( varargin )
% SatisfiesVODCondition returns a vector indicating which samples satisfy the
% value of data condition
%
% Keyword arguments (*required arguments):
% obs*: vector of observations numbers of the scenarios
% p*: vector of worst-case probability distribution
% phi*: PhiDivergence object or string defining the phi-divergence
% -------------------------------------------------------------------
% Input Parsing
% -------------------------------------------------------------------
requiredArgs = {'obs', 'p', 'phi'};
if mod(length(varargin),2) == 1
error('Arguments must be key, value pairs')
end
for vv = 1:2:length(varargin)
key = varargin{vv};
value = varargin{vv+1};
switch key
case 'obs'
obs = value;
case 'p'
p = value;
case 'phi'
if isa(value, 'PhiDivergence')
phi = value;
elseif ischar(value)
phi = PhiDivergence( value );
else
error('Phi must be PhiDivergence object or string')
end
otherwise
error(['Unknown variable ', key])
end
end
% -------------------------------------------------------------------
% Default variable assignments
% -------------------------------------------------------------------
% -------------------------------------------------------------------
% Required Variables
% -------------------------------------------------------------------
for aa = requiredArgs
if ~exist(aa{1}, 'var')
error([aa{1}, ' is required but not defined'])
end
end
% -------------------------------------------------------------------
% Value Checking
% -------------------------------------------------------------------
p = p(:);
obs = obs(:);
if size(p) ~= size(obs)
error('Size of p and obs must match')
end
% -------------------------------------------------------------------
% -------------------------------------------------------------------
N = sum(obs);
q = obs/N;
switch phi.divergence
case 'burg'
cost_dec = p./q < N/(N+1);
case 'chi2'
const = sum(q.*q./p) + sqrt((N+1)/N);
cost_dec = const < 2*p./q;
case 'hellinger'
const = sum(q.*sqrt(p./q));
cost_dec = const + sqrt(p./q) < 2*N/(N+1);
case 'mchi2'
const = 2 * sum(p.*p./q) - (N+1)^2/N^2;
cost_dec = (p./q).^2 < const;
otherwise
error('No rule for divergence')
end
satisfiesVOD = cost_dec;
end