KalmanFilterTools.jl 44.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
@doc raw"""
Module KalmanFilterTools provides algorithms for computing the
Kalman filter, the Kalman smoother and the log-likelihood of a state
space model using the Kalman filter.

The state space is given by
```
    y_t = Z_t α_t + ϵ_t
    α_{t+1} = T α_t + Rη_t
```
"""
MichelJuillard's avatar
MichelJuillard committed
12
13
14
module KalmanFilterTools

include("kalman_base.jl")
15
include("univariate_step.jl")
MichelJuillard's avatar
MichelJuillard committed
16
17
18
19

"""
State space specification:
    y_t = Z*a_t + epsilon_t
20
    a_{t+1} = T*a_t- + R eta_t
MichelJuillard's avatar
MichelJuillard committed
21
22
23
24
25
26
27
    E(epsilon_t epsilon_t') = H
    E(eta_t eta_t') = Q
"""

using LinearAlgebra
using LinearAlgebra.BLAS

MichelJuillard's avatar
MichelJuillard committed
28
export KalmanLikelihoodWs, FastKalmanLikelihoodWs, DiffuseKalmanLikelihoodWs, KalmanSmootherWs, kalman_likelihood, kalman_likelihood_monitored, fast_kalman_likelihood, diffuse_kalman_likelihood, kalman_filter!, kalman_smoother!
MichelJuillard's avatar
MichelJuillard committed
29
30
31
32

abstract type KalmanWs{T, U} end

struct KalmanLikelihoodWs{T, U} <: KalmanWs{T, U}
MichelJuillard's avatar
MichelJuillard committed
33
    csmall::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
34
35
36
37
38
39
40
41
    Zsmall::Matrix{T}
    # necessary for Z selecting vector with missing variables
    iZsmall::Vector{U}
    RQ::Matrix{T}
    QQ::Matrix{T}
    v::Vector{T}
    F::Matrix{T}
    cholF::Matrix{T}
42
43
    cholH::Matrix{T}
    LTcholH::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
44
45
46
47
    iFv::Vector{T}
    a1::Vector{T}
    K::Matrix{T}
    ZP::Matrix{T}
48
    iFZ::SubArray{T}
MichelJuillard's avatar
MichelJuillard committed
49
50
51
    PTmp::Matrix{T}
    oldP::Matrix{T}
    lik::Vector{T}
52
53
54
    cholHset::Bool
    ystar::Vector{T}
    Zstar::Matrix{T}
55
    Hstar::Matrix{T}
56
57
58
    tmp_ns::Vector{T}
    PZi::Vector{T}
    kalman_tol::T
59

MichelJuillard's avatar
MichelJuillard committed
60
    function KalmanLikelihoodWs{T, U}(ny::U, ns::U, np::U, nobs::U) where {T <: AbstractFloat, U <: Integer}
MichelJuillard's avatar
MichelJuillard committed
61
        csmall = Vector{T}(undef, ny)
MichelJuillard's avatar
MichelJuillard committed
62
63
64
65
66
67
        Zsmall = Matrix{T}(undef, ny, ns)
        iZsmall = Vector{U}(undef, ny)
        RQ = Matrix{T}(undef, ns, np)
        QQ = Matrix{T}(undef, ns, ns)
        F = Matrix{T}(undef, ny, ny)
        cholF = Matrix{T}(undef, ny, ny)
68
69
        cholH = Matrix{T}(undef, ny, ny)
        LTcholH = Matrix{T}(undef, ny, ny)
MichelJuillard's avatar
MichelJuillard committed
70
71
72
73
74
75
76
77
78
        v = Vector{T}(undef, ny)
        iFv = Vector{T}(undef, ny)
        a1 = Vector{T}(undef, ns)
        K = Matrix{T}(undef, ny, ns)
        PTmp = Matrix{T}(undef, ns, ns)
        oldP = Matrix{T}(undef, ns, ns)
        ZP = Matrix{T}(undef, ny, ns)
        iFZ = view(PTmp,1:ny,:)
        lik = Vector{T}(undef, nobs)
79
80
81
        cholHset = false
        ystar = Vector{T}(undef, ny)
        Zstar = Matrix{T}(undef, ny, ns)
82
        Hstar = Matrix{T}(undef, ny, ny)
83
84
85
        tmp_ns = Vector{T}(undef, ns)
        PZi = Vector{T}(undef, ns)
        kalman_tol = 1e-12
86

MichelJuillard's avatar
MichelJuillard committed
87
         new(csmall, Zsmall, iZsmall, RQ, QQ, v, F, cholF, cholH, LTcholH,
88
            iFv, a1, K, ZP, iFZ, PTmp, oldP, lik, cholHset, ystar,
89
            Zstar, Hstar, tmp_ns, PZi, kalman_tol)
MichelJuillard's avatar
MichelJuillard committed
90
91
92
    end
end

MichelJuillard's avatar
MichelJuillard committed
93
KalmanLikelihoodWs(ny, ns, np, nobs) = KalmanLikelihoodWs{Float64, Int64}(ny, ns, np, nobs)
MichelJuillard's avatar
MichelJuillard committed
94

MichelJuillard's avatar
MichelJuillard committed
95
96
97
98
99
100
101
102
103
104
105
106
107
# Z can be either a matrix or a selection vector
function kalman_likelihood(Y::AbstractArray{U},
                           Z::AbstractArray{W},
                           H::AbstractArray{U},
                           T::AbstractArray{U},
                           R::AbstractArray{U},
                           Q::AbstractArray{U},
                           a::AbstractArray{U},
                           P::AbstractArray{U},
                           start::V,
                           last::V,
                           presample::V,
                           ws::KalmanWs) where {U <: AbstractFloat, W <: Real, V <: Integer}
108
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
109
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
110
111
112
113
    # QQ = R*Q*R'
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    lik_cst = (nobs - presample)*ny*log(2*pi)
    fill!(ws.lik, 0.0)
114
    cholHset = false
MichelJuillard's avatar
MichelJuillard committed
115
    t = start
MichelJuillard's avatar
MichelJuillard committed
116
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
117
118
119
120
121
122
    @inbounds while t <= last
        # v  = Y[:,t] - Z*a
        get_v!(ws.v, Y, Z, a, iy, ny)
        iy += ny
        # F  = Z*P*Z' + H
        get_F!(ws.F, ws.ZP, Z, P, H)
