'Loglikelihood of AR(p) process using exact maximum likelihood'

def _loglike_mle(self, params):

    nobs = self.nobs
    X = self.X
    endog = self.endog
    k_ar = self.k_ar
    k_trend = self.k_trend
    if self.transparams:
        params = self._transparams(params)
    yp = endog[:k_ar].copy()
    if k_trend:
        c = ([params[0]] * k_ar)
    else:
        c = [0]
    mup = np.asarray((c / (1 - np.sum(params[k_trend:]))))
    diffp = (yp - mup[:, None])
    Vpinv = self._presample_varcov(params)
    diffpVpinv = np.dot(np.dot(diffp.T, Vpinv), diffp).item()
    ssr = sumofsq((endog[k_ar:].squeeze() - np.dot(X, params)))
    sigma2 = ((1.0 / nobs) * (diffpVpinv + ssr))
    self.sigma2 = sigma2
    logdet = slogdet(Vpinv)[1]
    loglike = (((-1) / 2.0) * ((((nobs * (np.log((2 * np.pi)) + np.log(sigma2))) - logdet) + (diffpVpinv / sigma2)) + (ssr / sigma2)))
    return loglike
