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

Error small Variance #24

Open
johannesgiersdorf opened this issue Apr 9, 2021 · 3 comments
Open

Error small Variance #24

johannesgiersdorf opened this issue Apr 9, 2021 · 3 comments

Comments

@johannesgiersdorf
Copy link
Collaborator

The estimated variance is sometimes to small, so the Normal distribution can't be constructed.

Error message

ERROR: LoadError: ArgumentError: Normal: the condition σ >= zero(σ) is not satisfied.
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/Distributions/cNe2C/src/utils.jl:6 [inlined]
 [2] #Normal#112
   @ ~/.julia/packages/Distributions/cNe2C/src/univariate/continuous/normal.jl:37 [inlined]
 [3] Normal::Float64, σ::Float64)
   @ Distributions ~/.julia/packages/Distributions/cNe2C/src/univariate/continuous/normal.jl:37
 [4] quadrature(bquad::BayesQuad{SqExponentialKernel, Float64, Float64}, model::BayesModel{ZeroMeanDiagNormal{Tuple{Base.OneTo{Int64}}}, typeof(log_f)}, samples::Vector{Vector{Float64}})
   @ BayesianQuadrature ~/.julia/packages/BayesianQuadrature/eNnqB/src/bayesquads/bayesquad.jl:58
 [5] (::BayesQuad{SqExponentialKernel, Float64, Float64})(rng::MersenneTwister, model::BayesModel{ZeroMeanDiagNormal{Tuple{Base.OneTo{Int64}}}, typeof(log_f)}, sampler::PriorSampling; x_init::Vector{Any}, nsamples::Int64, callback::Nothing)
   @ BayesianQuadrature ~/.julia/packages/BayesianQuadrature/eNnqB/src/interface.jl:10
 [6] top-level scope
   @ ~/Documents/uni/master/master-thesis-code-base/src/min_example_normal.jl:13
 [7] include(fname::String)
   @ Base.MainInclude ./client.jl:444
 [8] top-level scope
   @ REPL[8]:1

Example

using BayesianQuadrature
using Distributions
using KernelFunctions
using Random

rng = Random.MersenneTwister(42)

p_0 = MvNormal(ones(2)) # As for now the prior must be a MvNormal
log_f(x) = logpdf(MvNormal(0.5 * ones(2)), x) 
m = BayesModel(p_0, log_f) 
bquad = BayesQuad(SEKernel(); l=10.0, σ=1.0) 
sampler = PriorSampling() 
p_I, _ = bquad(rng, m, sampler; nsamples=200) # Returns a Normal distribution
@show p_I 
johannesgiersdorf pushed a commit that referenced this issue Apr 9, 2021
@johannesgiersdorf
Copy link
Collaborator Author

It is possible to create a Normal with no checks. It seems that the estimated sigma is sometimes negative. On first glace it seems this happens less often using the test environment. I tested different seeds in the unit test vs just calling it directly (where i got negative values for different seeds)

vals = [std(bquad(Random.MersenneTwister(i), m, sampler; nsamples=200)[1]) for i in 1:300] 
> sum(vals .<= 0)/length(vals) = 0.7866666666666666 #pakage environment
> sum(vals .<= 0)/length(vals) = 0.07333333333333333 #test environment

@johannesgiersdorf
Copy link
Collaborator Author

It seems to me to be a numerical error in evaluate_var. The easiest fix should be to take 0 instead of a negative value.

@johannesgiersdorf
Copy link
Collaborator Author

For now added a warning in case of numerical error #25

theogf added a commit that referenced this issue Apr 13, 2021
* fix negative values due to numerical error #24

* add warning in case of numerical error src/bayesquads/bayesquad.jl

Co-authored-by: Théo Galy-Fajou <[email protected]>

* add missing syntax

* Update src/bayesquads/bayesquad.jl

Co-authored-by: Théo Galy-Fajou <[email protected]>

Co-authored-by: Johannes Giersdorf <[email protected]>
Co-authored-by: Théo Galy-Fajou <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant