Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed cholesky error in simulate #314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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