Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing component GARCH process #188

Open
wants to merge 102 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
3d80b3d
print start
RichardMM Jul 9, 2017
7be31f6
added recursion and cgarch process
RichardMM Jul 9, 2017
bf63a26
included cgarch in init
RichardMM Jul 9, 2017
a9bc455
Add .suo to gitignore
RichardMM Jul 10, 2017
0a27e12
Created empty forecast methods
RichardMM Jul 13, 2017
3ec5f3b
Add pyproj to gitignore
RichardMM Jul 22, 2017
da87613
Merge remote-tracking branch 'refs/remotes/upstream/master'
RichardMM Jul 22, 2017
fb21649
Add CGARCH forecast
RichardMM Jul 24, 2017
53d1ea3
Add CGARCH docs
RichardMM Jul 24, 2017
edb2aa3
Add .sln to gitignore
RichardMM Jul 24, 2017
27ff140
Merge remote-tracking branch 'refs/remotes/upstream/master'
RichardMM Jul 24, 2017
2e06bc3
Correct import future statement
RichardMM Jul 25, 2017
6ed9a23
Ignore ipynb checkpoints
RichardMM Jul 25, 2017
5125121
Correct recursion problem
RichardMM Jul 28, 2017
134a285
Change cgarch constraints
RichardMM Jul 29, 2017
0df9e57
Add local files to gitignore
RichardMM Jul 31, 2017
0ec4f80
Change starting values
RichardMM Jul 31, 2017
120d259
Change parameter bounds
RichardMM Jul 31, 2017
529d2a0
Correct recursion problem
RichardMM Jul 31, 2017
f2aacf2
Correct recursion
RichardMM Jul 31, 2017
5bd7baf
Add cgarch recursion test
RichardMM Aug 1, 2017
5b30174
Files change
RichardMM Aug 1, 2017
f1d40ae
Add pylint suggestions
RichardMM Aug 1, 2017
9727ec1
Add cgarch class tests to tests file
RichardMM Aug 1, 2017
8b24df0
Change recursion used
RichardMM Aug 1, 2017
18a572c
Remove test for cython cgarch recursion
RichardMM Aug 2, 2017
7232806
Modify super to work in python 2
RichardMM Aug 2, 2017
d318b7b
Correct failed tests
RichardMM Aug 2, 2017
d15ea69
Change super to support python 2
RichardMM Aug 2, 2017
f40ff77
correct undefined name
RichardMM Aug 2, 2017
d92cb72
Corrrect bug in simulate method in cgarch
RichardMM Aug 2, 2017
8105337
Add pylint suggestions
RichardMM Aug 2, 2017
cfef023
Fix index error bug
RichardMM Aug 2, 2017
b7b4c99
Fix simulation bug
RichardMM Aug 2, 2017
453f712
Fix simulation bug
RichardMM Aug 2, 2017
5e5f7c5
Fix simulation bug
RichardMM Aug 2, 2017
cc158df
Fix cgarch simulation bug
RichardMM Aug 2, 2017
3cae18b
Fix simulation bug
RichardMM Aug 2, 2017
137ae8b
Fix simulation bug
RichardMM Aug 2, 2017
e5cc606
Add travis flake8 suggestions
RichardMM Aug 2, 2017
56d6bea
Add pylint guidelines
RichardMM Aug 2, 2017
6b49db1
Add pylint suggestions
RichardMM Aug 3, 2017
04f916b
Add pylint suggestions
RichardMM Aug 3, 2017
659d8c1
Add pylint suggestions
RichardMM Aug 3, 2017
2a8f58d
Add cython cgarch recursion
RichardMM Aug 3, 2017
e03dffa
Add test for cython cgarch recursion
RichardMM Aug 3, 2017
0028d7e
Add cython recursion test for cgarch
RichardMM Aug 3, 2017
8e4bef6
Add pylint suggestions
RichardMM Aug 3, 2017
b8548f8
Add CGARCH to archmodel function
RichardMM Aug 3, 2017
fc0ef5a
correct forecast method
RichardMM Aug 3, 2017
e1264fb
Remove pyproj file
RichardMM Aug 3, 2017
2435705
Add analytic forcast for CGARCH
RichardMM Aug 3, 2017
71b0734
Add pylint suggestions
RichardMM Aug 3, 2017
484a8b7
Fix forecast bug
RichardMM Aug 3, 2017
1b901cc
Merge branch 'master' of https://github.com/bashtage/arch
RichardMM Aug 3, 2017
bdd84b3
Add pylint suggestions
RichardMM Aug 3, 2017
17f7e47
Add pylint suggestions
RichardMM Aug 3, 2017
d2dcf94
Add pylint suggestion
RichardMM Aug 3, 2017
1380e05
Modify compute variance to return 2 arrays
RichardMM Aug 3, 2017
9bf03fd
Remove unused method
RichardMM Aug 3, 2017
bdded03
Add longterm var attribute
RichardMM Aug 3, 2017
9e7c159
Fix missing argument bug
RichardMM Aug 3, 2017
b10bae2
Fix optimization bug
RichardMM Aug 3, 2017
352a196
Fix assignment error
RichardMM Aug 3, 2017
6ba62d7
Fix assignment bug
RichardMM Aug 3, 2017
6bae580
Modify doc for longterm_variance
RichardMM Aug 4, 2017
e782651
Add longterm forecast property to VarianceForecast class
RichardMM Aug 4, 2017
6693e42
Modify _one_step_forecast to cater for cgarch
RichardMM Aug 4, 2017
3bda0f7
Modify CGARCH forecast methods
RichardMM Aug 4, 2017
8f56e9a
Fix assignment bug in VarianceForecast
RichardMM Aug 4, 2017
74a0d56
Modify ARCHModelForecast to handle component forecasts
RichardMM Aug 4, 2017
c32ac8d
Modify HARX model to cater for component volatility processes
RichardMM Aug 4, 2017
9b7e680
Fix bug in HARX model forecast return
RichardMM Aug 4, 2017
82b5b7f
Fix bug in cgarch forecasts
RichardMM Aug 4, 2017
b4f3719
Fix bug in handling tuple return
RichardMM Aug 4, 2017
edba0ce
Fix bug in cgarch forecasts
RichardMM Aug 4, 2017
d30c929
Format to conform to pylint
RichardMM Aug 4, 2017
ae62731
Improve docs
RichardMM Aug 4, 2017
095a5f0
Merge from master
RichardMM Aug 4, 2017
4611fea
Add some unneeded files
RichardMM Aug 4, 2017
8d00212
Merge pull request #2 from RichardMM/cgarch
RichardMM Aug 4, 2017
5d3c5d2
Modify docs
RichardMM Aug 4, 2017
870bcb8
Tweak starting values
RichardMM Aug 4, 2017
0e4edca
Modify starting values
RichardMM Aug 5, 2017
3ab7a84
Modify starting values for cgarch
RichardMM Aug 5, 2017
2c3e457
Modify starting values
RichardMM Aug 5, 2017
0cd6f57
Modify cgarch starting values and bounds
RichardMM Aug 5, 2017
101ecf3
Correct typo in docs
RichardMM Aug 5, 2017
e13e29b
Increase iterations for fit
RichardMM Aug 5, 2017
27ff1a2
Modify starting values for cgarch
RichardMM Aug 5, 2017
7f2c208
Add CGarch example notebook
RichardMM Aug 6, 2017
eed02f6
Modifycgarchbounds
RichardMM Aug 6, 2017
feef7e1
Change back iter parameter to 100
RichardMM Aug 6, 2017
16514b7
Modify docs for cgarch
RichardMM Aug 6, 2017
bbff084
Include cgarch in list of volatility models
RichardMM Aug 6, 2017
0df4501
Remove whitespace in blankline
RichardMM Aug 6, 2017
580ae56
Merge pull request #3 from RichardMM/cgarch
RichardMM Aug 6, 2017
1d2d55a
Merge branch 'master' into master
RichardMM Sep 21, 2017
5ad0aae
Correct error in cgarch simulation
RichardMM Sep 24, 2017
61a646f
Add new commits from bashtage and clear conflicts
Nov 19, 2017
327de18
Merge branch 'master' of https://github.com/RichardMM/arch into cgarch
Nov 19, 2017
f0f65c5
Merge branch 'master' of https://github.com/RichardMM/arch into cgarch
Nov 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ build/
dist
.benchmarks/
.cache/
README.rst
README.rst
*.suo

