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.