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

fix Julia 1.7 tests #85

Merged
merged 6 commits into from
Dec 31, 2021
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
4 changes: 2 additions & 2 deletions src/laplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ end
function _newton_inner_loop(dist_y_given_f, ys, K; f_init, maxiter, callback=nothing)
@assert maxiter >= 1
f = f_init
cache = nothing
local cache
for i in 1:maxiter
@debug " - Newton iteration $i: f[1:3]=$(f[1:3])"
fnew, cache = _newton_step(dist_y_given_f, ys, K, f)
Expand All @@ -232,7 +232,7 @@ function _newton_inner_loop(dist_y_given_f, ys, K; f_init, maxiter, callback=not
f = fnew
end
end
return f, something(cache)
return f, cache
end

function ChainRulesCore.frule(Δargs, ::typeof(_newton_inner_loop), args...; kwargs...)
Expand Down
23 changes: 14 additions & 9 deletions test/laplace.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@testset "laplace" begin
function generate_data()
Random.seed!(1)
X = range(0, 23.5; length=48)
fs = @. 3 * sin(10 + 0.6X) + sin(0.1X) - 1
# invlink = normcdf
invlink = logistic
ps = invlink.(fs)
Y = [rand(Bernoulli(p)) for p in ps]
# The random number generator changed in 1.6->1.7. The following vector was generated in Julia 1.6.
# The generating code below is only kept for illustrative purposes.
#! format: off
Y = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
st-- marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't we just use rng=MersenneTwister() or whatever?

#! format: on
# Random.seed!(1)
# fs = @. 3 * sin(10 + 0.6X) + sin(0.1X) - 1
# # invlink = normcdf
# invlink = logistic
# ps = invlink.(fs)
# Y = [rand(Bernoulli(p)) for p in ps]
return X, Y
end

Expand Down Expand Up @@ -96,7 +101,7 @@
end
fd_grad = only(FiniteDifferences.grad(central_fdm(5, 1), objective, theta0))
ad_grad = only(Zygote.gradient(objective, theta0))
@test ad_grad ≈ fd_grad
@test ad_grad ≈ fd_grad rtol = 1e-6
end

@testset "_newton_inner_loop derivatives not defined" begin
Expand Down Expand Up @@ -185,7 +190,7 @@

@testset "optimization" begin
X, Y = generate_data()
theta0 = [0.0, 1.0]
theta0 = [5.0, 1.0]

@testset "reference optimum" begin
function objective(theta)
Expand All @@ -199,7 +204,7 @@
res = Optim.optimize(objective, theta0, NelderMead())
#@info res

@test res.minimizer ≈ expected_thetahat
@test res.minimizer ≈ expected_thetahat rtol = 1e-4
end

@testset "gradient-based" begin
Expand Down