2

I have a log-likelihood function and I want to maximize it in respect to theta (N), and it is defined as:

 function loglik(theta,n,r)
    N=theta;k=length(n);
    ar1=float(lgamma(N+1));ar2=sum(n)*log(sum(n)/(k*N));ar3=(k*N-sum(n))*log(1-(sum(n))/(k*N));
    par=float(lgamma((N-r)+1));
    return(-(ar1+ar2+ar3-par)) end

The I use Optim.jl's optimize function as:

   r=optimize(b->loglik(b,nn, 962), 978, BFGS() ); 

Where nn is an array. And I get this error:

   ERROR:MethodError no method matching optimize (::#46#47,::Float64, ::Optim.BFGS)

Can anyone help?

Michael K. Borregaard
  • 7,864
  • 1
  • 28
  • 35
Alex
  • 99
  • 1
  • 7

1 Answers1

3

You're almost there! You need to initialize it with an array.

optimize(b->loglik(first(b),nn,962), [978.,], BFGS())

(though you still need to provide us with nn for this answer to show the output)

edit: since b is a scalar in loglik, I changed it to b->loglik(first(b),nn, 962) as suggested by Chris Rackauckas below.

pkofod
  • 764
  • 7
  • 18
  • Still doesn't work, nn is an 91-element Array{Int64,1} – Alex Oct 13 '17 at 10:17
  • 2
    `b->loglik(first(b),nn,962)` or `b->loglik(b[1],nn,962)` since it wants a scalar. – Chris Rackauckas Oct 13 '17 at 12:55
  • 1
    Great, yeah, I'm so used to `x` being a vector that I hadn't noticed it was a scalar in maxlik. Don't forget to accept an answer when you have a solution that works. Would be great if you could generate an `nn` in your question as well. – pkofod Oct 13 '17 at 13:28