Skip to content

Commit

Permalink
Merge pull request #3 from RichardMM/cgarch
Browse files Browse the repository at this point in the history
Cgarch
  • Loading branch information
RichardMM committed Aug 6, 2017
2 parents 8d00212 + 0df4501 commit 580ae56
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ README.rst
/totestimports.py
*.user
/.vs/config/applicationhost.config
/examples/.ipynb_checkpoints/univariate_volatility_modeling-checkpoint.ipynb
/local_cythonrecursion.py
/.vs/arch/v14/.suo
*.pyproj
*.sln

/.vs/config/applicationhost.config
.vscode/settings.json
**/*.ipynb_checkpoints/

4 changes: 2 additions & 2 deletions arch/tests/univariate/test_volatility.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def test_cgarch(self):
bounds = cgarch.bounds(self.resids)
assert_equal(bounds[0], (0, 1))
assert_equal(bounds[1], (0, 1))
assert_equal(bounds[2], (-1, 1))
assert_equal(bounds[3], (0, 1))
assert_equal(bounds[2], (0, 1))
assert_equal(bounds[3], (0.79, 1))
assert_equal(bounds[4], (0, 1))
backcast = cgarch.backcast(self.resids)
w = 0.94 ** np.arange(75)
Expand Down
4 changes: 2 additions & 2 deletions arch/univariate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ class ARCHModelResult(ARCHModelFixedResult):
volatility : array
Conditional volatility from model
longterm_var : array
Longterm variance from model
Longterm variance from model if the volatility process outputs it
cov_type : str
String describing the covariance estimator used
dep_var: Series
Expand Down Expand Up @@ -1328,7 +1328,7 @@ class ARCHModelResult(ARCHModelFixedResult):
resid : {array, Series}
nobs element array containing model residuals
model : ARCHModel
Model instance used to produce the fit
Model instance used to produce the fit.
"""

def __init__(self, params, param_cov, r2, resid, volatility, longterm_var,
Expand Down
2 changes: 1 addition & 1 deletion arch/univariate/mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ class LS(HARX):
Notes
-----
The AR-X model is described by
The LS model is described by
.. math::
Expand Down
66 changes: 53 additions & 13 deletions arch/univariate/volatility.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def compute_variance(self, parameters, resids, sigma2, backcast,
Value to use when initializing ARCH recursion
var_bounds : array
Array containing columns of lower and upper bounds
Returns
-------
sigma : array
array of computed variances.
"""
raise NotImplementedError('Must be overridden') # pragma: no cover

Expand Down Expand Up @@ -2006,27 +2011,29 @@ def _simulation_forecast(self, parameters, resids, backcast, var_bounds, start,

class CGARCH(GARCH):
r"""
Component GARCH model. A restricted version of GARCH(2,2) by Engle and Lee
Parameters
----------
All parameters are estimated
Component GARCH model. A restricted version of GARCH(2,2) by Engle and Lee.
Decomposes volatility into its long term and short term components.
No parameters needed
Attributes
----------
num_params : int
The number of parameters in the model
The number of parameters in the model
Examples
--------
>>> from arch.univariate import CGARCH
>>> cgarch = CGARCH()
Notes
-----
In this class of processes, the variance dynamics are
.. math::
\sigma^{2}_{t}=q_{t}+g_{t}
q_{t}=\omega + \rho q_{t-1} + \phi(r^{2}_{t-1}-\sigma^{2}_{t-1})
g_{t} = \alpha(r^{2}_{t-1}-q_{t-1})+\beta g_{t-1}
\sigma^{2}_{t}=q_{t}+g_{t}\\
q_{t}=\omega + \rho q_{t-1} + \phi(r^{2}_{t-1}-\sigma^{2}_{t-1})\\
g_{t} = \alpha(r^{2}_{t-1}-q_{t-1})+\beta g_{t-1}\\.
"""

def __init__(self):
Expand Down Expand Up @@ -2054,14 +2061,47 @@ def backcast(self, resids):
return super(CGARCH, self).backcast(resids)

def bounds(self, resids):
return [(0, 1), (0, 1), (-1, 1), (0, 1), (0, 1)]
return [(0, 1), (0, 1), (0, 1), (0.79, 1), (0, 1)]

def starting_values(self, resids):
return np.array([0.1, 0.4, np.var(resids)/2, 0.8, 0.2])
alphas = [0.08, 0.2]
betas = [0.6, 0.3, 0.01]
omegas = [np.var(resids), 0.1]
rhos = [0.98, 0.8, 0.7]
phis = [0.01, 0.2]
combos = list(itertools.product(*[alphas, betas, omegas, rhos, phis]))
llfs = np.ndarray(len(combos))

for i, values in enumerate(combos):
llfs[i] = self._gaussian_loglikelihood(np.array(values), resids,
self.backcast(resids),
self.variance_bounds(resids))

return np.array(combos[np.argmax(llfs)])

def compute_variance(self, parameters, resids, sigma2, backcast,
var_bounds):
# this returns both sigma and long term variance
"""
Compute the variance for the ARCH model
Parameters
----------
resids : array
Vector of mean zero residuals
sigma2 : array
Array with same size as resids to store the conditional variance
backcast : float
Value to use when initializing ARCH recursion
var_bounds : array
Array containing columns of lower and upper bounds
Returns
-------
sigma : array
array of computed variances.
q2 : array
array of computed longterm variances.
"""
fresids = resids**2
nobs = len(fresids)
g2, q2 = np.ndarray(nobs*2).reshape(2, nobs)
Expand Down
2 changes: 2 additions & 0 deletions doc/source/changes/4.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Changes since 4.0
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``
- Added Component GARCH process which decomposes volatility into long run and short run components.
The model comes with an extra ``longterm_var`` attribute when fit.
5 changes: 4 additions & 1 deletion doc/source/univariate/forecasting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,16 @@ Any call to :py:meth:`~arch.univariate.base.ARCHModelResult.forecast` returns a
attributes and 1 which may be useful when using simulation- or bootstrap-based
forecasts.

The three core attributes are
The four core attributes are

* ``mean`` - The forecast conditional mean.
* ``variance`` - The forecast conditional variance.
* ``residual_variance`` - The forecast conditional variance of residuals.
This will differ from ``variance`` whenever the model has dynamics
(e.g. an AR model) for horizons larger than 1.
* ``longterm_component`` - The forecast longterm conditional variance of residuals.
This is present only if the volatility process supports it
(e.g. ``CGARCH process``)

Each attribute contains a ``DataFrame`` with a common structure.

Expand Down
1 change: 1 addition & 0 deletions doc/source/univariate/univariate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Univariate Volatility Models
Forecasting Examples <univariate_volatility_forecasting>
Mean Models <mean>
Volatility Processes <volatility>
CGARCH Model <CGARCH modelling>
Using the Fixed Variance Process <univariate_using_fixed_variance>
Distributions <distribution>
Background and References <background>
6 changes: 6 additions & 0 deletions doc/source/univariate/volatility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ ARCH
.. autoclass:: ARCH
:members: starting_values, backcast, compute_variance, bounds, constraints, simulate

CGARCH
------

.. autoclass:: CGARCH
:members: starting_values, backcast, compute_variance, bounds, constraints, simulate

Parameterless Variance Processes
--------------------------------
Some volatility processes use fixed parameters and so have no parameters that
Expand Down
Loading

0 comments on commit 580ae56

Please sign in to comment.