This function conducts an integer golden search minimisation of a univariate function.

goldopt(fn, interval, tol = 1)

Arguments

fn

function to be minimised. fn should return a list, with fval as the function value.

interval

a vector of length two containing the minimum and maximum interger values within which to search for the minimiser.

tol

the tolerance level. Defaults at 1

Value

k minimiser of fn()

crit the minimum

iter total number of iterations

iterfn total number of function evaluations of fn()

fobj an object of the function minimisation

key a logical for warning if fobj may not correspond to k

Examples

set.seed(14) #Generate data N = 1000; (bets = rep(-2:2,4)); p = length(bets); X = matrix(rnorm(N*p),N,p)
#> [1] -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2
Y = cbind(1,X)%*%matrix(c(0.5,bets),ncol = 1) fn=function(k){du=CCRls.coord(Y,X,k=k,nC=1) return(list(fval=BIC(du$mobj),obj=du))} goldopt(fn=fn,interval=c(2,7),tol=1)
#> Inner iter = 1, RSS = 960.65936, and reldev = -0.01256 #> Inner iter = 1, RSS = 1746.15365, and reldev = -0.84049 #> Inner iter = 1, RSS = 660.88739, and reldev = 0.30341 #> Inner iter = 2, RSS = 660.88739, and reldev = 0.00000 #> Inner iter = 1, RSS = 660.88739, and reldev = 0.30341 #> Inner iter = 2, RSS = 660.88739, and reldev = 0.00000
#> iter = 1
#> $k #> [1] 5 #> #> $crit #> [1] -63095.73 #> #> $iter #> [1] 1 #> #> $iterfn #> [1] 4 #> #> $fobj #> $fobj$fval #> [1] -63095.73 #> #> $fobj$obj #> $fobj$obj$mobj #> #> Call: #> stats::lm(formula = Y ~ as.matrix(cbind(Xnc, X1)[, -1])) #> #> Coefficients: #> (Intercept) as.matrix(cbind(Xnc, X1)[, -1]) #> 5.00e-01 -2.00e+00 #> as.matrix(cbind(Xnc, X1)[, -1]) as.matrix(cbind(Xnc, X1)[, -1]) #> -1.00e+00 4.01e-16 #> as.matrix(cbind(Xnc, X1)[, -1]) as.matrix(cbind(Xnc, X1)[, -1]) #> 1.00e+00 2.00e+00 #> #> #> $fobj$obj$clus #> [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 #> #> #> #> $key #> [1] 0 #>