123
124
125
126
127
128
129
        info = get_cholF!(ws.cholF, ws.F)
        if info != 0
            # F is near singular
            if !cholHset
                get_cholF!(ws.cholH, H)
                cholHset = true
            end
MichelJuillard's avatar
MichelJuillard committed
130
            ws.lik[t] = univariate_step!(Y, t, Z, H, T, ws.QQ, a, P, ws.kalman_tol, ws)
131
132
133
134
        else
            # iFv = inv(F)*v
            get_iFv!(ws.iFv, ws.cholF, ws.v)
            ws.lik[t] = log(det_from_cholesky(ws.cholF)) + LinearAlgebra.dot(ws.v, ws.iFv)
MichelJuillard's avatar
MichelJuillard committed
135
            # K = iF*Z*P
136
            get_K!(ws.K, ws.ZP, ws.cholF)
MichelJuillard's avatar
MichelJuillard committed
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
            # a = T(a + K'*v)
            update_a!(a, ws.K, ws.v, ws.a1, T)
            # P = T*(P - K'*Z*P)*T'+ QQ
            update_P!(P, T, ws.QQ, ws.K, ws.ZP, ws.PTmp)
        end
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(lik_cst + sum(view(ws.lik,(presample+1):nobs)))
    else
        LIK = -0.5*(lik_cst + sum(ws.lik))
    end
    LIK
end

function kalman_likelihood(Y::AbstractArray{U},
                           Z::AbstractArray{W},
                           H::AbstractArray{U},
                           T::AbstractArray{U},
                           R::AbstractArray{U},
                           Q::AbstractArray{U},
                           a::AbstractArray{U},
                           P::AbstractArray{U},
                           start::V,
                           last::V,
                           presample::V,
                           ws::KalmanWs,
                           data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat, W <: Real, V <: Integer}
165
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
166
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
167
168
169
170
171
    # QQ = R*Q*R'
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    l2pi = log(2*pi)
    fill!(ws.lik, 0.0)
    t = start
MichelJuillard's avatar
MichelJuillard committed
172
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
173
    ncolZ = size(Z, 2)
MichelJuillard's avatar
MichelJuillard committed
174
    cholHset = false
MichelJuillard's avatar
MichelJuillard committed
175
176
177
178
179
180
181
182
    @inbounds while t <= last
        pattern = data_pattern[t]
        ndata = length(pattern)
        vH = view(H, pattern, pattern)
        vv = view(ws.v, 1:ndata)
        vF = view(ws.F, 1:ndata, 1:ndata)
        vZP = view(ws.ZP, 1:ndata, :)
        vcholF = view(ws.cholF, 1:ndata, 1:ndata)
MichelJuillard's avatar
MichelJuillard committed
183
        vcholH = view(ws.cholF, 1:ndata, 1:ndata)
MichelJuillard's avatar
MichelJuillard committed
184
185
        viFv = view(ws.iFv, 1:ndata)
        vK = view(ws.K, 1:ndata, :)
MichelJuillard's avatar
MichelJuillard committed
186
        vZsmall = get_vZsmall(ws.Zsmall, ws.iZsmall, Z, pattern, ndata, ny)
187

MichelJuillard's avatar
MichelJuillard committed
188
189
190
191
192
        # v  = Y[:,t] - Z*a
        get_v!(vv, Y, vZsmall, a, t, pattern)
        iy += ny
        # F  = Z*P*Z' + H
        get_F!(vF, vZP, vZsmall, P, vH)
193
194
195
196
197
198
199
200
        info = get_cholF!(vcholF, vF)
        if info != 0
            @show info
            # F is near singular
            if !cholHset
                get_cholF!(vcholH, vH)
                cholHset = true
            end
MichelJuillard's avatar
MichelJuillard committed
201
            ws.lik[t] = univariate_step!(Y, t, Z, H, T, ws.QQ, a, P, ws.kalman_tol, ws)
202
203
204
205
        else
            # iFv = inv(F)*v
            get_iFv!(viFv, vcholF, vv)
            ws.lik[t] = ndata*l2pi + log(det_from_cholesky(vcholF)) + LinearAlgebra.dot(vv, viFv)
MichelJuillard's avatar
MichelJuillard committed
206
            # K = iF*Z*P
207
            get_K!(vK, vZP, vcholF)
MichelJuillard's avatar
MichelJuillard committed
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
            # a = T(a + K'*v)
            update_a!(a, vK, vv, ws.a1, T)
            # P = T*(P - K'*Z*P)*T'+ QQ
            update_P!(P, T, ws.QQ, vK, vZP, ws.PTmp)
        end
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*sum(view(ws.lik,(presample+1):nobs))
    else
        LIK = -0.5*sum(ws.lik)
    end
    LIK
end

function kalman_likelihood_monitored(Y::Matrix{U},
                                     Z::AbstractArray{W},
                                     H::Matrix{U},
                                     T::Matrix{U},
                                     R::Matrix{U},
                                     Q::Matrix{U},
                                     a::Vector{U},
                                     P::Matrix{U},
                                     start::V,
                                     last::V,
                                     presample::V,
                                     ws::KalmanWs) where {U <: AbstractFloat, V <: Integer, W <: Real}
235
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
236
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
237
238
239
240
241
242
    ns = size(T,1)
    # QQ = R*Q*R'
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    lik_cst = (nobs - presample)*ny*log(2*pi)
    fill!(ws.lik, 0.0)
    t = start
MichelJuillard's avatar
MichelJuillard committed
243
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
244
245
246
247
248
249
250
251
252
    steady = false
    copy!(ws.oldP, P)
    @inbounds while t <= last
        # v  = Y[:,t] - Z*a
        get_v!(ws.v, Y, Z, a, iy, ny)
        iy += ny
        if !steady
            # F  = Z*P*Z' + H
            get_F!(ws.F, ws.ZP, Z, P, H)
253
254
255
256
257
258
259
260
            info = get_cholF!(ws.cholF, ws.F)
            if info != 0
                @show info
                # F is near singular
                if !cholHset
                    get_cholF!(ws.cholH, H)
                    cholHset = true
                end
