Time Series Features
Feature Engineering Queries and Time Series Features
Time series features.
Functions:
| Name | Description |
|---|---|
query_abs_energy |
Absolute energy is defined as Sum(x_i^2). |
query_approx_entropy |
Approximate sample entropies of a time series given the filtering level. It is highly |
query_ar_coeffs |
Computes the autoregressive coefficients for the given lag. The bias/intercept term will be the last value in the |
query_auto_corr |
Computes the auto correlation with the given lag. |
query_avg_streak |
Finds the average streak length where the condition |
query_c3_stats |
Measure of non-linearity in the time series using c3 statistics. |
query_cid_ce |
Estimates the time series complexity. |
query_cond_entropy |
Queries the conditional entropy of x on y, aka. H(x|y). |
query_cond_indep |
Computes the conditional independance of |
query_copula_entropy |
Estimates Copula Entropy via rank statistics. |
query_count_uniques |
Returns the count of unique values. |
query_cv |
Returns the coefficient of variation for the variable. This is a shorthand for std / mean. |
query_entropy |
Computes the entropy of any discrete column. This is shorthand for x.unique_counts().entropy() |
query_first_digit_cnt |
Finds the first digit count in the data. This is closely related to Benford's law, |
query_knn_entropy |
Computes KNN entropy among all the rows. |
query_lempel_ziv |
Computes Lempel Ziv complexity on a boolean column. Null will be mapped to False. |
query_longest_streak |
Finds the longest streak length where the condition |
query_mean_abs_change |
Returns the mean of all successive differences |X_i - X_i-1| |
query_mean_n_abs_max |
Returns the average of the top |
query_mid_range |
A shorthand for (pl.col(x).max() - pl.col(x).min()) / 2. |
query_permute_entropy |
Computes permutation entropy. |
query_range_count |
Returns the number of values inside [ |
query_sample_entropy |
Calculate the sample entropy of this column. It is highly |
query_similar_count |
Given a query subsequence, find the number of same-sized subsequences (windows) in target |
query_streak |
Finds the streak length where the condition |
query_symm_ratio |
Returns the symmetric ratio: |mean - median| / (max - min). Note the closer to 0 this value is, |
query_time_reversal_asymmetry_stats |
Queries the Time Reversal Asymmetry Statistic, which is the average of |
query_transfer_entropy |
Estimating transfer entropy from |
query_abs_energy(x)
Absolute energy is defined as Sum(x_i^2).
Source code in python/polars_ds/exprs/ts_features.py
68 69 70 71 72 73 | |
query_approx_entropy(ts, m, filtering_level, scale_by_std=True, parallel=True)
Approximate sample entropies of a time series given the filtering level. It is highly recommended that the user impute nulls before calling this.
If NaN/some error is returned/thrown, it is likely that: (1) Too little data, e.g. m + 1 > length (2) filtering_level or (filtering_level * std) is too close to 0 or std is null/NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
str | Expr
|
A time series |
required |
m
|
int
|
Length of compared runs of data. This is |
required |
filtering_level
|
float
|
Filtering level, must be positive. This is |
required |
scale_by_std
|
bool
|
Whether to scale filter level by std of data. In most applications, this is the default behavior, but not in some other cases. |
True
|
parallel
|
bool
|
Whether to run this in parallel or not. This is recommended when you are running only this expression, and not in group_by context. |
True
|
Reference
https://en.wikipedia.org/wiki/Approximate_entropy
Source code in python/polars_ds/exprs/ts_features.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
query_ar_coeffs(x, lag, add_bias=True, null_policy='raise')
Computes the autoregressive coefficients for the given lag. The bias/intercept term will be the last value in the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str | Expr
|
The feature |
required |
lag
|
int
|
The lag |
required |
add_bias
|
bool
|
Whether to add a bias/intercept term |
True
|
null_policy
|
NullPolicy
|
One of "raise", "one", "zero", or a finite numeric string. |
'raise'
|
Source code in python/polars_ds/exprs/ts_features.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
query_auto_corr(x, lag, ddof=0, normalize=True)
Computes the auto correlation with the given lag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str | Expr
|
The feature |
required |
lag
|
int
|
The lag |
required |
ddof
|
int
|
The ddof for the variance |
0
|
normalize
|
bool
|
Whether to normalize the value to [-1, 1] or not. |
True
|
Source code in python/polars_ds/exprs/ts_features.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
query_avg_streak(where)
Finds the average streak length where the condition where is true. The average is taken on
the true set.
Note: the query is still runnable when where doesn't represent boolean column / boolean expressions.
However, if that is the case the answer will not be easily interpretable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
str | Expr
|
If where is string, the string must represent the name of a string column. If where is an expression, the expression must evaluate to some boolean expression. |
required |
Source code in python/polars_ds/exprs/ts_features.py
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 | |
query_benford(var)
Finds the first digit counts which is used in Benford's law. This is an alias to
query_first_digit_cnt.
Source code in python/polars_ds/exprs/ts_features.py
231 232 233 234 235 236 | |
query_c3_stats(x, lag)
Measure of non-linearity in the time series using c3 statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Expr
|
Either the name of the column or a Polars expression |
required |
lag
|
int
|
The lag that should be used in the calculation of the feature. |
required |
Reference
https://arxiv.org/pdf/chao-dyn/9909043
Source code in python/polars_ds/exprs/ts_features.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
query_cid_ce(x, normalize=False)
Estimates the time series complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Expr
|
Either the name of the column or a Polars expression |
required |
normalize
|
bool
|
If True, z-normalizes the time-series before computing the feature. Default is False. |
False
|
Reference
https://www.cs.ucr.edu/~eamonn/Complexity-Invariant%20Distance%20Measure.pdf
Source code in python/polars_ds/exprs/ts_features.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
query_cond_entropy(x, y)
Queries the conditional entropy of x on y, aka. H(x|y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str | Expr
|
Either a string or a polars expression |
required |
y
|
str | Expr
|
Either a string or a polars expression |
required |
Source code in python/polars_ds/exprs/ts_features.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
query_cond_indep(x, y, z, k=3, parallel=False)
Computes the conditional independance of x and y, conditioned on z
Reference
Jian Ma. Multivariate Normality Test with Copula Entropy. arXiv preprint arXiv:2206.05956, 2022.
Source code in python/polars_ds/exprs/ts_features.py
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
query_copula_entropy(*features, k=3, parallel=False)
Estimates Copula Entropy via rank statistics.
Reference
Jian Ma and Zengqi Sun. Mutual information is copula entropy. Tsinghua Science & Technology, 2011, 16(1): 51-54.
Source code in python/polars_ds/exprs/ts_features.py
682 683 684 685 686 687 688 689 690 691 | |
query_count_uniques(x)
Returns the count of unique values.
Source code in python/polars_ds/exprs/ts_features.py
107 108 109 110 111 | |
query_cv(x, ddof=1)
Returns the coefficient of variation for the variable. This is a shorthand for std / mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str | Expr
|
The variable |
required |
ddof
|
int
|
The delta degree of frendom used in std computation |
1
|
Source code in python/polars_ds/exprs/ts_features.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
query_entropy(x, base=math.e, normalize=True)
Computes the entropy of any discrete column. This is shorthand for x.unique_counts().entropy()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str | Expr
|
Either a string or a polars expression |
required |
base
|
float
|
Base for the log in the entropy computation |
e
|
normalize
|
bool
|
Normalize if the probabilities don't sum to 1. |
True
|
Source code in python/polars_ds/exprs/ts_features.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
query_first_digit_cnt(var)
Finds the first digit count in the data. This is closely related to Benford's law, which states that the the first digits (1-9) follow a certain distribution.
The output is a single element column of type list[u32]. The first value represents the count of 1s that are the first digit, the second value represents the count of 2s that are the first digit, etc.
E.g. first digit of 12 is 1, of 0.0312 is 3. For integers, it is possible to have value = 0, and this will not be counted as a first digit.
Reference
https://en.wikipedia.org/wiki/Benford%27s_law
Source code in python/polars_ds/exprs/ts_features.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
query_knn_entropy(*features, k=3, dist='l2', parallel=False)
Computes KNN entropy among all the rows.
Note if rows <= k, NaN will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*features
|
str | Expr
|
Columns used as features |
()
|
k
|
int
|
The number of nearest neighbor to consider. Usually 2 or 3. |
3
|
dist
|
Literal[`l2`, `inf`]
|
Note |
'l2'
|
parallel
|
bool
|
Whether to run the distance query in parallel. This is recommended when you are running only this expression, and not in group_by context. |
False
|
Reference
https://arxiv.org/pdf/1506.06501v1.pdf
Source code in python/polars_ds/exprs/ts_features.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
query_lempel_ziv(b, as_ratio=True)
Computes Lempel Ziv complexity on a boolean column. Null will be mapped to False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
str | Expr
|
A boolean column |
required |
as_ratio
|
bool
|
If true, return complexity / length. |
True
|
Source code in python/polars_ds/exprs/ts_features.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
query_longest_streak(where)
Finds the longest streak length where the condition where is true.
Note: the query is still runnable when where doesn't represent boolean column / boolean expressions.
However, if that is the case the answer will not be easily interpretable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
str | Expr
|
If where is string, the string must represent the name of a string column. If where is an expression, the expression must evaluate to some boolean expression. |
required |
Source code in python/polars_ds/exprs/ts_features.py
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 | |
query_mean_abs_change(x)
Returns the mean of all successive differences |X_i - X_i-1|
Source code in python/polars_ds/exprs/ts_features.py
76 77 78 79 80 | |
query_mean_n_abs_max(x, n_maxima)
Returns the average of the top n_maxima of |x|.
Source code in python/polars_ds/exprs/ts_features.py
83 84 85 86 87 88 89 | |
query_mid_range(x)
A shorthand for (pl.col(x).max() - pl.col(x).min()) / 2.
Source code in python/polars_ds/exprs/ts_features.py
51 52 53 54 55 56 | |
query_permute_entropy(ts, tau=1, n_dims=3, base=math.e)
Computes permutation entropy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
str | Expr
|
A time series |
required |
tau
|
int
|
The embedding time delay which controls the number of time periods between elements of each of the new column vectors. |
1
|
n_dims
|
int, > 1
|
The embedding dimension which controls the length of each of the new column vectors |
3
|
base
|
float
|
The base for log in the entropy computation |
e
|
Reference
https://www.aptech.com/blog/permutation-entropy/
Source code in python/polars_ds/exprs/ts_features.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
query_range_count(x, lower, upper)
Returns the number of values inside [lower, upper].
Source code in python/polars_ds/exprs/ts_features.py
114 115 116 117 118 | |
query_sample_entropy(ts, ratio=0.2, m=2, parallel=False)
Calculate the sample entropy of this column. It is highly recommended that the user impute nulls before calling this.
If NaN/some error is returned/thrown, it is likely that: (1) Too little data, e.g. m + 1 > length (2) ratio or (ratio * std) is too close to or below 0 or std is null/NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
str | Expr
|
A time series |
required |
ratio
|
float
|
The tolerance parameter. Default is 0.2. |
0.2
|
m
|
int
|
Length of a run of data. Most common run length is 2. |
2
|
parallel
|
bool
|
Whether to run this in parallel or not. This is recommended when you are running only this expression, and not in group_by context. |
False
|
Reference
https://en.wikipedia.org/wiki/Sample_entropy
Source code in python/polars_ds/exprs/ts_features.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
query_similar_count(query, target, threshold, metric='sqzl2', parallel=False, return_ratio=False)
Given a query subsequence, find the number of same-sized subsequences (windows) in target series that have distance < threshold from it.
Note: If target is largely null, errors may occur. If metric is sqzl2, a mininum variance of 1e-10 is applied to all variance calculations to avoid division by 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Iterable[float]
|
The query subsequence. Must not contain nulls. |
required |
target
|
str | Expr
|
The target time series. |
required |
threshold
|
float
|
The distance threshold |
required |
metric
|
Literal['sql2', 'sqzl2']
|
Either 'sql2' or 'sqzl2', which stands for squared l2 and squared z-normalized l2. |
'sqzl2'
|
parallel
|
bool
|
Only applies when method is |
False
|
return_ratio
|
bool
|
If true, return # of similar subseuqnces / total number of subsequences. |
False
|
Source code in python/polars_ds/exprs/ts_features.py
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 | |
query_streak(where)
Finds the streak length where the condition where is true. This returns a full column of streak lengths.
Note: the query is still runnable when where doesn't represent boolean column / boolean expressions.
However, if that is the case the answer will not be easily interpretable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
str | Expr
|
If where is string, the string must represent the name of a boolean column. If where is an expression, the expression must evaluate to some boolean series. |
required |
Source code in python/polars_ds/exprs/ts_features.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
query_symm_ratio(x)
Returns the symmetric ratio: |mean - median| / (max - min). Note the closer to 0 this value is, the more symmetric the series is.
Source code in python/polars_ds/exprs/ts_features.py
59 60 61 62 63 64 65 | |
query_time_reversal_asymmetry_stats(x, n_lags)
Queries the Time Reversal Asymmetry Statistic, which is the average of (L^2(x) * L(x) - L(x) * x^2), where L is the lag operator.
Source code in python/polars_ds/exprs/ts_features.py
382 383 384 385 386 387 388 389 390 | |
query_transfer_entropy(x, source, lag=1, k=3, parallel=False)
Estimating transfer entropy from source to x with a lag
Reference
Jian Ma. Estimating Transfer Entropy via Copula Entropy. arXiv preprint arXiv:1910.04375, 2019.
Source code in python/polars_ds/exprs/ts_features.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |