# Loop for minimizing functions with a varying number of arguments

**URL:** https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816
**Category:** General Usage
**Tags:** optim, optimization, loops, function
**Created:** [July 10, 2020, 1:33am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816 "2020-07-10T01:33:39Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 1:33am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/1 "2020-07-10T01:33:39Z")

</div>

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:

```julia

X=rand(150,100)
Y=rand(150,1)

```

At each iteration I select randomly some columns of `X` to build my function.

```julia

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:

```julia
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:

```julia
function funcoptim(arguments)

end

```

where `arguments` can vary every time the function is called inside of the loop, but I’m stuck…

---

<div class="post-metadata">

### Author: ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)
#### Post date: [July 10, 2020, 1:43am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/2 "2020-07-10T01:43:41Z")

</div>

Look into macros.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 1:46am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/3 "2020-07-10T01:46:33Z")

</div>

@Joris_Pinkse Into macros? 🤔

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [July 10, 2020, 2:15am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/4 "2020-07-10T02:15:26Z")

</div>

~~Why not pass the vector of arguments as a single argument.~~

Ok myabe not, why not splatting the arguments?

```julia
h(betas...) = -sum(Y .* Xsub .* betas) + sum(log.(1 .+ exp.(Xsub .* betas))) 
              + (size(Xsub, 2)+1)/2*log(2*pi) + 1/2 * sum(betas .^ 2)

```

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 2:23am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/5 "2020-07-10T02:23:12Z")

</div>

Yes, but at each iteration the vector of arguments is different. For exemple, if `k=6` I have the vector `beta1,beta2,beta3,beta4,beta5,beta6` but if `k=50` the vector wil be `beta1,beta2, ..., beta50` (I don’t know how to create an argument with so many variables btw… ☹ ). I need all the components of the vector to appear as they are the arguments to minimize the function.

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [July 10, 2020, 2:49am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/6 "2020-07-10T02:49:55Z")

</div>

yeah sorry I realised that while you were typing. See my edited answer: use splats

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 2:55am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/7 "2020-07-10T02:55:26Z")

</div>

Yes I just saw! I’ll try your suggestion and keep you in touch! Just one detail: I must multiply each row of `Xsub` with `betas`. It’s a matrix multiplication and not an element-wise multiplication. It shoud be `Xsub*betas` instead of `Xsub .* betas` as I had in my code.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 3:53am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/8 "2020-07-10T03:53:05Z")

</div>

It does not work ☹ I get a `ERROR: MethodError: no method matching exp(::Array{Float64,1})` error. Even with a simple case as

```julia
h(betas...)=(betas .- 1)^2
optimize(h,zeros(size(Xsub,2)),method = ConjugateGradient(),autodiff = :forward,x_tol = 1e-7).minimizer

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 10, 2020, 3:56am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/9 "2020-07-10T03:56:39Z")

</div>

use

```julia
h(betas...)=(betas .- 1) .^ 2

```

Are you really sure you need your function to have `k` arguments though? I seriously think you should be using a vector for this.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 4:04am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/10 "2020-07-10T04:04:16Z")

</div>

Yes… The number of arguments changes at each iteration. The number of arguments is indicated by `k`. And it doesn’t work even with `h(betas...)=(betas .- 1) .^ 2`. I get `ERROR: MethodError: no method matching -(::Array{Float64,1}, ::Int64)`.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 10, 2020, 4:10am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/11 "2020-07-10T04:10:33Z")

</div>

> I get `ERROR: MethodError: no method matching -(::Array{Float64,1}, ::Int64)` .

Then look for the line (it should be indicated in your stacktrace) where that error come from. There’s probabably a `v - 1` that should be `v .- 1` for some `v::Vector`.

> Yes… The number of arguments changes at each iteration. The number of arguments is indicated by `k` .

Does it need to be that way though? I still don’t understand why you can’t just have a function of a single vector and have the length of that vector change in each iteration? This will likely be a lot more efficient than writing a function of 50 variables (in terms of compile time, runtime and writing time).

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 10, 2020, 4:14am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/12 "2020-07-10T04:14:46Z")

</div>

Ok I’ll check the error. Yes you’re right! It can be a vector whose length changes at each iteration. The important is to find the value of the vector which minimizes the function at each iteration because I need the minimizers.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 11, 2020, 12:03am UTC](https://discourse.julialang.org/t/loop-for-minimizing-functions-with-a-varying-number-of-arguments/42816/14 "2020-07-11T00:03:56Z")

</div>

Ah I think I’m close to the solution with the single vector with varying length you proposed!