MichelJuillard's avatar
MichelJuillard committed
261
262
                ws.lik[t] = univariate_step!(Y, t, Z, H, T, ws.QQ, a, P, ws.kalman_tol, ws)
                t += 1
263
264
                continue
            end
MichelJuillard's avatar
MichelJuillard committed
265
266
267
268
        end
        # iFv = inv(F)*v
        get_iFv!(ws.iFv, ws.cholF, ws.v)
        ws.lik[t] = log(det_from_cholesky(ws.cholF)) + LinearAlgebra.dot(ws.v, ws.iFv)
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        if !steady
            # K = iF*Z*P
            get_K!(ws.K, ws.ZP, ws.cholF)
        end
        # a = T(a + K'*v)
        update_a!(a, ws.K, ws.v, ws.a1, T)
        if !steady
            # P = T*(P - K'*Z*P)*T'+ QQ
            update_P!(P, T, ws.QQ, ws.K, ws.ZP, ws.PTmp)
            ws.oldP .-= P
            if norm(ws.oldP) < ns*eps()
                steady = true
            else
                copy!(ws.oldP, P)
MichelJuillard's avatar
MichelJuillard committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
            end
        end
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(lik_cst + sum(view(ws.lik,(presample+1):nobs)))
    else
        LIK = -0.5*(lik_cst + sum(ws.lik))
    end
    LIK
end

function kalman_likelihood_monitored(Y::AbstractArray{U},
                                     Z::AbstractArray{W},
                                     H::AbstractArray{U},
                                     T::AbstractArray{U},
                                     R::AbstractArray{U},
                                     Q::AbstractArray{U},
                                     a::AbstractArray{U},
                                     P::AbstractArray{U},
                                     start::V,
                                     last::V,
                                     presample::V,
                                     ws::KalmanWs,
                                     data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat, W <: Real, V <: Integer}
308
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
309
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
310
311
312
313
314
315
    ns = size(T,1)
    # QQ = R*Q*R'
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    l2pi = log(2*pi)
    fill!(ws.lik, 0.0)
    t = start
MichelJuillard's avatar
MichelJuillard committed
316
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
317
318
    steady = false
    copy!(ws.oldP, P)
MichelJuillard's avatar
MichelJuillard committed
319
    cholHset = false
MichelJuillard's avatar
MichelJuillard committed
320
321
322
323
324
325
326
327
328
329
    @inbounds while t <= last
        pattern = data_pattern[t]
        ndata = length(pattern)
        vH = view(H, pattern, pattern)
        vv = view(ws.v, 1:ndata)
        vF = view(ws.F, 1:ndata, 1:ndata)
        vZP = view(ws.ZP, 1:ndata, :)
        vcholF = view(ws.cholF, 1:ndata, 1:ndata)
        viFv = view(ws.iFv, 1:ndata)
        vK = view(ws.K, 1:ndata, :)
330

MichelJuillard's avatar
MichelJuillard committed
331
332
333
334
335
336
        # v  = Y[:,t] - Z*a
        get_v!(vv, Y, Z, a, t, pattern)
        iy += ny
        if !steady
            # F  = Z*P*Z' + H
            get_F!(vF, vZP, Z, P, vH)
337
338
339
340
341
342
343
            info = get_cholF!(ws.cholF, ws.F)
            if info != 0
                # F is near singular
                if !cholHset
                    get_cholF!(ws.cholH, H)
                    cholHset = true
                end
MichelJuillard's avatar
MichelJuillard committed
344
                ws.lik[t] = univariate_step!(Y, t, Z, H, T, ws.QQ, a, P, ws.kalman_tol, ws)
345
346
347
                t += 1
                continue
            end
MichelJuillard's avatar
MichelJuillard committed
348
349
350
351
        end
        # iFv = inv(F)*v
        get_iFv!(viFv, vcholF, vv)
        ws.lik[t] = ndata*l2pi + log(det_from_cholesky(vcholF)) + LinearAlgebra.dot(vv, viFv)
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        if !steady
            # K = iF*Z*P
            get_K!(vK, vZP, vcholF)
        end
        # a = T(a + K'*v)
        update_a!(a, vK, vv, ws.a1, T)
        if !steady
            # P = T*(P - K'*Z*P)*T'+ QQ
            update_P!(P, T, ws.QQ, vK, vZP, ws.PTmp)
            ws.oldP .-= P
            if norm(ws.oldP) < ns*eps()
                steady = true
            else
                copy!(ws.oldP, P)
MichelJuillard's avatar
MichelJuillard committed
366
367
368
369
370
371
372
373
374
375
376
377
378
            end
        end
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(sum(view(ws.lik,(presample+1):nobs)))
    else
        LIK = -0.5*(sum(ws.lik))
    end
    LIK
end

struct FastKalmanLikelihoodWs{T, U} <: KalmanWs{T, U}
MichelJuillard's avatar
MichelJuillard committed
379
    csmall::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    Zsmall::Matrix{T}
    iZsmall::Vector{U}
    QQ::Matrix{T}
    v::Vector{T}
    F::Matrix{T}
    cholF::Matrix{T}
    iFv::Vector{T}
    a1::Vector{T}
    K::Matrix{T}
    RQ::Matrix{T}
    ZP::Matrix{T}
    M::Matrix{T}
    W::Matrix{T}
    ZW::Matrix{T}
    ZWM::Matrix{T}
    iFZWM::Matrix{T}
    TW::Matrix{T}
    iFZW::Matrix{T}
    KtiFZW::Matrix{T}
399
400
401
    ystar::Vector{T}
    Zstar::Matrix{T}
    Hstar::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
402
    lik::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
403
    kalman_tol::T
404

MichelJuillard's avatar
MichelJuillard committed
405
    function FastKalmanLikelihoodWs{T, U}(ny::U, ns::U, np::U, nobs::U) where {T <: AbstractFloat, U <: Integer}
MichelJuillard's avatar
MichelJuillard committed
406
        csmall = Vector{T}(undef, ny)
MichelJuillard's avatar
MichelJuillard committed
407
408
409
410
411
412
413
        Zsmall = Matrix{T}(undef, ny, ns)
        iZsmall = Vector{U}(undef, ny)
        QQ = Matrix{T}(undef, ns, ns)
        RQ = Matrix{T}(undef, ns, np)
        F = Matrix{T}(undef, ny, ny)
        cholF = Matrix{T}(undef, ny, ny)
        v = Vector{T}(undef, ny)
