0

I want to minimize the negative loglikelihood in order to approach an earlier calculated distribution as closely as possible. My distribution is a Beta-Binomial distribution and I need to pick the a (lpha) and b(eta) parameters. I based my function that calculates the negative loglikelihood on one of the answers of another topic.

def nll(a, b):
    k = players['Goals'].sum() # equal to the number of successes
    n = players['Shots'].sum() # equal to the number of trials
    log = gammaln(n + 1) + gammaln(k + a) + gammaln(n - k + b) + gammaln(a + b) - (gammaln(k + 1) + gammaln(n - k + 1) + gammaln(a) + gammaln(b) + gammaln(n + a + b))
    return -(np.exp(log))

Next, I want to minimize the loglikelihood. In R this can be done with the following script:

# maximum likelihood estimation
m <- mle(ll, start = list(alpha = 1, beta = 10), 
method = "L-BFGS-B", lower = c(0.0001, 0.1))

ab <- coef(m) 

I am trying to reproduce the R-script in Python as follows:

 init_params = [1, 10] # This is equal to the start-argument in R

 res = minimize(nll, x0=init_params, method='L-BFGS-B', options={'disp' : True, 'maxiter': 250})

Running this, yields the following error:

return function(*(wrapper_args + args)) TypeError: nll() missing 1 required positional argument: 'b'

What am I exactly doing wrong? I inserted b=10 right?

HJA24
  • 410
  • 2
  • 11
  • 33

1 Answers1

0

optimize.minimize expects its first argument to be a function of this form:

def func(params, args):
    ...

where params represents all the parameters optimize.minimize is trying to minimize. args can be used to pass additional arguments which are not to be minimized over; they are essentially constants as far as optimize.minimize is concerned.

In this case params represents two values, a and b, so write nll like this:

def nll(params):
    a, b = params
    ...
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677