statistics_functions¶
shortfx.fxNumeric.statistics_functions
¶
Numeric Statistics Module.
This module provides comprehensive statistical analysis functions for numeric data, including measures of central tendency, dispersion, correlation, percentiles, regression, and advanced statistical summaries.
Key Features
- Descriptive statistics (mean, median, mode, range, geometric/harmonic mean)
- Measures of dispersion (variance, standard deviation, IQR, skewness, kurtosis)
- Correlation and covariance calculations
- Percentile and frequency calculations
- Regression and forecasting (slope, intercept, forecast)
- Fisher transformation, z-score, confidence intervals
- Sum of squares computation
Example
from shortfx.fxNumeric.statistics_functions import calculate_mean, calculate_median calculate_mean([1, 2, 3, 4, 5]) 3.0 calculate_median([1, 2, 3, 4]) 2.5
Functions¶
absolute_error(measured: Union[int, float], actual: Union[int, float]) -> float
¶
Compute the absolute error: |measured - actual|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measured
|
Union[int, float]
|
Measured/observed value. |
required |
actual
|
Union[int, float]
|
True/expected value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Absolute error as a non-negative float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
absolute_error(10.5, 10.0) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
adjusted_r_squared(r2: float, n: int, p: int) -> float
¶
Adjusted R² penalised for number of predictors.
R²_adj = 1 − (1 − R²)(n − 1) / (n − p − 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r2
|
float
|
Coefficient of determination. |
required |
n
|
int
|
Number of observations (> p + 1). |
required |
p
|
int
|
Number of predictors (≥ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Adjusted R² as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If n ≤ p + 1 or p < 1. |
Usage Example
round(adjusted_r_squared(0.997, 5, 1), 4) 0.996
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
aggregate(data: list[int | float], operation: str = 'sum', ignore_errors: bool = False) -> float
¶
Perform a named aggregation on a numeric list.
Supported operations: "sum", "avg", "max", "min",
"count", "product".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[int | float]
|
List of numeric values. |
required |
operation
|
str
|
Aggregation name (case-insensitive). |
'sum'
|
ignore_errors
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Aggregated numeric result. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty or operation is unknown. |
Example
aggregate([1, 2, 3, 4], "sum") 10 aggregate([1, 2, 3, 4], "avg") 2.5
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 | |
anderson_darling(data: list[float | int]) -> float
¶
Compute the Anderson-Darling normality test statistic (standalone).
Tests whether a sample comes from a normal distribution. Higher values indicate stronger evidence against normality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
List of numeric observations (n ≥ 8 recommended). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Anderson-Darling A² statistic. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data has fewer than 2 elements or zero variance. |
Example
round(anderson_darling([1, 2, 3, 4, 5, 6, 7, 8]), 4) 0.2253
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 | |
argmax(data: List[Union[int, float]]) -> int
¶
Returns the index of the maximum value in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Zero-based index of the first maximum value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
argmax([3, 1, 4, 1, 5, 9]) 5
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
argmin(data: List[Union[int, float]]) -> int
¶
Returns the index of the minimum value in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Zero-based index of the first minimum value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
argmin([3, 1, 4, 1, 5, 9]) 1
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
auto_correlation(data: List[Union[int, float]], lag: int = 1) -> float
¶
Calculates the autocorrelation of a time series at a given lag.
Measures how a signal correlates with a delayed copy of itself. Returns a value in [-1, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values (time series). |
required |
lag
|
int
|
Number of periods to shift (must be >= 0 and < len(data)). |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Autocorrelation coefficient at the given lag. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If lag is out of range or data too short. |
Example
round(auto_correlation([1, 2, 3, 4, 5, 6, 7], 1), 6) 0.761905
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
autocorrelation(data: list[float], lag: int = 1) -> float
¶
Sample autocorrelation at a given lag.
r(k) = Σ (x_t − x̄)(x_{t+k} − x̄) / Σ (x_t − x̄)².
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length > lag). |
required |
lag
|
int
|
Lag offset (positive integer). |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Autocorrelation coefficient in [−1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data too short or lag invalid. |
Usage Example
round(autocorrelation([1, 2, 3, 4, 5, 4, 3, 2, 1], 1), 4) 0.5397
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
average_deviation(data: List[Union[int, float]]) -> float
¶
Calculates the average of absolute deviations from the mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The average absolute deviation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
average_deviation([2, 3, 4, 5, 6]) 1.2
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
average_if(values: list[int | float], criteria_range: list, criteria) -> float
¶
Return the average of values where the parallel criteria_range meets criteria.
Equivalent to the Excel AVERAGEIF / AVERAGEIFS function (single criteria pair).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[int | float]
|
Numeric values to average. |
required |
criteria_range
|
list
|
Range to test against criteria (same length as values). |
required |
criteria
|
Excel-style criteria (e.g. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average of matching values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no values match or lengths differ. |
Example
average_if([10, 20, 30, 40], ["A", "B", "A", "B"], "A") 20.0 average_if([1, 2, 3, 4], [10, 20, 30, 40], ">15") 3.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
balanced_accuracy_scalar(tp: Union[int, float], tn: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate balanced accuracy: (Sensitivity + Specificity) / 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
tn
|
Union[int, float]
|
True negatives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Balanced accuracy in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or denominators are zero. |
Example
balanced_accuracy_scalar(80, 900, 100, 20) 0.85
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
bayes_posterior(prior: float, likelihood: float, evidence: float) -> float
¶
Compute the Bayesian posterior probability.
P(H|E) = P(E|H) × P(H) / P(E)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior
|
float
|
Prior probability P(H). |
required |
likelihood
|
float
|
Likelihood P(E|H). |
required |
evidence
|
float
|
Marginal probability P(E). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Posterior probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If evidence is zero or probabilities out of [0, 1]. |
Example
bayes_posterior(0.01, 0.9, 0.05) 0.18
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
bayes_theorem(prior: float, likelihood: float, evidence: float) -> float
¶
Posterior probability via Bayes' theorem.
P(H|E) = P(E|H) × P(H) / P(E).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior
|
float
|
Prior probability P(H) in (0, 1]. |
required |
likelihood
|
float
|
Likelihood P(E|H) in [0, 1]. |
required |
evidence
|
float
|
Evidence probability P(E) in (0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Posterior probability as a float in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(bayes_theorem(0.01, 0.9, 0.05), 4) 0.18
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
bayesian_update(prior: float, likelihoods: list[float], marginals: list[float]) -> float
¶
Sequential Bayesian update with multiple evidence events.
Applies Bayes' theorem iteratively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior
|
float
|
Initial prior probability in (0, 1]. |
required |
likelihoods
|
list[float]
|
P(E_i|H) for each evidence (all in [0, 1]). |
required |
marginals
|
list[float]
|
P(E_i) for each evidence (all in (0, 1]). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Updated posterior as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If lengths differ or constraints violated. |
Usage Example
round(bayesian_update(0.5, [0.8, 0.9], [0.5, 0.6]), 4) 1.2
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
benford_distribution(data: list[float | int]) -> dict[int, float]
¶
Compute the leading-digit frequency distribution for Benford's law analysis.
Returns observed frequencies for digits 1-9.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
Positive numeric values. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, float]
|
Dict mapping digit (1-9) to observed frequency. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data is empty. |
Example
d = benford_distribution([1, 20, 300, 4000]) d[1] 0.25
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
beta_function(a: float, b: float) -> float
¶
Compute the beta function B(a, b) = Γ(a)Γ(b)/Γ(a+b).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
First parameter (> 0). |
required |
b
|
float
|
Second parameter (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Beta function value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If a or b ≤ 0. |
Usage Example
round(beta_function(2.0, 3.0), 4) 0.0833
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
beta_function_value(a: Union[int, float], b: Union[int, float]) -> float
¶
Return the value of the Beta function B(a, b).
B(a, b) = Γ(a) · Γ(b) / Γ(a + b).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Union[int, float]
|
First parameter (must be > 0). |
required |
b
|
Union[int, float]
|
Second parameter (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Beta function value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If a ≤ 0 or b ≤ 0. |
Example
round(beta_function_value(2, 3), 6) 0.083333
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
bfill(data: List[Any]) -> List[Any]
¶
Backward-fills None values with the next non-None value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A list that may contain None values. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A new list with None values replaced by the next non-None value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
Example
bfill([None, None, 3, None, 5]) [3, 3, 3, 5, 5]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
binary_entropy(p: Union[int, float]) -> float
¶
Compute binary (Bernoulli) entropy: -plog2(p) - (1-p)log2(1-p).
Measures uncertainty of a single binary event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Union[int, float]
|
Probability in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Binary entropy in bits, in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p is not in [0, 1]. |
Example
binary_entropy(0.5) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
binomial_pmf(k: int, n: int, p: float) -> float
¶
Probability mass function of the binomial distribution.
P(X=k) = C(n,k) · p^k · (1−p)^{n−k}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of successes (0 ≤ k ≤ n). |
required |
n
|
int
|
Number of trials (positive integer). |
required |
p
|
float
|
Probability of success in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k or n are not int, or p is not numeric. |
ValueError
|
If constraints are violated. |
Usage Example
round(binomial_pmf(3, 10, 0.5), 4) 0.1172
Complexity: O(k)
Source code in shortfx/fxNumeric/statistics_functions.py
binomial_probability(n: int, k: int, p: Union[int, float]) -> float
¶
Calculate binomial probability P(X=k) = C(n,k)·p^k·(1-p)^(n-k).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of trials. |
required |
k
|
int
|
Number of successes. |
required |
p
|
Union[int, float]
|
Probability of success per trial, in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of exactly k successes. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k not int, or p not numeric. |
ValueError
|
If n < 0, k < 0, k > n, or p not in [0, 1]. |
Example
round(binomial_probability(10, 3, 0.5), 6) 0.117188
Complexity: O(k)
Source code in shortfx/fxNumeric/statistics_functions.py
bootstrap_mean_ci(data: List[float], confidence: float = 0.95, n_resamples: int = 1000, seed: Optional[int] = None) -> Tuple[float, float]
¶
Estimates a confidence interval for the mean via bootstrap resampling.
Draws n_resamples samples with replacement, computes their means, and returns the percentile-based interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[float]
|
Numeric data list. |
required |
confidence
|
float
|
Confidence level (0 < confidence < 1). |
0.95
|
n_resamples
|
int
|
Number of bootstrap resamples. |
1000
|
seed
|
Optional[int]
|
Optional random seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple (lower_bound, upper_bound) of the confidence interval. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list. |
ValueError
|
If data is empty or confidence is not in (0, 1). |
Example
lo, hi = bootstrap_mean_ci([1, 2, 3, 4, 5], 0.95, 5000, seed=42) lo < 3.0 < hi True
Complexity: O(n × n_resamples)
Source code in shortfx/fxNumeric/statistics_functions.py
bowley_skewness(data: list[float]) -> float
¶
Bowley (quartile) skewness: (Q3 + Q1 − 2·Q2) / (Q3 − Q1).
A robust alternative to the moment-based skewness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 4, IQR > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Bowley skewness in [−1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data has fewer than 4 elements or IQR is zero. |
Usage Example
round(bowley_skewness([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 4) 0.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_covariance(data1: List[Union[int, float]], data2: List[Union[int, float]], sample: bool = True) -> float
¶
Calculates the covariance between two lists of numbers.
Covariance measures how two variables change together. A positive covariance indicates that the variables tend to move in the same direction, while a negative covariance indicates they tend to move in opposite directions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
The first list of numeric values. |
required |
data2
|
List[Union[int, float]]
|
The second list of numeric values. |
required |
sample
|
bool
|
If True, calculates sample covariance (divides by n-1). If False, calculates population covariance (divides by n). Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The covariance between the two datasets. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lists are of different lengths or have fewer than 2 elements for sample covariance. |
TypeError
|
If inputs are not lists or contain non-numeric values. |
Example
calculate_covariance([1, 2, 3], [2, 4, 6]) # Positive correlation 2.0 calculate_covariance([1, 2, 3], [6, 4, 2]) # Negative correlation -2.0
Cost: O(n), where n is the length of the lists.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_frecuency(data: List[Union[int, float]]) -> dict
¶
Calculates the frequency of each value in a list of numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary where keys are unique values and values are their frequencies. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_frecuency([1, 2, 2, 3, 3, 3])
Cost: O(n), where n is the number of elements in the list.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_interquartile_range(data: List[Union[int, float]]) -> float
¶
Calculates the Interquartile Range (IQR) of a list of numbers.
The IQR is the range between the first quartile (Q1, 25th percentile) and the third quartile (Q3, 75th percentile) of a dataset. It is a measure of statistical dispersion, representing the middle 50% of the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Interquartile Range (IQR). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list has fewer than 4 elements. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_interquartile_range([1, 2, 3, 4, 5, 6, 7, 8, 9]) 4.0 calculate_interquartile_range([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 5.0
Cost: O(n log n), includes list sorting.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_mean(data: List[Union[int, float]]) -> float
¶
Calculates the arithmetic mean (average) of a list of numbers.
The mean is the sum of all values divided by the number of values. It's a measure of central tendency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The arithmetic mean of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_mean([1, 2, 3, 4, 5]) 3.0 calculate_mean([10, 20, 30]) 20.0
Cost: O(n), where n is the number of elements.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_median(data: List[Union[int, float]]) -> Union[int, float]
¶
Calculates the median (middle value) of a list of numbers.
The median is the middle number in a sorted, ascending or descending, list of numbers and is a measure of central tendency. If the list has an even number of elements, the median is the average of the two middle values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The median of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_median([1, 2, 3, 4, 5]) 3 calculate_median([1, 2, 3, 4]) 2.5
Cost: O(n log n), includes list sorting.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_mode(data: List[Union[int, float]]) -> List[Union[int, float]]
¶
Calculates the mode(s) of a list of numbers.
The mode is the value that appears most frequently in a dataset. A dataset can have one mode (unimodal), multiple modes (multimodal), or no mode if all values appear with the same frequency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[Union[int, float]]
|
List[Union[int, float]]: A list of mode(s). Returns an empty list if no mode. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_mode([1, 2, 2, 3, 4]) [2] calculate_mode([1, 2, 2, 3, 3, 4]) [2, 3] calculate_mode([1, 2, 3]) [1, 2, 3] # or similar depending on statistics.mode behavior for unique elements
Cost: O(n), where n is the number of elements.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_pearson_correlation(data1: List[Union[int, float]], data2: List[Union[int, float]]) -> float
¶
Calculates the Pearson product-moment correlation coefficient between two lists of numbers.
Pearson correlation measures the linear relationship between two datasets. Its value ranges from -1 to 1. - +1 indicates a perfect positive linear relationship. - -1 indicates a perfect negative linear relationship. - 0 indicates no linear relationship.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
The first list of numeric values. |
required |
data2
|
List[Union[int, float]]
|
The second list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Pearson correlation coefficient. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lists are of different lengths or have insufficient data points (less than 2). Also if standard deviation is zero (no variance in one of the datasets). |
TypeError
|
If inputs are not lists or contain non-numeric values. |
Example
calculate_pearson_correlation([1, 2, 3], [2, 4, 6]) 1.0 calculate_pearson_correlation([1, 2, 3], [6, 4, 2]) -1.0 round(calculate_pearson_correlation([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]), 10) -1.0
Cost: O(n), correlation calculation.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_percentile(data: List[Union[int, float]], percentile: float) -> float
¶
Calculates the specified percentile of a list of numbers.
A percentile indicates the value below which a given percentage of observations in a group of observations falls. For example, the 90th percentile is the value below which 90% of the observations may be found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
percentile
|
float
|
The desired percentile, a value between 0 and 100. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The value at the specified percentile. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty, or if percentile is out of range [0, 100]. |
TypeError
|
If the input is not a list or contains non-numeric values, or if percentile is not numeric. |
Example
calculate_percentile([10, 20, 30, 40, 50], 50) # Median 30.0 calculate_percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 90) 9.0
Cost: O(n log n), includes sorting to calculate percentile.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_range(data: List[Union[int, float]]) -> float
¶
Calculates the range of a list of numbers.
The range is the difference between the maximum and minimum values in a dataset. It's a simple measure of dispersion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The range of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_range([1, 5, 2, 8, 3]) 7.0 calculate_range([10, 10, 10]) 0.0
Cost: O(n), searching for maximum and minimum.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_standard_deviation(data: List[Union[int, float]], sample: bool = True) -> float
¶
Calculates the standard deviation of a list of numbers.
Standard deviation is the square root of the variance. It's a measure of the amount of variation or dispersion of a set of values. A low standard deviation indicates that the values tend to be close to the mean of the set, while a high standard deviation indicates that the values are spread out over a wider range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
sample
|
bool
|
If True, calculates sample standard deviation. If False, calculates population standard deviation. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The standard deviation of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list has fewer than 2 elements for sample standard deviation, or is empty for population standard deviation. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
round(calculate_standard_deviation([1, 2, 3, 4, 5]), 2) # Sample std dev 1.58 round(calculate_standard_deviation([1, 2, 3, 4, 5], sample=False), 2) # Population std dev 1.41
Cost: O(n), standard deviation calculation.
Source code in shortfx/fxNumeric/statistics_functions.py
calculate_variance(data: List[Union[int, float]], sample: bool = True) -> float
¶
Calculates the variance of a list of numbers.
Variance measures how far each number in the set is from the mean, and therefore from every other number in the set. For a sample, it's the sum of squared differences from the mean, divided by (n-1). For a population, it's divided by n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
sample
|
bool
|
If True, calculates sample variance (divides by n-1). If False, calculates population variance (divides by n). Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The variance of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list has fewer than 2 elements for sample variance, or is empty for population variance. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
calculate_variance([1, 2, 3, 4, 5]) # Sample variance 2.5 calculate_variance([1, 2, 3, 4, 5], sample=False) # Population variance 2.0
Cost: O(n), variance calculation over the list.
Source code in shortfx/fxNumeric/statistics_functions.py
cauchy_pdf(x: float, x0: float = 0.0, gamma: float = 1.0) -> float
¶
Compute the Cauchy distribution PDF.
f(x) = 1 / (πγ(1 + ((x-x0)/γ)²))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
x0
|
float
|
Location parameter (default 0). |
0.0
|
gamma
|
float
|
Scale parameter (default 1, > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If gamma ≤ 0. |
Usage Example
round(cauchy_pdf(0.0), 4) 0.3183
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
celu(x: float, alpha: float = 1.0) -> float
¶
Continuously Differentiable Exponential Linear Unit (CELU).
Description
CELU(x) = max(0, x) + min(0, α(e^{x/α} − 1)). Provides a smooth, continuously differentiable alternative to ELU that avoids the non-differentiable point at x = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Input value. |
required |
alpha
|
float
|
Scale for the negative region (> 0). Default 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
CELU activation value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or alpha is not numeric. |
ValueError
|
If alpha is not positive. |
Usage Example
celu(1.0) 1.0 round(celu(-1.0, 1.0), 6) -0.632121
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
chi_squared_pdf(x: float, k: int) -> float
¶
Compute the chi-squared distribution PDF.
f(x) = x^(k/2-1)·e^(-x/2) / (2^(k/2)·Γ(k/2))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ 0). |
required |
k
|
int
|
Degrees of freedom (≥ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric or k is not int. |
ValueError
|
If x < 0 or k < 1. |
Usage Example
round(chi_squared_pdf(2.0, 3), 4) 0.2076
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
chi_squared_statistic(observed: list[float], expected: list[float]) -> float
¶
Chi-squared test statistic: Σ (O − E)² / E.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed frequencies (all ≥ 0). |
required |
expected
|
list[float]
|
Expected frequencies (all > 0, same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Chi-squared statistic as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or expected ≤ 0. |
Usage Example
round(chi_squared_statistic([10, 20, 30], [15, 15, 30]), 4) 3.3333
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
chisq_test(observed: List[List[Union[int, float]]], expected: Optional[List[List[Union[int, float]]]] = None) -> float
¶
Chi-squared test for independence.
Description
Returns the p-value for a chi-squared test of independence on a contingency table. Equivalent to Excel CHISQ.TEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
List[List[Union[int, float]]]
|
2-D contingency table of observed frequencies. |
required |
expected
|
Optional[List[List[Union[int, float]]]]
|
Optional 2-D table of expected frequencies. If None, expected values are computed automatically. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
p-value of the chi-squared test. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not 2-D lists of numbers. |
ValueError
|
If dimensions mismatch or values invalid. |
Example
round(chisq_test([[58, 35], [11, 25], [10, 23]]), 4) 0.0004
Complexity: O(r × c)
Source code in shortfx/fxNumeric/statistics_functions.py
coefficient_of_quartile_deviation(data: list) -> float
¶
Calculate the Coefficient of Quartile Deviation (CQD).
Description
CQD = (Q3 − Q1) / (Q3 + Q1). A dimensionless relative dispersion measure based on quartiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list
|
A list of numeric values (at least 2 elements). |
required |
Returns:
| Type | Description |
|---|---|
float
|
CQD value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If data has fewer than 2 elements or Q3 + Q1 == 0. |
Usage Example
coefficient_of_quartile_deviation([2, 4, 6, 8, 10]) 0.5
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
coefficient_of_variation(data: list[float]) -> float
¶
Coefficient of variation (CV) — ratio of std dev to mean.
CV = σ / |μ|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 2, mean ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
CV as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data has fewer than 2 elements or mean is zero. |
Usage Example
round(coefficient_of_variation([2, 4, 4, 4, 5, 5, 7, 9]), 4) 0.4276
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cohens_d(group1: list[float], group2: list[float]) -> float
¶
Cohen's d effect size between two independent groups.
d = (M1 − M2) / s_pooled where s_pooled uses Bessel-corrected variances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group1
|
list[float]
|
First sample (length ≥ 2). |
required |
group2
|
list[float]
|
Second sample (length ≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cohen's d as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If either group has fewer than 2 elements or pooled SD is 0. |
Usage Example
round(cohens_d([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]), 4) -1.2649
Complexity: O(n + m)
Source code in shortfx/fxNumeric/statistics_functions.py
cohens_kappa_scalar(tp: Union[int, float], tn: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate Cohen's kappa from a 2×2 confusion matrix.
κ = (p_o - p_e) / (1 - p_e)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
tn
|
Union[int, float]
|
True negatives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cohen's kappa coefficient. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If total is zero or p_e is 1. |
Example
round(cohens_kappa_scalar(50, 40, 10, 5), 6) 0.714286
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
confidence_norm(alpha: float, standard_dev: float, size: int) -> float
¶
Calculates the confidence interval half-width using the normal distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level (e.g. 0.05 for 95% confidence). |
required |
standard_dev
|
float
|
Population standard deviation. |
required |
size
|
int
|
Sample size. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The margin of error (half-width of the confidence interval). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If alpha is not in (0, 1), standard_dev <= 0, or size < 1. |
Example
round(confidence_norm(0.05, 2.5, 50), 6) 0.692952
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
confidence_t(alpha: float, standard_dev: float, size: int) -> float
¶
Calculates the confidence interval half-width using the Student's t-distribution.
Requires scipy for the t-distribution inverse CDF. Falls back to normal approximation if scipy is unavailable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Significance level (e.g. 0.05 for 95% confidence). |
required |
standard_dev
|
float
|
Sample standard deviation. |
required |
size
|
int
|
Sample size. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The margin of error (half-width of the confidence interval). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If alpha is not in (0, 1), standard_dev <= 0, or size < 2. |
Example
round(confidence_t(0.05, 2.5, 50), 4) 0.7091
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
contraharmonic_mean(data: list) -> float
¶
Calculate the contraharmonic mean.
Description
Contraharmonic Mean = Σx² / Σx. Always greater than or equal to the arithmetic mean for positive values. Useful in image processing and statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list
|
A list of numeric values (at least 1 element, sum ≠ 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The contraharmonic mean as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If data is empty or sum of values is zero. |
Usage Example
contraharmonic_mean([1, 2, 3, 4, 5]) 3.6666666666666665
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cosine_similarity(a: list[float], b: list[float]) -> float
¶
Cosine similarity between two vectors.
cos(θ) = (a·b) / (||a|| × ||b||).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
list[float]
|
First vector (non-empty). |
required |
b
|
list[float]
|
Second vector (same length, non-empty). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cosine similarity in [−1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or a zero-norm vector. |
Usage Example
round(cosine_similarity([1, 2, 3], [4, 5, 6]), 4) 0.9746
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cosine_similarity_scalar(a1: Union[int, float], a2: Union[int, float], b1: Union[int, float], b2: Union[int, float]) -> float
¶
Calculate cosine similarity between two 2D vectors (a1,a2) and (b1,b2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a1
|
Union[int, float]
|
First component of vector A. |
required |
a2
|
Union[int, float]
|
Second component of vector A. |
required |
b1
|
Union[int, float]
|
First component of vector B. |
required |
b2
|
Union[int, float]
|
Second component of vector B. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cosine similarity in [-1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If either vector has zero magnitude. |
Example
cosine_similarity_scalar(1, 0, 0, 1) 0.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
count_true_with_sum(boolean_list: List[bool]) -> int
¶
Counts the number of True values in a boolean list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boolean_list
|
List[bool]
|
A list containing True or False values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of True values in the list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If 'boolean_list' is not a list or contains non-boolean elements. |
Example
count_true_with_sum([True, False, True, True]) 3 count_true_with_sum([False, False, False]) 0
Cost: O(n), where n is the number of elements in the list.
Source code in shortfx/fxNumeric/statistics_functions.py
covariance_matrix(data: List[List[Union[int, float]]]) -> List[List[float]]
¶
Computes the sample covariance matrix for a set of variables.
Each inner list represents one variable's observations. All variables must have the same number of observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[List[Union[int, float]]]
|
A list of lists where |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
A square matrix (list of lists) where element |
List[List[float]]
|
sample covariance between variable i and variable j. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numeric lists. |
ValueError
|
If fewer than 2 variables or observations are given, or if observation lengths differ. |
Example
covariance_matrix([[1, 2, 3], [4, 5, 6]]) [[1.0, 1.0], [1.0, 1.0]] covariance_matrix([[1, 2, 3], [6, 5, 4]]) [[1.0, -1.0], [-1.0, 1.0]]
Complexity: O(k^2 * n) where k = number of variables, n = observations.
Source code in shortfx/fxNumeric/statistics_functions.py
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 | |
cramers_v(chi2: float, n: int, k: int) -> float
¶
Cramér's V — effect size for chi-squared tests.
V = √(χ² / (n × (k − 1))).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chi2
|
float
|
Chi-squared statistic (≥ 0). |
required |
n
|
int
|
Total sample size (> 0). |
required |
k
|
int
|
Minimum of (rows, columns) in the contingency table (≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cramér's V in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If constraints are violated. |
Usage Example
round(cramers_v(10.0, 100, 3), 4) 0.2236
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
cross_entropy(p: list[float], q: list[float]) -> float
¶
Cross-entropy H(P, Q) = −Σ p·log₂(q).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list[float]
|
True distribution (probabilities summing to 1, each > 0). |
required |
q
|
list[float]
|
Predicted distribution (same length, each > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cross-entropy in bits as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or constraints violated. |
Usage Example
round(cross_entropy([0.5, 0.5], [0.6, 0.4]), 4) 1.0294
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cross_entropy_binary(y_true: Union[int, float], y_pred: Union[int, float]) -> float
¶
Calculate binary cross-entropy loss for a single observation.
H = -(y·log(p) + (1-y)·log(1-p))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Union[int, float]
|
Actual label (0 or 1). |
required |
y_pred
|
Union[int, float]
|
Predicted probability, in (0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cross-entropy loss. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If y_true not in {0, 1} or y_pred not in (0, 1). |
Example
round(cross_entropy_binary(1, 0.9), 6) 0.105361
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_max(data: List[Union[int, float]]) -> List[float]
¶
Returns the running cumulative maximum of a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A list of the same length with cumulative maxima. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
cumulative_max([3, 1, 4, 1, 5]) [3, 3, 4, 4, 5]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_min(data: List[Union[int, float]]) -> List[float]
¶
Returns the running cumulative minimum of a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A list of the same length with cumulative minima. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
cumulative_min([3, 1, 4, 1, 5]) [3, 1, 1, 1, 1]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_moving_average(data: list[float]) -> list[float]
¶
Cumulative moving average (expanding mean).
CMA_t = (1/(t+1)) Σ_{i=0}^{t} data_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (non-empty). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of CMA values (same length as data). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data is empty. |
Usage Example
cumulative_moving_average([1, 2, 3, 4, 5]) [1.0, 1.5, 2.0, 2.5, 3.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_product(data: List[Union[int, float]]) -> List[float]
¶
Returns the running cumulative product of a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A list of the same length with cumulative products. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
cumulative_product([1, 2, 3, 4]) [1, 2, 6, 24]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_return(returns: List[Union[int, float]]) -> float
¶
Computes the total cumulative return from a list of periodic returns.
cumulative = Π(1 + rᵢ) − 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
List[Union[int, float]]
|
List of periodic returns as decimals (e.g. 0.05 = 5%). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The cumulative return as a decimal. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If returns is empty or any return <= -1. |
Example
round(cumulative_return([0.10, 0.05, -0.02]), 4) 0.1319
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
cumulative_sum(data: List[Union[int, float]]) -> List[float]
¶
Returns the running cumulative sum of a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A list of the same length with cumulative sums. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
cumulative_sum([1, 2, 3, 4]) [1, 3, 6, 10]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
describe(data: List[Union[int, float]]) -> dict
¶
Returns a summary of descriptive statistics for a list (like pandas describe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dict with keys: |
dict
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
result = describe([1, 2, 3, 4, 5]) result["count"] 5 result["mean"] 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
deviation_squared(data: List[Union[int, float]]) -> float
¶
Calculates the sum of squared deviations from the mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of squared deviations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
deviation_squared([2, 5, 8]) 18.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
dice_coefficient_scalar(tp: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate the Sørensen–Dice coefficient from counts: 2·TP / (2·TP + FP + FN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives count. |
required |
fp
|
Union[int, float]
|
False positives count. |
required |
fn
|
Union[int, float]
|
False negatives count. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Dice coefficient in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If any count is negative or all are zero. |
Example
dice_coefficient_scalar(30, 10, 5) 0.8
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
diff(data: List[Union[int, float]], periods: int = 1) -> List[Optional[float]]
¶
Calculates consecutive differences in a list (like pandas Series.diff).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
periods
|
int
|
Number of positions to shift for the difference (default 1). |
1
|
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length. The first periods entries are |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or periods < 1. |
Example
diff([1, 3, 6, 10]) [None, 2.0, 3.0, 4.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
dixon_q_test(data: list[float | int]) -> dict
¶
Perform Dixon's Q test for outlier detection.
Tests the smallest or largest value. Applicable for small samples (3 ≤ n ≤ 25).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
List of numeric observations. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with: |
dict
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data has fewer than 3 elements. |
Example
result = dixon_q_test([1, 2, 3, 4, 5, 100]) result["position"] 'max'
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
durbin_watson(residuals: List[float]) -> float
¶
Computes the Durbin-Watson statistic for autocorrelation in residuals.
DW ≈ 2 means no autocorrelation, DW < 2 positive, DW > 2 negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
residuals
|
List[float]
|
List of regression residuals. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Durbin-Watson statistic (0 to 4). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If residuals is not a list. |
ValueError
|
If residuals has fewer than 2 elements. |
Example
round(durbin_watson([0.1, -0.2, 0.15, -0.1, 0.05]), 4) 2.5824
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
elu(x: Union[int, float], alpha: float = 1.0) -> float
¶
Compute Exponential Linear Unit: x if x > 0, else alpha * (exp(x) - 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
alpha
|
float
|
Scale for negative values (default 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
ELU of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or alpha is not numeric. |
Example
elu(1.0) 1.0 round(elu(-1.0), 4) -0.6321
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
empirical_cdf(data: List[float], x: float) -> float
¶
Computes the empirical cumulative distribution function at x.
ECDF(x) = (number of observations ≤ x) / n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[float]
|
Numeric data list. |
required |
x
|
float
|
The point at which to evaluate the ECDF. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Proportion of data values ≤ x (between 0.0 and 1.0). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or x is not numeric. |
Example
empirical_cdf([1, 2, 3, 4, 5], 3) 0.6
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
entropy(data: List[Union[int, float, str]]) -> float
¶
Calculates the Shannon entropy of a discrete dataset.
Entropy quantifies the average level of uncertainty or information inherent in a variable's possible outcomes. Higher entropy means higher unpredictability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float, str]]
|
A list of categorical or numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Shannon entropy in bits (log base 2). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
ValueError
|
If the list is empty. |
Example
round(entropy([1, 1, 2, 2]), 4) 1.0 round(entropy(["a", "a", "a", "a"]), 4) 0.0 round(entropy([1, 2, 3, 4]), 4) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
entropy_binary(p: Union[int, float]) -> float
¶
Calculate binary entropy H(p) = -p·log₂(p) - (1-p)·log₂(1-p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Union[int, float]
|
Probability of positive class, in (0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Binary entropy in bits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p not in (0, 1). |
Example
round(entropy_binary(0.5), 6) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
erlang_pdf(x: Union[int, float], k: int, lam: Union[int, float]) -> float
¶
Return the Erlang probability density at x.
PDF = λ^k · x^(k−1) · exp(−λx) / (k−1)! for x ≥ 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Point at which to evaluate (must be ≥ 0). |
required |
k
|
int
|
Shape parameter (positive integer). |
required |
lam
|
Union[int, float]
|
Rate parameter (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or lam not numeric, or k not int. |
ValueError
|
If x < 0, k < 1, or lam ≤ 0. |
Example
round(erlang_pdf(1, 2, 1), 6) 0.367879
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
euclidean_distance(a: list[float], b: list[float]) -> float
¶
Euclidean distance between two vectors.
d = √(Σ (a_i − b_i)²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
list[float]
|
First vector. |
required |
b
|
list[float]
|
Second vector (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Euclidean distance as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ or empty. |
Usage Example
round(euclidean_distance([1, 2, 3], [4, 5, 6]), 4) 5.1962
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
ewma_variance(data: list[float | int], alpha: float = 0.3) -> list[float]
¶
Compute exponentially weighted moving variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
List of numeric observations. |
required |
alpha
|
float
|
Smoothing factor in (0, 1). |
0.3
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of EWMA variance values (same length as data). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If alpha is not in (0, 1) or data is empty. |
Example
ewma_variance([1, 2, 3, 4, 5], 0.3)[:2][0.0, 0.21]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
exponential_cdf(x: float, lam: float) -> float
¶
Compute the exponential distribution CDF.
F(x) = 1 - e^(-λx), x ≥ 0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ 0). |
required |
lam
|
float
|
Rate parameter λ (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cumulative probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x < 0 or lam ≤ 0. |
Usage Example
round(exponential_cdf(1.0, 1.0), 4) 0.6321
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
exponential_decay_rate(initial: Union[int, float], final: Union[int, float], time: Union[int, float]) -> float
¶
Calculate the decay constant λ given initial/final values and time.
N(t) = N₀ * e^(-λt) → λ = -ln(N_final / N_initial) / t
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial
|
Union[int, float]
|
Initial value (> 0). |
required |
final
|
Union[int, float]
|
Final value (> 0, ≤ initial). |
required |
time
|
Union[int, float]
|
Elapsed time (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Decay constant λ. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If constraints violated. |
Example
round(exponential_decay_rate(100, 50, 5), 4) 0.1386
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
exponential_moving_average(data: list[float], alpha: float) -> list[float]
¶
Exponential moving average (EMA) with smoothing factor alpha.
EMA_t = α × data_t + (1 − α) × EMA_{t−1}, with EMA_0 = data_0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (non-empty). |
required |
alpha
|
float
|
Smoothing factor in (0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of EMA values (same length as data). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is empty or alpha out of range. |
Usage Example
[round(v, 2) for v in exponential_moving_average([1, 2, 3, 4, 5], 0.5)][1.0, 1.5, 2.25, 3.12, 4.06]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
exponential_pdf(x: float, lam: float) -> float
¶
Compute the exponential distribution PDF.
f(x) = λ·e^(-λx), x ≥ 0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ 0). |
required |
lam
|
float
|
Rate parameter λ (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x < 0 or lam ≤ 0. |
Usage Example
round(exponential_pdf(1.0, 1.0), 4) 0.3679
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
exponential_smoothing_single(value: float, previous_smoothed: float, alpha: float) -> float
¶
Computes one step of single exponential smoothing.
S_t = α × X_t + (1 − α) × S_{t−1}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Current observation X_t. |
required |
previous_smoothed
|
float
|
Previous smoothed value S_{t−1}. |
required |
alpha
|
float
|
Smoothing factor (0 < alpha ≤ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
New smoothed value S_t. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If alpha is not in (0, 1]. |
Example
exponential_smoothing_single(10, 8, 0.3) 8.6
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
f1_score_scalar(tp: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate F1-score from counts: 2·TP / (2·TP + FP + FN).
The harmonic mean of precision and recall.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
F1-score in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or denominator is zero. |
Example
f1_score_scalar(80, 10, 20) 0.8421052631578947
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
f_test(data1: List[Union[int, float]], data2: List[Union[int, float]]) -> float
¶
F-test for comparing two variances.
Description
Returns the two-tailed p-value of an F-test comparing the variances of two datasets. Equivalent to Excel F.TEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
First dataset. |
required |
data2
|
List[Union[int, float]]
|
Second dataset. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Two-tailed p-value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If datasets have fewer than 2 elements. |
Example
round(f_test([6, 7, 9, 15, 21], [20, 28, 31, 38, 40]), 4) 0.6482
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
false_discovery_rate(fp: Union[int, float], tp: Union[int, float]) -> float
¶
Calculate False Discovery Rate: FDR = FP / (FP + TP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
Union[int, float]
|
False positives. |
required |
tp
|
Union[int, float]
|
True positives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
FDR in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts negative or both zero. |
Example
false_discovery_rate(100, 80) 0.5555555555555556
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
ffill(data: List[Any]) -> List[Any]
¶
Forward-fills None values with the last non-None value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A list that may contain None values. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A new list with None values replaced by the previous non-None value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
Example
ffill([1, None, None, 4, None]) [1, 1, 1, 4, 4]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
fillna(data: List[Any], value: Any = 0) -> List[Any]
¶
Replaces None values in a list with a specified fill value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A list that may contain None values. |
required |
value
|
Any
|
The replacement value (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
A new list with None values replaced. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
Example
fillna([1, None, 3, None, 5]) [1, 0, 3, 0, 5]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
fisher(x: float) -> float
¶
Calculates the Fisher transformation.
Transforms a Pearson correlation coefficient into a normally distributed variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
A value in the range (-1, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Fisher transformation value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is not in (-1, 1). |
Example
round(fisher(0.75), 6) 0.972955
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
fisher_inv(y: float) -> float
¶
Calculates the inverse Fisher transformation.
Converts a Fisher-transformed value back to a correlation coefficient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
A Fisher-transformed value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The corresponding correlation coefficient in (-1, 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If y is not numeric. |
Example
round(fisher_inv(0.972955), 6) 0.75
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
focal_loss(y_true: Union[int, float], y_pred: Union[int, float], gamma: Union[int, float] = 2.0, alpha: Union[int, float] = 0.25) -> float
¶
Calculate focal loss for a single sample.
FL(p_t) = -α_t * (1 - p_t)^γ * log(p_t)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Union[int, float]
|
True label (0 or 1). |
required |
y_pred
|
Union[int, float]
|
Predicted probability in (0, 1). |
required |
gamma
|
Union[int, float]
|
Focusing parameter (default 2.0). |
2.0
|
alpha
|
Union[int, float]
|
Balancing factor (default 0.25). |
0.25
|
Returns:
| Type | Description |
|---|---|
float
|
Focal loss value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If y_true not in {0, 1} or y_pred not in (0, 1). |
Example
round(focal_loss(1, 0.9), 6) 0.000264
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
forecast_ets(target: Union[int, float], values: List[Union[int, float]], timeline: List[Union[int, float]], seasonality: int = 1, data_completion: int = 1, aggregation: int = 1) -> float
¶
Forecast value using exponential smoothing (ETS).
Description
Simplified ETS forecast using double exponential smoothing (Holt's method). Equivalent to Excel FORECAST.ETS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Union[int, float]
|
Target timeline point to forecast. |
required |
values
|
List[Union[int, float]]
|
Known values. |
required |
timeline
|
List[Union[int, float]]
|
Known timeline points. |
required |
seasonality
|
int
|
Seasonal period (1 = no seasonality). |
1
|
data_completion
|
int
|
1 = fill missing with interpolation. |
1
|
aggregation
|
int
|
1 = average for duplicates. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Forecasted value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not valid types. |
ValueError
|
If arrays differ in length or are too short. |
Example
round(forecast_ets(6, [10, 20, 30, 40, 50], [1, 2, 3, 4, 5]), 1) 60.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 | |
forecast_linear(x: Union[int, float], known_y: List[Union[int, float]], known_x: List[Union[int, float]]) -> float
¶
Predicts a value using linear regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The independent value for which to predict y. |
required |
known_y
|
List[Union[int, float]]
|
Known dependent data values. |
required |
known_x
|
List[Union[int, float]]
|
Known independent data values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The predicted y value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid. |
ValueError
|
If data is insufficient. |
Example
forecast_linear(4, [2, 4, 6], [1, 2, 3]) 8.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
frequency_bins(data: List[Union[int, float]], bins: List[Union[int, float]]) -> List[int]
¶
Calculates frequency distribution of data across bin boundaries.
Returns how many values fall in each bin. The result has len(bins) + 1 elements: values <= bins[0], between bins[0] and bins[1], ..., > bins[-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
bins
|
List[Union[int, float]]
|
A sorted list of bin upper boundaries. |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
A list of counts per bin. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If data is empty. |
Example
frequency_bins([1, 2, 3, 4, 5, 6], [2, 4]) [2, 2, 2]
Complexity: O(n * m) where n = len(data), m = len(bins)
Source code in shortfx/fxNumeric/statistics_functions.py
gamma_pdf(x: float, alpha: float, beta: float) -> float
¶
Compute the gamma distribution PDF.
f(x) = β^α · x^(α-1) · e^(-βx) / Γ(α)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ 0). |
required |
alpha
|
float
|
Shape parameter (> 0). |
required |
beta
|
float
|
Rate parameter (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x < 0, alpha ≤ 0, or beta ≤ 0. |
Usage Example
round(gamma_pdf(1.0, 2.0, 1.0), 4) 0.3679
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
gelu(x: Union[int, float]) -> float
¶
Compute the Gaussian Error Linear Unit activation.
GELU(x) = x * Φ(x) where Φ is the standard normal CDF. Uses the tanh approximation: 0.5x(1+tanh(sqrt(2/π)(x+0.044715x³)))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
GELU of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
round(gelu(1.0), 4) 0.8412
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
geometric_mean(data: List[Union[int, float]]) -> float
¶
Calculates the geometric mean of a list of positive numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of positive numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The geometric mean. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or contains non-positive values. |
Example
geometric_mean([2, 8]) 4.0 round(geometric_mean([1, 3, 9, 27]), 6) 5.196152
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
geometric_pdf(k: int, p: Union[int, float]) -> float
¶
Return the geometric probability mass at trial k.
P(X = k) = (1 − p)^(k−1) · p, where k ≥ 1. This counts the number of trials until the first success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Trial number (must be ≥ 1). |
required |
p
|
Union[int, float]
|
Success probability per trial (0 < p ≤ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability mass as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k is not an integer or p not numeric. |
ValueError
|
If k < 1 or p is out of (0, 1]. |
Example
geometric_pdf(3, 0.5) 0.125
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
geometric_pmf(k: int, p: float) -> float
¶
Compute the geometric distribution PMF.
P(X = k) = (1-p)^(k-1) · p, k = 1, 2, 3, ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of trials until first success (≥ 1). |
required |
p
|
float
|
Success probability (0 < p ≤ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of first success on trial k. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k is not int or p is not numeric. |
ValueError
|
If k < 1 or p not in (0, 1]. |
Usage Example
round(geometric_pmf(3, 0.5), 4) 0.125
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
gini_coefficient(data: List[Union[int, float]]) -> float
¶
Computes the Gini coefficient measuring statistical dispersion.
A Gini of 0 represents perfect equality; 1 represents maximal inequality. Uses the relative mean absolute difference formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A non-empty list of non-negative numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Gini coefficient in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data is empty or contains negative values. |
Example
gini_coefficient([1, 1, 1, 1]) 0.0 round(gini_coefficient([1, 2, 3, 4, 5]), 4) 0.2667 round(gini_coefficient([0, 0, 0, 0, 100]), 2) 0.8
Complexity: O(n²)
Source code in shortfx/fxNumeric/statistics_functions.py
gini_impurity_binary(p: Union[int, float]) -> float
¶
Calculate binary Gini impurity G = 2·p·(1-p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Union[int, float]
|
Probability of positive class, in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Gini impurity in [0, 0.5]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p not in [0, 1]. |
Example
gini_impurity_binary(0.5) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
glass_delta(treatment: list[float], control: list[float]) -> float
¶
Glass's Δ — effect size using the control group's SD.
Δ = (M_treatment − M_control) / SD_control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
treatment
|
list[float]
|
Treatment group sample (length ≥ 1). |
required |
control
|
list[float]
|
Control group sample (length ≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Glass's Δ as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If treatment is empty, control has < 2, or control SD is 0. |
Usage Example
round(glass_delta([3, 4, 5, 6, 7], [1, 2, 3, 4, 5]), 4) 1.2649
Complexity: O(n + m)
Source code in shortfx/fxNumeric/statistics_functions.py
growth(known_y: List[Union[int, float]], known_x: Optional[List[Union[int, float]]] = None, new_x: Optional[List[Union[int, float]]] = None) -> List[float]
¶
Predicted values along an exponential trend.
Description
Uses least-squares exponential regression to predict y values. Equivalent to Excel GROWTH. Fits y = b * m^x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Known y-values (must be positive). |
required |
known_x
|
Optional[List[Union[int, float]]]
|
Known x-values (defaults to 1, 2, ..., n). |
None
|
new_x
|
Optional[List[Union[int, float]]]
|
X-values for which to predict (defaults to known_x). |
None
|
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: Predicted y-values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If known_y contains non-positive values. |
Example
[round(v, 2) for v in growth([33100, 47300, 69000, 102000, 150000, 220000])][32618.2, 47729.42, 69841.3, 102197.07, 149542.49, 218821.87]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
grubbs_test(data: list[float | int]) -> dict
¶
Perform Grubbs' test for a single outlier.
Tests whether the maximum or minimum value is an outlier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
List of numeric observations (n ≥ 3). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: |
dict
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data has fewer than 3 elements or zero variance. |
Example
result = grubbs_test([1, 2, 3, 4, 5, 100]) result["position"] 'max'
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
gumbel_pdf(x: Union[int, float], mu: Union[int, float] = 0.0, beta: Union[int, float] = 1.0) -> float
¶
Return the Gumbel (Type I extreme value) density at x.
PDF = (1/β) · exp(−z − exp(−z)), where z = (x − μ) / β.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Point at which to evaluate. |
required |
mu
|
Union[int, float]
|
Location parameter. |
0.0
|
beta
|
Union[int, float]
|
Scale parameter (must be > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If beta ≤ 0. |
Example
round(gumbel_pdf(0), 6) 0.367879
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
hard_sigmoid(x: float) -> float
¶
Hard sigmoid: piecewise-linear approximation of the sigmoid function.
Description
hard_sigmoid(x) = max(0, min(1, (x + 3) / 6)). Computationally cheap alternative used in mobile and edge models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Approximated sigmoid value in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
hard_sigmoid(0.0) 0.5 hard_sigmoid(3.0) 1.0 hard_sigmoid(-3.0) 0.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
hard_swish(x: float) -> float
¶
Hard swish: efficient approximation of the swish activation.
Description
hard_swish(x) = x × hard_sigmoid(x). Used in MobileNetV3 and similar efficient architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Hard-swish activation value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
hard_swish(0.0) 0.0 hard_swish(3.0) 3.0 round(hard_swish(-1.0), 6) -0.333333
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
harmonic_mean(data: List[Union[int, float]]) -> float
¶
Calculates the harmonic mean of a list of positive numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of positive numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The harmonic mean. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or contains non-positive values. |
Example
harmonic_mean([1, 4, 4]) 2.0 round(harmonic_mean([2, 3, 6]), 6) 3.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
hedges_g(group1: list[float], group2: list[float]) -> float
¶
Hedges' g — bias-corrected effect size (for small samples).
g = Cohen's d × (1 − 3 / (4(n1+n2) − 9)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group1
|
list[float]
|
First sample (length ≥ 2). |
required |
group2
|
list[float]
|
Second sample (length ≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Hedges' g as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If either group has fewer than 2 elements. |
Usage Example
round(hedges_g([1, 2, 3, 4, 5], [3, 4, 5, 6, 7]), 4) -1.1425
Complexity: O(n + m)
Source code in shortfx/fxNumeric/statistics_functions.py
hinge_loss(y_true: Union[int, float], y_pred: Union[int, float]) -> float
¶
Compute hinge loss for a single sample.
Used in SVM classifiers. L = max(0, 1 - y_true * y_pred). y_true should be -1 or +1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Union[int, float]
|
True label (-1 or +1). |
required |
y_pred
|
Union[int, float]
|
Predicted score (real-valued). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Hinge loss value (non-negative). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If y_true is not -1 or +1. |
Example
hinge_loss(1, 0.8) 0.19999999999999996 hinge_loss(1, 2.0) 0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
holt_linear_forecast(data: list[float], alpha: float, beta: float, steps: int = 1) -> list[float]
¶
Holt's linear exponential smoothing forecast.
Double exponential smoothing with level and trend components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
Historical time series (length ≥ 2). |
required |
alpha
|
float
|
Level smoothing factor in (0, 1]. |
required |
beta
|
float
|
Trend smoothing factor in (0, 1]. |
required |
steps
|
int
|
Number of future steps to forecast (≥ 1). |
1
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of forecasted values (length = steps). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data has < 2 elements or parameters out of range. |
Usage Example
[round(v, 2) for v in holt_linear_forecast([1, 2, 3, 4, 5], 0.8, 0.2, 3)][6.0, 7.0, 8.0]
Complexity: O(n + steps)
Source code in shortfx/fxNumeric/statistics_functions.py
huber_loss(y_true: Union[int, float], y_pred: Union[int, float], delta: Union[int, float] = 1.0) -> float
¶
Compute the Huber loss for a single prediction.
Less sensitive to outliers than squared error. Equals squared error for small residuals and absolute error for large residuals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Union[int, float]
|
True value. |
required |
y_pred
|
Union[int, float]
|
Predicted value. |
required |
delta
|
Union[int, float]
|
Threshold at which to switch from squared to linear (positive). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Huber loss value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If delta is not positive. |
Example
huber_loss(1.0, 1.5) 0.125 huber_loss(1.0, 5.0) 3.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
hypergeometric_pmf(k: int, N: int, K: int, n: int) -> float
¶
Compute the hypergeometric distribution PMF.
P(X = k) = C(K,k)·C(N-K,n-k) / C(N,n)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of successes drawn. |
required |
N
|
int
|
Population size. |
required |
K
|
int
|
Number of success states in population. |
required |
n
|
int
|
Number of draws. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not integers. |
ValueError
|
If parameters are out of range. |
Usage Example
round(hypergeometric_pmf(2, 52, 13, 5), 4) 0.2743
Complexity: O(1) using math.comb
Source code in shortfx/fxNumeric/statistics_functions.py
intercept(known_y: List[Union[int, float]], known_x: List[Union[int, float]]) -> float
¶
Calculates the y-intercept of the linear regression line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Dependent data values. |
required |
known_x
|
List[Union[int, float]]
|
Independent data values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The y-intercept (b) of the best-fit line y = mx + b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length or have < 2 elements. |
Example
intercept([2, 4, 6], [1, 2, 3]) 0.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
jaccard_index_scalar(tp: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate the Jaccard index from scalar confusion-matrix counts.
J = TP / (TP + FP + FN)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Jaccard index in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If tp + fp + fn is zero. |
Example
jaccard_index_scalar(50, 10, 5) 0.7692307692307693
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
jaccard_similarity(set_a: List, set_b: List) -> float
¶
Computes the Jaccard similarity index between two collections.
J(A, B) = |A ∩ B| / |A ∪ B|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
set_a
|
List
|
First collection of elements. |
required |
set_b
|
List
|
Second collection of elements. |
required |
Returns:
| Type | Description |
|---|---|
float
|
A float in [0, 1]. Returns 1.0 if both are empty. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists. |
Example
jaccard_similarity([1, 2, 3], [2, 3, 4]) 0.5
Complexity: O(n + m)
Source code in shortfx/fxNumeric/statistics_functions.py
jarque_bera(data: List[float]) -> Tuple[float, bool]
¶
Performs the Jarque-Bera test for normality.
JB = (n/6) × (S² + K²/4) where S = skewness, K = excess kurtosis. Under normality, JB is asymptotically χ²(2). The null hypothesis of normality is rejected when JB > 5.99 (α=0.05).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[float]
|
Numeric data list (n ≥ 3). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (JB statistic, is_normal) where is_normal is True |
bool
|
if the null hypothesis of normality is NOT rejected at α=0.05. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list. |
ValueError
|
If data has fewer than 3 elements. |
Example
jb, normal = jarque_bera([1, 2, 3, 4, 5]) normal True
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
js_divergence(p: list[float], q: list[float]) -> float
¶
Jensen-Shannon divergence: symmetric, bounded version of KL.
JSD(P || Q) = ½ D_KL(P || M) + ½ D_KL(Q || M) where M = ½(P+Q).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list[float]
|
Distribution P (probabilities summing to 1, each > 0). |
required |
q
|
list[float]
|
Distribution Q (same length, each > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
JS divergence in bits, in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or constraints violated. |
Usage Example
round(js_divergence([0.5, 0.5], [0.6, 0.4]), 4) 0.0073
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
kendall_tau(data1: List[Union[int, float]], data2: List[Union[int, float]]) -> float
¶
Calculates the Kendall rank correlation coefficient (tau-b).
A non-parametric measure of the ordinal association between two ranked datasets. Handles tied pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
First list of numeric values. |
required |
data2
|
List[Union[int, float]]
|
Second list of numeric values (same length as data1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Kendall tau-b in the range [-1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists have different lengths or fewer than 2 elements. |
Example
round(kendall_tau([1, 2, 3, 4, 5], [5, 6, 7, 8, 7]), 6) 0.738549
Complexity: O(n^2)
Source code in shortfx/fxNumeric/statistics_functions.py
kl_divergence(p: list[float], q: list[float]) -> float
¶
Kullback-Leibler divergence D_KL(P || Q) = Σ p·log₂(p/q).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
list[float]
|
True distribution (probabilities summing to 1, each > 0). |
required |
q
|
list[float]
|
Approximating distribution (same length, each > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
KL divergence in bits as a float (≥ 0). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or constraints violated. |
Usage Example
round(kl_divergence([0.5, 0.5], [0.6, 0.4]), 4) 0.0294
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
kurtosis(data: List[Union[int, float]], excess: bool = True) -> float
¶
Calculates the kurtosis of a dataset.
Kurtosis measures the "tailedness" of a probability distribution. Excess kurtosis subtracts 3 so that a normal distribution has kurtosis 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values (at least 4 elements). |
required |
excess
|
bool
|
If True, returns excess kurtosis (Fisher). Default True. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
The kurtosis value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list has fewer than 4 elements or zero variance. |
Example
round(kurtosis([1, 2, 3, 4, 5]), 4) -1.3
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
lag(data: List[Any], periods: int = 1, default: Any = None) -> List[Any]
¶
Shifts values forward (access previous values), like SQL LAG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A list of values. |
required |
periods
|
int
|
Number of positions to lag (default 1). |
1
|
default
|
Any
|
Value to fill for positions without a predecessor. |
None
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list of the same length, shifted forward. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
ValueError
|
If periods < 1. |
Example
lag([10, 20, 30, 40], 1) [None, 10, 20, 30]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
laplace_pdf(x: float, mu: float = 0.0, b: float = 1.0) -> float
¶
Compute the Laplace distribution PDF.
f(x) = (1/(2b)) · e^(-|x-μ|/b)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
mu
|
float
|
Location parameter (default 0). |
0.0
|
b
|
float
|
Scale parameter (default 1, > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If b ≤ 0. |
Usage Example
round(laplace_pdf(0.0), 4) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
large(data: List[Union[int, float]], k: int) -> Union[int, float]
¶
Returns the k-th largest value in a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
k
|
int
|
The rank (1 = largest, 2 = second largest, etc.). |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
The k-th largest value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers or k is not int. |
ValueError
|
If the list is empty or k is out of range. |
Example
large([3, 1, 4, 1, 5, 9], 1) 9 large([3, 1, 4, 1, 5, 9], 3) 4
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
lead(data: List[Any], periods: int = 1, default: Any = None) -> List[Any]
¶
Shifts values backward (access subsequent values), like SQL LEAD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Any]
|
A list of values. |
required |
periods
|
int
|
Number of positions to lead (default 1). |
1
|
default
|
Any
|
Value to fill for positions without a successor. |
None
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list of the same length, shifted backward. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
ValueError
|
If periods < 1. |
Example
lead([10, 20, 30, 40], 1) [20, 30, 40, None]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
leaky_relu(x: Union[int, float], alpha: float = 0.01) -> float
¶
Compute Leaky Rectified Linear Unit: x if x > 0 else alpha * x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
alpha
|
float
|
Slope for negative values (default 0.01). |
0.01
|
Returns:
| Type | Description |
|---|---|
float
|
Leaky ReLU of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or alpha is not numeric. |
Example
leaky_relu(5) 5 leaky_relu(-10) -0.1
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
linest(known_y: List[Union[int, float]], known_x: Optional[List[Union[int, float]]] = None) -> Dict[str, float]
¶
Linear regression statistics.
Description
Returns slope, intercept, r_squared, standard_error, and other statistics for a linear regression. Equivalent to Excel LINEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Known y-values. |
required |
known_x
|
Optional[List[Union[int, float]]]
|
Known x-values (defaults to 1, 2, ..., n). |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dict with keys: slope, intercept, r_squared, std_err_slope, |
Dict[str, float]
|
std_err_intercept, f_statistic, df, ss_reg, ss_resid. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If arrays are empty or mismatched. |
Example
result = linest([1, 9, 5, 7]) round(result['slope'], 4) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 | |
ljung_box(residuals: list[float | int], lags: int = 10) -> float
¶
Compute the Ljung-Box Q statistic for autocorrelation testing.
Q = n(n+2) Σ_{k=1}^{h} r_k² / (n-k)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
residuals
|
list[float | int]
|
Time-series residuals. |
required |
lags
|
int
|
Number of lags to test. |
10
|
Returns:
| Type | Description |
|---|---|
float
|
The Q statistic. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If residuals is not a list of numbers. |
ValueError
|
If residuals has fewer elements than lags + 1. |
Example
round(ljung_box([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1], 5), 2) 72.0
Complexity: O(n × h)
Source code in shortfx/fxNumeric/statistics_functions.py
log_cosh_loss(actual: Union[int, float], predicted: Union[int, float]) -> float
¶
Calculate the log-cosh loss for a single observation.
L = log(cosh(predicted - actual))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
Union[int, float]
|
True value. |
required |
predicted
|
Union[int, float]
|
Predicted value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log-cosh loss (always non-negative). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
round(log_cosh_loss(3, 5), 6) 1.32461
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
log_loss(y_true: int, y_pred: Union[int, float], eps: float = 1e-15) -> float
¶
Compute binary cross-entropy (log loss) for a single sample.
L = -(yln(p) + (1-y)ln(1-p))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
int
|
True label (0 or 1). |
required |
y_pred
|
Union[int, float]
|
Predicted probability in (0, 1). |
required |
eps
|
float
|
Clipping epsilon to avoid log(0). |
1e-15
|
Returns:
| Type | Description |
|---|---|
float
|
Log loss value (non-negative). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If y_true is not 0 or 1, or y_pred not in [0, 1]. |
Example
round(log_loss(1, 0.9), 4) 0.1054
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
log_normal_pdf(x: float, mu: float = 0.0, sigma: float = 1.0) -> float
¶
Compute the log-normal distribution PDF.
f(x) = (1/(xσ√(2π))) · exp(-(ln(x)-μ)²/(2σ²))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (> 0). |
required |
mu
|
float
|
Mean of the log (default 0). |
0.0
|
sigma
|
float
|
Std dev of the log (default 1, > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x ≤ 0 or sigma ≤ 0. |
Usage Example
round(log_normal_pdf(1.0), 4) 0.3989
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
logest(known_y: List[Union[int, float]], known_x: Optional[List[Union[int, float]]] = None) -> Dict[str, float]
¶
Exponential regression statistics.
Description
Returns base (m), coefficient (b), and r_squared for an exponential curve y = b * m^x. Equivalent to Excel LOGEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Known y-values (must be positive). |
required |
known_x
|
Optional[List[Union[int, float]]]
|
Known x-values (defaults to 1, 2, ..., n). |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dict with keys: base, coefficient, r_squared. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If known_y has non-positive values. |
Example
result = logest([33100, 47300, 69000, 102000, 150000, 220000]) round(result['base'], 4) 1.4633
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
logistic_cdf(x: float, mu: float = 0.0, s: float = 1.0) -> float
¶
Compute the logistic distribution CDF.
F(x) = 1 / (1 + e^(-(x-μ)/s))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
mu
|
float
|
Location parameter (default 0). |
0.0
|
s
|
float
|
Scale parameter (default 1, > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Cumulative probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If s ≤ 0. |
Usage Example
logistic_cdf(0.0) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
logistic_function(x: Union[int, float], L: Union[int, float] = 1.0, k: Union[int, float] = 1.0, x0: Union[int, float] = 0.0) -> float
¶
Calculate the generalised logistic (sigmoid) function.
f(x) = L / (1 + e^(-k·(x - x₀)))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
L
|
Union[int, float]
|
Supremum of output (default 1). |
1.0
|
k
|
Union[int, float]
|
Steepness (default 1). |
1.0
|
x0
|
Union[int, float]
|
Midpoint (default 0). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Logistic value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
logistic_function(0) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
logistic_pdf(x: float, mu: float = 0.0, s: float = 1.0) -> float
¶
Compute the logistic distribution PDF.
f(x) = e^(-(x-μ)/s) / (s(1 + e^(-(x-μ)/s))²)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
mu
|
float
|
Location parameter (default 0). |
0.0
|
s
|
float
|
Scale parameter (default 1, > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If s ≤ 0. |
Usage Example
round(logistic_pdf(0.0), 4) 0.25
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
logit(p: Union[int, float]) -> float
¶
Compute the logit (log-odds) function, inverse of sigmoid.
logit(p) = log(p / (1 - p))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Union[int, float]
|
Probability in (0, 1) exclusive. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Log-odds value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p is not in (0, 1). |
Example
logit(0.5) 0.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
mahalanobis_distance_1d(x: float, data: list[float]) -> float
¶
Mahalanobis distance of a point from a 1D dataset.
D = |x − μ| / σ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The point to measure. |
required |
data
|
list[float]
|
The reference dataset (length ≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mahalanobis distance as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data has < 2 elements or SD is zero. |
Usage Example
round(mahalanobis_distance_1d(10, [2, 4, 4, 4, 5, 5, 7, 9]), 4) 2.3385
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
manhattan_distance(a: list[float], b: list[float]) -> float
¶
Manhattan (L1) distance between two vectors.
d = Σ |a_i − b_i|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
list[float]
|
First vector. |
required |
b
|
list[float]
|
Second vector (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Manhattan distance as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ or empty. |
Usage Example
manhattan_distance([1, 2, 3], [4, 5, 6]) 9.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
mann_whitney_u(data1: List[float], data2: List[float]) -> Tuple[float, float]
¶
Performs the Mann-Whitney U test (standalone, no scipy).
Non-parametric test for whether two independent samples come from the same distribution. Returns U statistic and approximate z-score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[float]
|
First sample. |
required |
data2
|
List[float]
|
Second sample. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (U statistic, z_score). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists. |
ValueError
|
If either sample is empty. |
Example
u, z = mann_whitney_u([1, 2, 3], [4, 5, 6]) u 0.0
Complexity: O(n₁ × n₂)
Source code in shortfx/fxNumeric/statistics_functions.py
markov_chain_steady_state(transition: list[list[float]], tol: float = 1e-10, max_iter: int = 1000) -> list[float]
¶
Compute the steady-state distribution of an ergodic Markov chain.
Uses the power method: repeatedly multiplies a uniform distribution vector by the transition matrix until convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transition
|
list[list[float]]
|
Square row-stochastic transition matrix. |
required |
tol
|
float
|
Convergence tolerance. |
1e-10
|
max_iter
|
int
|
Maximum iterations. |
1000
|
Returns:
| Type | Description |
|---|---|
list[float]
|
Stationary distribution as a list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If transition is not a list of lists. |
ValueError
|
If matrix is empty or not square. |
Example
pi = markov_chain_steady_state([[0.9, 0.1], [0.5, 0.5]]) [round(p, 4) for p in pi][0.8333, 0.1667]
Complexity: O(n² × iterations)
Source code in shortfx/fxNumeric/statistics_functions.py
7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 | |
matthews_corrcoef_scalar(tp: Union[int, float], tn: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate Matthews Correlation Coefficient from confusion matrix counts.
MCC = (TP·TN - FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
tn
|
Union[int, float]
|
True negatives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
MCC in [-1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or denominator is zero. |
Example
round(matthews_corrcoef_scalar(80, 900, 100, 20), 4) 0.5765
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
max_drawdown(values: List[Union[int, float]]) -> float
¶
Computes the maximum drawdown of a value series.
Maximum peak-to-trough decline as a positive fraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
List[Union[int, float]]
|
List of portfolio values or price series (positive). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The maximum drawdown as a positive fraction in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If values is empty or contains non-positive values. |
Example
max_drawdown([100, 120, 90, 110, 80]) 0.3333333333333333
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
max_if(values: list[int | float], criteria_range: list, criteria) -> float
¶
Return the maximum of values where the parallel criteria_range meets criteria.
Equivalent to the Excel MAXIFS function (single criteria pair).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[int | float]
|
Numeric values to evaluate. |
required |
criteria_range
|
list
|
Range to test against criteria (same length as values). |
required |
criteria
|
Excel-style criteria (e.g. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Maximum among matching values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no values match or lengths differ. |
Example
max_if([5, 10, 15, 20], ["A", "B", "A", "B"], "A") 15 max_if([1, 2, 3, 4], [10, 20, 30, 40], ">15") 4
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
maxwell_boltzmann_pdf(v: Union[int, float], a: Union[int, float]) -> float
¶
Return the Maxwell-Boltzmann speed distribution density.
PDF = √(2/π) · v² · exp(−v² / (2a²)) / a³, where a = √(kT / m).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
Union[int, float]
|
Speed (must be ≥ 0). |
required |
a
|
Union[int, float]
|
Scale parameter a = √(kT/m) (must be > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If v < 0 or a ≤ 0. |
Example
round(maxwell_boltzmann_pdf(1, 1), 6) 0.483941
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
mean_absolute_error(observed: list[float], predicted: list[float]) -> float
¶
Mean absolute error (MAE).
MAE = (1/n) Σ |observed_i − predicted_i|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed values. |
required |
predicted
|
list[float]
|
Predicted values (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
MAE as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ or are empty. |
Usage Example
mean_absolute_error([1, 2, 3], [1.5, 2.5, 3.5]) 0.5
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
mean_absolute_percentage_error(observed: list[float], predicted: list[float]) -> float
¶
Mean absolute percentage error (MAPE).
MAPE = (100/n) Σ |observed_i − predicted_i| / |observed_i|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed values (all non-zero). |
required |
predicted
|
list[float]
|
Predicted values (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
MAPE as a percentage float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or any observed value is zero. |
Usage Example
round(mean_absolute_percentage_error([100, 200, 300], [110, 190, 280]), 2) 7.22
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
mean_bias_error(measured: Union[int, float], actual: Union[int, float]) -> float
¶
Calculate mean bias error for a single observation: measured - actual.
A positive value indicates overestimation, negative indicates underestimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measured
|
Union[int, float]
|
Measured/predicted value. |
required |
actual
|
Union[int, float]
|
Actual/true value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Signed error (bias). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
mean_bias_error(105, 100) 5.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
mean_percentage_error(actual: Union[int, float], predicted: Union[int, float]) -> float
¶
Calculate mean percentage error for a single observation.
MPE = (actual - predicted) / actual * 100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
Union[int, float]
|
Actual (true) value. |
required |
predicted
|
Union[int, float]
|
Predicted value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Percentage error (positive = underestimate). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If actual is zero. |
Example
mean_percentage_error(100, 90) 10.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
mean_squared_error(observed: List[Union[int, float]], predicted: List[Union[int, float]]) -> float
¶
Calculates the mean squared error (MSE) between observed and predicted values.
MSE is a standard metric for evaluating prediction accuracy. Lower values indicate better fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
List[Union[int, float]]
|
A list of actual values. |
required |
predicted
|
List[Union[int, float]]
|
A list of predicted values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The mean squared error. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length or are empty. |
Example
mean_squared_error([3, -0.5, 2, 7], [2.5, 0.0, 2, 8]) 0.375 mean_squared_error([1, 2, 3], [1, 2, 3]) 0.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
median_absolute_deviation(data: list[float]) -> float
¶
Median absolute deviation (MAD) — robust measure of dispersion.
MAD = median(|x_i − median(x)|).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (non-empty). |
required |
Returns:
| Type | Description |
|---|---|
float
|
MAD as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data is empty. |
Usage Example
median_absolute_deviation([1, 2, 3, 4, 5]) 1.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
midhinge(data: list) -> float
¶
Calculate the midhinge of a dataset.
Description
Midhinge = (Q1 + Q3) / 2. The average of the first and third quartiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list
|
A list of numeric values (at least 2 elements). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The midhinge as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If data has fewer than 2 elements. |
Usage Example
midhinge([1, 2, 3, 4, 5]) 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
midrange(data: list) -> float
¶
Calculate the midrange of a dataset.
Description
Midrange = (max + min) / 2. The arithmetic mean of the extreme values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list
|
A list of numeric values (at least 1 element). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The midrange as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If data is empty. |
Usage Example
midrange([2, 4, 6, 8, 10]) 6.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
min_if(values: list[int | float], criteria_range: list, criteria) -> float
¶
Return the minimum of values where the parallel criteria_range meets criteria.
Equivalent to the Excel MINIFS function (single criteria pair).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[int | float]
|
Numeric values to evaluate. |
required |
criteria_range
|
list
|
Range to test against criteria (same length as values). |
required |
criteria
|
Excel-style criteria (e.g. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Minimum among matching values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no values match or lengths differ. |
Example
min_if([5, 10, 15, 20], ["A", "B", "A", "B"], "A") 5 min_if([1, 2, 3, 4], [10, 20, 30, 40], ">15") 2
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
minkowski_distance(vec_a: List[Union[int, float]], vec_b: List[Union[int, float]], p: Union[int, float] = 2) -> float
¶
Computes the Minkowski distance of order p between two vectors.
d = (Σ|aᵢ − bᵢ|^p)^(1/p). p=1 gives Manhattan, p=2 gives Euclidean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_a
|
List[Union[int, float]]
|
First numeric vector. |
required |
vec_b
|
List[Union[int, float]]
|
Second numeric vector (same length). |
required |
p
|
Union[int, float]
|
Order of the distance (must be >= 1). |
2
|
Returns:
| Type | Description |
|---|---|
float
|
The Lp distance (non-negative float). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers or p is not numeric. |
ValueError
|
If vectors are empty, different lengths, or p < 1. |
Example
round(minkowski_distance([1, 2, 3], [4, 6, 3], 3), 4) 4.4979
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
mish(x: Union[int, float]) -> float
¶
Compute Mish activation: x * tanh(softplus(x)).
A self-regularized non-monotonic activation proposed by Misra (2019).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mish of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
round(mish(1.0), 4) 0.8651
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
mode_mult(data: List[Union[int, float]]) -> List[Union[int, float]]
¶
Returns all modes (most frequent values) in a dataset.
Description
Returns a list of all values that appear with the highest frequency. Equivalent to Excel MODE.MULT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
List |
List[Union[int, float]]
|
All modal values sorted ascending. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list. |
ValueError
|
If no mode exists (all values unique). |
Example
mode_mult([1, 2, 3, 4, 3, 2, 1, 2, 3]) [2, 3]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
mode_single(data: List[Union[int, float]]) -> float
¶
Returns the single most frequent value in a dataset.
Description
Returns the first value with the highest frequency in order of appearance. Equivalent to Excel MODE.SNGL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The first modal value by order of appearance. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list. |
ValueError
|
If data is empty or all values are unique. |
Example
mode_single([1, 2, 3, 3, 4]) 3.0 mode_single([1, 2, 2, 3, 3]) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
moors_kurtosis(data: list[float]) -> float
¶
Moors' octile-based kurtosis measure (robust alternative).
K_M = [(E7 − E5) + (E3 − E1)] / (E6 − E2) where E_i are octiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 8). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Moors' kurtosis as a float (normal ≈ 1.23). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data has fewer than 8 elements. |
Usage Example
round(moors_kurtosis(list(range(1, 101))), 2) 1.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
moving_median(data: List[Union[int, float]], window: int) -> List[Optional[float]]
¶
Computes a rolling median over a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
window
|
int
|
Size of the sliding window (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length as data. Elements before the window |
List[Optional[float]]
|
is full are None; subsequent elements are the median of the |
List[Optional[float]]
|
preceding window values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers or window is not int. |
ValueError
|
If data is empty or window < 1. |
Example
moving_median([1, 3, 5, 7, 9], 3) [None, None, 3.0, 5.0, 7.0]
Complexity: O(n * w log w)
Source code in shortfx/fxNumeric/statistics_functions.py
multinomial_coefficient(*counts: int) -> int
¶
Compute the multinomial coefficient n! / (k1!·k2!·...·km!).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*counts
|
int
|
Non-negative integer counts. |
()
|
Returns:
| Type | Description |
|---|---|
int
|
Multinomial coefficient. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any count is not int. |
ValueError
|
If any count < 0. |
Usage Example
multinomial_coefficient(2, 3, 4) 1260
Complexity: O(m) where m = number of counts
Source code in shortfx/fxNumeric/statistics_functions.py
mutual_information(joint: list[list[float]]) -> float
¶
Mutual information I(X;Y) from a joint probability table.
I(X;Y) = Σ_x Σ_y p(x,y) log₂(p(x,y) / (p(x)·p(y))).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
joint
|
list[list[float]]
|
2D list (matrix) of joint probabilities. All > 0, sum to 1. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Mutual information in bits as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If joint is not a list of lists of numerics. |
ValueError
|
If empty, non-rectangular, or values invalid. |
Usage Example
round(mutual_information([[0.25, 0.25], [0.25, 0.25]]), 4) 0.0
Complexity: O(rows × cols)
Source code in shortfx/fxNumeric/statistics_functions.py
negative_binomial_pmf(k: int, r: int, p: float) -> float
¶
Compute the negative binomial distribution PMF.
P(X = k) = C(k+r-1, k) · p^r · (1-p)^k, k = 0, 1, 2, ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of failures before the r-th success (≥ 0). |
required |
r
|
int
|
Number of successes (≥ 1). |
required |
p
|
float
|
Success probability (0 < p ≤ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k or r not int, p not numeric. |
ValueError
|
If k < 0, r < 1, or p not in (0, 1]. |
Usage Example
round(negative_binomial_pmf(3, 2, 0.5), 4) 0.125
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
negative_predictive_value(tn: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate Negative Predictive Value: NPV = TN / (TN + FN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tn
|
Union[int, float]
|
True negatives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
NPV in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts negative or both zero. |
Example
negative_predictive_value(900, 20) 0.9782608695652174
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
normal_cdf(x: float, mu: float = 0.0, sigma: float = 1.0) -> float
¶
Compute the normal distribution CDF using the error function.
F(x) = 0.5(1 + erf((x - μ) / (σ√2)))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
mu
|
float
|
Mean (default 0). |
0.0
|
sigma
|
float
|
Standard deviation (default 1, must be > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Cumulative probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If sigma ≤ 0. |
Usage Example
round(normal_cdf(0.0), 4) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
normal_pdf(x: float, mu: float = 0.0, sigma: float = 1.0) -> float
¶
Probability density function of the normal distribution.
f(x) = (1 / (σ√(2π))) exp(−(x−μ)² / (2σ²)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Point at which to evaluate. |
required |
mu
|
float
|
Mean (default 0). |
0.0
|
sigma
|
float
|
Standard deviation (> 0, default 1). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
PDF value as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If sigma is not positive. |
Usage Example
round(normal_pdf(0), 4) 0.3989
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
odds_ratio(tp: Union[int, float], tn: Union[int, float], fp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate the odds ratio from a 2×2 confusion matrix.
OR = (TP × TN) / (FP × FN)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
tn
|
Union[int, float]
|
True negatives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Odds ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or denominator is zero. |
Example
odds_ratio(80, 900, 100, 20) 360.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
outliers_iqr(data: List[Union[int, float]], factor: float = 1.5) -> List[Union[int, float]]
¶
Detects outliers using the Interquartile Range (IQR) method.
A value is considered an outlier if it lies below Q1 - factorIQR or above Q3 + factorIQR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values (at least 4 elements). |
required |
factor
|
float
|
Multiplier for the IQR to set the fences (default 1.5). |
1.5
|
Returns:
| Type | Description |
|---|---|
List[Union[int, float]]
|
A list containing only the outlier values (preserving order). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list has fewer than 4 elements or factor <= 0. |
Example
outliers_iqr([1, 2, 3, 4, 5, 100]) [100] outliers_iqr([10, 12, 14, 16, 18]) []
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
pareto_pdf(x: float, x_m: float, alpha: float) -> float
¶
Compute the Pareto distribution PDF.
f(x) = α·x_m^α / x^(α+1) for x ≥ x_m.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ x_m). |
required |
x_m
|
float
|
Scale (minimum value, > 0). |
required |
alpha
|
float
|
Shape parameter (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x_m ≤ 0 or alpha ≤ 0. |
Usage Example
round(pareto_pdf(2.0, 1.0, 3.0), 4) 0.1875
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
partial_autocorrelation(data: list[float], max_lag: int) -> list[float]
¶
Partial autocorrelation function (PACF) via Durbin-Levinson recursion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length > max_lag + 1). |
required |
max_lag
|
int
|
Maximum lag to compute (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of PACF values [pacf(1), pacf(2), … pacf(max_lag)]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is too short or max_lag invalid. |
Usage Example
[round(v, 4) for v in partial_autocorrelation([1, 2, 3, 4, 5, 4, 3, 2, 1], 2)][0.5397, -0.43]
Complexity: O(max_lag² + n)
Source code in shortfx/fxNumeric/statistics_functions.py
9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 | |
pct_change(data: List[Union[int, float]]) -> List[Optional[float]]
¶
Calculates the percentage change between consecutive elements.
The first element is always None (no predecessor). Zero values
in the denominator produce None for that position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length with fractional changes (0.1 = 10 %). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
pct_change([100, 110, 99]) [None, 0.1, -0.1] pct_change([50, 0, 100]) [None, -1.0, None]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
percentile_exc(data: List[Union[int, float]], k: float) -> float
¶
k-th percentile using exclusive interpolation.
Description
Returns the k-th percentile using the exclusive method (k in range (0, 1) exclusive). Equivalent to Excel PERCENTILE.EXC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset. |
required |
k
|
float
|
Percentile (0 < k < 1, exclusive). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Percentile value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not valid types. |
ValueError
|
If k ≤ 0 or k ≥ 1. |
Example
percentile_exc([1, 2, 3, 4], 0.5) 2.5
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
percentile_rank(data: list[float], value: float) -> float
¶
Percentile rank of a value within a dataset.
Returns the percentage of values that fall at or below the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (non-empty). |
required |
value
|
float
|
The value to rank. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Percentile rank in [0, 100]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is empty. |
Usage Example
percentile_rank([1, 2, 3, 4, 5], 3) 60.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
percentile_to_z(p: Union[int, float]) -> float
¶
Converts a percentile (0–100) to a z-score using inverse erf approximation.
Uses a rational approximation of the inverse normal CDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Union[int, float]
|
Percentile in (0, 100) exclusive. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The z-score. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p is not in (0, 100). |
Example
round(percentile_to_z(97.5), 2) 1.96
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
percentile_to_z_score(p: float) -> float
¶
Convert a percentile (cumulative probability) to a z-score.
Uses the rational approximation by Abramowitz & Stegun (probit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Percentile in (0, 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Corresponding z-score. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If p is not numeric. |
ValueError
|
If p is outside (0, 1). |
Example
round(percentile_to_z_score(0.975), 2) 1.96
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
percentrank_exc(data: List[Union[int, float]], x: Union[int, float], significance: int = 3) -> float
¶
Rank of a value as a percentage (exclusive).
Description
Returns the rank of a value among a dataset as a percentage, using the exclusive method. Equivalent to Excel PERCENTRANK.EXC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset. |
required |
x
|
Union[int, float]
|
Value to rank. |
required |
significance
|
int
|
Number of significant digits (default 3). |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Percentage rank (0 to 1, exclusive). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not valid types. |
ValueError
|
If x is outside the range of data. |
Example
percentrank_exc([1, 2, 3, 6, 6, 6, 7, 8, 9], 7) 0.778
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
point_biserial_correlation(binary: list[int], continuous: list[float]) -> float
¶
Point-biserial correlation between a binary and continuous variable.
r_pb = (M1 − M0) / s × √(n0·n1 / n²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary
|
list[int]
|
List of 0s and 1s. |
required |
continuous
|
list[float]
|
List of corresponding continuous values (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Point-biserial r in [−1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If lengths differ, < 2 elements, or binary not {0,1}. |
Usage Example
round(point_biserial_correlation([0, 0, 1, 1], [10, 12, 20, 22]), 4) 0.8492
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
poisson_pmf(k: int, lam: float) -> float
¶
Probability mass function of the Poisson distribution.
P(X=k) = (λ^k · e^{−λ}) / k!.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of events (non-negative integer). |
required |
lam
|
float
|
Average rate of events λ (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k is not int or lam is not numeric. |
ValueError
|
If k < 0 or lam ≤ 0. |
Usage Example
round(poisson_pmf(3, 2.5), 4) 0.2138
Complexity: O(k)
Source code in shortfx/fxNumeric/statistics_functions.py
poisson_probability(k: int, lam: Union[int, float]) -> float
¶
Calculate Poisson probability P(X=k) = e^(-λ) · λ^k / k!.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Number of observed events (non-negative integer). |
required |
lam
|
Union[int, float]
|
Expected rate (λ > 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of exactly k events. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k is not int or lam not numeric. |
ValueError
|
If k < 0 or lam <= 0. |
Example
round(poisson_probability(3, 2.5), 6) 0.213763
Complexity: O(k)
Source code in shortfx/fxNumeric/statistics_functions.py
precision_score_scalar(tp: Union[int, float], fp: Union[int, float]) -> float
¶
Calculate precision from counts: TP / (TP + FP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Precision in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or both zero. |
Example
precision_score_scalar(80, 20) 0.8
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
probability_range(x_range: list[float], prob_range: list[float], lower_limit: float, upper_limit: float | None = None) -> float
¶
Sum of probabilities for values within a range.
Description
Given parallel arrays of discrete values and their probabilities, returns the sum of probabilities for values between lower_limit and upper_limit (inclusive). Equivalent to Excel PROB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_range
|
list[float]
|
Array of discrete values. |
required |
prob_range
|
list[float]
|
Corresponding probabilities (must sum to ≈ 1). |
required |
lower_limit
|
float
|
Lower bound of the range. |
required |
upper_limit
|
float | None
|
Upper bound (defaults to lower_limit for exact match). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Sum of matching probabilities. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If arrays differ in length, probabilities are negative, or they don't sum to approximately 1. |
Example
probability_range([0, 1, 2, 3], [0.1, 0.2, 0.4, 0.3], 1, 2) 0.6 probability_range([10, 20, 30], [0.3, 0.5, 0.2], 20) 0.5
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
quantile(data: List[Union[int, float]], q: float) -> float
¶
Calculates the q-th quantile of a dataset using linear interpolation.
Equivalent to the exclusive percentile with interpolation. Generalization of median (q=0.5), quartiles, and percentiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A non-empty list of numeric values. |
required |
q
|
float
|
Quantile to compute, in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The q-th quantile value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers or q is not numeric. |
ValueError
|
If q is outside [0, 1] or data is empty. |
Example
quantile([1, 2, 3, 4, 5], 0.25) 2.0 quantile([1, 2, 3, 4, 5], 0.5) 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
quartile_deviation(data: list[float]) -> float
¶
Quartile deviation (semi-interquartile range): (Q3 − Q1) / 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 4). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Quartile deviation as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numerics. |
ValueError
|
If data has fewer than 4 elements. |
Usage Example
quartile_deviation([1, 2, 3, 4, 5, 6, 7, 8]) 1.75
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
quartile_exc(data: List[Union[int, float]], quart: int) -> float
¶
Quartile using exclusive interpolation.
Description
Returns a quartile value using the exclusive method. Equivalent to Excel QUARTILE.EXC. quart must be 1, 2, or 3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset. |
required |
quart
|
int
|
1 (25th), 2 (50th), or 3 (75th). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Quartile value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not valid types. |
ValueError
|
If quart not in {1, 2, 3}. |
Example
quartile_exc([6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49], 1) 15.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
r_squared(observed: list[float], predicted: list[float]) -> float
¶
Coefficient of determination (R²).
R² = 1 − SS_res / SS_tot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed values (length ≥ 2). |
required |
predicted
|
list[float]
|
Predicted values (same length as observed). |
required |
Returns:
| Type | Description |
|---|---|
float
|
R² as a float (can be negative for poor models). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, < 2 elements, or total SS is zero. |
Usage Example
round(r_squared([1, 2, 3, 4, 5], [1.1, 2.0, 2.9, 4.1, 5.0]), 4) 0.997
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
r_squared_scalar(ss_res: Union[int, float], ss_tot: Union[int, float]) -> float
¶
Calculate the coefficient of determination (R²).
R² = 1 - SS_res / SS_tot
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ss_res
|
Union[int, float]
|
Residual sum of squares. |
required |
ss_tot
|
Union[int, float]
|
Total sum of squares. |
required |
Returns:
| Type | Description |
|---|---|
float
|
R-squared value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If ss_tot is zero. |
Example
r_squared_scalar(20, 100) 0.8
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
rank(data: List[Union[int, float]], method: str = 'average', ascending: bool = True) -> List[float]
¶
Assigns a rank to each value in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
method
|
str
|
How to handle ties — |
'average'
|
ascending
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
List[float]
|
A list of floats with the rank for each position. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or method is invalid. |
Example
rank([40, 10, 30, 20]) [4.0, 1.0, 3.0, 2.0]
rank([10, 20, 20, 30], method="min") [1.0, 2.0, 2.0, 4.0]
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 | |
rayleigh_pdf(x: Union[int, float], sigma: Union[int, float] = 1.0) -> float
¶
Return the Rayleigh probability density at x.
PDF = (x / σ²) · exp(-x² / (2σ²)) for x ≥ 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Point at which to evaluate (must be ≥ 0). |
required |
sigma
|
Union[int, float]
|
Scale parameter (must be > 0). |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
Probability density as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If x < 0 or sigma ≤ 0. |
Example
round(rayleigh_pdf(1), 6) 0.606531
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
recall_score_scalar(tp: Union[int, float], fn: Union[int, float]) -> float
¶
Calculate recall (sensitivity) from counts: TP / (TP + FN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
Union[int, float]
|
True positives. |
required |
fn
|
Union[int, float]
|
False negatives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Recall in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or both zero. |
Example
recall_score_scalar(80, 20) 0.8
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
relative_error(measured: Union[int, float], actual: Union[int, float]) -> float
¶
Compute the relative error: |measured - actual| / |actual|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measured
|
Union[int, float]
|
Measured/observed value. |
required |
actual
|
Union[int, float]
|
True/expected value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Relative error as a non-negative float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If actual is zero. |
Example
relative_error(10.5, 10.0) 0.05
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
relative_standard_deviation(data: List[Union[int, float]]) -> float
¶
Computes the Relative Standard Deviation (coefficient of variation) as a percentage.
RSD = (sample_std / mean) * 100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A non-empty list of numeric values with a non-zero mean. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The relative standard deviation as a percentage. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data is empty or the mean is zero. |
Example
round(relative_standard_deviation([10, 12, 11, 9, 13]), 2) 14.37
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
relu(x: Union[int, float]) -> Union[int, float]
¶
Compute the Rectified Linear Unit: max(0, x).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
max(0, x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
relu(-3) 0 relu(5) 5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
residual_standard_error(observed: list[float], predicted: list[float], p: int = 1) -> float
¶
Residual standard error (RSE) of a regression model.
RSE = √(SS_res / (n − p − 1)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed values. |
required |
predicted
|
list[float]
|
Predicted values (same length). |
required |
p
|
int
|
Number of predictors (default 1). |
1
|
Returns:
| Type | Description |
|---|---|
float
|
RSE as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If lengths differ or degrees of freedom ≤ 0. |
Usage Example
round(residual_standard_error([1, 2, 3, 4, 5], [1.1, 2.0, 2.9, 4.1, 5.0], 1), 4) 0.1
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
risk_ratio(exposed_events: Union[int, float], exposed_total: Union[int, float], control_events: Union[int, float], control_total: Union[int, float]) -> float
¶
Calculate the relative risk (risk ratio): RR = (a/n₁) / (c/n₂).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exposed_events
|
Union[int, float]
|
Events in exposed group. |
required |
exposed_total
|
Union[int, float]
|
Total in exposed group. |
required |
control_events
|
Union[int, float]
|
Events in control group. |
required |
control_total
|
Union[int, float]
|
Total in control group. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Risk ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If totals not positive or control risk is zero. |
Example
risk_ratio(30, 100, 10, 100) 3.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
rolling_correlation(data1: List[Union[int, float]], data2: List[Union[int, float]], window: int) -> List[Optional[float]]
¶
Computes rolling Pearson correlation over a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
First numeric series. |
required |
data2
|
List[Union[int, float]]
|
Second numeric series (same length). |
required |
window
|
int
|
Size of the sliding window (>= 2). |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length. Elements before the window is full |
List[Optional[float]]
|
are None; subsequent elements are the Pearson r. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are invalid types. |
ValueError
|
If data is empty, different lengths, or window < 2. |
Example
result = rolling_correlation([1,2,3,4,5], [2,4,6,8,10], 3) result[:2][None, None] round(result[2], 4) 1.0
Complexity: O(n * w)
Source code in shortfx/fxNumeric/statistics_functions.py
4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 | |
rolling_mean(data: List[Union[int, float]], window: int) -> List[Optional[float]]
¶
Calculates the rolling (moving) average over a fixed window size.
The first window - 1 positions are None (insufficient data).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
window
|
int
|
Number of consecutive elements per window (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length; each entry is the mean of the preceding |
List[Optional[float]]
|
window elements or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers or window is not int. |
ValueError
|
If the list is empty or window < 1. |
Example
rolling_mean([1, 2, 3, 4, 5], 3) [None, None, 2.0, 3.0, 4.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
rolling_median(data: List[Union[int, float]], window: int) -> List[Optional[float]]
¶
Calculates a rolling (moving) median over a dataset.
For each position, returns the median of the preceding window
elements. Positions with fewer than window elements return None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
window
|
int
|
The size of the rolling window (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length as |
List[Optional[float]]
|
(or None where the window is incomplete). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If window is less than 1 or data is empty. |
Example
rolling_median([1, 3, 5, 7, 9], 3) [None, None, 3.0, 5.0, 7.0]
Complexity: O(n * w) where n is the data length and w is the window size.
Source code in shortfx/fxNumeric/statistics_functions.py
rolling_std(data: List[Union[int, float]], window: int, sample: bool = True) -> List[Optional[float]]
¶
Calculates the rolling (moving) standard deviation over a fixed window.
The first window - 1 positions are None (insufficient data).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
window
|
int
|
Number of consecutive elements per window (>= 2). |
required |
sample
|
bool
|
If True, uses sample std dev (n-1); otherwise population. |
True
|
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length; each entry is the standard deviation of |
List[Optional[float]]
|
the preceding window elements or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers or window is not int. |
ValueError
|
If the list is empty or window < 2. |
Example
result = rolling_std([2, 4, 4, 4, 5, 5, 7, 9], 3) [round(v, 4) if v is not None else None for v in result][None, None, 1.1547, 0.0, 0.5774, 0.5774, 1.1547, 2.0]
Complexity: O(n * window)
Source code in shortfx/fxNumeric/statistics_functions.py
rolling_sum(data: List[Union[int, float]], window: int) -> List[Optional[float]]
¶
Calculates the rolling (moving) sum over a fixed window size.
The first window - 1 positions are None (insufficient data).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
window
|
int
|
Number of consecutive elements per window (must be >= 1). |
required |
Returns:
| Type | Description |
|---|---|
List[Optional[float]]
|
A list of the same length; each entry is the sum of the preceding |
List[Optional[float]]
|
window elements or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers or window is not int. |
ValueError
|
If the list is empty or window < 1. |
Example
rolling_sum([1, 2, 3, 4, 5], 3) [None, None, 6.0, 9.0, 12.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
root_mean_square(data: List[Union[int, float]]) -> float
¶
Calculates the root mean square (RMS) of a list of numbers.
RMS = sqrt(mean(x^2)). Used extensively in signal processing, electrical engineering, and physics to measure the effective magnitude of a varying quantity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A non-empty list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The RMS value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data is empty. |
Example
round(root_mean_square([1, 2, 3, 4, 5]), 6) 3.316625
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
root_mean_squared_error(actual: List[Union[int, float]], predicted: List[Union[int, float]]) -> float
¶
Computes the Root Mean Squared Error between actual and predicted values.
RMSE = sqrt((1/n) * Σ(actual_i − predicted_i)²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
List[Union[int, float]]
|
List of observed values. |
required |
predicted
|
List[Union[int, float]]
|
List of predicted values (same length as actual). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The root mean squared error. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists are empty or different lengths. |
Example
round(root_mean_squared_error([3, -0.5, 2, 7], [2.5, 0.0, 2, 8]), 4) 0.6124
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
root_mean_squared_log_error(observed: list[float], predicted: list[float]) -> float
¶
Root mean squared logarithmic error (RMSLE).
RMSLE = √((1/n) Σ (ln(1+observed) − ln(1+predicted))²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observed
|
list[float]
|
Observed values (all ≥ 0). |
required |
predicted
|
list[float]
|
Predicted values (all ≥ 0, same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
RMSLE as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not lists of numerics. |
ValueError
|
If lengths differ, empty, or negative values present. |
Usage Example
round(root_mean_squared_log_error([3, 5, 2.5], [2.5, 5, 4]), 4) 0.2199
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
running_max(data: list[float], window: int) -> list[float]
¶
Running (rolling) maximum over a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ window). |
required |
window
|
int
|
Window size (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of rolling max values (length = len(data) − window + 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is shorter than window. |
Usage Example
running_max([1, 3, 2, 5, 4], 3) [3.0, 5.0, 5.0]
Complexity: O(n × window)
Source code in shortfx/fxNumeric/statistics_functions.py
running_min(data: list[float], window: int) -> list[float]
¶
Running (rolling) minimum over a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ window). |
required |
window
|
int
|
Window size (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of rolling min values (length = len(data) − window + 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is shorter than window. |
Usage Example
running_min([1, 3, 2, 5, 4], 3) [1.0, 2.0, 2.0]
Complexity: O(n × window)
Source code in shortfx/fxNumeric/statistics_functions.py
running_std(data: list[float], window: int) -> list[float]
¶
Running (rolling) standard deviation over a sliding window (sample).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ window, window ≥ 2). |
required |
window
|
int
|
Window size (integer ≥ 2). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of rolling SD values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is shorter than window or window < 2. |
Usage Example
[round(v, 4) for v in running_std([1, 2, 3, 4, 5], 3)][1.0, 1.0, 1.0]
Complexity: O(n × window)
Source code in shortfx/fxNumeric/statistics_functions.py
runs_test_statistic(binary_sequence: List[int]) -> Tuple[int, float]
¶
Performs the Wald-Wolfowitz runs test for randomness.
Counts the number of runs (consecutive identical values) in a binary sequence and computes the z-score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_sequence
|
List[int]
|
List of 0s and 1s. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[int, float]
|
Tuple of (number_of_runs, z_score). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list. |
ValueError
|
If sequence is empty or contains non-binary values. |
Example
runs, z = runs_test_statistic([1, 1, 0, 0, 1, 0, 1]) runs 5
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
shannon_entropy(probabilities: list[float]) -> float
¶
Shannon entropy H(X) = −Σ p·log₂(p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
list[float]
|
List of probabilities that sum to 1. Each in (0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Entropy in bits as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If probabilities is not a list of numerics. |
ValueError
|
If empty, any value ≤ 0, or sum ≠ 1 (within tolerance). |
Usage Example
round(shannon_entropy([0.5, 0.5]), 4) 1.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
shapiro_wilk_w(data: list[float | int]) -> float
¶
Compute the Shapiro-Wilk W statistic for normality testing.
Uses the simplified approximation with order-statistic expectations for small to moderate samples (n ≤ 5000).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float | int]
|
Numeric observations (at least 3 values). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The W statistic in (0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list of numbers. |
ValueError
|
If data has fewer than 3 elements. |
Example
round(shapiro_wilk_w([1, 2, 3, 4, 5]), 4) 0.9867
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 | |
sigmoid(x: Union[int, float]) -> float
¶
Compute the logistic sigmoid function σ(x) = 1 / (1 + e⁻ˣ).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sigmoid value in (0, 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
sigmoid(0) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
simple_moving_average(data: list[float], window: int) -> list[float]
¶
Simple moving average (SMA) over a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ window). |
required |
window
|
int
|
Window size (positive integer). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of SMA values (length = len(data) − window + 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is shorter than window. |
Usage Example
simple_moving_average([1, 2, 3, 4, 5], 3) [2.0, 3.0, 4.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
skew_p(data: List[Union[int, float]]) -> float
¶
Population skewness of a distribution.
Description
Returns the skewness based on the entire population (not sample). Equivalent to Excel SKEW.P.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Numeric dataset (at least 3 values). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Population skewness. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list. |
ValueError
|
If fewer than 3 elements or zero std deviation. |
Example
round(skew_p([3, 4, 5, 2, 3, 4, 5, 6, 4, 7]), 6) 0.303193
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
skewness(data: List[Union[int, float]]) -> float
¶
Calculates the skewness of a dataset.
Skewness measures the asymmetry of the probability distribution. A positive value indicates a right-skewed tail, negative indicates left-skewed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values (at least 3 elements). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The skewness value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list has fewer than 3 elements or zero variance. |
Example
round(skewness([1, 2, 3, 4, 100]), 4) 2.0076
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
slope(known_y: List[Union[int, float]], known_x: List[Union[int, float]]) -> float
¶
Calculates the slope of the linear regression line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Dependent data values. |
required |
known_x
|
List[Union[int, float]]
|
Independent data values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The slope (m) of the best-fit line y = mx + b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length, have < 2 elements, or x has zero variance. |
Example
slope([2, 4, 6], [1, 2, 3]) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
small(data: List[Union[int, float]], k: int) -> Union[int, float]
¶
Returns the k-th smallest value in a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
k
|
int
|
The rank (1 = smallest, 2 = second smallest, etc.). |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
The k-th smallest value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers or k is not int. |
ValueError
|
If the list is empty or k is out of range. |
Example
small([3, 1, 4, 1, 5, 9], 1) 1 small([3, 1, 4, 1, 5, 9], 3) 3
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
softplus(x: Union[int, float]) -> float
¶
Compute the softplus activation: softplus(x) = log(1 + eˣ).
Numerically stable for large positive x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Softplus value (always positive). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
round(softplus(0), 4) 0.6931
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
spearman_correlation(data1: List[Union[int, float]], data2: List[Union[int, float]]) -> float
¶
Calculates the Spearman rank correlation coefficient.
Spearman correlation is a non-parametric measure of rank correlation. It assesses how well the relationship between two variables can be described using a monotonic function (not necessarily linear).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
The first list of numeric values. |
required |
data2
|
List[Union[int, float]]
|
The second list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Spearman correlation coefficient between -1 and 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length or have fewer than 3 elements. |
Example
spearman_correlation([1, 2, 3, 4, 5], [5, 6, 7, 8, 7]) 0.8207826816681233 spearman_correlation([1, 2, 3], [3, 2, 1]) -1.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
spearman_rank_correlation(data1: List[Union[int, float]], data2: List[Union[int, float]]) -> float
¶
Computes Spearman's rank correlation coefficient.
A non-parametric measure of rank correlation that assesses how well the relationship between two variables can be described using a monotonic function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
First list of numeric values. |
required |
data2
|
List[Union[int, float]]
|
Second list of numeric values (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Spearman's rho in [-1, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists are empty or different lengths. |
Example
round(spearman_rank_correlation([1, 2, 3, 4, 5], [5, 6, 7, 8, 7]), 2) 0.82
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 | |
specificity_score_scalar(tn: Union[int, float], fp: Union[int, float]) -> float
¶
Calculate specificity (true negative rate): TN / (TN + FP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tn
|
Union[int, float]
|
True negatives. |
required |
fp
|
Union[int, float]
|
False positives. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Specificity in [0, 1]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If counts are negative or both zero. |
Example
specificity_score_scalar(900, 100) 0.9
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
standard_error(data: List[Union[int, float]]) -> float
¶
Calculates the standard error of the mean (SEM).
SEM = sample standard deviation / sqrt(n). It estimates how much the sample mean is expected to vary from the true population mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values (at least 2 elements). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The standard error of the mean. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list has fewer than 2 elements. |
Example
round(standard_error([2, 4, 4, 4, 5, 5, 7, 9]), 6) 0.755929
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
standard_error_estimate(known_y: List[Union[int, float]], known_x: List[Union[int, float]]) -> float
¶
Returns the standard error of the predicted y-value for each x.
Description
Computes the standard error of the estimate for a linear regression of known_y on known_x. Measures how far observed y-values fall from the regression line. Equivalent to Excel STEYX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
The dependent data (observed y-values). |
required |
known_x
|
List[Union[int, float]]
|
The independent data (observed x-values). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The standard error of the estimate. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If arrays differ in length, have fewer than 3 points, or are empty. |
Example
round(standard_error_estimate([2, 3, 9, 1, 8, 7, 5], [6, 5, 11, 7, 5, 4, 4]), 6) 3.305719
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
standardize(x: float, mean: float, standard_dev: float) -> float
¶
Normalized value from a distribution.
Description
Returns (x - mean) / standard_dev. Equivalent to Excel STANDARDIZE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value to normalize. |
required |
mean
|
float
|
Mean of the distribution. |
required |
standard_dev
|
float
|
Standard deviation (> 0). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Standardized z-value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If standard_dev ≤ 0. |
Example
standardize(42, 40, 1.5) 1.3333333333333333
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
sum_if(values: list[int | float], criteria_range: list, criteria) -> float
¶
Return the sum of values where the parallel criteria_range meets criteria.
Equivalent to the Excel SUMIF / SUMIFS function (single criteria pair).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[int | float]
|
Numeric values to sum. |
required |
criteria_range
|
list
|
Range to test against criteria (same length as values). |
required |
criteria
|
Excel-style criteria (e.g. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of matching values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no values match or lengths differ. |
Example
sum_if([10, 20, 30, 40], ["A", "B", "A", "B"], "A") 40 sum_if([1, 2, 3, 4], [10, 20, 30, 40], ">15") 9
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
sum_list(data: List[Union[int, float]]) -> Union[int, float]
¶
Calculates the sum of all values in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
The sum of all values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
sum_list([1, 2, 3, 4]) 10 sum_list([1.5, 2.5]) 4.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
sum_of_squared_deviations(data: list[int | float]) -> float
¶
Return the sum of squared deviations from the mean.
Equivalent to the Excel DEVSQ function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[int | float]
|
List of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of (x − mean)² for every x in data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is empty. |
Example
round(sum_of_squared_deviations([4, 5, 6, 7, 5, 4, 3]), 4) 10.8571
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
sum_of_squares(data: List[Union[int, float]]) -> float
¶
Calculates the sum of squares of a list of numbers.
The sum of squares is a fundamental component in many statistical formulas, such as variance and standard deviation. It represents the sum of the squared differences of each data point from the mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The sum of squares. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input list is empty. |
TypeError
|
If the input is not a list or contains non-numeric values. |
Example
sum_of_squares([1, 2, 3, 4, 5]) 10.0 sum_of_squares([1, 1, 1]) 0.0
Cost: O(n), sum of squares over the list.
Source code in shortfx/fxNumeric/statistics_functions.py
sum_product(list1: List[Union[int, float]], list2: List[Union[int, float]]) -> float
¶
Return the sum of element-wise products of two lists.
Equivalent to Excel's SUMPRODUCT for two arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list1
|
List[Union[int, float]]
|
First numeric list. |
required |
list2
|
List[Union[int, float]]
|
Second numeric list (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of list1[i] * list2[i] for all i. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lists have different lengths. |
Example
sum_product([1, 2, 3], [4, 5, 6]) 32.0 sum_product([10, 20], [0.5, 0.3]) 11.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
swish(x: Union[int, float]) -> float
¶
Compute Swish activation: x * sigmoid(x).
Self-gated activation function proposed by Google Brain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Swish of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
swish(0) 0.0 round(swish(1), 4) 0.7311
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
symmetric_mape(actual: Union[int, float], predicted: Union[int, float]) -> float
¶
Calculate symmetric mean absolute percentage error for a single observation.
sMAPE = |actual - predicted| / ((|actual| + |predicted|) / 2) * 100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
Union[int, float]
|
Actual (true) value. |
required |
predicted
|
Union[int, float]
|
Predicted value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
sMAPE value in [0, 200]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If both actual and predicted are zero. |
Example
symmetric_mape(100, 80) 22.22222222222222
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
t_test(data1: List[Union[int, float]], data2: List[Union[int, float]], tails: int = 2, type_: int = 2) -> float
¶
Student's t-test.
Description
Returns the p-value associated with a t-test. Equivalent to Excel T.TEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
List[Union[int, float]]
|
First dataset. |
required |
data2
|
List[Union[int, float]]
|
Second dataset. |
required |
tails
|
int
|
1 for one-tailed, 2 for two-tailed. |
2
|
type_
|
int
|
1 = paired, 2 = equal variance, 3 = unequal variance. |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
p-value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If parameters out of range or datasets too small. |
Example
round(t_test([3, 4, 5, 8, 9, 1, 2, 4, 5], [6, 19, 3, 2, 14, 4, 5, 17, 1]), 4) 0.1961
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
tanh_activation(x: Union[int, float]) -> float
¶
Compute the hyperbolic tangent activation function.
Convenience wrapper mapping any numeric input to (-1, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
Input value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
tanh(x) in the range (-1, 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Example
tanh_activation(0) 0.0 round(tanh_activation(1), 4) 0.7616
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
theil_sen_slope(x_data: List[float], y_data: List[float]) -> float
¶
Computes the Theil-Sen slope estimator (median of all pairwise slopes).
Robust to outliers — unlike ordinary least squares, up to ~29% of data can be outliers without affecting the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_data
|
List[float]
|
Independent variable values. |
required |
y_data
|
List[float]
|
Dependent variable values (same length as x_data). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Median slope. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists. |
ValueError
|
If lists have different lengths or fewer than 2 points. |
Example
theil_sen_slope([1, 2, 3, 4], [2, 4, 6, 8]) 2.0
Complexity: O(n²)
Source code in shortfx/fxNumeric/statistics_functions.py
trend(known_y: List[Union[int, float]], known_x: Optional[List[Union[int, float]]] = None, new_x: Optional[List[Union[int, float]]] = None, const: bool = True) -> List[float]
¶
Predicted values along a linear trend.
Description
Uses least-squares linear regression to predict y values for new_x values. Equivalent to Excel TREND.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
known_y
|
List[Union[int, float]]
|
Known y-values. |
required |
known_x
|
Optional[List[Union[int, float]]]
|
Known x-values (defaults to 1, 2, ..., n). |
None
|
new_x
|
Optional[List[Union[int, float]]]
|
X-values for which to predict (defaults to known_x). |
None
|
const
|
bool
|
If True, calculate intercept normally. If False, force the regression line through the origin. |
True
|
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: Predicted y-values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If arrays are empty or mismatched. |
Example
[round(v, 2) for v in trend([1, 9, 5, 7])][2.0, 4.0, 6.0, 8.0]
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 | |
triangular_pdf(x: float, a: float, b: float, c: float) -> float
¶
Compute the triangular distribution PDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
a
|
float
|
Lower limit. |
required |
b
|
float
|
Upper limit. |
required |
c
|
float
|
Mode (a ≤ c ≤ b). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If a ≥ b or c not in [a, b]. |
Usage Example
round(triangular_pdf(0.5, 0.0, 1.0, 0.5), 4) 2.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
trimmed_mean(data: List[Union[int, float]], percent: float) -> float
¶
Calculates the mean excluding a percentage of outliers from each end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
percent
|
float
|
Fraction of data to exclude from each end (0 to 0.5). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The trimmed mean. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or percent is out of range. |
Example
trimmed_mean([1, 2, 3, 4, 100], 0.2) 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
trimmed_variance(data: list[float], proportion: float = 0.05) -> float
¶
Variance of the trimmed dataset (after removing tails).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 3). |
required |
proportion
|
float
|
Fraction of values to trim on each tail, in [0, 0.5). |
0.05
|
Returns:
| Type | Description |
|---|---|
float
|
Trimmed variance as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is too short, proportion out of range, or trimmed set has fewer than 2 elements. |
Usage Example
round(trimmed_variance([1, 2, 3, 4, 5, 6, 7, 8, 9, 100], 0.1), 4) 6.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
tukey_trimean(data: list) -> float
¶
Calculate Tukey's trimean.
Description
Trimean = (Q1 + 2·Q2 + Q3) / 4. A robust measure of central tendency that weights the median double, dampening the influence of outliers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list
|
A list of numeric values (at least 3 elements). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The trimean as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not a list or contains non-numeric values. |
ValueError
|
If data has fewer than 3 elements. |
Usage Example
tukey_trimean([1, 2, 3, 4, 5]) 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
uniform_cdf(x: float, a: float, b: float) -> float
¶
Compute the continuous uniform distribution CDF.
F(x) = (x-a)/(b-a) for a ≤ x ≤ b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
a
|
float
|
Lower bound. |
required |
b
|
float
|
Upper bound. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cumulative probability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If a ≥ b. |
Usage Example
uniform_cdf(0.5, 0.0, 1.0) 0.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
uniform_pdf(x: float, a: float, b: float) -> float
¶
Compute the continuous uniform distribution PDF.
f(x) = 1/(b-a) for a ≤ x ≤ b, else 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value. |
required |
a
|
float
|
Lower bound. |
required |
b
|
float
|
Upper bound. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If a ≥ b. |
Usage Example
uniform_pdf(0.5, 0.0, 1.0) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
weibull_pdf(x: float, k: float, lam: float) -> float
¶
Compute the Weibull distribution PDF.
f(x) = (k/λ)(x/λ)^(k-1)·e^(-(x/λ)^k)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value (≥ 0). |
required |
k
|
float
|
Shape parameter (> 0). |
required |
lam
|
float
|
Scale parameter (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability density. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments are not numeric. |
ValueError
|
If x < 0, k ≤ 0, or lam ≤ 0. |
Usage Example
round(weibull_pdf(1.0, 1.0, 1.0), 4) 0.3679
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
weighted_mean(data: List[Union[int, float]], weights: List[Union[int, float]]) -> float
¶
Calculates the weighted arithmetic mean of a list of numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
weights
|
List[Union[int, float]]
|
A list of corresponding weights (must be positive). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The weighted mean. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length, are empty, or weights are non-positive. |
Example
weighted_mean([80, 90], [3, 7]) 87.0 weighted_mean([10, 20, 30], [1, 1, 1]) 20.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
weighted_median(data: List[Union[int, float]], weights: List[Union[int, float]]) -> float
¶
Calculates the weighted median of a dataset.
The weighted median is the value separating the higher half from the lower half of a dataset when each observation is weighted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
weights
|
List[Union[int, float]]
|
A list of corresponding positive weights. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The weighted median value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists differ in length, are empty, or weights are non-positive. |
Example
weighted_median([1, 2, 3], [1, 1, 1]) 2.0 weighted_median([1, 2, 3, 4, 5], [5, 1, 1, 1, 1]) 1.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
weighted_moving_average(data: list[float], weights: list[float]) -> list[float]
¶
Weighted moving average (WMA) with custom weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ len(weights)). |
required |
weights
|
list[float]
|
List of weights (non-empty, all positive). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
List of WMA values (length = len(data) − len(weights) + 1). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is shorter than weights or weights invalid. |
Usage Example
[round(v, 4) for v in weighted_moving_average([1, 2, 3, 4, 5], [1, 2, 3])][2.3333, 3.3333, 4.3333]
Complexity: O(n × w)
Source code in shortfx/fxNumeric/statistics_functions.py
weighted_variance(data: List[Union[int, float]], weights: List[Union[int, float]], sample: bool = True) -> float
¶
Calculates the weighted variance of a dataset.
Uses reliability weights. For populations pass sample=False,
for samples pass sample=True which applies Bessel's correction
adapted for weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
List of numeric values. |
required |
weights
|
List[Union[int, float]]
|
Corresponding list of non-negative weights. |
required |
sample
|
bool
|
If True, apply Bessel's correction for sample variance. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
Weighted variance. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If lists are empty or of different length. |
Example
round(weighted_variance([2, 4, 6], [1, 2, 1], sample=False), 6) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
winsorize(data: List[Union[int, float]], percent: float) -> List[float]
¶
Replaces extreme values with the corresponding percentile boundaries.
Unlike trimmed_mean, winsorization preserves the original list length
by capping values at the lower and upper percentile thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
percent
|
float
|
Fraction of data to replace on each end (0 to 0.5 exclusive). |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
A new list with extreme values capped. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty or percent is out of range. |
Example
winsorize([1, 2, 3, 4, 100], 0.2) [2.0, 2.0, 3.0, 4.0, 4.0]
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
winsorized_mean(data: list[float], proportion: float = 0.05) -> float
¶
Winsorized mean: replace extreme values before averaging.
Replaces the lowest and highest proportion of values with the nearest non-replaced values, then computes the arithmetic mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
List of numeric values (length ≥ 2). |
required |
proportion
|
float
|
Fraction of values to replace on each tail, in [0, 0.5). |
0.05
|
Returns:
| Type | Description |
|---|---|
float
|
Winsorized mean as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If arguments have wrong types. |
ValueError
|
If data is too short or proportion is out of range. |
Usage Example
winsorized_mean([1, 2, 3, 4, 100], 0.2) 3.0
Complexity: O(n log n)
Source code in shortfx/fxNumeric/statistics_functions.py
z_score(value: float, mean: float, std_dev: float) -> float
¶
Standard score (z-score).
z = (x − μ) / σ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The observed value. |
required |
mean
|
float
|
Population or sample mean. |
required |
std_dev
|
float
|
Standard deviation (> 0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
z-score as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any argument is not numeric. |
ValueError
|
If std_dev is not positive. |
Usage Example
z_score(85, 70, 10) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
z_score_single(value: Union[int, float], mean: Union[int, float], std_dev: Union[int, float]) -> float
¶
Calculate the z-score of a single observation: (x - μ) / σ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[int, float]
|
Observation. |
required |
mean
|
Union[int, float]
|
Population mean. |
required |
std_dev
|
Union[int, float]
|
Population standard deviation. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Z-score. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If std_dev is not positive. |
Example
z_score_single(85, 70, 10) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
z_score_to_percentile(z: float) -> float
¶
Approximate percentile from z-score using the cumulative normal.
Uses the Abramowitz & Stegun rational approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
float
|
Standard z-score. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Percentile in [0, 100]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If z is not numeric. |
Usage Example
round(z_score_to_percentile(1.96), 2) 97.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
z_test(data: List[Union[int, float]], x: float, sigma: Optional[float] = None) -> float
¶
One-tailed p-value of a z-test.
Description
Returns the one-tailed p-value of a z-test. Equivalent to Excel Z.TEST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
Sample data. |
required |
x
|
float
|
Hypothesized population mean. |
required |
sigma
|
Optional[float]
|
Known population standard deviation. If None, uses sample standard deviation. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
One-tailed p-value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not valid types. |
ValueError
|
If data has fewer than 2 elements. |
Example
round(z_test([3, 6, 7, 8, 6, 5, 4, 2, 1, 9], 4), 4) 0.0907
Complexity: O(n)
Source code in shortfx/fxNumeric/statistics_functions.py
z_to_percentile(z: Union[int, float]) -> float
¶
Converts a z-score to a percentile (0–100) using the error function.
percentile = 100 × 0.5 × (1 + erf(z / √2)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Union[int, float]
|
Standard z-score. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Percentile in [0, 100]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If z is not numeric. |
Example
round(z_to_percentile(1.96), 2) 97.5
Complexity: O(1)
Source code in shortfx/fxNumeric/statistics_functions.py
zipf_pmf(k: int, s: Union[int, float], n: int) -> float
¶
Return the Zipf probability mass for rank k.
P(X = k) = (1 / k^s) / H(n, s), where H is the generalised harmonic number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
Rank (1 ≤ k ≤ n). |
required |
s
|
Union[int, float]
|
Exponent parameter (must be > 0). |
required |
n
|
int
|
Number of elements (must be ≥ 1). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability mass as a float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If k or n is not int, or s not numeric. |
ValueError
|
If k < 1, k > n, s ≤ 0, or n < 1. |
Example
round(zipf_pmf(1, 1, 10), 6) 0.341417
Complexity: O(n)