414

MichelJuillard's avatar
MichelJuillard committed
415
416
417
418
419
420
421
422
423
424
425
426
        iFv = Vector{T}(undef, ny)
        a1 = Vector{T}(undef, ns)
        K = Matrix{T}(undef, ny, ns)
        M = Matrix{T}(undef, ny, ny)
        W = Matrix{T}(undef, ns, ny)
        ZP = Matrix{T}(undef, ny, ns)
        ZW = Matrix{T}(undef, ny, ny)
        ZWM = Matrix{T}(undef, ny, ny)
        iFZWM = Matrix{T}(undef, ny, ny)
        TW = Matrix{T}(undef, ns, ny)
        iFZW = Matrix{T}(undef, ny, ny)
        KtiFZW = Matrix{T}(undef, ns, ny)
Michel Juillard's avatar
Michel Juillard committed
427
428
429
        ystar = Vector{T}(undef, ny)
        Zstar = Matrix{T}(undef, ny, ns)
        Hstar = Matrix{T}(undef, ny, ny)
MichelJuillard's avatar
MichelJuillard committed
430
        lik = Vector{T}(undef, nobs)
MichelJuillard's avatar
MichelJuillard committed
431
        kalman_tol = 1e-12
432
433
        new(csmall, Zsmall, iZsmall, QQ, v, F, cholF, iFv, a1, K, RQ, ZP, M, W,
            ZW, ZWM, iFZWM, TW, iFZW, KtiFZW, ystar, Ztar, Hstar, lik, kalman_tol)
MichelJuillard's avatar
MichelJuillard committed
434
435
436
    end
end

MichelJuillard's avatar
MichelJuillard committed
437
438
FastKalmanLikelihoodWs(ny, ns, np, nobs) = FastKalmanLikelihoodWs{Float64, Int64}(ny, ns, np, nobs)

