function [ L ] = logl_AR( b0 )
% Loglikelihood for AR(1).

global x T

mean = b0(1);                                                               % Mean.
stddev = b0(2);                                                             % Standard deviation.
theta1 = b0(3);                                                             % AR coefficient.

L = -0.5*log(2*pi) - 0.5*log(stddev^2/(1-theta1^2)) ...
                - 0.5*((x(1) - mean/(1-theta1))^2/(stddev^2/(1-theta1^2)));

for t = 2:T
    L = L - 0.5*log(2*pi) - 0.5*log(stddev^2) ...
                        - 0.5*(((x(t) - mean - theta1*x(t-1))^2/stddev^2));
end
                    
L = -L;                                                                     % Negative of loglikelihood function (for minimization).

end

