1

I am trying to estimate a Beta-Binomial model in Matlab (the data set is available here).

My likelihood function:

function  out= log_Lik(x,Data);

ms=Data(:,1);
xs=Data (:,2);

alpha=x(1)
beta=x(2)


P12=exp(gammaln(ms+1)-gammaln(xs+1)-gammaln(ms- xs+1)).*exp(gammaln(alpha+xs)+gammaln(beta+ms-xs)-gammaln(alpha+beta+ms));
P3= exp(gammaln(alpha+beta)-gammaln(alpha)-gammaln(beta));
P=P12.*P3;
Like=log(P);
out= -sum(Like);

My maximum likelihood estimation code:

Data=readtable('prob3.xls');

ms=Data.m_s;
xs=Data.x_s;

%% Data parsed to optimisation function
Data = [ms xs ];

f = @(x)log_Lik(x, Data);

%%
options = optimoptions('fminunc','Display','iter','Algorithm','quasi-  newton','MaxIter',10000,'TolX',10^-30,'TolFun',10^-30);
alpha0 = 1;
beta0=1;
x0 = [alpha0 beta0];
[x,fval,exitflag,output,grad,hessian] = fminunc(f,x0,options)

However, when I run the above code, I get an error saying:

Error using gammaln
Input must be nonnegative.

which I believe is coming from passing random negative values to alpha while optimising the likelihood function. Now I was wondering if there is any way to define a constraint for the values passed as my parameters (alpha and beta), when running the optimization command. Any hints would be highly appreciated.

2
  • Use fmincon and add a constraint to keep alpha positive Commented Nov 5, 2016 at 10:36
  • Thanks Matthew! could you let me know where in my code and how this can be accommodated? Sorry I'm very new to matlab Commented Nov 5, 2016 at 10:46

1 Answer 1

1

fminunc, the function you are currently using, finds a local minimum of an unconstrained optimization problem.

On the other hand, fmincon finds a local minimum of a constrained optimization problem.

It appears you have a constraint that alpha should be positive, so you should use fmincon and pass in the constraint.

fmincon operates similarly to fminunc. You should change "optimoptions('fminunc'..." to read "optimoptions('fmincon'..."

You want to set a lower bound of [0; -inf] to keep alpha non-negative. For the other inputs you don't care about, simply pass an empty array [].

The documentation for fmincon is going to be clearer than anything I have the time to write here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.