Skip to content

Commit

Permalink
adjust tol on find shocks, no early termination
Browse files Browse the repository at this point in the history
  • Loading branch information
thorek1 committed Oct 26, 2024
1 parent c19f969 commit 5929e92
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/filter/find_shocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function find_shocks(::Val{:LagrangeNewton},
𝐒ⁱ²ᵉ::AbstractMatrix{Float64},
shock_independent::Vector{Float64};
max_iter::Int = 1000,
tol::Float64 = 1e-13) # will fail for higher or lower precision
tol::Float64 = 1e-14) # will fail for higher or lower precision
x = copy(initial_guess)

λ = zeros(size(𝐒ⁱ, 1))
Expand Down Expand Up @@ -102,15 +102,15 @@ function find_shocks(::Val{:LagrangeNewton},

.axpby!(1, shock_independent, -1, x̂)

if.norm(x̂) / max(norm1,norm2) < eps() &&.norm(Δxλ) /.norm(xλ) < tol
if.norm(x̂) / max(norm1,norm2) < tol &&.norm(Δxλ) /.norm(xλ) < sqrt(tol)
# println("LagrangeNewton: $i, Tol reached, $x")
break
end

if i > 5 &&.norm(Δxλ) > 1e-12 &&.norm(Δxλ) > Δnorm
# println("LagrangeNewton: $i, Norm increase")
return x, false
end
# if i > 500 && ℒ.norm(Δxλ) > 1e-11 && ℒ.norm(Δxλ) > Δnorm
# # println("LagrangeNewton: $i, Norm increase")
# return x, false
# end
# # if i == max_iter
# println("LagrangeNewton: $i, Max iter reached")
# println(ℒ.norm(Δxλ) / ℒ.norm(xλ))
Expand All @@ -121,7 +121,7 @@ function find_shocks(::Val{:LagrangeNewton},
# println("Norm: $(ℒ.norm(x̂) / max(norm1,norm2))")
# println(ℒ.norm(Δxλ))
# println(ℒ.norm(Δxλ) / ℒ.norm(xλ))
return x, ℒ.norm(x̂) / max(norm1,norm2) < tol &&.norm(Δxλ) /.norm(xλ) < tol
return x, ℒ.norm(x̂) / max(norm1,norm2) < tol &&.norm(Δxλ) /.norm(xλ) < sqrt(tol)
end


Expand All @@ -136,7 +136,7 @@ function rrule(::typeof(find_shocks),
𝐒ⁱ²ᵉ::AbstractMatrix{Float64},
shock_independent::Vector{Float64};
max_iter::Int = 1000,
tol::Float64 = 1e-13)
tol::Float64 = 1e-14)

x, matched = find_shocks(Val(:LagrangeNewton),
initial_guess,
Expand Down Expand Up @@ -202,7 +202,7 @@ function find_shocks(::Val{:LagrangeNewton},
𝐒ⁱ³ᵉ::AbstractMatrix{Float64},
shock_independent::Vector{Float64};
max_iter::Int = 1000,
tol::Float64 = 1e-13) # will fail for higher or lower precision
tol::Float64 = 1e-14) # will fail for higher or lower precision
x = copy(initial_guess)

λ = zeros(size(𝐒ⁱ, 1))
Expand Down Expand Up @@ -307,19 +307,19 @@ function find_shocks(::Val{:LagrangeNewton},

.axpby!(1, shock_independent, -1, x̂)

if.norm(x̂) / max(norm1,norm2) < eps() &&.norm(Δxλ) /.norm(xλ) < tol
if.norm(x̂) / max(norm1,norm2) < tol &&.norm(Δxλ) /.norm(xλ) < sqrt(tol)
# println("LagrangeNewton: $i, Tol reached")
break
end

if i > 5 &&.norm(Δxλ) > 1e-12 &&.norm(Δxλ) > Δnorm
# println(ℒ.norm(Δxλ))
# println(ℒ.norm(x̂) / max(norm1,norm2))
# println("LagrangeNewton: $i, Norm increase")
return x, false
end
# if i > 500 && ℒ.norm(Δxλ) > 1e-11 && ℒ.norm(Δxλ) > Δnorm
# # println(ℒ.norm(Δxλ))
# # println(ℒ.norm(x̂) / max(norm1,norm2))
# # println("LagrangeNewton: $i, Norm increase")
# return x, false
# end
# if i == max_iter
# println("LagrangeNewton: $i, Max iter reached")
# println("LagrangeNewton: $i, Max iter reached")
# # println(ℒ.norm(Δxλ))
# end
end
Expand All @@ -341,7 +341,7 @@ function find_shocks(::Val{:LagrangeNewton},
# println("Norm: $(ℒ.norm(x̂) / max(norm1,norm2))")
# println(ℒ.norm(Δxλ))
# println(ℒ.norm(x̂) / max(norm1,norm2) < tol && ℒ.norm(Δxλ) / ℒ.norm(xλ) < tol)
return x, ℒ.norm(x̂) / max(norm1,norm2) < tol &&.norm(Δxλ) /.norm(xλ) < tol
return x, ℒ.norm(x̂) / max(norm1,norm2) < 1e-14 &&.norm(Δxλ) /.norm(xλ) < tol
end


Expand All @@ -362,7 +362,7 @@ function rrule(::typeof(find_shocks),
𝐒ⁱ³ᵉ::AbstractMatrix{Float64},
shock_independent::Vector{Float64};
max_iter::Int = 1000,
tol::Float64 = 1e-13)
tol::Float64 = 1e-14)

x, matched = find_shocks(Val(:LagrangeNewton),
initial_guess,
Expand Down

0 comments on commit 5929e92

Please sign in to comment.