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