1
- from itertools import product
2
1
from typing import Optional , Tuple
3
2
4
3
import numpy as np
5
4
from numpy .testing import assert_allclose
6
5
import pandas as pd
7
6
import pytest
8
7
8
+ from arch .covariance .kernel import CovarianceEstimate
9
9
from arch .covariance .var import PreWhitenRecoloredCovariance
10
10
from arch .typing import NDArray
11
11
12
- DATA_PARAMS = list (product ([1 , 3 ], [True , False ], [0 ])) # , 1, 3]))
13
- DATA_IDS = [f"dim: { d } , pandas: { p } , order: { o } " for d , p , o in DATA_PARAMS ]
14
12
KERNELS = [
15
13
"Bartlett" ,
16
14
"Parzen" ,
@@ -32,40 +30,6 @@ def kernel(request):
32
30
return request .param
33
31
34
32
35
- @pytest .fixture (scope = "module" , params = DATA_PARAMS , ids = DATA_IDS )
36
- def data (request ):
37
- dim , pandas , order = request .param
38
- rs = np .random .RandomState ([839084 , 3823810 , 982103 , 829108 ])
39
- burn = 100
40
- shape = (burn + 500 ,)
41
- if dim > 1 :
42
- shape += (3 ,)
43
- rvs = rs .standard_normal (shape )
44
- phi = np .zeros ((order , dim , dim ))
45
- if order > 0 :
46
- phi [0 ] = np .eye (dim ) * 0.4 + 0.1
47
- for i in range (1 , order ):
48
- phi [i ] = 0.3 / (i + 1 ) * np .eye (dim )
49
- for i in range (order , burn + 500 ):
50
- for j in range (order ):
51
- if dim == 1 :
52
- rvs [i ] += np .squeeze (phi [j ] * rvs [i - j - 1 ])
53
- else :
54
- rvs [i ] += phi [j ] @ rvs [i - j - 1 ]
55
- if order > 1 :
56
- p = np .eye (dim * order , dim * order , - dim )
57
- for j in range (order ):
58
- p [:dim , j * dim : (j + 1 ) * dim ] = phi [j ]
59
- v , _ = np .linalg .eig (p )
60
- assert np .max (np .abs (v )) < 1
61
- rvs = rvs [burn :]
62
- if pandas and dim == 1 :
63
- return pd .Series (rvs , name = "x" )
64
- elif pandas :
65
- return pd .DataFrame (rvs , columns = [f"x{ i } " for i in range (dim )])
66
- return rvs
67
-
68
-
69
33
def direct_var (
70
34
x , const : bool , full_order : int , diag_order : int , max_order : Optional [int ] = None
71
35
) -> Tuple [NDArray , NDArray ]:
@@ -140,29 +104,36 @@ def direct_ic(
140
104
@pytest .mark .parametrize ("diag_order" , [3 , 5 ])
141
105
@pytest .mark .parametrize ("max_order" , [None , 10 ])
142
106
@pytest .mark .parametrize ("ic" , ["aic" , "bic" , "hqc" ])
143
- def test_direct_var (data , const , full_order , diag_order , max_order , ic ):
144
- direct_ic (data , ic , const , full_order , diag_order , max_order )
107
+ def test_direct_var (covariance_data , const , full_order , diag_order , max_order , ic ):
108
+ direct_ic (covariance_data , ic , const , full_order , diag_order , max_order )
145
109
146
110
147
111
@pytest .mark .parametrize ("center" , [True , False ])
148
112
@pytest .mark .parametrize ("diagonal" , [True , False ])
149
113
@pytest .mark .parametrize ("method" , ["aic" , "bic" , "hqc" ])
150
- def test_ic (data , center , diagonal , method ):
114
+ def test_ic (covariance_data , center , diagonal , method ):
151
115
pwrc = PreWhitenRecoloredCovariance (
152
- data , center = center , diagonal = diagonal , method = method , bandwidth = 0.0 ,
116
+ covariance_data , center = center , diagonal = diagonal , method = method , bandwidth = 0.0 ,
153
117
)
154
118
cov = pwrc .cov
155
- expected_type = np .ndarray if isinstance (data , np .ndarray ) else pd .DataFrame
119
+ expected_type = (
120
+ np .ndarray if isinstance (covariance_data , np .ndarray ) else pd .DataFrame
121
+ )
156
122
assert isinstance (cov .short_run , expected_type )
157
- expected_max_lag = int (data .shape [0 ] ** (1 / 3 ))
123
+ expected_max_lag = int (covariance_data .shape [0 ] ** (1 / 3 ))
158
124
assert pwrc ._max_lag == expected_max_lag
159
125
expected_ics = {}
160
126
for full_order in range (expected_max_lag + 1 ):
161
127
diag_limit = expected_max_lag + 1 if diagonal else full_order + 1
162
128
for diag_order in range (full_order , diag_limit ):
163
129
key = (full_order , diag_order )
164
130
expected_ics [key ] = direct_ic (
165
- data , method , center , full_order , diag_order , max_order = expected_max_lag
131
+ covariance_data ,
132
+ method ,
133
+ center ,
134
+ full_order ,
135
+ diag_order ,
136
+ max_order = expected_max_lag ,
166
137
)
167
138
assert tuple (sorted (pwrc ._ics .keys ())) == tuple (sorted (expected_ics .keys ()))
168
139
for key in expected_ics :
@@ -175,13 +146,18 @@ def test_ic(data, center, diagonal, method):
175
146
@pytest .mark .parametrize ("diagonal" , [True , False ])
176
147
@pytest .mark .parametrize ("method" , ["aic" , "bic" , "hqc" ])
177
148
@pytest .mark .parametrize ("lags" , [0 , 1 , 3 ])
178
- def test_short_long_run (data , center , diagonal , method , lags ):
149
+ def test_short_long_run (covariance_data , center , diagonal , method , lags ):
179
150
pwrc = PreWhitenRecoloredCovariance (
180
- data , center = center , diagonal = diagonal , method = method , lags = lags , bandwidth = 0.0 ,
151
+ covariance_data ,
152
+ center = center ,
153
+ diagonal = diagonal ,
154
+ method = method ,
155
+ lags = lags ,
156
+ bandwidth = 0.0 ,
181
157
)
182
158
cov = pwrc .cov
183
159
full_order , diag_order = pwrc ._order
184
- params , resids = direct_var (data , center , full_order , diag_order )
160
+ params , resids = direct_var (covariance_data , center , full_order , diag_order )
185
161
nobs , nvar = resids .shape
186
162
expected_short_run = resids .T @ resids / nobs
187
163
assert_allclose (cov .short_run , expected_short_run )
@@ -195,12 +171,29 @@ def test_short_long_run(data, center, diagonal, method, lags):
195
171
assert_allclose (cov .long_run , expected_long_run )
196
172
197
173
174
+ @pytest .mark .parametrize ("force_int" , [True , False ])
175
+ def test_pwrc_attributes (covariance_data , force_int ):
176
+ pwrc = PreWhitenRecoloredCovariance (covariance_data , force_int = force_int )
177
+ assert isinstance (pwrc .bandwidth_scale , float )
178
+ assert isinstance (pwrc .kernel_const , float )
179
+ assert isinstance (pwrc .rate , float )
180
+ assert isinstance (pwrc ._weights (), np .ndarray )
181
+ assert pwrc .force_int == force_int
182
+ expected_type = (
183
+ np .ndarray if isinstance (covariance_data , np .ndarray ) else pd .DataFrame
184
+ )
185
+ assert isinstance (pwrc .cov .short_run , expected_type )
186
+ assert isinstance (pwrc .cov .long_run , expected_type )
187
+ assert isinstance (pwrc .cov .one_sided , expected_type )
188
+ assert isinstance (pwrc .cov .one_sided_strict , expected_type )
189
+
190
+
198
191
@pytest .mark .parametrize ("sample_autocov" , [True , False ])
199
- def test_data (data , sample_autocov ):
192
+ def test_data (covariance_data , sample_autocov ):
200
193
pwrc = PreWhitenRecoloredCovariance (
201
- data , sample_autocov = sample_autocov , bandwidth = 0.0
194
+ covariance_data , sample_autocov = sample_autocov , bandwidth = 0.0
202
195
)
203
- pwrc .cov
196
+ assert isinstance ( pwrc .cov , CovarianceEstimate )
204
197
205
198
206
199
def test_pwrc_errors ():
@@ -216,4 +209,9 @@ def test_pwrc_errors():
216
209
def test_pwrc_warnings ():
217
210
x = np .random .standard_normal ((9 , 5 ))
218
211
with pytest .warns (RuntimeWarning , match = "The maximum number of lags is 0" ):
219
- PreWhitenRecoloredCovariance (x ).cov
212
+ assert isinstance (PreWhitenRecoloredCovariance (x ).cov , CovarianceEstimate )
213
+
214
+
215
+ def test_unknown_kernel (covariance_data ):
216
+ with pytest .raises (ValueError , match = "" ):
217
+ PreWhitenRecoloredCovariance (covariance_data , kernel = "unknown" )
0 commit comments