NR {lazy.mat} | R Documentation |
Newton-Raphson Method for Minimization
Description
Newton-Raphson Method for Minimization
Usage
NR(
x,
obj,
gradient = NULL,
hessian = NULL,
sd = 0,
flipd = 0,
maxiter = 100,
eps = 1e-07,
epsg = 1e-09,
epsx = 1e-09,
maxiter2 = 10,
normalize = NULL,
check_pm = 0,
print = 1,
...
)
Arguments
x |
initial parameter vector |
obj |
function to be minimized, the first arg of which must be x. |
gradient |
function which returns the gradient vector or NULL |
hessian |
function which returns the Hessian matrix or NULL |
sd |
= 1 to use steepest descent |
flipd |
change the sign of d if
|
maxiter |
max # of iterations |
eps |
eps for the relative change of the function value |
epsg |
eps for the mazimum absolute value of the gradient |
epsx |
eps for the maximum absolute change of the parameter value |
maxiter2 |
max # of stepsize halving |
normalize |
a function to normalize the solution after each iteration |
check_pm |
= 1 to verify partial match of the ... argument |
print |
= 1 to print the result, = 2 to print the history |
... |
additional parameters to obj, gradient and hessian |
Details
parameter values will be updated as
x_k+1 = x_k - alpha ginv(Hessian) grad
where alpha
is the stepsize to be halved, from 1,
when obj
increases.
When gradient or hessian are NULL,
JacobianMat
of this package
will be used to calculate them numerically.
See the examples in here ExamplesOfGNNR
.
Warning
When the formal arguments of this function partially match
the names given as ..., this function does NOT work.
In order to avoid this, if any of ... matches the formal arguments,
specify those formal arguments by their complete names
when calling this function.
Value
A list of:
par
the parameter value at minimum
objective
the function value minimized
grad
the gradient vector
hist
a matrix containing the iteration history
iterations
number of iterations performed
Examples
# function to be minimized
# x is the variable w.r.t. which f is minimized.
# a and b are the optional parameters of f.
#
symbolic_f <- expression( exp(a*x)/(sin(b*x^2)+x) )
# convert symbolic expression to numeric function with optional parameters
# eval(parse(text=paste("f <- function(x) ", symbolic_f)))
f <- function(x, a=1, b=1){eval(symbolic_f)}
# symbolic differentiation of f with optional parameters
symbolic_df2 <- deriv(symbolic_f,"x", hessian=TRUE
, function(x, a=0, b=0){} )
# convert symbolic expression to numeric function with optional parameters
df <- function(x, a=1, b=1){
attributes(symbolic_df2(x, a=a, b=b))$gradient[,1, drop=0] }
df2 <- function(x, a=1, b=1){
matrix(attributes(symbolic_df2(x, a=a, b=b))$hessian[,1,1]
, length(x),length(x)) }
# optional parameter to f
a=2; b=5
## Not run:
# shape of f
x=seq(1,3,0.01)
plot( x, f(x,a,b), type="l",main=paste(symbolic_f, " with a=",a,", b=",b) )
## End(Not run)
# initial value
xinit=2.65
#
# minimization of f by NR
#
# numerical gradient and Hessian
resNR1=NR( xinit, f, flipd=1, a=a,b=b, print=2 )
xNR1=resNR1$par
gNR1=df( xNR1, a=a, b=b )
# analytic gradient and numerical Hessian
resNR2=NR( xinit, f, flipd=1, gradient=df, a=a,b=b, print=2 )
xNR2=resNR2$par
gNR2=df( xNR2, a=a, b=b )
# analytic gradient and Hessian
resNR3=NR( xinit, f, flipd=1, gradient=df, hessian=df2, a=a,b=b, print=2 )
xNR3=resNR3$par
gNR3=df( xNR3, a=a, b=b )
printm(xNR1,xNR2,xNR3)
printm(gNR1,gNR2,gNR3)
## Not run:
#
# minimization of f by native nlminb
#
res1=nlminb( xinit, f, control=list(trace=0), a=a, b=b )
x1=res1$par
g1=df( x1, a=a, b=b )
res2=nlminb( xinit, f, control=list(trace=0), a=a, b=b, gradient=df )
x2=res2$par
g2=df( x2, a=a, b=b )
res3=nlminb( xinit, f, control=list(trace=0), a=a, b=b
, gradient=df, hessian=df2 )
x3=res3$par
g3=df( x3, a=a, b=b )
printm(x1,x2,x3)
printm(g1,g2,g3)
## End(Not run)