Skip to content

Commit a93ff73

Browse files
authored
Merge pull request #957 from bashtage/a90-dtype1-dtypes
ENH: One data type rule, with complex data in the covariance estimators
2 parents d921c35 + 8fb9911 commit a93ff73

22 files changed

Lines changed: 2105 additions & 209 deletions

‎arch/bootstrap/base.py‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ def optimal_block_length(x: ArrayLike1D | ArrayLike2D) -> pd.DataFrame:
157157
----------
158158
x : array_like
159159
The data, which can be one-dimensional or two-dimensional. Operates
160-
column by column if 2-dimensional. The data are converted to float64
161-
and must be finite and have at least 11 observations.
160+
column by column if 2-dimensional. Integer, Boolean and float32 data are
161+
converted to float64. float16 and complex data are not accepted. The data
162+
must be finite and have at least 11 observations.
162163
163164
Returns
164165
-------
@@ -172,8 +173,8 @@ def optimal_block_length(x: ArrayLike1D | ArrayLike2D) -> pd.DataFrame:
172173
Raises
173174
------
174175
TypeError
175-
If ``x`` cannot be converted to float64 (strings, dates, complex
176-
numbers or None).
176+
If ``x`` cannot be converted to float64 (strings, dates, float16 or
177+
complex data, or None).
177178
ValueError
178179
If ``x`` has more than 2 dimensions, no columns or fewer than 11
179180
observations, or contains NaN or inf.
@@ -367,6 +368,26 @@ def _check_block_size(block_size: Any, integer: bool = True) -> int | float:
367368
return checked
368369

369370

371+
def _replication_dtype(base: np.ndarray) -> type:
372+
"""
373+
The data type of the replications of a statistic.
374+
375+
The imaginary part of a complex statistic must not be discarded when the
376+
replications are stored.
377+
"""
378+
return np.complex128 if base.dtype.kind == "c" else np.float64
379+
380+
381+
def _check_real_statistic(base: np.ndarray) -> None:
382+
"""Raise a TypeError if the statistic is complex, which has no interval."""
383+
if base.dtype.kind == "c":
384+
raise TypeError(
385+
"the value returned by func must be real numbers, since a confidence "
386+
f"interval is not defined for complex values; got {base.dtype} values "
387+
"(ndarray)"
388+
)
389+
390+
370391
def _check_extra_kwargs(
371392
kwargs: dict[str, Any], extra_kwargs: Mapping[str, Any] | None = None
372393
) -> Mapping[str, Any] | None:
@@ -817,7 +838,8 @@ def conf_int(
817838
If ``func`` or ``std_err_func`` is not callable, ``reps`` or
818839
``studentize_reps`` is not an int, ``size`` is not a number,
819840
``reuse`` is not a bool, ``method``, ``tail`` or ``sampling`` is not
820-
a str, or ``extra_kwargs`` is not a dict.
841+
a str, ``extra_kwargs`` is not a dict, or ``func`` returns complex
842+
values, which have no confidence interval.
821843
ValueError
822844
If ``reps`` is less than 1, ``size`` is not in (0, 1),
823845
``studentize_reps`` is less than 1 when it is used, ``method``,
@@ -940,11 +962,13 @@ def conf_int(
940962
std_err_func=std_err_func,
941963
studentize_reps=studentize_reps,
942964
sampling=sampling,
965+
real_only=True,
943966
)
944967

945968
base, results = self._base, self._results
946969
assert results is not None
947970
assert base is not None
971+
_check_real_statistic(base)
948972
studentized_results = self._studentized_results
949973

950974
std_err = np.empty((0,))
@@ -1124,7 +1148,8 @@ def apply(
11241148
-------
11251149
ndarray
11261150
The reps by nparam array of computed function values where each row
1127-
corresponds to a bootstrap iteration.
1151+
corresponds to a bootstrap iteration. The array is complex128 if
1152+
func returns complex values and float64 otherwise.
11281153
11291154
Raises
11301155
------
@@ -1164,7 +1189,7 @@ def apply(
11641189
kwargs = _add_extra_kwargs(self._kwargs, extra_kwargs)
11651190
base = np.asarray(func(*self._args, **kwargs))
11661191
num_params = 1 if base.ndim == 0 else base.shape[0]
1167-
results = np.zeros((reps, num_params))
1192+
results = np.zeros((reps, num_params), dtype=_replication_dtype(base))
11681193
count = 0
11691194
for pos_data, kw_data in self.bootstrap(reps):
11701195
kwargs = _add_extra_kwargs(kw_data, extra_kwargs)
@@ -1182,6 +1207,7 @@ def _construct_bootstrap_estimates(
11821207
sampling: Literal[
11831208
"nonparametric", "semi-parametric", "semi", "parametric", "semiparametric"
11841209
] = "nonparametric",
1210+
real_only: bool = False,
11851211
) -> None:
11861212
eps = np.finfo(np.double).eps
11871213
# Private, more complicated version of apply
@@ -1195,9 +1221,11 @@ def _construct_bootstrap_estimates(
11951221
kwargs = _add_extra_kwargs(self._kwargs, extra_kwargs)
11961222
# Allows lists, tuples, scalars and 0-d arrays
11971223
base = np.asarray(func(*self._args, **kwargs))
1224+
if real_only:
1225+
_check_real_statistic(base)
11981226

11991227
num_params = 1 if base.ndim == 0 else base.shape[0]
1200-
results = np.zeros((reps, num_params))
1228+
results = np.zeros((reps, num_params), dtype=_replication_dtype(base))
12011229
studentized_results = np.zeros((reps, num_params))
12021230

12031231
count = 0
@@ -1263,7 +1291,9 @@ def cov(
12631291
Returns
12641292
-------
12651293
ndarray
1266-
Bootstrap covariance estimator.
1294+
Bootstrap covariance estimator. If func returns complex values, this
1295+
is the complex Hermitian covariance, which uses the conjugate
1296+
transpose, and a complex128 array.
12671297
12681298
Raises
12691299
------
@@ -1339,6 +1369,9 @@ def _bootstrap_cov(
13391369
else:
13401370
errors = results - base
13411371

1372+
if np.iscomplexobj(errors):
1373+
# Hermitian covariance of a complex statistic
1374+
return errors.T.dot(errors.conj()) / reps
13421375
return errors.T.dot(errors) / reps
13431376

13441377
def var(
@@ -1368,7 +1401,9 @@ def var(
13681401
Returns
13691402
-------
13701403
ndarray
1371-
Bootstrap variance estimator.
1404+
Bootstrap variance estimator. If func returns complex values, this
1405+
is the real variance, the average of the squared absolute values of
1406+
the errors.
13721407
13731408
Raises
13741409
------
@@ -1432,6 +1467,9 @@ def var(
14321467
else:
14331468
errors = results - base
14341469

1470+
if np.iscomplexobj(errors):
1471+
# The variance of a complex statistic is real
1472+
return (errors.real**2 + errors.imag**2).sum(0) / reps
14351473
return (errors**2).sum(0) / reps
14361474

14371475
def update_indices(self) -> BootstrapIndexT:

0 commit comments

Comments
 (0)