'The unconditional loglikelihood of an AR(p) process
Notes
Contains constant term.'

def loglike(self, params):

    nobs = self.nobs
    y = self.endog
    ylag = self.exog
    penalty = self.penalty
    if isinstance(params, tuple):
        params = np.asarray(params)
    usepenalty = False
    if ((not np.all((np.abs(params) < 1))) and penalty):
        oldparams = params
        params = np.array([0.9999])
        usepenalty = True
    diffsumsq = sumofsq((y - np.dot(ylag, params)))
    sigma2 = ((1 / nobs) * (diffsumsq - ((ylag[0] ** 2) * (1 - (params ** 2)))))
    loglike = (((((((- nobs) / 2) * np.log((2 * np.pi))) - ((nobs / 2) * np.log(sigma2))) + (0.5 * np.log((1 - (params ** 2))))) - ((0.5 * diffsumsq) / sigma2)) - (((ylag[0] ** 2) * (1 - (params ** 2))) / (2 * sigma2)))
    if usepenalty:
        loglike -= (1000 * ((oldparams - 0.9999) ** 2))
    return loglike
