Utils Module
Statistical helper functions for outlier removal and curve analysis.
utils
Statistical and utility helpers for trade geometry analysis.
trim_iqr(x, y, k=1.5)
Remove outliers using IQR (Interquartile Range) fences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
X-axis values (e.g., MFE) |
required |
y
|
ndarray
|
Y-axis values (e.g., MAE) |
required |
k
|
float
|
IQR multiplier for fence distance (default 1.5, use 3.0 for very permissive) |
1.5
|
Returns:
| Name | Type | Description |
|---|---|---|
x_trimmed |
ndarray
|
Trimmed x values |
y_trimmed |
ndarray
|
Trimmed y values |
keep_mask |
ndarray
|
Boolean mask indicating which points were kept |
Notes
Fences are applied independently to both x and y: - Lower fence: Q1 - k * IQR - Upper fence: Q3 + k * IQR Points outside either fence are removed.
Source code in signal_analyzer/core/utils.py
trim_percentile(x, y, p=(1.0, 99.0))
Remove outliers using percentile bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
X-axis values (e.g., MFE) |
required |
y
|
ndarray
|
Y-axis values (e.g., MAE) |
required |
p
|
tuple of (low, high)
|
Percentile bounds (default: 1st to 99th percentile) |
(1.0, 99.0)
|
Returns:
| Name | Type | Description |
|---|---|---|
x_trimmed |
ndarray
|
Trimmed x values |
y_trimmed |
ndarray
|
Trimmed y values |
keep_mask |
ndarray
|
Boolean mask indicating which points were kept |
Source code in signal_analyzer/core/utils.py
knee_point(curve_x, curve_y)
Find the knee point (elbow) on a curve using maximum distance to line method.
The knee point is the point on the curve with maximum perpendicular distance to the straight line connecting the first and last points of the curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve_x
|
ndarray
|
X-coordinates of the curve (should be sorted) |
required |
curve_y
|
ndarray
|
Y-coordinates of the curve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
knee_x |
float
|
X-coordinate of the knee point |
knee_y |
float
|
Y-coordinate of the knee point |
Notes
This is a simple but robust method for finding diminishing returns points on frontier curves. It works well for convex/concave curves but may not be ideal for noisy or multi-modal curves.
Source code in signal_analyzer/core/utils.py
safe_divide(a, b, fill_value=0.0)
Safe element-wise division handling division by zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
Numerator |
required |
b
|
ndarray
|
Denominator |
required |
fill_value
|
float
|
Value to use when denominator is zero or invalid |
0.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Result of division with fill_value for invalid cases |