# Sanity check with optimization function

**URL:** <https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836>\
**Category:** New to Julia\
**Created:** [December 18, 2017, 4:23pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836 "2017-12-18T16:23:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 18, 2017, 4:23pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836/1 "2017-12-18T16:23:32Z")

</div>

Hey I have a quick question about my workflow with a problem.  
I am writing a little logistic regression function for Andrew Ng’s ML course on coursera. I have `X` and `y`, where y is 0 or 1. I want to make sure that the scopes I am using are correct

```julia
function logit(X, y)
    function costFunction(theta)
        cost = f(theta, X, y) # X and y are only available to this function, so they are type stable
        return cost
    end

theta_0 = [0, 0, 0] # X has width of 3, for example
theta = optimize(costFunction, theta_0) # not the complete code or correct syntax
return theta

```

I am writing this because the course is done in Octave / Matlab. There, the syntax is

```julia
[theta, cost] = ...
    fminunc(@(t)(costFunction(t, X, y)), initial theta, options);

```

In Julia, it seems that Optim.jl and JuMP don’t allow your function to take in _anything_ other than your parameters of interest, so having `X` and `y` in the cost function wouldn’t work. This has to do with the way Julia optimizes for the types of variables, so my procedure above is the Julian way of working with this problem.  
Is my understanding correct?

PS perhaps this should be in gitter? I just want to ensure I have the right understanding.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [December 18, 2017, 4:28pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836/2 "2017-12-18T16:28:14Z")

</div>

> [@pdeffebach](#):
>
> In Julia, it seems that Optim.jl and JuMP don’t allow your function to take in anything other than your parameters of interest, so having X and y in the cost function wouldn’t work

If you look at your matlab code you’ll see that it does so as well. The line `@(t)(...)` is an anonymous function of just the parameters to optimize. In julia you could write it as `t->constFunction(t, X, y)`

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 18, 2017, 4:28pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836/3 "2017-12-18T16:28:52Z")

</div>

This looks good. This “building another function that encloses data” (here, `costFunction` which encloses `X` and `y`) is usually called a closure. You can also much more simply use an anonymous funciton: `theta->f(theta,X,y)`. Julia compiles the closures so you don’t need to worry about speed with this setup, it’s basically just an interface change.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [December 18, 2017, 4:34pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836/4 "2017-12-18T16:34:11Z")

</div>

Note that in Julia you can define functions by assignment and returns are implied, so a somewhat more compact version of your code might be

```julia
function logit(X, y)
    costfunction(θ) = f(θ, X, y)
    θ₀ = zeros(Float64, 3)
    optimize(costfunction, θ₀)
end 

```

in case you were interested in what common practice in the language is like.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 18, 2017, 4:57pm UTC](https://discourse.julialang.org/t/sanity-check-with-optimization-function/7836/5 "2017-12-18T16:57:04Z")

</div>

Thanks all for the help! Julia is definitely more transparent than Matlab on this stuff.
