-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.jl
257 lines (211 loc) · 6.33 KB
/
update.jl
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module updateModule
include("ellipsoidal.jl")
export updateTraditionalEllipsoid, updateCorrectedEllipsoid, initializeEllipsoid, initializeExtendedCrossBox, updatePolyhedral, updateBayesApproximation
using Distributions
using JuMP
import .ellipsoidal
# Does not update anything. Used by probabilistic polyhedral and robust
#
# Parameters:
#
# - Q, c, A, b = description of uncertainty set (see runsimulation in geometric.jl)
# - X = array of all questions asked
# - answers = array of all answers
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - precomp = precompuded data
# - parameters = list of parameters
#
# Returns
#
# - status of update always equal to "Normal"
function updateNull(Q,c,A,b,X,answers,mu,sigma,precomp,parameters)
return "Normal"
end
## Polyhedral
# Updates the uncertainty set by adding the inequality
#
# x[i] <= x[answer] for all profiles i
#
# Parameters:
#
# - Q, c, A, b = description of uncertainty set (see runsimulation in geometric.jl)
# - X = array of all questions asked
# - answers = array of all answers
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - precomp = precompuded data
# - parameters = list of parameters
#
# Returns
#
# - status of update always equal to "Normal"
function updatePolyhedral(Q,c,A,b,X,answers,mu,sigma,precomp,parameters)
x = X[end]
answer = answers[end]
n = 0
if length(A) > 0
n = length(A[1])
else
n = length(c[1])
end
dimension = length(x[1])
for i in setdiff(1:length(x),answer)
push!(A,[ x[i] - x[answer]; zeros(n-dimension)])
push!(b,0)
end
return "Normal"
end
## Ellispoidal
# Returns the "confidece" confidence level credibility ellipsoid
# for a gaussian with mean mu and covariate matrix sigma
#
# Parameters:
#
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - confidence = confidence level for the ellipsoid
#
# Returns
#
# - ellipsoid (beta - c)'*Q^(-1)*(beta - c)
function initializeEllipsoid(mu,sigma,precomp)
Q = Array{Float64,2}[]
push!(Q,sigma*precomp["rhs"])
c = Array{Float64,1}[]
push!(c,mu)
return Q,c,Array{Float64,1}[],Float64[]
end
# Assumes the uncertainty set is just one ellipsoid. If there are only two profiles it updates
# with the minimum volume ellipsoid that contains the original ellipsoid intersected with
#
# x["notanswer"] <= x[answer]
#
# If there are more than two profiles it iteratively repeats the update for each constraint
#
# x[i] <= x[answer]
#
# Parameters:
#
# - Q, c, A, b = description of uncertainty set (see runsimulation in geometric.jl)
# - X = array of all questions asked
# - answers = array of all answers
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - precomp = precompuded data
# - parameters = list of parameters
#
# Returns
#
# - status of update. Is "Normal" for normal termination and "NoUpdate" if the minimum
# volume ellipsoid is the original ellipsoid.
function updateTraditionalEllipsoid(Q,c,A,b,X,answers,mu,sigma,precomp,parameters)
x = X[end]
answer = answers[end]
n = length(c[1])
update = false
for i in setdiff(1:length(x),answer)
question = x[i] - x[answer]
scale = sqrt(question'*Q[1]*question)[1]
alpha = dot(question,c[1])/scale
b = Q[1]*question/scale
if alpha > -1/n
update = true
s1 = ((n^2)/(n^2-1))*(1-alpha^2)
s2 = 2*(1+n*alpha)/((n+1)*(1+alpha))
c[1] = c[1] - ((1+n*alpha)/(n+1))*b
Q[1] = s1*( Q[1] - s2*b*b' )
end
end
if update
return "Normal"
else
return "NoUpdate"
end
end
# Assumes the uncertainty set is just one ellipsoid. If there are only two profiles it updates
# with a correction of minimum volume ellipsoid that contains the original ellipsoid intersected with
#
# x["notanswer"] <= x[answer]
#
# If there are more than two profiles it iteratively repeats the update for each constraint
#
# x[i] <= x[answer]
#
# Parameters:
#
# - Q, c, A, b = description of uncertainty set (see runsimulation in geometric.jl)
# - X = array of all questions asked
# - answers = array of all answers
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - precomp = precompuded data
# - parameters = list of parameters
#
# Returns
#
# - status of update. Is "Normal" for normal termination and "NoUpdate" if the minimum
# volume ellipsoid is the original ellipsoid.
function updateCorrectedEllipsoid(Q,c,A,b,X,answers,mu,sigma,precomp,parameters)
x = X[end]
answer = answers[end]
n = length(c[1])
update = false
for i in setdiff(1:length(x),answer)
question = x[i] - x[answer]
scale = sqrt(question'*Q[1]*question)[1]
alpha = dot(question,c[1])/scale
b = Q[1]*question/scale
if alpha > -1/n
update = true
s1 = ((n^2)/(n^2-1))*(1-alpha^2)
s2 = 2*(1+n*alpha)/((n+1)*(1+alpha))
c[1] = c[1] - ((1+n*alpha)/(n+1))*b
Q[1] = Q[1] - (1+s1*s2-s1)*b*b'
end
end
if update
return "Normal"
else
return "NoUpdate"
end
end
# Assumes the uncertainty set is just one ellipsoid, which is the credibility ellipsoid of a gaussian
# prior distribution. It updates the ellipsoid with the credibility ellipsoid of the posterior distribution
# obtained through an MCMC procedure that exploits the geometry of the update so that the dimension of the
# sample is only one less than the number of profiles
#
# Parameters:
#
# - Q, c, A, b = description of uncertainty set (see runsimulation in geometric.jl)
# - X = array of all questions asked
# - answers = array of all answers
# - mu = mean of original prior distribution of beta
# - sigma = covariance matrix of original prior distribution of beta
# - precomp = precompuded data
# - parameters = list of parameters
#
# Returns
#
# - status of update always equal to "Normal"
function updateBayesApproximation(Q,c,A,b,X,answers,mu,sigma,precomp,parameters)
if answers[end] == 1
x = vec(X[end][1])
y = vec(X[end][2])
else
x = vec(X[end][2])
y = vec(X[end][1])
end
cov = Q[1]/precomp["rhs"]
center = c[1]
newcenter, newcov = ellipsoidal.momentmatchingupdate(x,y,center,cov)
c[1] = vec(newcenter)
Q[1] = newcov*precomp["rhs"]
return "Normal"
end
# function fisherupdate(x,y,μ,Σ)
#
# μ, inv(inv(Σ)+(x-y)*(x-y)'*(2+2*cosh(dot(x-y,μ)))^(-1))
#
# end
end