Skip to content

Commit

Permalink
Merge 5a4aca8 into e36ed9e
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed Jan 6, 2020
2 parents e36ed9e + 5a4aca8 commit db0307e
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 212 deletions.
37 changes: 14 additions & 23 deletions arch/bootstrap/base.py
Expand Up @@ -126,17 +126,13 @@ class IIDBootstrap(object, metaclass=DocStringInheritor):
Attributes
----------
index : ndarray
The current index of the bootstrap
data : tuple
Two-element tuple with the pos_data in the first position and kw_data
in the second (pos_data, kw_data)
pos_data : tuple
Tuple containing the positional arguments (in the order entered)
kw_data : dict
Dictionary containing the keyword arguments
random_state : RandomState
RandomState instance used by bootstrap
Notes
-----
Expand Down Expand Up @@ -242,7 +238,19 @@ def _repr_html(self):

@property
def random_state(self):
"""Set or get the instance random state"""
"""
Set or get the instance random state
Parameters
----------
random_state : RandomState
RandomState instance used by bootstrap
Returns
-------
RandomState
RandomState instance used by bootstrap
"""
return self._random_state

@random_state.setter
Expand All @@ -254,7 +262,7 @@ def random_state(self, value):
@property
def index(self):
"""
Returns the current index of the bootstrap
The current index of the bootstrap
"""
return self._index

Expand All @@ -278,7 +286,6 @@ def set_state(self, state):
state : RandomState state vector
Array containing the state
"""

return self.random_state.set_state(state)

def seed(self, value):
Expand Down Expand Up @@ -907,17 +914,13 @@ class IndependentSamplesBootstrap(IIDBootstrap):
Attributes
----------
index : ndarray
The current index of the bootstrap
data : tuple
Two-element tuple with the pos_data in the first position and kw_data
in the second (pos_data, kw_data)
pos_data : tuple
Tuple containing the positional arguments (in the order entered)
kw_data : dict
Dictionary containing the keyword arguments
random_state : RandomState
RandomState instance used by bootstrap
Notes
-----
Expand Down Expand Up @@ -1064,17 +1067,13 @@ class CircularBlockBootstrap(IIDBootstrap):
Attributes
----------
index : ndarray
The current index of the bootstrap
data : tuple
Two-element tuple with the pos_data in the first position and kw_data
in the second (pos_data, kw_data)
pos_data : tuple
Tuple containing the positional arguments (in the order entered)
kw_data : dict
Dictionary containing the keyword arguments
random_state : RandomState
RandomState instance used by bootstrap
Notes
-----
Expand Down Expand Up @@ -1167,17 +1166,13 @@ class StationaryBootstrap(CircularBlockBootstrap):
Attributes
----------
index : ndarray
The current index of the bootstrap
data : tuple
Two-element tuple with the pos_data in the first position and kw_data
in the second (pos_data, kw_data)
pos_data : tuple
Tuple containing the positional arguments (in the order entered)
kw_data : dict
Dictionary containing the keyword arguments
random_state : RandomState
RandomState instance used by bootstrap
Notes
-----
Expand Down Expand Up @@ -1245,17 +1240,13 @@ class MovingBlockBootstrap(CircularBlockBootstrap):
Attributes
----------
index : ndarray
The current index of the bootstrap
data : tuple
Two-element tuple with the pos_data in the first position and kw_data
in the second (pos_data, kw_data)
pos_data : tuple
Tuple containing the positional arguments (in the order entered)
kw_data : dict
Dictionary containing the keyword arguments
random_state : RandomState
RandomState instance used by bootstrap
Notes
-----
Expand Down
36 changes: 8 additions & 28 deletions arch/bootstrap/multiple_comparison.py
Expand Up @@ -49,7 +49,7 @@ def reset(self):

def seed(self, value):
"""
Seeds the bootstrap's random number generator
Seed the bootstrap's random number generator
Parameters
----------
Expand Down Expand Up @@ -85,11 +85,6 @@ class MCS(MultipleComparison):
'circular' or 'cbb': Circular block bootstrap
'moving block' or 'mbb': Moving block bootstrap
Methods
-------
compute
Compute the set ofmodels in the confidence set.
References
----------
Hansen, P. R., Lunde, A., & Nason, J. M. (2011). The model confidence set.
Expand Down Expand Up @@ -156,7 +151,7 @@ def _format_pvalues(self, eliminated):

