Skip to content

Commit 1dfc0c2

Browse files
authored
fixed cholesky error in simulate (#314)
* fixed cholesky error in simulate * created cholesky_decomposition function * increase rounding digits * updated version in Project.toml
1 parent 407f051 commit 1dfc0c2

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StateSpaceModels"
22
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
33
authors = ["raphaelsaavedra <[email protected]>, guilhermebodin <[email protected]>, mariohsouto"]
4-
version = "0.6.4"
4+
version = "0.6.5"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

src/models/basicstructural_explanatory.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function simulate(
192192
alpha = Matrix{Fl}(undef, n + 1, m)
193193
# Sampling errors
194194
chol_H = sqrt(sys.H[1])
195-
chol_Q = cholesky(sys.Q[1])
195+
chol_Q = cholesky_decomposition(sys.Q[1])
196196
standard_ε = randn(n)
197197
standard_η = randn(n + 1, size(sys.Q[1], 1))
198198

src/systems.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,23 @@ function to_multivariate_time_variant(system::LinearUnivariateTimeVariant{Fl}) w
312312
end
313313

314314
to_multivariate_time_variant(system::LinearMultivariateTimeVariant) = system
315+
ispossemdef(M::Matrix{Fl}) where Fl = all(i -> i >= 0.0, eigvals(M))
316+
317+
function cholesky_decomposition(M::Matrix{Fl}) where Fl
318+
if isposdef(M)
319+
return cholesky(M)
320+
elseif ispossemdef(M)
321+
size_matrix = size(M, 1)
322+
chol_M = cholesky(M .+ I(size_matrix) .* floatmin(Fl))
323+
chol_M.L[:, :] = round.(chol_M.L; digits = 10)
324+
chol_M.U[:, :] = round.(chol_M.U; digits = 10)
325+
chol_M.UL[:, :] = round.(chol_M.UL; digits = 10)
326+
327+
return chol_M
328+
else
329+
@error("Matrix is not positive definite or semidefinite. Cholesky decomposition cannot be performed.")
330+
end
331+
end
315332

316333
# Functions for simulations
317334
function simulate(
@@ -325,7 +342,7 @@ function simulate(
325342
alpha = Matrix{Fl}(undef, n + 1, m)
326343
# Sampling errors
327344
chol_H = sqrt(sys.H)
328-
chol_Q = cholesky(sys.Q)
345+
chol_Q = cholesky_decomposition(sys.Q)
329346
standard_ε = randn(n)
330347
standard_η = randn(n + 2, size(sys.Q, 1))
331348

@@ -357,7 +374,7 @@ function simulate(
357374
alpha = Matrix{Fl}(undef, n + 1, m)
358375
# Sampling errors
359376
chol_H = cholesky(sys.H)
360-
chol_Q = cholesky(sys.Q)
377+
chol_Q = cholesky_decomposition(sys.Q)
361378
standard_ε = randn(n, size(sys.H, 1))
362379
standard_η = randn(n + 2, size(sys.Q, 1))
363380

0 commit comments

Comments
 (0)