kups.core.utils.block_average
¶
Block averaging for error estimation in correlated time series.
This module implements block averaging to estimate the standard error of the mean for time series data with autocorrelation. By dividing the time series into independent blocks, the variance of block means provides an unbiased estimate of the standard error.
BlockAverageResult
¶
Bases: NamedTuple
Result of block averaging analysis.
Attributes:
| Name | Type | Description |
|---|---|---|
mean |
Array
|
Mean value over the entire time series. Shape matches input with time axis removed. |
sem |
Array
|
Standard error of the mean estimated from block variance.
Shape matches |
n_blocks |
int
|
Number of blocks used in the analysis. |
Source code in src/kups/core/utils/block_average.py
BlockTransformResult
¶
Bases: NamedTuple
Result of block transformation analysis.
Attributes:
| Name | Type | Description |
|---|---|---|
block_sizes |
Array
|
Array of block sizes tested. |
sems |
Array
|
Standard error estimates for each block size. Shape is
|
mean |
Array
|
Overall mean of the data. |
Source code in src/kups/core/utils/block_average.py
block_average(data, n_blocks, axis=0)
¶
Compute block average statistics for error estimation.
Divides the time series into blocks and uses the variance of block means to estimate the standard error of the overall mean. This accounts for autocorrelation in the data.
The standard error is computed as:
where \(\sigma_{\text{blocks}}\) is the standard deviation of block means.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Input array with time series data along |
required |
n_blocks
|
int
|
Number of blocks to divide the data into. Samples that don't fit into complete blocks are discarded from the end. |
required |
axis
|
int
|
Axis along which to perform block averaging. Defaults to 0. |
0
|
Returns:
| Type | Description |
|---|---|
BlockAverageResult
|
BlockAverageResult containing mean, standard error, and number of blocks. |
Example
# Time series of 1000 samples
data = jnp.array([...]) # shape (1000,)
result = block_average(data, n_blocks=10)
print(f"Mean: {result.mean:.3f} +/- {result.sem:.3f}")
# Multidimensional data: 1000 timesteps, 3 components
data = jnp.array([...]) # shape (1000, 3)
result = block_average(data, n_blocks=10, axis=0)
# result.mean has shape (3,), result.sem has shape (3,)
Note
For reliable error estimates, choose n_blocks such that each block
is longer than the autocorrelation time of the data.
Source code in src/kups/core/utils/block_average.py
block_average_from_blocks(block_values, axis=0)
¶
Compute block average statistics from pre-computed block values.
Use this when you have already computed per-block values (e.g., derived quantities like heat of adsorption computed per block).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_values
|
Array
|
Array where |
required |
axis
|
int
|
Axis along which blocks are indexed. Defaults to 0. |
0
|
Returns:
| Type | Description |
|---|---|
BlockAverageResult
|
BlockAverageResult with mean and SEM computed from block values. |
Example
Source code in src/kups/core/utils/block_average.py
block_transform(data, n_blocks_range=None, axis=0, min_blocks=4)
¶
Compute SEM across a range of block sizes for plateau detection.
Performs block averaging for multiple block counts to enable identification of the plateau where SEM stabilizes. The plateau value corresponds to the true standard error when blocks are longer than the autocorrelation time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Input array with time series data along |
required |
n_blocks_range
|
Sequence[int] | None
|
Sequence of block counts to test. If None, automatically
generates a geometric series from |
None
|
axis
|
int
|
Axis along which to perform block averaging. Defaults to 0. |
0
|
min_blocks
|
int
|
Minimum number of blocks when auto-generating range. Defaults to 4. |
4
|
Returns:
| Type | Description |
|---|---|
BlockTransformResult
|
BlockTransformResult containing block sizes, corresponding SEMs, and |
BlockTransformResult
|
the overall mean. |
Example
Source code in src/kups/core/utils/block_average.py
compute_block_means(data, n_blocks, axis=0)
¶
Compute per-block means without aggregating across blocks.
This is the core building block for block averaging. Use this when you need per-block values for computing derived quantities before averaging.
Note
This function is not JIT-compiled directly. When used inside a JIT-compiled function, the compilation will include this code. When used standalone, consider wrapping the calling code with JIT for performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Input array with time series data along |
required |
n_blocks
|
int
|
Number of blocks to divide the data into. Samples that don't fit into complete blocks are discarded from the end. |
required |
axis
|
int
|
Axis along which to compute block means. Defaults to 0. |
0
|
Returns:
| Type | Description |
|---|---|
Array
|
Array of per-block means. The |
Array
|
E.g., input shape (1000, 3) with n_blocks=10, axis=0 → output (10, 3). |
Example
Source code in src/kups/core/utils/block_average.py
optimal_block_average(data, axis=0, min_blocks=4, rtol=0.05)
¶
Automatically select optimal block size and compute block average.
Performs block transformation analysis and detects the plateau where SEM stabilizes. Returns the block average result at the optimal block size.
The plateau is detected by finding where the relative change in SEM falls
below rtol. For multidimensional data, the maximum relative change across
all elements is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Input array with time series data along |
required |
axis
|
int
|
Axis along which to perform block averaging. Defaults to 0. |
0
|
min_blocks
|
int
|
Minimum number of blocks to consider. Defaults to 4. |
4
|
rtol
|
float
|
Relative tolerance for plateau detection. The plateau is reached when the relative change in SEM is below this threshold. Defaults to 0.05 (5%). |
0.05
|
Returns:
| Type | Description |
|---|---|
BlockAverageResult
|
BlockAverageResult at the optimal block size. |