/totestimports.py
*.user
/.vs/config/applicationhost.config
/local_cythonrecursion.py
/.vs/arch/v14/.suo
*.pyproj
*.sln

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

20 changes: 20 additions & 0 deletions arch/tests/univariate/test_recursions.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ def test_egarch(self):
sigma2[t] = np.exp(lnsigma2[t])
assert_almost_equal(sigma2_python, sigma2)

def test_cgarch(self):
T, resids, = self.T, self.resids
sigma2, backcast = self.sigma2, self.backcast

parameters = np.array([0.1, 0.4, 0.75, 0.8, 0.2])
fresids = resids ** 2.0
q2 = np.ndarray(T)
g2 = np.ndarray(T)
recpy.cgarch_recursion(parameters, fresids, sigma2, backcast,
self.var_bounds, g2, q2)
sigma2_numba = sigma2.copy()
recpy.cgarch_recursion_python(parameters, fresids, sigma2,
backcast, self.var_bounds, g2, q2)
sigma2_python = sigma2.copy()
rec.cgarch_recursion(parameters, fresids, sigma2, backcast,
self.var_bounds, g2, q2)

assert_almost_equal(sigma2_numba, sigma2)
assert_almost_equal(sigma2_python, sigma2)

@pytest.mark.skipif(missing_numba or missing_extension, reason='numba not installed')
def test_garch_performance(self):
garch_setup = """
Expand Down
81 changes: 80 additions & 1 deletion arch/tests/univariate/test_volatility.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
except ImportError:
from arch.univariate import recursions_python as rec
from arch.univariate.volatility import GARCH, ARCH, HARCH, ConstantVariance, \
EWMAVariance, RiskMetrics2006, EGARCH, FixedVariance
EWMAVariance, RiskMetrics2006, EGARCH, FixedVariance, CGARCH
from arch.univariate.distribution import Normal, StudentsT, SkewStudent
from arch.compat.python import range

Expand Down Expand Up @@ -91,6 +91,85 @@ def test_garch(self):
assert_equal(garch.o, 0)
assert_equal(garch.q, 1)

def test_cgarch(self):
cgarch = CGARCH()
sv = cgarch.starting_values(self.resids)
assert_equal(sv.shape[0], cgarch.num_params)
parameters = np.array([0.1, 0.4, 0.06, 0.8, 0.2])
bounds = cgarch.bounds(self.resids)
assert_equal(bounds[0], (0, 1))
assert_equal(bounds[1], (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)
assert_almost_equal(backcast,
np.sum((self.resids[:75] ** 2) * (w / w.sum())))
var_bounds = cgarch.variance_bounds(self.resids)
cgarch.compute_variance(parameters, self.resids, self.sigma2,
backcast, var_bounds)
cond_var_direct = np.zeros_like(self.sigma2)
g2 = np.ndarray(self.T)
q2 = g2.copy()
rec.cgarch_recursion(parameters,
self.resids ** 2.0,
cond_var_direct,
backcast,
var_bounds, g2, q2)
assert_allclose(self.sigma2, cond_var_direct)

a, b = cgarch.constraints()
a_target = np.array([[0, 1, 0, 0, -1], [-1, -1, 0, 1, 0], [0, 0, 0, -1, 0]])
b_target = np.array([0, 0, -1])
assert_array_equal(a, a_target)
assert_array_equal(b, b_target)
# test simulated data
state = np.random.get_state()
rng = Normal()
sim_data = cgarch.simulate(parameters, self.T, rng.simulate([]))
np.random.set_state(state)
e = np.random.standard_normal(self.T + 500)
alpha, beta, omega, rho, phi = parameters
converted_params = cgarch._covertparams(parameters)
fromgarch = converted_params[0]/(1-(np.sum(converted_params[1:])))
fromcg = omega/(1-rho)
aver = (fromcg + fromgarch)/2
if (aver > 0.0) and (aver < 0.2):
initial_value = aver
else:
initial_value = 0.1
sigma2 = np.zeros(self.T + 500)
sigma2[0] = initial_value
g2 = np.ndarray(self.T + 500)
q2 = g2.copy()
q2[0] = initial_value * 0.65
g2[0] = initial_value - q2[0]
data = np.zeros(self.T + 500)
data[0] = e[0] * np.sqrt(sigma2[0])

for i in range(1, self.T + 500):
g2[i] = alpha * (data[i - 1]**2 - q2[i - 1]) + beta * g2[i - 1]
q2[i] = omega + rho * q2[i - 1] + phi * (data[i - 1]**2 - sigma2[i - 1])
sigma2[i] = g2[i] + q2[i]
data[i] = e[i] * (np.sqrt(sigma2[i]))

data = data[500:]
sigma2 = sigma2[500:]
assert_almost_equal(data / sim_data[0], np.ones_like(data))
assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2))

names = cgarch.parameter_names()
names_target = ["alpha", "beta", "omega", "rho", "phi"]
assert_equal(names, names_target)

assert isinstance(cgarch.__str__(), str)

assert_equal(cgarch.name, 'ComponentGARCH')
assert_equal(cgarch.num_params, 5)
assert_equal(cgarch.p, 2)
assert_equal(cgarch.q, 2)

def test_garch_power(self):
garch = GARCH(power=1.0)
assert_equal(garch.num_params, 3)
Expand Down
4 changes: 2 additions & 2 deletions arch/univariate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import absolute_import

from arch.univariate.mean import HARX, ConstantMean, ZeroMean, ARX, arch_model, LS
from arch.univariate.volatility import (GARCH, ARCH, HARCH, ConstantVariance, EWMAVariance,
from arch.univariate.volatility import (GARCH, ARCH, HARCH, CGARCH, ConstantVariance, EWMAVariance,
RiskMetrics2006, EGARCH, FixedVariance)
from arch.univariate.distribution import Distribution, Normal, StudentsT, SkewStudent

__all__ = ['HARX', 'ConstantMean', 'ZeroMean', 'ARX', 'arch_model', 'LS',
'GARCH', 'ARCH', 'HARCH', 'ConstantVariance',
'GARCH', 'ARCH', 'HARCH', 'ConstantVariance', 'CGARCH',
'EWMAVariance', 'RiskMetrics2006', 'EGARCH',
'Distribution', 'Normal', 'StudentsT', 'SkewStudent',
'FixedVariance']
Loading