def compute(self):
"""
Computes the model confidence set
Compute the set of models in the confidence set.
"""
if self.method.lower() == 'r':
self._compute_r()
Expand Down Expand Up @@ -339,11 +334,6 @@ class StepM(MultipleComparison):
studentization. Default is False. Note that this can be slow since
the procedure requires k extra bootstraps.
Methods
-------
compute
Compute the set of superior models.
References
----------
Romano, J. P., & Wolf, M. (2005). "Stepwise multiple testing as formalized
Expand Down Expand Up @@ -388,7 +378,7 @@ def __init__(self, benchmark, models, size=0.05, block_size=None,

def compute(self):
"""
Computes the set of superior models
Compute the set of superior models.
"""
# 1. Run SPA
self.spa.compute()
Expand Down Expand Up @@ -461,19 +451,6 @@ class SPA(MultipleComparison, metaclass=DocStringInheritor):
studentization. Default is False. Note that this can be slow since
the procedure requires k extra bootstraps.
Methods
-------
compute
Compute the bootstrap pvalue. Must be called before accessing the
pvalue
seed
Pass seed to bootstrap implementation
reset
Reset the bootstrap to its initial state
better_models
Produce a list of column indices or names (if models is a DataFrame)
that are rejected given a test size
References
----------
White, H. (2000). "A reality check for data snooping." Econometrica 68,
Expand Down Expand Up @@ -535,7 +512,7 @@ def __init__(self, benchmark, models, block_size=None, reps=1000,

def reset(self):
"""
Reset the bootstrap to it's initial state.
Reset the bootstrap to its initial state.
"""
super(SPA, self).reset()
self._pvalues = None
Expand All @@ -555,8 +532,11 @@ def subset(self, selector):

def compute(self):
"""
Compute the bootstrap p-value
Compute the bootstrap pvalue.
Notes
-----
Must be called before accessing the pvalue.
"""
# Plan
# 1. Compute variances
Expand Down
69 changes: 69 additions & 0 deletions arch/tests/univariate/test_moment.py
@@ -0,0 +1,69 @@
import pytest
from arch.univariate.distribution import (SkewStudent, Normal, StudentsT,
GeneralizedError)
from numpy import exp, inf, log, nan, pi
from numpy.testing import assert_equal, assert_almost_equal
from scipy.integrate import quad
from scipy.special import gammaln

DISTRIBUTIONS = [
(SkewStudent(), [6, -0.1]),
(SkewStudent(), [6, -0.5]),
(SkewStudent(), [6, 0.1]),
(SkewStudent(), [6, 0.5]),
(GeneralizedError(), [1.5]),
(GeneralizedError(), [2.1]),
(StudentsT(), [6]),
(StudentsT(), [7]),
(Normal(), None)]


@pytest.mark.parametrize('dist, params', DISTRIBUTIONS)
def test_moment(dist, params):
"""
Ensures that Distribtion.moment and .partial_moment agree
with numeric integrals for order n=0,...,5 and z=+/-1,...,+/-5
Parameters
----------
dist : distribution.Distribution
The distribution whose moments are being checked
params : List
List of parameters
"""

assert_equal(dist.moment(-1, params), nan)
assert_equal(dist.partial_moment(-1, 0., params), nan)

# verify moments that exist
def f(x, n):
return (x**n) * exp(dist.loglikelihood(params, x, 1, True))

for n in range(6): # moments 0-5

# complete moments
m_quad = quad(f, -inf, inf, args=n)[0]
m_method = dist.moment(n, params)
assert_almost_equal(m_quad, m_method)

# partial moments at z=+/-1,...,+/-5
# SkewT integral is broken up for numerical stability
for z in range(-5, 5): # partial moments at +-1,...,+-5
if isinstance(dist, SkewStudent):
eta, lam = params
c = (gammaln((eta + 1) / 2) - gammaln(eta / 2) -
log(pi * (eta - 2)) / 2)
a = 4 * lam * exp(c) * (eta - 2) / (eta - 1)
b = (1 + 3 * lam ** 2 - a ** 2) ** .5
loc = -a/b
if z < loc:
m_quad = quad(f, -inf, z, args=n)[0]
else:
m_quad = quad(f, -inf, loc-1e-9, args=n)[0] + \
quad(f, loc+1e-9, z, args=n)[0]

else:
m_quad = quad(f, -inf, z, args=n)[0]

m_method = dist.partial_moment(n, z, params)
assert_almost_equal(m_quad, m_method)

0 comments on commit db0307e

Please sign in to comment.