Skip to content

Commit

Permalink
robust findiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore Kockerols authored and Thore Kockerols committed Nov 29, 2024
1 parent 8a0a4fc commit e754614
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions test/test_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,18 @@ if !test_higher_order

back_grad = Zygote.gradient(x-> get_loglikelihood(model, simulated_data(observables, :, :simulate), x, verbose = true), model.parameter_values)

fin_grad = FiniteDifferences.grad(FiniteDifferences.central_fdm(4,1),x-> get_loglikelihood(model, simulated_data(observables, :, :simulate), x, verbose = true), model.parameter_values)
# fin_grad = FiniteDifferences.grad(FiniteDifferences.central_fdm(4,1),x-> get_loglikelihood(model, simulated_data(observables, :, :simulate), x, verbose = true), model.parameter_values)

@test isapprox(back_grad[1], fin_grad[1], rtol = 1e-6)
for i in 1:100
local fin_grad = FiniteDifferences.grad(FiniteDifferences.central_fdm(4,1),x-> get_loglikelihood(model, simulated_data(observables, :, :simulate), x, verbose = true), model.parameter_values)
if isfinite(ℒ.norm(fin_grad))
println("Finite differences worked after $i iterations")
@test isapprox(back_grad[1], fin_grad[1], rtol = 1e-6)
break
end
end

# @test isapprox(back_grad[1], fin_grad[1], rtol = 1e-6)

write_to_dynare_file(Backus_Kehoe_Kydland_1992)
translate_dynare_file("Backus_Kehoe_Kydland_1992.mod")
Expand Down

0 comments on commit e754614

Please sign in to comment.