First steps with Julia
16 October 2014

I went back to Julia this week. I took a very cursory look at the documentation when I was entering the final leg of my dissertation a couple of years ago but I did not pay much attention to the project –learning another language was the last thing on my mind back then. However, this week I decided that it was about time to give it a proper try: I have been wanting to expand my toolset and Julia seems as good a candidate as any other, specially given its growing popularity and its impecable credentials. Because the language is still in its infancy, by now I will behave strategically and not make a big investment in it, but at least I want to know the basics so I can use it if I need to.

I am still taking baby steps, which is another way of saying that I am working through fairly trivial examples, just trying to make things work without worrying much about optimizing performance or about whether my code is idiomatic. That will come later.

The first thing I could think of was the simplest possible MLE problem: estimating the $\lambda$ parameter in a Poisson distribution.

using Distributions
using Optim

srand(2013)

λ = 3.2
a = rand(Poisson(λ), 100)

function poisson(μ::Float64)
    n = length(a)
    ll = map(x -> -μ + x*log(μ), a)
    return -sum(ll)
end

est = optimize(poisson, 0., 10.)

which results in

println(est.minimum)
3.340000048501796

which is, obviously, the same result one would get from the analytical derivation:

println(1/length(a)*sum(a))
3.34

The experience so far is being very enjoyable and I have to admit that, against all my original prejudices, I do like the idea of using Unicode for variable names.