Skip to content

Commit 079b1b3

Browse files
fix performance of getters
1 parent bcd2311 commit 079b1b3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/kalman_filter_and_smoother.jl

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,36 @@ function get_standard_innovations(fo::FilterOutput)
8888
return v[:, 1] ./ sqrt.(F[1, 1, :])
8989
end
9090

91+
function vector_of_vector_to_matrix(vov::Vector{Vector{T}}) where T
92+
n = length(vov)
93+
p = length(vov[1])
94+
mat = Matrix{T}(undef, n, p)
95+
for i in 1:n, j in 1:p
96+
mat[i, j] = vov[i][j]
97+
end
98+
return mat
99+
end
100+
101+
function vector_of_matrix_to_array(vom::Vector{Matrix{T}}) where T
102+
n = length(vom)
103+
p = length(vom[1])
104+
arr = Array{T, 3}(undef, n, p, p)
105+
for i in 1:n, j in 1:p, k in 1:p
106+
arr[i, j, k] = vom[i][j, k]
107+
end
108+
return arr
109+
end
110+
91111
"""
92112
get_innovations
93113
94114
Returns the innovations `v` obtained with the Kalman filter.
95115
"""
96116
function get_innovations end
97117

98-
get_innovations(filter::FilterOutput) = permutedims(cat(filter.v...; dims=2))
118+
function get_innovations(filter::FilterOutput)
119+
return vector_of_vector_to_matrix(filter.v)
120+
end
99121

100122
function get_innovations(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
101123
assert_possible_to_filter(model)
@@ -109,7 +131,9 @@ Returns the variance `F` of innovations obtained with the Kalman filter.
109131
"""
110132
function get_innovations_variance end
111133

112-
get_innovations_variance(filter_output::FilterOutput) = cat(filter_output.F...; dims=3)
134+
function get_innovations_variance(filter::FilterOutput)
135+
return vector_of_matrix_to_array(filter.F)
136+
end
113137

114138
function get_innovations_variance(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
115139
assert_possible_to_filter(model)
@@ -123,7 +147,9 @@ Returns the filtered state `att` obtained with the Kalman filter.
123147
"""
124148
function get_filtered_state end
125149

126-
get_filtered_state(filter::FilterOutput) = permutedims(cat(filter.att...; dims=2))
150+
function get_filtered_state(filter::FilterOutput)
151+
return vector_of_vector_to_matrix(filter.att)
152+
end
127153

128154
function get_filtered_state(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
129155
assert_possible_to_filter(model)
@@ -137,7 +163,7 @@ Returns the variance `Ptt` of the filtered state obtained with the Kalman filter
137163
"""
138164
function get_filtered_state_variance end
139165

140-
get_filtered_state_variance(filter::FilterOutput) = cat(filter.Ptt...; dims=3)
166+
get_filtered_state_variance(filter::FilterOutput) = vector_of_matrix_to_array(filter.Ptt)
141167

142168
function get_filtered_state_variance(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
143169
assert_possible_to_filter(model)
@@ -151,7 +177,7 @@ Returns the predictive state `a` obtained with the Kalman filter.
151177
"""
152178
function get_predictive_state end
153179

154-
get_predictive_state(filter::FilterOutput) = permutedims(cat(filter.a...; dims=2))
180+
get_predictive_state(filter::FilterOutput) = vector_of_vector_to_matrix(filter.a)
155181

156182
function get_predictive_state(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
157183
assert_possible_to_filter(model)
@@ -165,7 +191,7 @@ Returns the variance `P` of the predictive state obtained with the Kalman filter
165191
"""
166192
function get_predictive_state_variance end
167193

168-
get_predictive_state_variance(filter::FilterOutput) = cat(filter.P...; dims=3)
194+
get_predictive_state_variance(filter::FilterOutput) = vector_of_matrix_to_array(filter.P)
169195

170196
function get_predictive_state_variance(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
171197
assert_possible_to_filter(model)
@@ -229,7 +255,7 @@ Returns the smoothed state `alpha` obtained with the smoother.
229255
"""
230256
function get_smoothed_state end
231257

232-
get_smoothed_state(smoother::SmootherOutput) = permutedims(cat(smoother.alpha...; dims=2))
258+
get_smoothed_state(smoother::SmootherOutput) = vector_of_vector_to_matrix(smoother.alpha)
233259

234260
function get_smoothed_state(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
235261
assert_possible_to_filter(model)
@@ -243,9 +269,9 @@ Returns the variance `V` of the smoothed state obtained with the smoother.
243269
"""
244270
function get_smoothed_state_variance end
245271

246-
get_smoothed_state_variance(smoother::SmootherOutput) = cat(smoother.V...; dims=3)
272+
get_smoothed_state_variance(smoother::SmootherOutput) = vector_of_matrix_to_array(smoother.V)
247273

248274
function get_smoothed_state_variance(model::StateSpaceModel; filter::KalmanFilter=default_filter(model))
249275
assert_possible_to_filter(model)
250276
return get_smoothed_state_variance(kalman_smoother(model; filter=filter))
251-
end
277+
end

0 commit comments

Comments
 (0)