Skip to content

Commit

Permalink
BUG: Fix doesn't work with new models
Browse files Browse the repository at this point in the history
Fix regression in fix that was introduced when first_ and last_obs
were added
Add first_obs and last_obs options to fix
Add test for these cases

closes #156
  • Loading branch information
bashtage committed Feb 5, 2017
1 parent fa0882f commit cf2006f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion arch/univariate/base.py
Expand Up @@ -297,7 +297,7 @@ def _parse_parameters(self, x):
km, kv = self.num_params, self.volatility.num_params
return x[:km], x[km:km + kv], x[km + kv:]

def fix(self, params):
def fix(self, params, first_obs=None, last_obs=None):
"""
Allows an ARCHModelFixedResult to be constructed from fixed parameters.
Expand All @@ -307,6 +307,10 @@ def fix(self, params):
User specified parameters to use when generating the result. Must
have the correct number of parameters for a given choice of mean
model, volatility model and distribution.
first_obs : {int, str, datetime, Timestamp}
First observation to use when fixing model
last_obs : {int, str, datetime, Timestamp}
Last observation to use when fixing model
Returns
-------
Expand All @@ -319,6 +323,7 @@ def fix(self, params):
"""
v = self.volatility

self._adjust_sample(first_obs, last_obs)
resids = self.resids(self.starting_values())
sigma2 = np.zeros_like(resids)
backcast = v.backcast(resids)
Expand Down
24 changes: 24 additions & 0 deletions arch/univariate/tests/test_mean.py
Expand Up @@ -706,6 +706,30 @@ def test_fixed_user_parameters(self):
# Smoke for summary
fixed_res.summary()

def test_fixed_user_parameters_new_model(self):
am = arch_model(self.y_series)
res = am.fit(disp=DISPLAY)
new_am = arch_model(self.y_series)
fixed_res = new_am .fix(res.params)
assert_series_equal(res.conditional_volatility,
fixed_res.conditional_volatility)
assert_series_equal(res.params, fixed_res.params)
assert_equal(res.aic, fixed_res.aic)
assert_equal(res.bic, fixed_res.bic)
assert_equal(res.loglikelihood, fixed_res.loglikelihood)
assert_equal(res.num_params, fixed_res.num_params)

# Test first and last dates
am = arch_model(self.y_series)
res = am.fit(disp=DISPLAY, first_obs=100, last_obs=900)
new_am = arch_model(self.y_series)
fixed_res = new_am .fix(res.params, first_obs=100, last_obs=900)
assert_series_equal(res.params, fixed_res.params)
assert_equal(res.aic, fixed_res.aic)
assert_equal(res.bic, fixed_res.bic)
assert_equal(res.loglikelihood, fixed_res.loglikelihood)
assert_equal(res.num_params, fixed_res.num_params)

def test_output_options(self):
import sys
from arch.compat.python import StringIO
Expand Down
2 changes: 2 additions & 0 deletions doc/source/changes/4.0.txt
Expand Up @@ -4,3 +4,5 @@ Changes since 4.0
a mean model. This has been added to allow so-called zig-zag estimation where a mean model is
estimated with a fixed variance, and then a variance model is estimated on the residuals using
a ``ZeroMean`` variance process.
- Fixed a bug that prevented ```fix`` from being used with a new model (:issue:`156`)
- Added ``first_obs`` and ``last_obs`` parameters to ``fix`` to mimic ``fit``
3 changes: 3 additions & 0 deletions doc/source/conf.py
Expand Up @@ -38,6 +38,7 @@
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.ifconfig',
'numpydoc',
'IPython.sphinxext.ipython_console_highlighting',
'IPython.sphinxext.ipython_directive'
]
Expand Down Expand Up @@ -295,3 +296,5 @@
'numpy': ('http://docs.scipy.org/doc/numpy', None),
'pandas': ('http://pandas.pydata.org/pandas-docs/stable/', None)
}

extlinks = {'issue': ('https://github.com/bashtage/arch/%s', 'GH')}

0 comments on commit cf2006f

Please sign in to comment.