Statistics¶
mean / var / std / median / quantile / stdm / varm / cor / cov
work on both TSeries and MVTSeries (the matrix variants iterate the values flat,
matching Julia). mean / var / std / cor route through a Cython kernel when
the compiled extension is importable — see
design/cython_strategy.md and stats_is_cython().
tsecon._stats ¶
Statistics reductions for :class:TSeries and :class:MVTSeries.
Mirrors the Statistics.* overloads in TimeSeriesEcon.jl/src/tsmath.jl
(lines 238-298). Six TSeries reductions (mean / std / var /
median / quantile / stdm / varm) and a cor / cov
pair (both for one and two arguments). MVTSeries has the same six
single-argument reductions; cor(MVTSeries) / cov(MVTSeries) return
column-correlation / column-covariance matrices.
For non-BDaily series, the functions are thin wrappers around the
corresponding numpy calls. For :class:~tsecon.frequencies.BDaily series
they additionally accept skip_all_nans / skip_holidays /
holidays_map kwargs that filter the underlying values through
:func:tsecon._bdaily.cleanedvalues before the reduction. Passing those
kwargs on a non-BDaily series raises :class:TypeError.
The Julia overloads return scalar values for the single-series form and
matrices for the MVTSeries cor / cov. We follow the same shape; the
column orientation matches Julia (variables = columns) by passing
rowvar=False to np.corrcoef / np.cov.
stats_is_cython ¶
Return True iff the Cython-compiled stats kernels were importable.
Useful for tests, benchmarks, and diagnostic prints — the public
:func:mean / :func:var / :func:std / :func:cor are
implementation-agnostic. When this returns False the same calls
go through the pure-NumPy kernels in _stats_kernels.py;
behaviour is identical, only speed differs.
Source code in src/tsecon/_stats.py
mean ¶
mean(
t: TSeries | MVTSeries,
*,
axis: int | None = None,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Arithmetic mean.
For a :class:~tsecon.tseries.TSeries returns a scalar; for an
:class:~tsecon.mvtseries.MVTSeries the default (axis=None) returns
the overall mean (matching Julia's mean(::MVTSeries) which iterates
the matrix flat). axis=0 reduces along rows to a single-row
MVTSeries (per-column means); axis=1 reduces along columns to a 1-D
TSeries (per-row means).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries or MVTSeries
|
Input series. Any non-Unit frequency. |
required |
axis
|
int or None
|
Reduction axis (NumPy convention).
|
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging the business days to
include ( |
None
|
Returns:
| Type | Description |
|---|---|
float, MVTSeries, or TSeries
|
Scalar for |
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0, 4.0]))
>>> tse.mean(t)
2.5
Per-column reduction on an MVTSeries:
>>> m = tse.MVTSeries(tse.qq(2020, 1), ("a", "b"),
... np.array([[1.0, 10.0], [2.0, 20.0], [3.0, 30.0]]))
>>> tse.mean(m, axis=0)["a"][tse.qq(2020, 1)]
2.0
Per-row reduction on an MVTSeries:
Source code in src/tsecon/_stats.py
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
std ¶
std(
t: TSeries | MVTSeries,
*,
axis: int | None = None,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Sample standard deviation.
Uses ddof=1 to match Julia's Statistics.std (corrected=true
by default). Pass np.std(t.values, ddof=0) directly if you need the
population deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries or MVTSeries
|
Input series. Any non-Unit frequency. |
required |
axis
|
int or None
|
Reduction axis (NumPy convention; see :func: |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float, MVTSeries, or TSeries
|
Sample standard deviation with |
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0]))
>>> tse.std(t)
1.0
Source code in src/tsecon/_stats.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
var ¶
var(
t: TSeries | MVTSeries,
*,
axis: int | None = None,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Sample variance (ddof=1 to match Julia's Statistics.var).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries or MVTSeries
|
Input series. Any non-Unit frequency. |
required |
axis
|
int or None
|
Reduction axis (NumPy convention; see :func: |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float, MVTSeries, or TSeries
|
Sample variance with |
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0]))
>>> tse.var(t)
1.0
Source code in src/tsecon/_stats.py
median ¶
median(
t: TSeries | MVTSeries,
*,
axis: int | None = None,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Median value.
Mirrors Julia's Statistics.median. For an even-length input the
midpoint of the two middle values is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries or MVTSeries
|
Input series. Any non-Unit frequency. |
required |
axis
|
int or None
|
Reduction axis (NumPy convention; see :func: |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float, MVTSeries, or TSeries
|
The median of the resolved values. Scalar for |
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0, 4.0]))
>>> float(tse.median(t))
2.5
Source code in src/tsecon/_stats.py
quantile ¶
quantile(
t: TSeries | MVTSeries,
p: float | ndarray,
*,
axis: int | None = None,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
p-th quantile of the series' values.
Mirrors Julia's Statistics.quantile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries or MVTSeries
|
Input series. Any non-Unit frequency. |
required |
p
|
float or array-like of float
|
Probability (or probabilities) in |
required |
axis
|
int or None
|
Reduction axis (NumPy convention; see :func: |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float, ndarray, MVTSeries, or TSeries
|
Scalar when |
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0, 4.0]))
>>> float(tse.quantile(t, 0.5))
2.5
Source code in src/tsecon/_stats.py
564 565 566 567 568 569 570 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 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
stdm ¶
stdm(
t: TSeries,
m: float,
*,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Sample standard deviation with the mean m supplied externally.
Mirrors Julia's Statistics.stdm. Use this when you already have
the mean (perhaps a known population mean, or one shared across
several reductions) and want to avoid a redundant pass over t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries
|
Input series. Any non-Unit frequency. |
required |
m
|
float
|
The mean to centre around. Not validated against |
required |
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
|
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0]))
>>> tse.stdm(t, 2.0)
1.0
Source code in src/tsecon/_stats.py
varm ¶
varm(
t: TSeries,
m: float,
*,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Sample variance with the mean m supplied externally.
Mirrors Julia's Statistics.varm. The variance counterpart of
:func:stdm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
TSeries
|
Input series. Any non-Unit frequency. |
required |
m
|
float
|
The mean to centre around. Not validated against |
required |
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
|
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> t = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0]))
>>> tse.varm(t, 2.0)
1.0
Source code in src/tsecon/_stats.py
cor ¶
cor(
x: TSeries | MVTSeries,
y: TSeries | None = None,
*,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Pearson correlation.
Mirrors Julia's Statistics.cor overloads in tsmath.jl
(lines 249-272 and 297). Two call shapes:
cor(t)— for a :class:~tsecon.tseries.TSeriesthis returns1.0(the trivial self-correlation). For an :class:~tsecon.mvtseries.MVTSeriesit returns the column- correlation matrix (variables on rows = columns of the input).cor(x, y)— two TSeries of the same frequency, firstdate and length; returns the scalar Pearson correlation between them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
TSeries or MVTSeries
|
First operand (or sole operand in the |
required |
y
|
TSeries
|
Second operand. When given, both |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Scalar in the single-TSeries or two-TSeries forms; an |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
Notes
Correlation of a constant series is mathematically undefined (zero
variance in the denominator). cor(x, y) returns nan and emits
a RuntimeWarning when either input is bit-exactly constant, matching
NumPy's np.corrcoef semantics on FP-exact constant input. The
explicit guard is uniform across the NumPy and Cython kernels, so the
return is nan for both np.full(N, 1.0) (where np.corrcoef
itself would warn) and np.full(N, 1e-60) (where pairwise summation
in np.corrcoef would otherwise silently return 1.0).
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> x = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0, 4.0]))
>>> y = tse.TSeries(tse.qq(2020, 1), np.array([2.0, 4.0, 6.0, 8.0]))
>>> float(tse.cor(x, y))
1.0
Source code in src/tsecon/_stats.py
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 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 | |
cov ¶
cov(
x: TSeries | MVTSeries,
y: TSeries | None = None,
*,
skip_all_nans: bool = False,
skip_holidays: bool = False,
holidays_map: TSeries | None = None,
) -> Any
Sample covariance (ddof=1 to match Julia's Statistics.cov).
Same call shapes as :func:cor:
cov(t)for a TSeries collapses to :func:var.cov(mvts)returns the column-covariance matrix.cov(x, y)returns the scalar covariance between two TSeries of the same frequency, firstdate, and length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
TSeries or MVTSeries
|
First operand (or sole operand in the |
required |
y
|
TSeries
|
Second operand. When given, both |
None
|
skip_all_nans
|
bool
|
BDaily only. If |
False
|
skip_holidays
|
bool
|
BDaily only. If |
False
|
holidays_map
|
TSeries
|
BDaily only. Boolean TSeries flagging which business days to keep. |
None
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Scalar in the TSeries forms; an |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ValueError
|
|
Examples:
>>> import numpy as np
>>> import tsecon as tse
>>> x = tse.TSeries(tse.qq(2020, 1), np.array([1.0, 2.0, 3.0]))
>>> y = tse.TSeries(tse.qq(2020, 1), np.array([2.0, 4.0, 6.0]))
>>> float(tse.cov(x, y))
2.0
Source code in src/tsecon/_stats.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 | |