Пример #1
0
    def resid(self):
        model = self.model
        params = self.params
        y = model.endog.copy()

        #demean for exog != None
        k = model.k_exog + model.k_trend
        if k > 0:
            y -= dot(model.exog, params[:k])

        k_lags = model.k_lags
        k_ar = model.k_ar
        k_ma = model.k_ma

        if self.model.method != "css":
            #TODO: move get errors to cython-ized Kalman filter
            nobs = self.nobs

            Z_mat = KalmanFilter.Z(k_lags)
            m = Z_mat.shape[1]
            R_mat = KalmanFilter.R(params, k_lags, k, k_ma, k_ar)
            T_mat = KalmanFilter.T(params, k_lags, k, k_ar)

            #initial state and its variance
            alpha = zeros((m,1))
            Q_0 = dot(inv(identity(m**2)-kron(T_mat,T_mat)),
                                dot(R_mat,R_mat.T).ravel('F'))
            Q_0 = Q_0.reshape(k_lags,k_lags,order='F')
            P = Q_0

            resids = empty((nobs,1), dtype=params.dtype)
            for i in xrange(int(nobs)):
                # Predict
                v_mat = y[i] - dot(Z_mat,alpha) # one-step forecast error
                resids[i] = v_mat
                F_mat = dot(dot(Z_mat, P), Z_mat.T)
                Finv = 1./F_mat # always scalar for univariate series
                K = dot(dot(dot(T_mat,P),Z_mat.T),Finv) # Kalman Gain Matrix
                # update state
                alpha = dot(T_mat, alpha) + dot(K,v_mat)
                L = T_mat - dot(K,Z_mat)
                P = dot(dot(T_mat, P), L.T) + dot(R_mat, R_mat.T)
        else:
            b,a = np.r_[1,-params[k:k+k_ar]], np.r_[1,params[k+k_ar:]]
            zi = np.zeros((max(k_ar,k_ma)))
            for i in range(k_ar):
                zi[i] = sum(-b[:i+1][::-1] * y[:i+1])
            e = lfilter(b,a, y, zi=zi)
            resids = e[0][k_ar:]
        return resids.squeeze()
Пример #2
0
    def geterrors(self, params):
        """
        Get the errors of the ARMA process.

        Parameters
        ----------
        params : array-like
            The fitted ARMA parameters
        order : array-like
            3 item iterable, with the number of AR, MA, and exogenous
            parameters, including the trend
        """

        #start = self._get_predict_start(start) # will be an index of a date
        #end, out_of_sample = self._get_predict_end(end)
        params = np.asarray(params)
        k_ar, k_ma = self.k_ar, self.k_ma
        k = self.k_exog + self.k_trend


        if 'mle' in self.method: # use KalmanFilter to get errors
            (y, k, nobs, k_ar, k_ma, k_lags, newparams, Z_mat, m, R_mat,
            T_mat, paramsdtype) = KalmanFilter._init_kalman_state(params, self)
            errors = KalmanFilter.geterrors(y,k,k_ar,k_ma, k_lags, nobs,
                    Z_mat, m, R_mat, T_mat, paramsdtype)
            if isinstance(errors, tuple):
                errors = errors[0] # non-cython version returns a tuple
        else: # use scipy.signal.lfilter
            y = self.endog.copy()
            k = self.k_exog + self.k_trend
            if k > 0:
                y -= dot(self.exog, params[:k])

            k_ar = self.k_ar
            k_ma = self.k_ma


            (trendparams, exparams,
             arparams, maparams) = _unpack_params(params, (k_ar, k_ma),
                                        self.k_trend, self.k_exog,
                                        reverse=False)
            b,a = np.r_[1,-arparams], np.r_[1,maparams]
            zi = zeros((max(k_ar, k_ma)))
            for i in range(k_ar):
                zi[i] = sum(-b[:i+1][::-1]*y[:i+1])
            e = lfilter(b,a,y,zi=zi)
            errors = e[0][k_ar:]
        return errors.squeeze()
Пример #3
0
    def geterrors(self, params):
        """
        Get the errors of the ARMA process.

        Parameters
        ----------
        params : array-like
            The fitted ARMA parameters
        order : array-like
            3 item iterable, with the number of AR, MA, and exogenous
            parameters, including the trend
        """

        #start = self._get_predict_start(start) # will be an index of a date
        #end, out_of_sample = self._get_predict_end(end)
        params = np.asarray(params)
        k_ar, k_ma = self.k_ar, self.k_ma
        k = self.k_exog + self.k_trend

        if 'mle' in self.method:  # use KalmanFilter to get errors
            (y, k, nobs, k_ar, k_ma, k_lags, newparams, Z_mat, m, R_mat, T_mat,
             paramsdtype) = KalmanFilter._init_kalman_state(params, self)
            errors = KalmanFilter.geterrors(y, k, k_ar, k_ma, k_lags, nobs,
                                            Z_mat, m, R_mat, T_mat,
                                            paramsdtype)
            if isinstance(errors, tuple):
                errors = errors[0]  # non-cython version returns a tuple
        else:  # use scipy.signal.lfilter
            y = self.endog.copy()
            k = self.k_exog + self.k_trend
            if k > 0:
                y -= dot(self.exog, params[:k])

            k_ar = self.k_ar
            k_ma = self.k_ma

            (trendparams, exparams, arparams,
             maparams) = _unpack_params(params, (k_ar, k_ma),
                                        self.k_trend,
                                        self.k_exog,
                                        reverse=False)
            b, a = np.r_[1, -arparams], np.r_[1, maparams]
            zi = zeros((max(k_ar, k_ma)))
            for i in range(k_ar):
                zi[i] = sum(-b[:i + 1][::-1] * y[:i + 1])
            e = lfilter(b, a, y, zi=zi)
            errors = e[0][k_ar:]
        return errors.squeeze()
Пример #4
0
 def loglike_kalman(self, params):
     """
     Compute exact loglikelihood for ARMA(p,q) model using the Kalman Filter.
     """
     return KalmanFilter.loglike(params, self)
Пример #5
0
 def loglike_kalman(self, params):
     """
     Compute exact loglikelihood for ARMA(p,q) model using the Kalman Filter.
     """
     return KalmanFilter.loglike(params, self)