bootstrap_metric

Defined in fynance.research

bootstrap_metric(returns, metric, *, n_paths=1000, block=21, method='stationary', ci=0.95, seed=0)[source]

Block-bootstrap percentile confidence interval for an arbitrary metric.

Computes metric on the observed returns, then again on n_paths block-bootstrap replicates from resample_paths, and reports the percentile confidence interval of the replicate distribution.

Parameters:
returnsarray-like

Return series, shape (T,).

metriccallable

returns -> float, e.g. numpy.mean or a custom Sharpe-like statistic. Applied identically to the observed series and to every resampled path.

n_pathsint

Number of bootstrap replicates.

blockint

Block length ('circular') or mean block length ('stationary'), forwarded to resample_paths.

method{‘circular’, ‘stationary’}

Resampling scheme, forwarded to resample_paths.

cifloat

Confidence level in (0, 1), e.g. 0.95 for a 95% CI.

seedint

Seed for reproducibility, forwarded to resample_paths.

Returns:
dict

estimate (metric on the observed data), lo/hi (percentile CI bounds), distribution (the (n_paths,) array of per-replicate metric values).

Raises:
ValueError

If ci is not in (0, 1) (other invalid inputs are caught by resample_paths).

Examples

>>> import numpy as np
>>> from fynance.research import bootstrap_metric
>>> r = np.array([0.01, -0.02, 0.015, 0.005, -0.01, 0.02, 0.0, 0.01])
>>> out = bootstrap_metric(r, np.mean, n_paths=500, block=2, seed=0)
>>> sorted(out)
['distribution', 'estimate', 'hi', 'lo']
>>> round(out['estimate'], 5)
0.00375
>>> out['distribution'].shape
(500,)
>>> out['lo'] <= out['hi']
True