function ml = modellikel(y,x,bhatprior,g)
% Gives the Bayesian regression model log-likelihood based on g-prior. Formulas are based
% on Koop (2003), p. 271
% INPUTS: y - dependent variable, of size Tx1
%         x - independent variable, of size Txk
%         bhatprior - prior on the regression coefficients, of size kx1,
%         g - the value of g in the g prior on the regression coefficients, scalar
%         the prior on var-covar is assumed to be uninformative
% OUTPUTS: ml - marginal likelihood based
[n,k] = size(x);

if k == 0;
    sigs = y'*y;
else
    dy = y - (x*bhatprior);
    px = eye(n) - x*pinv(x'*x)*x';
    nsigs = 1/(1 + g)*y'*px*y + (g/(1 + g))*(dy)'*dy;
end

l = -((n - 1)/2)*log(nsigs);
ml = l + ((k/2)*log(g/(1 + g)));