Keywords - Function groups - @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Library: nummath
See also: nmgraditer nmgraddiff nmcongrad nmlinminappr

Quantlet: nmBFGS
Description: Broyden-Fletcher-Goldfarb-Shanno method to find a minimum of a given function.

Usage: min = nmBFGS(func,x0{,fder,linmin,ftol,gtol,maxiter,nowarn}) or min = nmBFGS(func,x0{,fder,opt})
Input:
func string, name of the function to minimize. The function should have just one parameter x, which is either a n x 1 vector, if fder is given (see below), or a n x k matrix (for any k >= 1) containing x = (x1,x2,...,xk), if the gradient is to be computed automatically; xi (n x 1 vector; i=1..k) represent points at which the function should be evaluated. As a result, the function should return a scalar.
x0 n x 1 vector, the initial estimate of the minimum
fder (optional) derivatives of func; there are several possible formats: 1. string, name of the function for computing the gradient of func; its output is a n x 1 vector 2. empty string; in this case the gradient will be computed automatically using the quantlet nmgraddiff with a default step h 3. scalar h; the gradient will be computed automatically using the quantlet nmgraddiff with the given step h
opt (optional) list containing all or some of the following items: linmin, ftol, maxiter and nowarn as described below
linmin (optional) string, name of the routine for 1D (line) minimization; default is linmin = "nmlinminappr"
ftol (optional) scalar, reserved for future usage; convergence tolerance of the function value, default is ftol = 1e-7
gtol (optional) scalar, convergence tolerance of the value of the function gradient; default is gtol = 1e-9
maxiter (optional) scalar, maximal number of iterations; default is maxiter = 250
nowarn (optional) scalar; by default, nowarn = 0. If nowarn is set to a nonzero value, no warnings will be shown and nowarn = 1 will be set to 1 for quantlets called by nmBFGS having this option
Output:
min.xmin n x 1 vector, minimum of func (isolated to a fractional precision of tol)
min.fmin scalar, minimal function value f(xmin)
min.iter scalar, number of performed iterations

Example:
library("nummath")
;
; definition of function
;
proc(p)=ftion(x)
  p = x[1,]^2 + 3*(x[2,]-1)^4
endp
;
nmBFGS("ftion",#(1,2))
;
; search for a minimum of f=x1^2 + 3*(x2-1)^4 with
; initial estimation x0=(1,2)

Result:
Contents of _tmp.xmin
[1,]  2.1573e-21
[2,]  0.99967
Contents of _tmp.fmin
[1,]  3.6672e-14
Contents of _tmp.iter
[1,]       25
Example:
library("nummath")
;
; variables: simulated probit
;
randomize(1)
n    = 1000
x    = normal(n,2)
eps  = normal(n)
x1   = matrix(n)~x
beta = #(0.5,1,-2)
y    = x1 * beta + eps
yd   =(y >= 0)
;
; definition of probit likelihood function
; L = log prod(cdfn(x1*beta)^yd *(1-cdfn(x1*beta))^(1-yd))
;		= sum log(...)
; for numerical reasons, we will define it rather as follows:
;
proc(L)=ftion(beta)
  yd = getglobal("yd")
  x1 = getglobal("x1")
  L1 = log((cdfn(x1*beta).*yd) +(1-yd))
  L2 = log(((1-cdfn(x1*beta)).*(1-yd)) + yd)
  L  = - sum(L1) - sum(L2)
  ; maximum of likelihood = minimum of L
endp
;
min = nmBFGS("ftion",#(0,0,0))
min
; notice that ML(beta=min.xmin) = -min.fmin

Result:
Contents of min.xmin
[1,]  0.53581
[2,]  0.93027
[3,]  -2.0261
Contents of min.fmin
[1,]    288.3
Contents of min.iter
[1,]       12
Example:
; shows inputing optional parameters as a list
library("nummath")
; definition of function
proc(p)=ftion(x)
  p = x[1,]^4 + 5*(x[2,]-7)^2 + 8
endp
;
gtol = 1e-5
maxiter = 15
opt = list(gtol,maxiter)
nmBFGS("ftion",#(1,2),"",opt)
;
; search for a minimum of f=x1^4 + 5*(x2-7)^2 + 8
; with initial estimation x0=(1,2)

Result:
Contents of _tmp.xmin
[1,] -0.012726
[2,]        7

Contents of _tmp.fmin
[1,]        8

Contents of _tmp.iter
[1,]       13



Author: L. Cizkova, 20021011 license MD*Tech
(C) MD*TECH Method and Data Technologies, 05.02.2006