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

Better treatment for numerical errors #330

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 <[email protected]>, guilhermebodin <[email protected]>, mariohsouto"]
version = "0.6.8"
version = "0.6.9"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
12 changes: 9 additions & 3 deletions src/filters/multivariate_kalman_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,15 @@ function update_P!(
end

function update_llk!(kalman_state::MultivariateKalmanState{Fl}) where Fl
kalman_state.llk -=
HALF_LOG_2_PI + 0.5 * (logdet(kalman_state.F) +
kalman_state.v' * inv(kalman_state.F) * kalman_state.v)
try
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (logdet(kalman_state.F) +
kalman_state.v' * inv(kalman_state.F) * kalman_state.v)
)
catch
@error("Numerical error in the log-likelihood calculation. F = $(kalman_state.F), v = $(kalman_state.v). det(F) can only be positive.")
rethrow()
end
return kalman_state
end

Expand Down
11 changes: 8 additions & 3 deletions src/filters/regression_kalman_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ function regression_update_v!(
end

function update_llk!(kalman_state::RegressionKalmanState{Fl}) where Fl
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
try
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
catch
@error("Numerical error in the log-likelihood calculation. F = $(kalman_state.F), v = $(kalman_state.v). F can only be positive.")
rethrow()
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if this is any different than just

catch e
     @error("Numerical error... :, $e)

But looks good to me!

Copy link
Member Author

Choose a reason for hiding this comment

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

this one does not stop the program, only logs to screen

julia> try; log(-10); catch; @error("oi"); rethrow() end
┌ Error: oi
└ @ Main REPL[102]:1
ERROR: DomainError with -10.0:
log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(f::Symbol, x::Float64)
   @ Base.Math .\math.jl:33
 [2] _log
   @ .\special\log.jl:295 [inlined]
 [3] log(x::Float64)
   @ Base.Math .\special\log.jl:261
 [4] log(x::Int64)
   @ Base.Math .\math.jl:1531
 [5] top-level scope
   @ REPL[102]:1

julia> try; log(-10); catch ex; @error("oi", ex) end
┌ Error: oi
│   ex =
│    DomainError with -10.0:
│    log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).
└ @ Main REPL[103]:1

Copy link
Member

Choose a reason for hiding this comment

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

Ah, it's been a while since I programmed in julia... forgot @error just logs! Thanks

end
return kalman_state
end

Expand Down
11 changes: 8 additions & 3 deletions src/filters/scalar_kalman_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ function scalar_update_P!(kalman_state::ScalarKalmanState{Fl}, T::Fl, RQR::Fl) w
end

function update_llk!(kalman_state::ScalarKalmanState{Fl}) where Fl
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
try
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
catch
@error("Numerical error in the log-likelihood calculation. F = $(kalman_state.F), v = $(kalman_state.v). F can only be positive.")
rethrow()
end
return kalman_state
end

Expand Down
11 changes: 8 additions & 3 deletions src/filters/sparse_univariate_kalman_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ function update_P!(
end

function update_llk!(kalman_state::SparseUnivariateKalmanState{Fl}) where Fl
kalman_state.llk -= (
HALF_LOG_2_PI + (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F) / 2
)
try
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
catch
@error("Numerical error in the log-likelihood calculation. F = $(kalman_state.F), v = $(kalman_state.v). F can only be positive.")
rethrow()
end
return kalman_state
end

Expand Down
11 changes: 8 additions & 3 deletions src/filters/univariate_kalman_filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,14 @@ function update_P!(
end

function update_llk!(kalman_state::UnivariateKalmanState{Fl}) where Fl
kalman_state.llk -= (
HALF_LOG_2_PI + (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F) / 2
)
try
kalman_state.llk -= (
HALF_LOG_2_PI + 0.5 * (log(kalman_state.F) + kalman_state.v^2 / kalman_state.F)
)
catch
@error("Numerical error in the log-likelihood calculation. F = $(kalman_state.F), v = $(kalman_state.v). F can only be positive.")
rethrow()
end
return kalman_state
end

Expand Down
Loading