def test_adf_critical_value(self): adf = ADF(self.inflation, trend='c', lags=3) adf_cv = adf.critical_values temp = polyval(tau_2010['c'][0, :, ::-1].T, 1. / adf.nobs) cv = {'1%': temp[0], '5%': temp[1], '10%': temp[2]} for k, v in iteritems(cv): assert_almost_equal(v, adf_cv[k])
def _repr_html_(self): """HTML representation for IPython Notebook""" descr = self._model_description() html = '<strong>' + self.name + '</strong>(' for key, val in iteritems(descr): html += '<strong>' + key + ': </strong>' + val + ',\n' html += '<strong>ID: </strong> ' + hex(id(self)) + ')' return html
def _info_to_str(model, info, is_repr=False, is_html=False): if is_html: model = '<strong>' + model + '</strong>' _str = model + '(' for k, v in iteritems(info): if k.lower() != 'id' or is_repr: if is_html: k = '<strong>' + k + '</strong>' _str += k + ': ' + v + ', ' return _str[:-2] + ')'
def _info_to_str(model, info, is_repr=False, is_html=False): if is_html: model = "<strong>" + model + "</strong>" _str = model + "(" for k, v in iteritems(info): if k.lower() != "id" or is_repr: if is_html: k = "<strong>" + k + "</strong>" _str += k + ": " + v + ", " return _str[:-2] + ")"
def __str__(self): descr = self._model_description() descr_str = self.name + '(' for key, val in iteritems(descr): if val: if key: descr_str += key + ': ' + val + ', ' descr_str = descr_str[:-2] # Strip final ', ' descr_str += ')' return descr_str
def test_multiple_lags(self): """Smoke test to ensure models estimate with multiple lags""" vp = {'garch': GARCH, 'egarch': EGARCH, 'harch': HARCH, 'arch': ARCH} cm = ConstantMean(self.y) for name, process in iteritems(vp): cm.volatility = process() cm.fit(update_freq=0, disp='off') for p in [1, 2, 3]: for o in [1, 2, 3]: for q in [1, 2, 3]: if name in ('arch', ): cm.volatility = process(p=p + o + q) cm.fit(update_freq=0, disp='off') elif name in ('harch', ): cm.volatility = process(lags=[p, p + o, p + o + q]) cm.fit(update_freq=0, disp='off') else: cm.volatility = process(p=p, o=o, q=q) cm.fit(update_freq=0, disp='off')
def __init__(self, *args, **kwargs): self._random_state = None self._args = args self._kwargs = kwargs random_state = self._kwargs.pop('random_state', None) self.random_state = random_state if random_state is not None else RandomState( ) self._initial_state = self._random_state.get_state() if args: self._num_items = len(args[0]) elif kwargs: key = list(kwargs.keys())[0] self._num_items = len(kwargs[key]) all_args = list(args) all_args.extend([v for v in itervalues(kwargs)]) if self._common_size_required: for arg in all_args: if len(arg) != self._num_items: raise ValueError("All inputs must have the same number of " "elements in axis 0") self._index = np.arange(self._num_items) self._parameters = [] self._seed = None self.pos_data = args self.kw_data = kwargs self.data = (args, kwargs) self._base = None self._results = None self._studentized_results = None self._last_func = None self._name = 'IID Bootstrap' for key, value in iteritems(kwargs): attr = getattr(self, key, None) if attr is None: self.__setattr__(key, value) else: raise ValueError(key + ' is a reserved name')
def __init__(self, *args, **kwargs): self._random_state = None self._args = args self._kwargs = kwargs random_state = self._kwargs.pop('random_state', None) self.random_state = random_state if random_state is not None else RandomState() self._initial_state = self._random_state.get_state() if args: self._num_items = len(args[0]) elif kwargs: key = list(kwargs.keys())[0] self._num_items = len(kwargs[key]) all_args = list(args) all_args.extend([v for v in itervalues(kwargs)]) if self._common_size_required: for arg in all_args: if len(arg) != self._num_items: raise ValueError("All inputs must have the same number of " "elements in axis 0") self._index = np.arange(self._num_items) self._parameters = [] self._seed = None self.pos_data = args self.kw_data = kwargs self.data = (args, kwargs) self._base = None self._results = None self._studentized_results = None self._last_func = None self._name = 'IID Bootstrap' for key, value in iteritems(kwargs): attr = getattr(self, key, None) if attr is None: self.__setattr__(key, value) else: raise ValueError(key + ' is a reserved name')
def test_multiple_lags(self): """Smoke test to ensure models estimate with multiple lags""" vp = {'garch': GARCH, 'egarch': EGARCH, 'harch': HARCH, 'arch': ARCH} cm = ConstantMean(self.y) for name, process in iteritems(vp): cm.volatility = process() cm.fit(update_freq=0, disp='off') for p in [1, 2, 3]: for o in [1, 2, 3]: for q in [1, 2, 3]: if name in ('arch',): cm.volatility = process(p=p + o + q) cm.fit(update_freq=0, disp='off') elif name in ('harch',): cm.volatility = process(lags=[p, p + o, p + o + q]) cm.fit(update_freq=0, disp='off') else: cm.volatility = process(p=p, o=o, q=q) cm.fit(update_freq=0, disp='off')
def _loo_jackknife(func, nobs, args, kwargs): """ Leave one out jackknife estimation Parameters ---------- func : callable Function that computes parameters. Called using func(*args, **kwargs) nobs : int Number of observation in the data args : list List of positional inputs (arrays, Series or DataFrames) kwargs : dict List of keyword inputs (arrays, Series or DataFrames) Returns ------- results : ndarray Array containing the jackknife results where row i corresponds to leaving observation i out of the sample """ results = [] for i in range(nobs): items = np.r_[0:i, i + 1:nobs] args_copy = [] for arg in args: if isinstance(arg, (pd.Series, pd.DataFrame)): args_copy.append(arg.iloc[items]) else: args_copy.append(arg[items]) kwargs_copy = {} for k, v in iteritems(kwargs): if isinstance(v, (pd.Series, pd.DataFrame)): kwargs_copy[k] = v.iloc[items] else: kwargs_copy[k] = v[items] results.append(func(*args_copy, **kwargs_copy)) return np.array(results)
def _resample(self): """ Resample all data using the values in _index """ indices = self._index pos_data = [] for values in self._args: if isinstance(values, (pd.Series, pd.DataFrame)): pos_data.append(values.iloc[indices]) else: pos_data.append(values[indices]) named_data = {} for key, values in iteritems(self._kwargs): if isinstance(values, (pd.Series, pd.DataFrame)): named_data[key] = values.iloc[indices] else: named_data[key] = values[indices] setattr(self, key, named_data[key]) self.pos_data = pos_data self.kw_data = named_data self.data = (pos_data, named_data) return self.data
def _resample(self): """ Resample all data using the values in _index """ pos_indices, kw_indices = self._index pos_data = [] for i, values in enumerate(self._args): if isinstance(values, (pd.Series, pd.DataFrame)): pos_data.append(values.iloc[pos_indices[i]]) else: pos_data.append(values[pos_indices[i]]) named_data = {} for key, values in iteritems(self._kwargs): if isinstance(values, (pd.Series, pd.DataFrame)): named_data[key] = values.iloc[kw_indices[key]] else: named_data[key] = values[kw_indices[key]] setattr(self, key, named_data[key]) self.pos_data = pos_data self.kw_data = named_data self.data = (pos_data, named_data) return self.data
'ct': [4.6476, -2.8932, 0.5832, -0.09990], 'ctt': [4.4599, -1.8635, 0.2126, -0.06070] } # These are Table 6 from MacKinnon (1994). # These are well-behaved in the right tail. # the approximation function is # p = norm.cdf(d_0 + d_1 * z + d_2*z**2 + d_3*z**3 + d_4*z**4) adf_z_large_p = { 'nc': [0.4927, 6.906, 13.2331, 12.099, 0], 'c': [1.7157, 0.5536, 4.5518, 2.2466, 4.2537], 'ct': [2.7119, 0.4594, 2.3747, 0.7488, 0.9333], 'ctt': [3.4216, 0.4170, 1.6939, 0.4203, 0.4153] } adf_z_large_p_scale = asarray([1.0, 1.0, 1e-2, 1e-3, 1e-5]) for k, v in iteritems(adf_z_large_p): adf_z_large_p[k] = asarray(adf_z_large_p[k]) * adf_z_large_p_scale adf_z_cv_approx = { 'ctt': asarray([[-36.5942942, 353.85126482, -2510.01976534, 9844.80839543], [-28.11046156, 211.97559729, -1238.23377747, 4268.99024104], [-24.18543008, 158.50672198, -837.17807309, 2729.45518704]]), 'c': asarray([[-20.62450004, 118.83362675, -534.54927244, 1471.9177227], [-14.09312678, 57.9842846, -196.6401263, 420.09313558], [-11.25012522, 38.19512941, -111.45098086, 226.91220644]]), 'nc': asarray([[-13.2907657, 323.225921, -8318.89213, 79857.3965], [-7.81966697, 217.366594, -5554.92749, 52618.7129], [-5.56726973, 166.689373, -4194.13783, 39301.1304]]),
"ct": [4.6476, -2.8932, 0.5832, -0.09990], "ctt": [4.4599, -1.8635, 0.2126, -0.06070], } # These are Table 6 from MacKinnon (1994). # These are well-behaved in the right tail. # the approximation function is # p = norm.cdf(d_0 + d_1 * z + d_2*z**2 + d_3*z**3 + d_4*z**4) adf_z_large_p = { "nc": [0.4927, 6.906, 13.2331, 12.099, 0], "c": [1.7157, 0.5536, 4.5518, 2.2466, 4.2537], "ct": [2.7119, 0.4594, 2.3747, 0.7488, 0.9333], "ctt": [3.4216, 0.4170, 1.6939, 0.4203, 0.4153], } adf_z_large_p_scale = asarray([1.0, 1.0, 1e-2, 1e-3, 1e-5]) for k, v in iteritems(adf_z_large_p): adf_z_large_p[k] = asarray(adf_z_large_p[k]) * adf_z_large_p_scale adf_z_cv_approx = { "ctt": asarray( [ [-36.5942942, 353.85126482, -2510.01976534, 9844.80839543], [-28.11046156, 211.97559729, -1238.23377747, 4268.99024104], [-24.18543008, 158.50672198, -837.17807309, 2729.45518704], ] ), "c": asarray( [ [-20.62450004, 118.83362675, -534.54927244, 1471.9177227], [-14.09312678, 57.9842846, -196.6401263, 420.09313558], [-11.25012522, 38.19512941, -111.45098086, 226.91220644],