Skip to content

Instantly share code, notes, and snippets.

@pat-alt
Created November 16, 2021 11:25
Show Gist options
  • Save pat-alt/26729245384a4f87e72f363f978e652e to your computer and use it in GitHub Desktop.
Save pat-alt/26729245384a4f87e72f363f978e652e to your computer and use it in GitHub Desktop.
Newton's method with Arminjo backtracking in Julia language.
# Newton's Method
function arminjo(𝓁, g_t, θ_t, d_t, args, ρ, c=1e-4)
𝓁(θ_t .+ ρ .* d_t, args...) <= 𝓁(θ_t, args...) .+ c .* ρ .* d_t'g_t
end
function newton(𝓁, θ, ∇𝓁, ∇∇𝓁, args; max_iter=100, τ=1e-5)
# Intialize:
converged = false # termination state
t = 1 # iteration count
θ_t = θ # initial parameters
# Descent:
while !converged && t<max_iter
global g_t = ∇𝓁(θ_t, args...) # gradient
global H_t = ∇∇𝓁(θ_t, args...) # hessian
converged = all(abs.(g_t) .< τ) && isposdef(H_t) # check first-order condition
# If not converged, descend:
if !converged
d_t = -inv(H_t)*g_t # descent direction
# Line search:
ρ_t = 1.0 # initialize at 1.0
count = 1
while !arminjo(𝓁, g_t, θ_t, d_t, args, ρ_t)
ρ_t /= 2
end
θ_t = θ_t .+ ρ_t .* d_t # update parameters
end
t += 1
end
# Output:
return θ_t, H_t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment