Skip to content

Commit 1b2be26

Browse files
authored
Merge pull request #190 from bashtage/summary-repr
ENH: Switch repr of results to summary
2 parents ef52881 + f67f2f5 commit 1b2be26

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

arch/tests/univariate/test_mean.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,12 @@ def test_arch_arx(self):
395395
'omega', 'alpha[1]', 'alpha[2]'])
396396

397397
am = ARX(y=y, lags=2, x=x)
398-
am.fit(disp=DISPLAY).summary()
398+
res = am.fit(disp=DISPLAY)
399+
summ = res.summary().as_text()
400+
repr = res.__repr__()
401+
assert str(hex(id(res))) in repr
402+
assert summ[:10] == repr[:10]
403+
399404
am.volatility = ARCH(p=2)
400405
results = am.fit(update_freq=0, disp='off')
401406
assert isinstance(results.pvalues, pd.Series)

arch/univariate/base.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,23 @@ def forecast(self, params, horizon=1, start=None, align='origin', method='analyt
722722
raise NotImplementedError('Subclasses must implement')
723723

724724

725-
class ARCHModelFixedResult(object):
725+
class _SummaryRepr(object):
726+
"""Base class for returning summary as repr and str"""
727+
728+
def summary(self):
729+
return Summary()
730+
731+
def __repr__(self):
732+
out = self.__str__() + '\n'
733+
out += self.__class__.__name__
734+
out += ', id: {0}'.format(hex(id(self)))
735+
return out
736+
737+
def __str__(self):
738+
return self.summary().as_text()
739+
740+
741+
class ARCHModelFixedResult(_SummaryRepr):
726742
"""
727743
Results for fixed parameters for an ARCHModel model
728744
@@ -1596,11 +1612,11 @@ class ARCHModelForecast(object):
15961612
simulations : ARCHModelForecastSimulation
15971613
Object containing detailed simulation results if using a simulation-based method
15981614
"""
1615+
15991616
def __init__(self, index, mean, variance, residual_variance,
16001617
simulated_paths=None, simulated_variances=None,
16011618
simulated_residual_variances=None, simulated_residuals=None,
16021619
align='origin'):
1603-
16041620
mean = _format_forecasts(mean, index)
16051621
variance = _format_forecasts(variance, index)
16061622
residual_variance = _format_forecasts(residual_variance, index)

arch/univariate/recursions.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ cdef extern from 'math.h':
1414
cdef extern from 'float.h':
1515
double DBL_MAX
1616

17+
cdef double LNSIGMA_MAX = log(DBL_MAX)
18+
1719
@cython.boundscheck(False)
1820
@cython.wraparound(False)
1921
@cython.cdivision(True)
@@ -265,14 +267,13 @@ def egarch_recursion(double[:] parameters,
265267
else:
266268
lnsigma2[t] += parameters[loc] * lnsigma2[t - 1 - j]
267269
loc += 1
270+
if lnsigma2[t] > LNSIGMA_MAX:
271+
lnsigma2[t] = LNSIGMA_MAX
268272
sigma2[t] = exp(lnsigma2[t])
269273
if sigma2[t] < var_bounds[t, 0]:
270274
sigma2[t] = var_bounds[t, 0]
271275
elif sigma2[t] > var_bounds[t, 1]:
272-
if sigma2[t] > DBL_MAX:
273-
sigma2[t] = var_bounds[t, 1] + 1000
274-
else:
275-
sigma2[t] = var_bounds[t, 1] + log(sigma2[t] / var_bounds[t, 1])
276+
sigma2[t] = var_bounds[t, 1] + log(sigma2[t]) - log(var_bounds[t, 1])
276277
std_resids[t] = resids[t] / sqrt(sigma2[t])
277278
abs_std_resids[t] = fabs(std_resids[t])
278279

arch/univariate/recursions_python.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
__all__ = ['harch_recursion', 'arch_recursion', 'garch_recursion',
1414
'egarch_recursion']
1515

16+
LNSIGMA_MAX = np.log(np.finfo(np.double).max) - .1
17+
1618

1719
def harch_recursion_python(parameters, resids, sigma2, lags, nobs, backcast,
1820
var_bounds):
@@ -225,14 +227,13 @@ def egarch_recursion_python(parameters, resids, sigma2, p, o, q, nobs,
225227
else:
226228
lnsigma2[t] += parameters[loc] * lnsigma2[t - 1 - j]
227229
loc += 1
230+
if lnsigma2[t] > LNSIGMA_MAX:
231+
lnsigma2[t] = LNSIGMA_MAX
228232
sigma2[t] = np.exp(lnsigma2[t])
229233
if sigma2[t] < var_bounds[t, 0]:
230234
sigma2[t] = var_bounds[t, 0]
231235
elif sigma2[t] > var_bounds[t, 1]:
232-
if not np.isinf(sigma2[t]):
233-
sigma2[t] = var_bounds[t, 1] + log(sigma2[t] / var_bounds[t, 1])
234-
else:
235-
sigma2[t] = var_bounds[t, 1] + 1000
236+
sigma2[t] = var_bounds[t, 1] + log(sigma2[t]) - log(var_bounds[t, 1])
236237
std_resids[t] = resids[t] / np.sqrt(sigma2[t])
237238
abs_std_resids[t] = np.abs(std_resids[t])
238239

0 commit comments

Comments
 (0)