nrdk.tss.stats
¶
Time series analysis.
nrdk.tss.stats.NDStats
dataclass
¶
Mean, variance, and ESS tracking for a n-dimensional stack of values.
Usage
- Initialize
NDStats
by either providing pre-computedn
,m1
,m2
, andess
values, or by usingfrom_values
to compute these for you. - Multiple
NDStats
can be stacked usingstack
; stacking can be performed multiple times. - If multiple sequences are provided to
from_values
, statistic computation (parallelized using a thread pool) and stacking is performed automatically.
Attributes:
Name | Type | Description |
---|---|---|
n |
Integer[ndarray, '*shape'] | integer
|
number of samples. |
m1 |
Float[ndarray, '*shape'] | floating
|
sum of values, i.e. accumulated first moment. |
m2 |
Float[ndarray, '*shape'] | floating
|
sum of squares, i.e. accumulated second moment. |
ess |
Float[ndarray, '*shape'] | floating
|
effective sample size estimate. |
Source code in src/nrdk/tss/stats.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
std
property
¶
Unbiased estimate of the sample standard deviation.
stderr
property
¶
Sample standard error, with effective sample size correction.
zscore
property
¶
Z-score, assuming a zero null hypothesis.
as_df
¶
Convert to a pandas dataframe.
Creates these columns:
Warning
This NDStats
must be a 1-dimensional vector of statistics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
Sequence[str]
|
name of each entry in the |
required |
prefix
|
str
|
optional prefix to add to each column. |
''
|
Returns:
Type | Description |
---|---|
DataFrame
|
A dataframe, where each entry is a row. |
Source code in src/nrdk/tss/stats.py
from_values
classmethod
¶
from_values(
values: NestedValues[Num[ndarray, _N]],
workers: int = -1,
t_max: int | None = None,
) -> NDStats
Initialize from 1-dimensional time series of values.
Info
If multiple time series are provided, the statistics are computed for each sequence, and the results are stacked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
NestedValues[Num[ndarray, _N]]
|
input time series; the first two moments and effective sample size are computed. |
required |
workers
|
int
|
number of parallel workers to use (only used in the case
of multiple time series). If |
-1
|
t_max
|
int | None
|
maximum time delay to consider when computing effective
sample size; if |
None
|
Returns:
Type | Description |
---|---|
NDStats
|
Computed |
Source code in src/nrdk/tss/stats.py
reshape
¶
stack
staticmethod
¶
stack(*stats) -> NDStats
Stack multiple NDStats containers.
Source code in src/nrdk/tss/stats.py
sum
¶
Get aggregate values.
If this NDStats
is not a vector, this simply returns the identity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis
|
int
|
axis to sum across. |
0
|
Returns:
Type | Description |
---|---|
NDStats
|
A new |
Source code in src/nrdk/tss/stats.py
nrdk.tss.stats.__pmean
¶
Calculate the partial mean for the first n
items.
Includes the remaining elements:
Source code in src/nrdk/tss/stats.py
nrdk.tss.stats.autocorrelation
¶
Calculate autocorrelation for time delays up to N/2.
Info
This method is O(n log n)
: first and second moments are calculated
with running means (O(n)
), while cross correlations are calculated
using the fft-based scipy.signal.correlate
(O(n log n)
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Num[ndarray, N]
|
time series data. |
required |
Returns:
Type | Description |
---|---|
Float[ndarray, N2] | None
|
Autocorrelation for time delays up to |
Source code in src/nrdk/tss/stats.py
nrdk.tss.stats.effective_sample_size
¶
Calculate effective sample size (ESS) for a univariate time series.
Let x
have N
samples. For autocorrelation rho_t
, where t
is the
delay, in samples, we use the estimate:
Info
This estimate is commonly used to estimate effective sample sizes for Markov Chain Monte Carlo (MCMC) techniques, though it is readily adaptable to other time series analysis tasks.
Note
In our estimate, we sum up to t = N/2
(the maximum value for which
rho_t
is empirically estimatable), as long as rho_t
is positive.
A simplified implementation is as follows:
rho = np.array([
np.cov(x[i:], x[:-i])[0, 1] / np.std(x[i:]) / np.std(x[:-i])
for i in range(1, x.shape[0] // 2)])
rho_valid = rho[:np.argmax(rho < 0)]
return x.shape[0] / (1 + 2 * np.sum(rho)
Our implementation is O(n log n)
, and is optimized to reuse moment
calculations and partial sums within moment calculations, as well as
use scipy's FFT-based
correlate
for calculating x[i:] * x[-i:]
for i = 1, 2, ... N / 2
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Num[ndarray, t]
|
time series data. |
required |
t_max
|
int | None
|
maximum time delay to consider; if |
None
|
Returns:
Type | Description |
---|---|
float
|
ESS estimate. In the edge case that |