def plot_with_error(y, error, x=None, axes=None, value_fmt='k', error_fmt='k--', alpha=0.05, stderr_type='asym'): """ Make plot with optional error bars Parameters ---------- y : error : array or None """ import matplotlib.pyplot as plt if axes is None: axes = plt.gca() x = x if x is not None else lrange(len(y)) plot_action = lambda y, fmt: axes.plot(x, y, fmt) plot_action(y, value_fmt) #changed this if error is not None: if stderr_type == 'asym': q = util.norm_signif_level(alpha) plot_action(y - q * error, error_fmt) plot_action(y + q * error, error_fmt) if stderr_type in ('mc', 'sz1', 'sz2', 'sz3'): plot_action(error[0], error_fmt) plot_action(error[1], error_fmt)
def plot_with_error(y, error, x=None, axes=None, value_fmt='k', error_fmt='k--', alpha=0.05, stderr_type = 'asym'): """ Make plot with optional error bars Parameters ---------- y : error : array or None """ import matplotlib.pyplot as plt if axes is None: axes = plt.gca() x = x if x is not None else range(len(y)) plot_action = lambda y, fmt: axes.plot(x, y, fmt) plot_action(y, value_fmt) #changed this if error is not None: if stderr_type == 'asym': q = util.norm_signif_level(alpha) plot_action(y - q * error, error_fmt) plot_action(y + q * error, error_fmt) if stderr_type in ('mc','sz1','sz2','sz3'): plot_action(error[0], error_fmt) plot_action(error[1], error_fmt)
def err_band_sz1(self, orth=False, svar=False, repl=1000, signif=0.05, seed=None, burn=100, component=None): """ IRF Sims-Zha error band method 1. Assumes symmetric error bands around mean. Parameters ---------- orth : bool, default False Compute orthogonalized impulse responses repl : int, default 1000 Number of MC replications signif : float (0 < signif < 1) Significance level for error bars, defaults to 95% CI seed : int, default None np.random seed burn : int, default 100 Number of initial simulated obs to discard component : neqs x neqs array, default to largest for each Index of column of eigenvector/value to use for each error band Note: period of impulse (t=0) is not included when computing principle component References ---------- Sims, Christopher A., and Tao Zha. 1999. "Error Bands for Impulse Response". Econometrica 67: 1113-1155. """ model = self.model periods = self.periods irfs = self._choose_irfs(orth, svar) neqs = self.neqs irf_resim = model.irf_resim(orth=orth, repl=repl, T=periods, seed=seed, burn=100) q = util.norm_signif_level(signif) W, eigva, k =self._eigval_decomp_SZ(irf_resim) if component is not None: if np.shape(component) != (neqs,neqs): raise ValueError("Component array must be " + str(neqs) + " x " + str(neqs)) if np.argmax(component) >= neqs*periods: raise ValueError("Atleast one of the components does not exist") else: k = component # here take the kth column of W, which we determine by finding the largest eigenvalue of the covaraince matrix lower = np.copy(irfs) upper = np.copy(irfs) for i in range(neqs): for j in range(neqs): lower[1:,i,j] = irfs[1:,i,j] + W[i,j,:,k[i,j]]*q*np.sqrt(eigva[i,j,k[i,j]]) upper[1:,i,j] = irfs[1:,i,j] - W[i,j,:,k[i,j]]*q*np.sqrt(eigva[i,j,k[i,j]]) return lower, upper
def forecast_interval(self, y, steps, alpha=0.05): """Construct forecast interval estimates assuming the y are Gaussian Parameters ---------- Notes ----- Lutkepohl pp. 39-40 Returns ------- (lower, mid, upper) : (ndarray, ndarray, ndarray) """ assert(0 < alpha < 1) q = util.norm_signif_level(alpha) point_forecast = self.forecast(y, steps) sigma = np.sqrt(self._forecast_vars(steps)) forc_lower = point_forecast - q * sigma forc_upper = point_forecast + q * sigma return point_forecast, forc_lower, forc_upper