Bootstrap¶
circumplex.analysis.bootstrap
¶
Bootstrap confidence interval calculation for SSM analysis.
This module implements bootstrap resampling with confidence interval calculation, including special handling for circular displacement data.
| FUNCTION | DESCRIPTION |
|---|---|
circular_quantile |
Calculate quantiles for circular data in radians. |
ssm_bootstrap |
Perform stratified bootstrap with confidence intervals. |
calculate_confidence_intervals |
Calculate confidence intervals from bootstrap results. |
circular_quantile
¶
circular_quantile(angles: NDArray[Any, Float], probs: list[float] | NDArray[Shape[Any], Float]) -> NDArray[Shape[Any], Float]
Calculate quantiles for circular data in radians.
Implements a circular quantile method that accounts for the periodic nature of angular data. Centers angles around their mean direction, calculates linear quantiles, then transforms back.
| PARAMETER | DESCRIPTION |
|---|---|
angles
|
Array of angles in radians, shape (n,)
TYPE:
|
probs
|
Probability points at which to calculate quantiles (e.g., [0.025, 0.975]) |
| RETURNS | DESCRIPTION |
|---|---|
NDArray[Shape[Any], Float]
|
Quantiles at the requested probability points |
Examples:
>>> angles = np.array([0.1, 0.2, 6.2, 6.3]) # Two near 0, two near 2π
>>> circular_quantile(angles, [0.25, 0.75])
array([6.25..., 0.15...])
Notes
This function mirrors the quantile.circumplex_radian method from the R package (R/ssm_bootstrap.R lines 72-82). It: 1. Computes mean direction using atan2 2. Centers all angles around the mean 3. Calculates linear quantiles on centered data 4. Transforms back to [0, 2π)
Source code in src/circumplex/analysis/bootstrap.py
ssm_bootstrap
¶
ssm_bootstrap(data: DataFrame, bootstrap_fn: Callable[[DataFrame, NDArray[Shape[Any], Float]], NDArray[Shape[Any], Float]], boots: int = 2000, grouping_col: str | None = None, *, seed: int | None = None) -> dict[str, Any]
Perform stratified bootstrap with confidence intervals.
Executes bootstrap resampling with stratification by group (if specified),
calculates point estimates and and bootstrap replicats;
use calculate_confidence_intervals() to derive confidence intervals.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
DataFrame containing all data for bootstrap sampling
TYPE:
|
bootstrap_fn
|
Function that takes (data, resample_indices) and returns flat array of parameters for all groups/measures
TYPE:
|
boots
|
Number of bootstrap resamples
TYPE:
|
grouping_col
|
Name of grouping column for stratified sampling. If None, uses simple random sampling.
TYPE:
|
seed
|
Random seed for reproducibility
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Dictionary containing:
|
Examples:
>>> def simple_mean(df, indices):
... return np.array([df.iloc[indices]['x'].mean()])
>>> data = pd.DataFrame({'x': [1, 2, 3, 4, 5]})
>>> result = ssm_bootstrap(data, simple_mean, boots=100, seed=123)
>>> result['t0'] # Observed mean
array([3.])
Notes
This function mirrors ssm_bootstrap() from the R package (R/ssm_bootstrap.R lines 1-55). It uses stratified sampling when a grouping variable is provided to ensure each bootstrap sample maintains the original group proportions.
Source code in src/circumplex/analysis/bootstrap.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
calculate_confidence_intervals
¶
calculate_confidence_intervals(bootstrap_results: dict[str, Any], interval: float = 0.95) -> pd.DataFrame
Calculate confidence intervals from bootstrap results.
Computes percentile confidence intervals for all parameters, with special circular handling for displacement parameters.
| PARAMETER | DESCRIPTION |
|---|---|
bootstrap_results
|
Dictionary from ssm_bootstrap() containing |
interval
|
Confidence level (e.g., 0.95 for 95% CI)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with columns:
|
Notes
This function mirrors the CI calculation in R's ssm_bootstrap() (R/ssm_bootstrap.R lines 38-54). It uses percentile method for linear parameters and circular_quantile() for displacement.