-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.jl
280 lines (210 loc) · 6.46 KB
/
util.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using LinearAlgebra
using ForwardDiff
function sqrtm(M::AbstractMatrix)
"Square root of matrix"
if size(M) == (2,2)
"https://en.wikipedia.org/wiki/Square_root_of_a_2_by_2_matrix"
A,C,B,D = M
# Determinant
δ = A*D - B*C
s = sqrt(δ)
# Trace
τ = A+D
t = sqrt(τ + 2s)
return 1/t*(M+s*Matrix{eltype(M)}(I,2,2))
else
"Babylonian method"
Xk = Matrix{eltype(M)}(I,size(M))
Xm = zeros(eltype(M), size(M))
while sum(abs.(Xk[:] .- Xm[:])) > 1e-3
Xm = Xk
Xk = (Xm + M/Xm)/2.0
end
return Xk
end
end
function proj2psd!(S::AbstractMatrix)
L,V = eigen(S)
S = V*diagm(max.(1e-8,L))*V'
return (S+S')/2
end
function sigma_points(m::AbstractFloat, v::AbstractFloat; α=1e-3, κ=0.0)
# Compute scaling parameter
λ = α^2*(1+κ)-1
# Preallocate
sigma = zeros(eltype(m),3)
# Sigma points
sigma[1] = m
sigma[2] = m + sqrt(1+λ)*sqrt(v)
sigma[3] = m - sqrt(1+λ)*sqrt(v)
return sigma
end
function sigma_points(m::AbstractVector, P::AbstractMatrix; α=1e-3, κ=0.0)
# Number of sigma points depends on dimensionality
N = size(P,2)
# Compute scaling parameter
λ = α^2*(N+κ)-N
# Square root of covariance matrix through Babylonian method
sP = sqrtm(P)
# Preallocate
sigma = Matrix(undef,N,2N+1)
# First point is mean
sigma[:,1] = m
# Positive
for n = 1:N
sigma[:,1+n] = m + sqrt(N+λ)*sP[:,n]
end
# Negative
for n = 1:N
sigma[:,1+N+n] = m - sqrt(N+λ)*sP[:,n]
end
return sigma
end
function ut_weights(; α=1e-3, β=2.0, κ=0.0, N=1)
# Compute scaling parameter
λ = α^2*(N+κ)-N
# Preallocate
Wm = Vector(undef, 2N+1)
Wc = Vector(undef, 2N+1)
# Zero-order weights
Wm[1] = λ/(N+λ)
Wc[1] = λ/(N+λ) + (1-α^2+β)
for n = 2:(2N+1)
Wm[n] = 1/(2(N+λ))
Wc[n] = 1/(2(N+λ))
end
return Wm,Wc
end
function UT(m::AbstractFloat, v::AbstractFloat, g; addmatrix=nothing, forceHermitian=false, α=1e-3, β=2.0, κ=0.0)
"Algorithm 5.12 in 'Bayesian filtering & smoothing'"
# Compute constant weigths
Wm, Wc = ut_weights(α=α, β=β, κ=κ, N=1)
# Form sigma points
σ = sigma_points(m,v, α=α, κ=κ)
y = g.(σ)
# Compute moments of approximated distribution
μ = y'*Wm
Σ = Wc[1]*(y[1] - μ)*(y[1] - μ)'
Γ = Wc[1]*(σ[1] - m)*(y[1] - μ)'
for i = 2:3
Σ += Wc[i]*(y[i] - μ)*(y[i] - μ)'
Γ += Wc[i]*(σ[i] - m)*(y[i] - μ)'
end
if addmatrix !== nothing; Σ += addmatrix; end
if forceHermitian; Σ = Hermitian(Σ); end
return μ,Σ,Γ
end
function UT(m::AbstractVector, P::AbstractMatrix, g; addmatrix=nothing, forceHermitian=false, α=1e-3, β=2.0, κ=0.0)
"Algorithm 5.12 in 'Bayesian filtering & smoothing'"
# Dimensionalities
D = length(g(m))
N = length(m)
# Compute constant weigths
Wm, Wc = ut_weights(α=α, β=β, κ=κ, N=N)
# Form sigma points
σ = sigma_points(m,P, α=α, κ=κ)
# Propagate sigma points through non-linearity
if D == 1
# y = Vector{Real}(undef, 2N+1)
y = zeros(eltype(m), 2N+1)
for i in 1:(2N+1)
y[i] = g(σ[:,i])
end
# Compute moments of approximated distribution
μ = y'*Wm
Σ = Wc[1]*(y[1] - μ)*(y[1] - μ)'
Γ = Wc[1]*(σ[:,1] - m)*(y[1] - μ)'
for i = 2:2N+1
Σ += Wc[i]*(y[i] - μ)*(y[i] - μ)'
Γ += Wc[i]*(σ[:,i] - m)*(y[i] - μ)'
end
else
y = Matrix(undef, D,2N+1)
for i in 1:(2N+1)
y[:,i] = g(σ[:,i])
end
# Compute moments of approximated distribution
μ = y*Wm
Σ = zeros(eltype(m), D,D)
Γ = zeros(eltype(m), N,D)
for i = 1:2N+1
Σ += Wc[i]*(y[:,i] - μ)*(y[:,i] - μ)'
Γ += Wc[i]*(σ[:,i] - m)*(y[:,i] - μ)'
end
end
if addmatrix !== nothing; Σ += addmatrix; end
if forceHermitian; Σ = Hermitian(Σ); end
return μ,Σ,Γ
end
function ET1(m::AbstractFloat, v::AbstractFloat, g; addmatrix=nothing)
jm = ForwardDiff.derivative(g, m)
mE = g(m)
SE = jm^2*v
CE = v*jm
if addmatrix !== nothing; SE += addmatrix; end
return mE,SE,CE
end
function ET1(m::AbstractVector, S::AbstractMatrix, g; addmatrix=nothing, forceHermitian=false)
Jm = ForwardDiff.jacobian(g, m)
mE = g(m)
SE = Jm*S*Jm'
CE = S*Jm'
if addmatrix !== nothing; SE += addmatrix; end
if forceHermitian; SE = Hermitian(SE); end
return mE,SE,CE
end
function ET2(m::AbstractFloat, v::AbstractFloat, g; addmatrix=nothing)
j(m) = ForwardDiff.derivative(g, m)
h(m) = ForwardDiff.derivative(j, m)
mE = g(m) + 1/2*h(m)*v
SE = j(m)*v*j(m) + 1/2*(h(m)*v*h(m)*v)
CE = v*j(m)
if addmatrix !== nothing; SE += addmatrix; end
return mE,SE,CE
end
function ET2(m::AbstractVector, S::AbstractMatrix, g; addmatrix=nothing, forceHermitian=false)
# Dimensionalities
M = length(m)
N = length(g(m))
if N == 1
Jm = ForwardDiff.gradient(g, m)
Hm = ForwardDiff.hessian(g, m)
# Auxiliary terms
aux1 = tr(Hm*S)
aux2 = tr(Hm*S*Hm*S)
# Mean, variance and covariance
mE = g(m) + 1/2*aux1
SE = Jm'*S*Jm + 1/2*aux2
CE = S*Jm
else
Jm = ForwardDiff.jacobian(g, m)
aux1 = zeros(eltype(m), N)
Hi = zeros(eltype(m), M,M,N)
for i in 1:N
g_i(x) = g(x)[i]
Hi[:,:,i] = ForwardDiff.hessian(g_i, m)
aux1 += e(i,N)*tr(Hi[:,:,i]*S)
end
# Auxiliary terms
aux2 = zeros(eltype(m), N,N)
for i in 1:N
for j in 1:N
aux2 += e(i,N)*e(j,N)'*tr(Hi[:,:,i]*S*Hi[:,:,j]*S)
end
end
# Mean, variance and covariance
mE = g(m) + 1/2*aux1
SE = Jm*S*Jm' + 1/2*aux2
CE = S*Jm'
end
if addmatrix !== nothing; SE += addmatrix; end
if forceHermitian; SE = Hermitian(SE); end
return mE,SE,CE
end
function e(i::Int64, n::Int64)
"Basis vector"
if i > n; error("IndexError: index larger than length of vector."); end
e = zeros(n)
e[i] = 1
return e
end