Skip to content

Commit

Permalink
VAR test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jan 31, 2011
1 parent 136edf9 commit f5a8560
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 42 deletions.
26 changes: 26 additions & 0 deletions .coveragerc
@@ -0,0 +1,26 @@
# .coveragerc to control coverage.py
[run]
branch = False

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain about missing debug-only code:
def __repr__
if self\.debug

# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError

# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:

ignore_errors = False

[html]
directory = coverage_html_report
14 changes: 7 additions & 7 deletions scikits/statsmodels/tsa/var/irf.py
Expand Up @@ -50,7 +50,7 @@ def cov(self, *args, **kwargs):
def cum_effect_cov(self, *args, **kwargs):
raise NotImplementedError

def plot(self, orth=False, impcol=None, rescol=None, signif=0.05,
def plot(self, orth=False, impulse=None, response=None, signif=0.05,
plot_params=None, subplot_params=None):
"""
Plot impulse responses
Expand All @@ -59,9 +59,9 @@ def plot(self, orth=False, impcol=None, rescol=None, signif=0.05,
----------
orth : bool, default False
Compute orthogonalized impulse responses
impcol : string or int
impulse : string or int
variable providing the impulse
rescol : string or int
response : string or int
variable affected by the impulse
signif : float (0 < signif < 1)
Significance level for error bars, defaults to 95% CI
Expand All @@ -82,12 +82,12 @@ def plot(self, orth=False, impcol=None, rescol=None, signif=0.05,
except NotImplementedError:
stderr = None

plotting.irf_grid_plot(irfs, stderr, impcol, rescol, self.model.names,
title, signif=signif,
plotting.irf_grid_plot(irfs, stderr, impulse, response,
self.model.names, title, signif=signif,
subplot_params=subplot_params,
plot_params=plot_params)

def plot_cum_effects(self, orth=False, impcol=None, rescol=None,
def plot_cum_effects(self, orth=False, impulse=None, response=None,
signif=0.05, plot_params=None,
subplot_params=None):
"""
Expand All @@ -108,7 +108,7 @@ def plot_cum_effects(self, orth=False, impcol=None, rescol=None,
except NotImplementedError:
stderr = None

plotting.irf_grid_plot(cum_effects, stderr, impcol, rescol,
plotting.irf_grid_plot(cum_effects, stderr, impulse, response,
self.model.names, title, signif=signif,
hlines=lr_effects, subplot_params=subplot_params,
plot_params=plot_params)
Expand Down
36 changes: 18 additions & 18 deletions scikits/statsmodels/tsa/var/model.py
Expand Up @@ -269,17 +269,11 @@ def _get_trendorder(trend='c'):
trendorder = 2
elif trend == 'ctt':
trendorder = 3
if trend != 'nc':
X = add_trend(X, prepend=True, trend=trend)

return trendorder

#-------------------------------------------------------------------------------
# VARProcess class: for known or unknown VAR process

def lag_select():
pass

class VAR(object):
r"""
Fit VAR(p) process and do lag order selection
Expand All @@ -304,9 +298,6 @@ def __init__(self, endog, names=None, dates=None):
assert(self.nobs == len(dates))
self.dates = dates

def loglike(self, params, omega):
pass

def fit(self, maxlags=None, method='ols', ic=None, trend='c',
verbose=False):
"""
Expand Down Expand Up @@ -353,10 +344,12 @@ def fit(self, maxlags=None, method='ols', ic=None, trend='c',
if verbose:
print 'Using %d based on %s criterion' % (lags, ic)

return self._estimate_var(lags)
return self._estimate_var(lags, trend=trend)

def _estimate_var(self, lags, trendorder=1):
z = self._get_z(lags)
def _estimate_var(self, lags, trend='c'):
trendorder = _get_trendorder(trend)

z = self._get_z(lags, trend=trend)
y_sample = self._get_y_sample(lags)

# Lutkepohl p75, about 5x faster than stated formula
Expand Down Expand Up @@ -410,7 +403,7 @@ def select_order(self, maxlags=None, verbose=True):
#-------------------------------------------------------------------------------
# Auxiliary functions for estimation

def _get_z(self, lags):
def _get_z(self, lags, trend='c'):
"""
Predictor matrix for VAR(p) process
Expand All @@ -420,13 +413,15 @@ def _get_z(self, lags):
Ref: Lutkepohl p.70 (transposed)
"""
y = self.y
T = self.nobs - lags

# Ravel C order, need to put in descending order
Z = mat([y[t-lags : t][::-1].ravel() for t in xrange(lags, self.nobs)])

# Add intercept
return np.concatenate((np.ones((T, 1)), Z), axis=1)
# Add constant, trend, etc.
if trend != 'nc':
Z = tsa.add_trend(Z, prepend=True, trend=trend)

return Z

def _get_y_sample(self, lags):
# drop presample observations
Expand Down Expand Up @@ -485,7 +480,7 @@ def plotsim(self, steps=1000):
Y = util.varsim(self.coefs, self.intercept, self.sigma_u, steps=steps)
plotting.plot_mts(Y)

def plotforc(self, y, steps, alpha=0.05):
def plot_forecast(self, y, steps, alpha=0.05):
"""
"""
Expand Down Expand Up @@ -791,6 +786,11 @@ def cov_params(self):
#------------------------------------------------------------
# Estimation-related things

@cache_readonly
def _zz(self):
# Z'Z
return np.dot(self.ys_lagged.T, self.ys_lagged)

def _est_var_ybar(self):
Ainv = L.inv(np.eye(self.neqs) - self.coefs.sum(0))
return chain_dot(Ainv, self.sigma_u, Ainv.T)
Expand Down Expand Up @@ -860,7 +860,7 @@ def forecast_interval(self, steps, alpha=0.05):
return VARProcess.forecast_interval(
self, self.y[-self.p:], steps, alpha=alpha)

def plotforc(self, steps, alpha=0.05):
def plot_forecast(self, steps, alpha=0.05):
"""
"""
Expand Down
63 changes: 47 additions & 16 deletions scikits/statsmodels/tsa/var/tests/test_var.py
Expand Up @@ -2,14 +2,17 @@
Test VAR Model
"""

import numpy as np
from cStringIO import StringIO
import nose
import os
import sys

import numpy as np
import matplotlib.pyplot as plt

import scikits.statsmodels.api as sm
import scikits.statsmodels.tsa.var.model as model
import scikits.statsmodels.tsa.var.util as util
reload(model)
from scikits.statsmodels.tsa.var.model import VAR

Expand All @@ -24,6 +27,18 @@
basepath = os.path.split(sm.__file__)[0]
resultspath = basepath + '/tsa/var/tests/results/'

def test_lutkepohl_parse():
lut_data = basepath + '/tsa/var/data/'
files = [lut_data + 'e%d.dat' % i for i in range(1, 7)]

try:
import pandas
except ImportError:
raise nose.SkipTest

for f in files:
data = util.parse_lutkepohl_data(f)

class CheckVAR(object):

def test_params(self):
Expand Down Expand Up @@ -82,11 +97,6 @@ def get_macrodata():
data = np.diff(np.log(data), axis=0)
return data, names

class TestIRF(object):

def test_plots(self):
pass

def generate_var():
from rpy2.robjects import r
import pandas.rpy.common as prp
Expand Down Expand Up @@ -156,7 +166,12 @@ def _check_irfs(self, py_irfs, r_irfs):
assert_almost_equal(ref_irfs, res_irfs)

def test_plot_irf(self):
pass
self.irf.plot()
self.irf.plot(impulse=0, response=1)

def test_plot_cum_effects(self):
self.irf.plot_cum_effects()
self.irf.plot_cum_effects(impulse=0, response=1)

class TestVARResults(CheckIRF):

Expand All @@ -165,20 +180,17 @@ def __init__(self):

data, names = get_macrodata()
self.ref = RResults()
self.res = VAR(data, names=names).fit(maxlags=self.p)
self.model = VAR(data, names=names)
self.res = self.model.fit(maxlags=self.p)
self.irf = self.res.irf(self.ref.nirfs)
self.nahead = self.ref.nahead
self.k = len(self.ref.names)

_old_stdout = None

# def test_aaamonkeypatches(self):
# from StringIO import StringIO
# self._old_stdout = sys.stdout
# sys.stdout = StringIO()
def test_aaamonkeypatches(self):
sys.stdout = StringIO()

# def test_zzzundomonkeypatches(self):
# sys.stdout = self._old_stdout
def test_zzzundomonkeypatches(self):
sys.stdout = sys.__stdout__

def test_params(self):
assert_almost_equal(self.res.params, self.ref.params, DECIMAL_3)
Expand Down Expand Up @@ -211,6 +223,16 @@ def test_ma_rep(self):
ma_rep = self.res.ma_rep(self.nahead)
assert_almost_equal(ma_rep, self.ref.ma_rep)

#--------------------------------------------------
# Lots of tests to make sure stuff works...need to check correctness

def test_causality(self):
pass

def test_select_order(self):
result = self.model.fit(10, ic='aic', verbose=True)
result = self.model.fit(10, ic='fpe', verbose=True)

def test_is_stable(self):
# may not necessarily be true for other datasets
assert(self.res.is_stable())
Expand All @@ -225,6 +247,15 @@ def test_acorr(self):
def test_plot_acorr(self):
self.res.plot_acorr()

def test_forecast(self):
point = self.res.forecast(self.res.y[-5:], 5)

def test_forecast_interval(self):
point, lower, upper = self.res.forecast_interval(5)

def test_plot_forecast(self):
self.res.plot_forecast(5)

# def test_neqs(self):
# assert_equal(self.res1.neqs, self.res2.neqs)

Expand Down
2 changes: 1 addition & 1 deletion scikits/statsmodels/tsa/var/util.py
Expand Up @@ -37,7 +37,7 @@ def interpret_data(data, names):
def struct_to_ndarray(arr):
return arr.view((float, len(arr.dtype.names)))

def parse_data(path):
def parse_lutkepohl_data(path): # pragma: no cover
"""
Parse data files from Lutkepohl (2005) book
Expand Down

0 comments on commit f5a8560

Please sign in to comment.