def forecast(self, steps=1, exog=None, alpha=.05): """ Out-of-sample forecasts Parameters ---------- steps : int The number of out of sample forecasts from the end of the sample. exog : array If the model is an ARMAX, you must provide out of sample values for the exogenous variables. This should not include the constant. alpha : float The confidence intervals for the forecasts are (1 - alpha) % Returns ------- forecast : array Array of out of sample forecasts stderr : array Array of the standard error of the forecasts. conf_int : array 2d array of the confidence interval for the forecast """ arparams = self.arparams maparams = self.maparams forecast = self.model._predict_out_of_sample(self.params, steps, self.resid, exog) # compute the standard errors sigma2 = self.sigma2 ma_rep = arma2ma(np.r_[1,-arparams], np.r_[1, maparams], nobs=steps) fcasterr = np.sqrt(sigma2 * np.cumsum(ma_rep**2)) const = norm.ppf(1 - alpha/2.) conf_int = np.c_[forecast - const*fcasterr, forecast + const*fcasterr] return forecast, fcasterr, conf_int
def forecast(self, steps=1, exog=None, alpha=.05): """ Out-of-sample forecasts Parameters ---------- steps : int The number of out of sample forecasts from the end of the sample. exog : array If the model is an ARMAX, you must provide out of sample values for the exogenous variables. This should not include the constant. alpha : float The confidence intervals for the forecasts are (1 - alpha) % Returns ------- forecast : array Array of out of sample forecasts stderr : array Array of the standard error of the forecasts. conf_int : array 2d array of the confidence interval for the forecast """ arparams = self.arparams maparams = self.maparams forecast = self.model._predict_out_of_sample(self.params, steps, self.resid, exog) # compute the standard errors sigma2 = self.sigma2 ma_rep = arma2ma(np.r_[1, -arparams], np.r_[1, maparams], nobs=steps) fcasterr = np.sqrt(sigma2 * np.cumsum(ma_rep**2)) const = norm.ppf(1 - alpha / 2.) conf_int = np.c_[forecast - const * fcasterr, forecast + const * fcasterr] return forecast, fcasterr, conf_int
def forecast(self, steps=1, exog=None, alpha=.05): """ Out-of-sample forecasts Parameters ---------- steps : int The number of out of sample forecasts from the end of the sample. exog : array If the model is an ARMAX, you must provide out of sample values for the exogenous variables. This should not include the constant. alpha : float The confidence intervals for the forecasts are (1 - alpha) % Returns ------- forecast : array Array of out of sample forecasts stderr : array Array of the standard error of the forecasts. conf_int : array 2d array of the confidence interval for the forecast """ k_trend, k_exog = self.k_trend, self.k_exog if exog is None and k_exog > 0: raise ValueError("You must provide exog for ARMAX") q, p = self.k_ma, self.k_ar maparams, arparams = self.maparams[::-1], self.arparams[::-1] if q: resid = np.zeros(2*q) resid[:q] = self.resid[-q:] #only need last q else: i = -1 # since we don't run first loop below y = self.model.endog if k_trend == 1: mu = self.params[0] * (1-arparams.sum()) # use expectation # not constant mu = np.array([mu]*steps) # repeat it so you can slice if exog else: mu = np.zeros(steps) if k_exog > 0: mu += np.dot(self.params[k_trend:k_exog+k_trend], exog) endog = np.zeros(p+steps-1) if p: endog[:p] = y[-p:] #only need p forecast = np.zeros(steps) for i in range(q): fcast = mu[i] + np.dot(arparams,endog[i:i+p]) + \ np.dot(maparams,resid[i:i+q]) forecast[i] = fcast endog[i+p] = fcast for i in range(i+1,steps-1): fcast = mu[i] + np.dot(arparams,endog[i:i+p]) forecast[i] = fcast endog[i+p] = fcast #need to do one more without updating endog forecast[-1] = mu[-1] + np.dot(arparams,endog[steps-1:]) #take care of exogenous # compute the standard errors sigma2 = self.sigma2 ma_rep = arma2ma(np.r_[1,-arparams[::-1]], np.r_[1, maparams[::-1]], nobs=steps) fcasterr = np.sqrt(sigma2 * np.cumsum(ma_rep**2)) const = norm.ppf(1 - alpha/2.) conf_int = np.c_[forecast - const*fcasterr, forecast + const*fcasterr] return forecast, fcasterr, conf_int