I have to build a loop where each iteration consists in building a function to minimize. At each iteration, the number of arguments is variable. To start, I have two matrices:
X=rand(150,100)
Y=rand(150,1)
At each iteration I select randomly some columns of X
to build my function.
k=sample(1:size(X,2))
m=sample(1:size(X,2),k,replace=false)
Let’s say that k=6
columns were selected. I have then to build a function with k=6
arguments:
using Optim
Xsub=X[:,m]
h(beta1,beta2,beta3,beta4,beta5,beta6)=-sum(Y.*Xsub*[beta1,beta2,beta3,beta4,beta5,beta6])+
sum(log.(1 .+exp.(Xsub*[beta1,beta2,beta3,beta4,beta5,beta6])))+(size(Xsub,2)+1)/2*log(2*pi)+1/2*sum([beta1^2,beta2^2,beta3^2,beta4^2,beta5^2,beta6^2])
f(betas) = h(betas...)
betasestim=optimize(f,zeros(size(Xsub,2)),method = ConjugateGradient(),autodiff = :forward,x_tol = 1e-7).minimizer
That’s an example for one particular iteration. But how can I do to build such a function for each iteration, provided that the number k
of selected columns is different at each time as they are randomly selected. How can I do to create the arguments beta1, beta2, ..., betak
as they are different at each time? I also tried an approach of creating a unique function that will be called at each iteration, such as:
function funcoptim(arguments)
end
where arguments
can vary every time the function is called inside of the loop, but I’m stuck…