[R] Constrained Optimization

Animesh rmails at quantanalysis.in
Thu Jun 16 10:25:27 CEST 2011


I am trying to optimize a simple function, but I want to optimize it for
values of k between 0 and 1. 

I read the help file for constrOptim function but it is always giving me
the error
> constrOptim((0.4),simulation,NULL,ui=rbind(c(0,0),c(1,1)))

"Error in ui %*% theta : non-conformable arguments"

My starting value is 0.4...i also tried c(0.4). simulation is the function name pasted below (It returns a scalar, as per the requirements of the constrOptim call). 
ui is the bound (0,0) to (1,1)

What am I doing wrong?


Thanks in advance!



> simulation
function(k)
{
sum = 0
firstnum<-runif(1)

if(firstnum > k)
{
	sum<-sum + firstnum
}
else
{
	sum<-sum + firstnum+runif(1)
}

if(sum>1)
{
	sum<-0
}

return (sum)
}



More information about the R-help mailing list