Core¶
Statistical primitives: EWMA, returns, moments, correlation/covariance matrices, and linear algebra.
core ¶
correlation_matrix ¶
Pearson correlation matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
2-D array of shape (n_observations, n_variables). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Symmetric correlation matrix of shape (n_variables, n_variables). |
covariance_matrix ¶
Sample covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
2-D array of shape (n_observations, n_variables). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Covariance matrix of shape (n_variables, n_variables). |
cumsum ¶
Cumulative sum of an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Cumulative sum with the same length as input. |
ewma ¶
Exponentially weighted moving average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input time series. |
required |
span
|
int
|
Decay span (alpha = 2 / (span + 1)). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
EWMA-smoothed series of the same length. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If span < 1 or values is empty. |
ewma_std ¶
Exponentially weighted moving standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input time series. |
required |
span
|
int
|
Decay span (alpha = 2 / (span + 1)). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
EWMA standard deviation series. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If span < 1 or values is empty. |
kurtosis ¶
Excess kurtosis (fourth standardized moment minus 3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array (length >= 4). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Excess kurtosis (0 for a normal distribution). |
log_returns ¶
Logarithmic returns from a price series.
Computes log(p[i] / p[i-1]) for each consecutive pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
ndarray
|
Price series of length n. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Log returns of length n-1. |
matrix_inverse ¶
Invert a square matrix using LU decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Square matrix of shape (n, n). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Inverse matrix of shape (n, n). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the matrix is singular or not square. |
mean ¶
Arithmetic mean of an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mean value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the array is empty. |
power_iteration_eig ¶
Eigendecomposition via power iteration.
Computes the top num_components eigenvalues and eigenvectors of a
symmetric matrix using deflated power iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
Symmetric square matrix. |
required |
num_components
|
int
|
Number of leading eigenvalues/vectors to compute. |
required |
max_iter
|
int
|
Maximum iterations per component. |
1000
|
tol
|
float
|
Convergence tolerance. |
1e-10
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
|
simple_returns ¶
Simple (arithmetic) returns from a price series.
Computes (p[i] - p[i-1]) / p[i-1] for each consecutive pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
ndarray
|
Price series of length n. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Simple returns of length n-1. |
skewness ¶
Sample skewness (third standardized moment).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array (length >= 3). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Skewness coefficient. |
std_dev ¶
Sample standard deviation with configurable degrees of freedom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array. |
required |
ddof
|
int
|
Delta degrees of freedom. |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Standard deviation. |
variance ¶
Sample variance with configurable degrees of freedom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input array. |
required |
ddof
|
int
|
Delta degrees of freedom. Use 0 for population variance, 1 for sample variance (Bessel's correction). |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Variance of the input. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the array has fewer elements than ddof + 1. |
weighted_mean ¶
Weighted arithmetic mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
Input values. |
required |
weights
|
ndarray
|
Non-negative weights (same length as values). Need not sum to 1. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Weighted mean. |