Skip to content

Commit

Permalink
fixed cholesky error in simulate (#314)
Browse files Browse the repository at this point in the history
* fixed cholesky error in simulate

* created cholesky_decomposition function

* increase rounding digits

* updated version in Project.toml
  • Loading branch information
dietzemarina authored Aug 4, 2022
1 parent 407f051 commit 1dfc0c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StateSpaceModels"
uuid = "99342f36-827c-5390-97c9-d7f9ee765c78"
authors = ["raphaelsaavedra <raphael.saavedra93@gmail.com>, guilhermebodin <guilherme.b.moraes@gmail.com>, mariohsouto"]
version = "0.6.4"
version = "0.6.5"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
2 changes: 1 addition & 1 deletion src/models/basicstructural_explanatory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function simulate(
alpha = Matrix{Fl}(undef, n + 1, m)
# Sampling errors
chol_H = sqrt(sys.H[1])
chol_Q = cholesky(sys.Q[1])
chol_Q = cholesky_decomposition(sys.Q[1])
standard_ε = randn(n)
standard_η = randn(n + 1, size(sys.Q[1], 1))

Expand Down
21 changes: 19 additions & 2 deletions src/systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,23 @@ function to_multivariate_time_variant(system::LinearUnivariateTimeVariant{Fl}) w
end

to_multivariate_time_variant(system::LinearMultivariateTimeVariant) = system
ispossemdef(M::Matrix{Fl}) where Fl = all(i -> i >= 0.0, eigvals(M))

function cholesky_decomposition(M::Matrix{Fl}) where Fl
if isposdef(M)
return cholesky(M)
elseif ispossemdef(M)
size_matrix = size(M, 1)
chol_M = cholesky(M .+ I(size_matrix) .* floatmin(Fl))
chol_M.L[:, :] = round.(chol_M.L; digits = 10)
chol_M.U[:, :] = round.(chol_M.U; digits = 10)
chol_M.UL[:, :] = round.(chol_M.UL; digits = 10)

return chol_M
else
@error("Matrix is not positive definite or semidefinite. Cholesky decomposition cannot be performed.")
end
end

# Functions for simulations
function simulate(
Expand All @@ -325,7 +342,7 @@ function simulate(
alpha = Matrix{Fl}(undef, n + 1, m)
# Sampling errors
chol_H = sqrt(sys.H)
chol_Q = cholesky(sys.Q)
chol_Q = cholesky_decomposition(sys.Q)
standard_ε = randn(n)
standard_η = randn(n + 2, size(sys.Q, 1))

Expand Down Expand Up @@ -357,7 +374,7 @@ function simulate(
alpha = Matrix{Fl}(undef, n + 1, m)
# Sampling errors
chol_H = cholesky(sys.H)
chol_Q = cholesky(sys.Q)
chol_Q = cholesky_decomposition(sys.Q)
standard_ε = randn(n, size(sys.H, 1))
standard_η = randn(n + 2, size(sys.Q, 1))

Expand Down

2 comments on commit 1dfc0c2

@raphaelsaavedra
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/65631

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.5 -m "<description of version>" 1dfc0c25cd3fce52657ae3ce785bd8e8f481b69b
git push origin v0.6.5

Please sign in to comment.