MichelJuillard's avatar
MichelJuillard committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""
K doesn't represent the same matrix as above
"""
function fast_kalman_likelihood(Y::Matrix{U},
                                Z::AbstractArray{W},
                                H::Matrix{U},
                                T::Matrix{U},
                                R::Matrix{U},
                                Q::Matrix{U},
                                a::Vector{U},
                                P::Matrix{U},
                                start::V,
                                last::V,
                                presample::V,
                                ws::KalmanWs) where {U <: AbstractFloat, V <: Integer, W <: Real}
454
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
455
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
456
457
458
459
460
461
462
463
464
465
466
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    lik_cst = (nobs - presample)*ny*log(2*pi)
    fill!(ws.lik, 0.0)
    # F  = Z*P*Z' + H
    get_F!(ws.F, ws.ZP, Z, P, H)
    get_cholF!(ws.cholF, ws.F)
    # K = Z*P
    copy!(ws.K, ws.ZP)
    # W = T*K'
    mul!(ws.W, T, transpose(ws.K))
    # M = -iF
467
    get_M!(ws.M, ws.cholF, ws.ZW)
MichelJuillard's avatar
MichelJuillard committed
468
469
    LIK = 0
    t = start
MichelJuillard's avatar
MichelJuillard committed
470
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
471
472
473
474
475
476
477
    @inbounds while t <= last
        # v  = Y[:,t] - Z*a
        get_v!(ws.v, Y, Z, a, iy, ny)
        iy += ny
        # iFv = inv(F)*v
        get_iFv!(ws.iFv, ws.cholF, ws.v)
        ws.lik[t] = log(det_from_cholesky(ws.cholF)) + LinearAlgebra.dot(ws.v, ws.iFv)
478
479
480
481
482
483
484
485
486
487
488
489
        # a = T(a + K'*iFv)
        update_a!(a, ws.K, ws.iFv, ws.a1, T)
        # M = M + M*W'*Z'iF*Z*W*M
        update_M!(ws.M, Z, ws.W, ws.cholF, ws.ZW, ws.ZWM, ws.iFZWM)
        # F =  F + Z*W*M*W'Z'
        gemm!('N', 'T', 1.0, ws.ZWM, ws.ZW, 1.0, ws.F)
        # cholF
        get_cholF!(ws.cholF, ws.F)
        # K = K + W*M*W'*Z'
        update_K!(ws.K, ws.ZWM, ws.W)
        # W = T(W - K'*iF*Z*W)
        update_W!(ws.W, ws.ZW, ws.cholF, T, ws.K, ws.iFZW, ws.KtiFZW)
MichelJuillard's avatar
MichelJuillard committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(lik_cst + sum(view(ws.lik, (presample+1):nobs)))
    else
        LIK = -0.5*(lik_cst + sum(ws.lik))
    end
    LIK
end

function fast_kalman_likelihood(Y::Matrix{U},
                                Z::AbstractArray{W},
                                H::Matrix{U},
                                T::Matrix{U},
                                R::Matrix{U},
                                Q::Matrix{U},
                                a::Vector{U},
                                P::Matrix{U},
                                start::V,
                                last::V,
                                presample::V,
                                ws::KalmanWs,
                                data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat, V <: Integer, W <: Real}
513
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
514
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
515
516
517
518
519
520
521
522
523
524
525
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    l2pi = log(2*pi)
    fill!(ws.lik, 0.0)
    # F  = Z*P*Z' + H
    get_F!(ws.F, ws.ZP, Z, P, H)
    get_cholF!(ws.cholF, ws.F)
    # K = Z*P
    copy!(ws.K, ws.ZP)
    # W = T*K'
    mul!(ws.W, T, transpose(ws.K))
    # M = -iF
526
    get_M!(ws.M, ws.cholF, ws.ZW)
MichelJuillard's avatar
MichelJuillard committed
527
528
    LIK = 0
    t = start
MichelJuillard's avatar
MichelJuillard committed
529
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
    @inbounds while t <= last
        pattern = data_pattern[t]
        ndata = length(pattern)
        vv = view(ws.v, 1:ndata)
        vF = view(ws.F, 1:ndata, 1:ndata)
        vcholF = view(ws.cholF, 1:ndata, 1:ndata)
        viFv = view(ws.iFv, 1:ndata)
        vK = view(ws.K, 1:ndata, :)
        vW = view(ws.W, :, 1:ndata)
        vZW = view(ws.ZW, 1:ndata, 1:ndata)
        viFZW = view(ws.iFZW, 1:ndata, 1:ndata)
        vZWM = view(ws.ZWM, 1:ndata, 1:ndata)
        viFZWM = view(ws.iFZWM, 1:ndata, 1:ndata)
        vKtiFZW = view(ws.KtiFZW, :, 1:ndata)
MichelJuillard's avatar
MichelJuillard committed
544
        vZsmall = get_vZsmall(ws.Zsmall, ws.iZsmall, Z, pattern, ndata, ny)
545

MichelJuillard's avatar
MichelJuillard committed
546
547
548
549
550
551
        # v  = Y[:,t] - Z*a
        get_v!(vv, Y, vZsmall, a, t, pattern)
        iy += ny
        # iFv = inv(F)*v
        get_iFv!(viFv, vcholF, vv)
        ws.lik[t] = ndata*l2pi + log(det_from_cholesky(vcholF)) + LinearAlgebra.dot(vv, viFv)
552
553
554
555
556
557
558
559
560
561
562
563
        # a = T(a + K'*iFv)
        update_a!(a, vK, viFv, ws.a1, T)
        # M = M + M*W'*Z'iF*Z*W*M
        update_M!(ws.M, Z, vW, vcholF, vZW, vZWM, viFZWM)
        # F =  F + Z*W*M*W'Z'
        gemm!('N', 'T', 1.0, vZWM, vZW, 1.0, vF)
        # cholF
        get_cholF!(vcholF, vF)
        # K = K + W*M*W'*Z'
        update_K!(vK, vZWM, vW)
        # W = T(W - K'*iF*Z*W)
        update_W!(vW, vZW, vcholF, T, vK, viFZW, vKtiFZW)
MichelJuillard's avatar
MichelJuillard committed
564
565
566
567
568
569
570
571
572
573
574
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(sum(view(ws.lik, (presample+1):nobs)))
    else
        LIK = -0.5*(sum(ws.lik))
    end
    LIK
end

struct DiffuseKalmanLikelihoodWs{T, U} <: KalmanWs{T, U}
MichelJuillard's avatar
MichelJuillard committed
575
    csmall::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
    Zsmall::Matrix{T}
    iZsmall::Vector{U}
    QQ::Matrix{T}
    RQ::Matrix{T}
    v::Vector{T}
    F::Matrix{T}
    iF::Matrix{T}
    iFv::Vector{T}
    a1::Vector{T}
    cholF::Matrix{T}
    ZP::Matrix{T}
    Fstar::Matrix{T}
    ZPstar::Matrix{T}
    K::Matrix{T}
    iFZ::Matrix{T}
    Kstar::Matrix{T}
    PTmp::Matrix{T}
    uKinf::Vector{T}
    uKstar::Vector{T}
    Kinf_Finf::Vector{T}
596
597
598
    ystar::Vector{T}
    Zstar::Matrix{T}
    Hstar::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
599
    lik::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
600
    kalman_tol::T
MichelJuillard's avatar
MichelJuillard committed
601
    function DiffuseKalmanLikelihoodWs{T, U}(ny::U, ns::U, np::U, nobs::U) where {T <: AbstractFloat, U <: Integer}
MichelJuillard's avatar
MichelJuillard committed
602
        csmall = Vector{T}(undef, ny)
MichelJuillard's avatar
MichelJuillard committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
        Zsmall = Matrix{T}(undef, ny, ns)
        iZsmall = Vector{U}(undef, ny)
        QQ = Matrix{T}(undef, ns, ns)
        RQ = Matrix{T}(undef, ns, np)
        v = Vector{T}(undef, ny)
        F = Matrix{T}(undef, ny, ny)
        iF = Matrix{T}(undef, ny,ny )
        iFv = Vector{T}(undef, ny)
        a1 = Vector{T}(undef, ns)
        cholF = Matrix{T}(undef, ny, ny)
        ZP = Matrix{T}(undef, ny, ns)
        Fstar = Matrix{T}(undef, ny, ny)
        ZPstar = Matrix{T}(undef, ny, ns)
        K = Matrix{T}(undef, ny, ns)
        iFZ = Matrix{T}(undef, ny, ns)
        Kstar = Matrix{T}(undef, ny, ns)
        PTmp = Matrix{T}(undef, ns, ns)
        uKinf = Vector{T}(undef, ns)
        uKstar = Vector{T}(undef, ns)
        Kinf_Finf = Vector{T}(undef, ns)
Michel Juillard's avatar
Michel Juillard committed
623
624
625
        ystar = Vector{T}(undef, ny)
        Zstar = Matrix{T}(undef, ny, ns)
        Hstar = Matrix{T}(undef, ny, ny)
MichelJuillard's avatar
MichelJuillard committed
626
        lik = zeros(T, nobs)
MichelJuillard's avatar
MichelJuillard committed
627
        kalman_tol = 1e-12
628
629
630
        new(csmall, Zsmall, iZsmall, QQ, RQ, v, F, iF, iFv, a1, cholF, ZP, Fstar,
            ZPstar, K, iFZ, Kstar, PTmp, uKinf, uKstar, Kinf_Finf, ystar, Zstar, Hstar,
            lik, kalman_tol)
MichelJuillard's avatar
MichelJuillard committed
631
632
633
    end
end

634
635
DiffuseKalmanLikelihoodWs(ny, ns, np, nobs) = DiffuseKalmanLikelihoodWs{Float64, Int64}(ny, ns, np, nobs)

MichelJuillard's avatar
MichelJuillard committed
636
637
638
639
640
641
642
643
644
645
646
647
648
649
function diffuse_kalman_likelihood_init!(Y::Matrix{U},
                                         Z::AbstractArray{W},
                                         H::Matrix{U},
                                         T::Matrix{U},
                                         QQ::Matrix{U},
                                         a::Vector{U},
                                         Pinf::Matrix{U},
                                         Pstar::Matrix{U},
                                         start::V,
                                         last::V,
                                         tol::U,
                                         ws::KalmanWs) where {U <: AbstractFloat,
                                                              V <: Integer,
                                                              W <: Real}
650

MichelJuillard's avatar
MichelJuillard committed
651
652
    ny = size(Y, 1)
    t = start
653
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
654
655
656
657
658
659
660
661
662
    diffuse_kalman_tol = 1e-8
    kalman_tol = 1e-8
    while t <= last
        # v  = Y[:,t] - Z*a
        get_v!(ws.v, Y, Z, a, iy, ny)
        iy += ny
        # Finf = Z*Pinf*Z'
        get_F!(ws.F, ws.ZP, Z, Pinf)
        info = get_cholF!(ws.cholF, ws.F)
663
        if info > 0
MichelJuillard's avatar
MichelJuillard committed
664
665
666
            if norm(ws.F) < tol
                return t - 1
            else
MichelJuillard's avatar
MichelJuillard committed
667
                ws.lik[t] += univariate_step(Y, t, Z, H, T, QQ, a, Pinf, Pstar, diffuse_kalman_tol, kalman_tol, ws)
MichelJuillard's avatar
MichelJuillard committed
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
            end
        else
            ws.lik[t] = log(det_from_cholesky(ws.cholF))
            # Kinf   = iFinf*Z*Pinf                                   %define Kinf'=T^{-1}*K_0 with M_{\infty}=Pinf*Z'
            copy!(ws.K, ws.ZP)
            LAPACK.potrs!('U', ws.cholF, ws.K)
            # Fstar  = Z*Pstar*Z' + H;                                        %(5.7) DK(2012)
            get_F!(ws.Fstar, ws.ZPstar, Z, Pstar, H)
            # Kstar  = iFinf*(Z*Pstar - Fstar*Kinf)                           %(5.12) DK(2012); note that there is a typo in DK (2003) with "+ Kinf" instead of "- Kinf", but it is correct in their appendix
            get_Kstar!(ws.Kstar, Z, Pstar, ws.Fstar, ws.K, ws.cholF)
            # Pstar  = T*(Pstar-Pstar*Z'*Kinf-Pinf*Z'*Kstar)*T'+QQ;         %(5.14) DK(2012)
            copy!(ws.PTmp, Pstar)
            gemm!('T','N',-1.0,ws.ZPstar, ws.K, 1.0, ws.PTmp)
            gemm!('T','N',-1.0,ws.ZP, ws.Kstar, 1.0, ws.PTmp)
            copy!(Pstar, ws.PTmp)
            mul!(ws.PTmp,T,Pstar)
            copy!(Pstar, QQ)
            gemm!('N','T',1.0,ws.PTmp,T,1.0,Pstar)
            # Pinf   = T*(Pinf-Pinf*Z'*Kinf)*T';                             %(5.14) DK(2012)
            gemm!('T','N', -1.0,ws.ZP, ws.K,1.0,Pinf)
            mul!(ws.PTmp,T,Pinf)
            mul!(Pinf,ws.PTmp,transpose(T))
            # a      = T*(a+Kinf*v);                                          %(5.13) DK(2012)
            update_a!(a, ws.K, ws.v, ws.a1, T)
        end
        t += 1
    end
    t
end

function diffuse_kalman_likelihood_init!(Y::Matrix{U},
                                         Z::AbstractArray{W},
                                         H::Matrix{U},
                                         T::Matrix{U},
                                         QQ::Matrix{U},
                                         a::Vector{U},
                                         Pinf::Matrix{U},
                                         Pstar::Matrix{U},
                                         start::V,
                                         last::V,
                                         tol::U,
                                         ws::KalmanWs,
                                         data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat,
                                                                                 V <: Integer,
                                                                                 W <: Real}
713

MichelJuillard's avatar
MichelJuillard committed
714
715
716
    ny = size(Y, 1)
    t = start
    LIK = 0
MichelJuillard's avatar
MichelJuillard committed
717
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
    diffuse_kalman_tol = 1e-8
    kalman_tol = 1e-8
    while t <= last
        pattern = data_pattern[t]
        ndata = length(pattern)
        vH = view(H, pattern, pattern)
        vv = view(ws.v, 1:ndata)
        vF = view(ws.F, 1:ndata, 1:ndata)
        vFstar = view(ws.Fstar, 1:ndata, 1:ndata)
        vZP = view(ws.ZP, 1:ndata, :)
        vZPstar = view(ws.ZPstar, 1:ndata, :)
        vcholF = view(ws.cholF, 1:ndata, 1:ndata)
        viFv = view(ws.iFv, 1:ndata)
        vK = view(ws.K, 1:ndata, :)
        vKstar = view(ws.Kstar, 1:ndata, :)
MichelJuillard's avatar
MichelJuillard committed
733
        vZsmall = get_vZsmall(ws.Zsmall, ws.iZsmall, Z, pattern, ndata, ny)
734

MichelJuillard's avatar
MichelJuillard committed
735
736
737
738
739
740
        # v  = Y[:,t] - Z*a
        get_v!(vv, Y, vZsmall, a, t, pattern)
        iy += ny
        # Finf = Z*Pinf*Z'
        get_F!(vF, vZP, vZsmall, Pinf)
        info = get_cholF!(vcholF, vF)
741
        if info > 0
MichelJuillard's avatar
MichelJuillard committed
742
743
744
            if norm(vF) < tol
                return t - 1
            else
MichelJuillard's avatar
MichelJuillard committed
745
                ws.lik[t] += univariate_step(Y, t, vZsmall, H, T, QQ, a, Pinf, Pstar, diffuse_kalman_tol, kalman_tol, pattern, ws)
MichelJuillard's avatar
MichelJuillard committed
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
            end
        else
            ws.lik[t] = log(det_from_cholesky(ws.cholF))
            # Kinf   = iFinf*Z*Pinf                                   %define Kinf'=T^{-1}*K_0 with M_{\infty}=Pinf*Z'
            copy!(vK, vZP)
            LAPACK.potrs!('U', vcholF, vK)
            # Fstar  = Z*Pstar*Z' + H;                                        %(5.7) DK(2012)
            get_F!(vFstar, vZPstar, vZsmall, Pstar, vH)
            # Kstar  = iFinf*(Z*Pstar - Fstar*Kinf)                           %(5.12) DK(2012); note that there is a typo in DK (2003) with "+ Kinf" instead of "- Kinf", but it is correct in their appendix
            get_Kstar!(vKstar, Z, Pstar, vFstar, vK, vcholF)
            # Pstar  = T*(Pstar-Pstar*Z'*Kinf-Pinf*Z'*Kstar)*T'+QQ;         %(5.14) DK(2012)
            copy!(ws.PTmp, Pstar)
            gemm!('T','N',-1.0, vZPstar, vK, 1.0, ws.PTmp)
            gemm!('T','N',-1.0, vZP, vKstar, 1.0, ws.PTmp)
            copy!(Pstar, ws.PTmp)
            mul!(ws.PTmp,T,Pstar)
            copy!(Pstar, QQ)
            gemm!('N','T',1.0,ws.PTmp,T,1.0,Pstar)
            # Pinf   = T*(Pinf-Pinf*Z'*Kinf)*T';                             %(5.14) DK(2012)
            gemm!('T','N', -1.0, vZP, ws.K,1.0,Pinf)
            mul!(ws.PTmp,T,Pinf)
            mul!(Pinf,ws.PTmp,transpose(T))
            # a      = T*(a+Kinf*v);                                          %(5.13) DK(2012)
            update_a!(a, vK, vv, ws.a1, T)
        end
        t += 1
    end
    t
end

function diffuse_kalman_likelihood(Y::Matrix{U},
                                   Z::AbstractArray{W},
                                   H::Matrix{U},
                                   T::Matrix{U},
780
                                   R::Matrix{U},
MichelJuillard's avatar
MichelJuillard committed
781
782
783
784
785
786
787
788
789
790
791
                                   Q::Matrix{U},
                                   a::Vector{U},
                                   Pinf::Matrix{U},
                                   Pstar::Matrix{U},
                                   start::V,
                                   last::V,
                                   presample::V,
                                   tol::U,
                                   ws::DiffuseKalmanLikelihoodWs) where {U <: AbstractFloat,
                                                                         V <: Integer,
                                                                         W <: Real}
792
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
793
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    lik_cst = (nobs - presample)*ny*log(2*pi)
    fill!(ws.lik, 0.0)
    t = diffuse_kalman_likelihood_init!(Y, Z, H, T, ws.QQ, a, Pinf, Pstar, start, last, tol, ws)
    kalman_likelihood(Y, Z, H, T, R, Q, a, Pstar, t, last, presample, ws)
    @inbounds if presample > 0
        LIK = -0.5*(lik_cst + sum(view(ws.lik, (presample+1):nobs)))
    else
        LIK = -0.5*(lik_cst + sum(ws.lik))
    end
    LIK
end

function diffuse_kalman_likelihood(Y::Matrix{U},
                                   Z::AbstractArray{W},
                                   H::Matrix{U},
                                   T::Matrix{U},
811
                                   R::Matrix{U},
MichelJuillard's avatar
MichelJuillard committed
812
813
814
815
816
817
818
819
820
821
822
823
                                   Q::Matrix{U},
                                   a::Vector{U},
                                   Pinf::Matrix{U},
                                   Pstar::Matrix{U},
                                   start::V,
                                   last::V,
                                   presample::V,
                                   tol::U,
                                   ws::DiffuseKalmanLikelihoodWs,
                                   data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat,
                                                                         V <: Integer,
                                                                         W <: Real}
824
    ny = size(Y,1)
MichelJuillard's avatar
MichelJuillard committed
825
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    get_QQ!(ws.QQ, R, Q, ws.RQ)
    lik_cst = (nobs - presample)*ny*log(2*pi)
    fill!(ws.lik, 0.0)
    t = diffuse_kalman_likelihood_init!(Y, Z, H, T, ws.QQ, a, Pinf, Pstar, start, last, tol, ws, data_pattern)
    kalman_likelihood(Y, Z, H, T, R, Q, a, Pstar, t, last, presample, ws, data_pattern)
    @inbounds if presample > 0
        LIK = -0.5*(lik_cst + sum(view(ws.lik, (presample+1):nobs)))
    else
        LIK = -0.5*(lik_cst + sum(ws.lik))
    end
    LIK
end

# Filters

841
function kalman_filter!(Y::AbstractArray{U},
MichelJuillard's avatar
MichelJuillard committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
                        c::AbstractArray{U},
                        Z::AbstractArray{W},
                        H::AbstractArray{U},
                        d::AbstractArray{U},
                        T::AbstractArray{U},
                        R::AbstractArray{U},
                        Q::AbstractArray{U},
                        a::AbstractArray{U},
                        P::AbstractArray{U},
                       start::V,
                       last::V,
                       presample::V,
                       ws::KalmanWs,
                       data_pattern::Vector{Vector{V}}) where {U <: AbstractFloat, W <: Real, V <: Integer}
    changeC = ndims(c) > 1
    changeZ = ndims(Z) > 2
    changeH = ndims(H) > 2
    changeD = ndims(d) > 1
    changeT = ndims(T) > 2
    changeR = ndims(R) > 2
    changeQ = ndims(Q) > 2
    changeA = ndims(a) > 1
    changeP = ndims(P) > 2
865

866
    ny = size(Y, 1)
MichelJuillard's avatar
MichelJuillard committed
867
    nobs = last - start + 1
MichelJuillard's avatar
MichelJuillard committed
868
869
870
871
872
873
874
875
    ns = size(T,1)
    # QQ = R*Q*R'
    vR = view(R, :, :, 1)
    vQ = view(Q, :, :, 1)
    get_QQ!(ws.QQ, vR, vQ, ws.RQ)
    l2pi = log(2*pi)
    fill!(ws.lik, 0.0)
    t = start
MichelJuillard's avatar
MichelJuillard committed
876
    iy = (start - 1)*ny + 1
MichelJuillard's avatar
MichelJuillard committed
877
878
879
    steady = false
    vP = view(P, :, :, 1)
    copy!(ws.oldP, vP)
MichelJuillard's avatar
MichelJuillard committed
880
    cholHset = false
MichelJuillard's avatar
MichelJuillard committed
881
    @inbounds while t <= last
MichelJuillard's avatar
MichelJuillard committed
882

MichelJuillard's avatar
MichelJuillard committed
883
884
        pattern = data_pattern[t]
        ndata = length(pattern)
MichelJuillard's avatar
MichelJuillard committed
885
886
        vc = changeC ? view(c, :, t) : view(c, :)
        ws.csmall .= view(vc, pattern)
MichelJuillard's avatar
MichelJuillard committed
887
        vZ = changeZ ? view(Z, :, :, t) : view(Z, :, :)
MichelJuillard's avatar
MichelJuillard committed
888
        vZsmall = get_vZsmall(ws.Zsmall, ws.iZsmall, vZ, pattern, ndata, ny)
MichelJuillard's avatar
MichelJuillard committed
889
890
891
        vH = changeH ? view(H, :, :, t) : view(H, :, :)
        vT = changeT ? view(T, :, :, t) : view(T, :, :)
        vR = changeR ? view(R, :, :, t) : view(R, :, :)
892
893
        vQ = changeQ ? view(Q, :, :, t) : view(Q, :, :)
        va = changeA ? view(a, :, t) : view(a, :)
MichelJuillard's avatar
MichelJuillard committed
894
895
896
897
898
899
900
901
902
        va1 = changeA ? view(a, :, t + 1) : view(a, :)
        vd = changeD ? view(d, :, t) : view(d, :)
        vP = changeP ? view(P, :, :, t) : view(P, :, :)
        vP1 = changeP ? view(P, :, :, t + 1) : view(P, :, :)
        if changeR || changeQ
            get_QQ!(ws.QQ, vR, vQ, ws.RQ)
        end
        vv = view(ws.v, 1:ndata)
        vF = view(ws.F, 1:ndata, 1:ndata)
903
        vvH = view(vH, pattern, pattern)
MichelJuillard's avatar
MichelJuillard committed
904
        vZP = view(ws.ZP, 1:ndata, :)
MichelJuillard's avatar
MichelJuillard committed
905
        vcholF = view(ws.cholF, 1:ndata, 1:ndata, 1)
MichelJuillard's avatar
MichelJuillard committed
906
        viFv = view(ws.iFv, 1:ndata)
MichelJuillard's avatar
MichelJuillard committed
907
        vK = view(ws.K, 1:ndata, :, 1)
908

MichelJuillard's avatar
MichelJuillard committed
909
        # v  = Y[:,t] - c - Z*a
MichelJuillard's avatar
MichelJuillard committed
910
        get_v!(vv, Y, vc, vZsmall, va, t, pattern)
MichelJuillard's avatar
MichelJuillard committed
911
912
913
        iy += ny
        if !steady
            # F  = Z*P*Z' + H
MichelJuillard's avatar
MichelJuillard committed
914
            get_F!(vF, vZP, vZsmall, vP, vvH)
MichelJuillard's avatar
MichelJuillard committed
915
            info = get_cholF!(vcholF, vF)
916
917
918
            if info != 0
                # F is near singular
                if !cholHset
MichelJuillard's avatar
MichelJuillard committed
919
                    get_cholF!(ws.cholH, vvH)
920
921
                    cholHset = true
                end
MichelJuillard's avatar
MichelJuillard committed
922
                ws.lik[t] = univariate_step!(Y, t, ws.Zsmall, vvH, T, ws.QQ, va, vP, ws.kalman_tol, ws)
923
924
                t += 1
                continue
MichelJuillard's avatar
MichelJuillard committed
925
            end
MichelJuillard's avatar
MichelJuillard committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
        end
        # iFv = inv(F)*v
        get_iFv!(viFv, vcholF, vv)
        ws.lik[t] = ndata*l2pi + log(det_from_cholesky(vcholF)) + LinearAlgebra.dot(vv, viFv)
        if t <= last
            if !steady
                # K = iF*Z*P
                get_K!(vK, vZP, vcholF)
            end
            # a = d + T(a + K'*v)
            update_a!(va1, va, vd, vK, vv, ws.a1, vT)
            if !steady
                # P = T*(P - K'*Z*P)*T'+ QQ
                copy!(vP1, vP)
                update_P!(vP1, vT, ws.QQ, vK, vZP, ws.PTmp)
MichelJuillard's avatar
MichelJuillard committed
941
                ws.oldP .-= vP1
MichelJuillard's avatar
MichelJuillard committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
                if norm(ws.oldP) < ns*eps()
                    steady = true
                else
                    copy!(ws.oldP, vP)
                end
            end
        end
        t += 1
    end
    @inbounds if presample > 0
        LIK = -0.5*(sum(view(ws.lik,(presample+1):nobs)))
    else
        LIK = -0.5*(sum(ws.lik))
    end
    LIK
end

struct KalmanSmootherWs{T, U} <: KalmanWs{T, U}
MichelJuillard's avatar
MichelJuillard committed
960
    csmall::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
961
962
963
964
965
    Zsmall::Matrix{T}
    # necessary for Z selecting vector with missing variables
    iZsmall::Vector{U}
    RQ::Matrix{T}
    QQ::Matrix{T}
966
    v::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
967
    F::Matrix{T}
968
    cholF::Matrix{T}
969
    cholH::Matrix{T}
970
    iF::Array{T}
MichelJuillard's avatar
MichelJuillard committed
971
972
    iFv::Matrix{T}
    r::Vector{T}
MichelJuillard's avatar
MichelJuillard committed
973
    r1::Vector{T}
974
    at_t::Matrix{T}
975
976
    K::Array{T}
    KDK::Array{T}
977
    L::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
978
979
    L1::Matrix{T}
    N::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
980
    N1::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
981
    ZP::Matrix{T}
982
    Pt_t::Array{T}
MichelJuillard's avatar
MichelJuillard committed
983
    Kv::Matrix{T}
984
    iFZ::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
985
986
987
    PTmp::Matrix{T}
    oldP::Matrix{T}
    lik::Vector{T}
988
989
    KT::Matrix{T}
    D::Matrix{T}
990
991
992
    ystar::Vector{T}
    Zstar::Matrix{T}
    Hstar::Matrix{T}
MichelJuillard's avatar
MichelJuillard committed
993
994
995
996
997
    tmp_np::Vector{T}
    tmp_ns::Vector{T}
    tmp_ny::Vector{T}
    tmp_ns_np::AbstractArray{T}
    tmp_ny_ny::AbstractArray{T}
998
999
    kalman_tol::T
    
MichelJuillard's avatar
MichelJuillard committed
1000
    function KalmanSmootherWs{T, U}(ny::U, ns::U, np::U, nobs::U) where {T <: AbstractFloat, U <: Integer}
For faster browsing, not all history is shown. View entire blame