Quant.Math.Utils (quant v0.1.0-alpha.1)
Shared utility functions for mathematical operations in the Quant.Math module.
This module provides common utilities used across different technical indicator implementations, including DataFrame/NX bridge functions, validation, and core mathematical operations.
Summary
Functions
Calculate exponential moving average over a tensor.
Calculate rolling mean over a tensor using a sliding window.
Convert an NX Tensor back to an Explorer Series.
Convert an Explorer Series to an NX Tensor.
Validate that a column exists in the DataFrame.
Validate that a column exists in the DataFrame and raise if not.
Validate that a DataFrame is valid and raise if not.
Validate that a period is a positive integer and raise if not.
Calculate weighted moving average over a tensor using a sliding window.
Functions
@spec exponential_mean(Nx.Tensor.t(), pos_integer(), float() | nil) :: Nx.Tensor.t()
Calculate exponential moving average over a tensor.
Parameters
tensor- The input NX Tensorperiod- The period for the EMA calculationalpha- Optional alpha (smoothing factor). If not provided, uses 2/(period+1)
Returns
Nx.Tensor.t()- Tensor with EMA values, first value is SMA of firstperiodvalues
Algorithm
- First EMA value = SMA of first
periodvalues - Subsequent EMA values = alpha current_price + (1 - alpha) previous_ema
- Default alpha = 2 / (period + 1)
@spec rolling_mean(Nx.Tensor.t(), pos_integer()) :: Nx.Tensor.t()
Calculate rolling mean over a tensor using a sliding window.
Parameters
tensor- The input NX Tensorwindow_size- The size of the rolling window
Returns
Nx.Tensor.t()- Tensor with rolling means, NaN for insufficient data
@spec to_series(Nx.Tensor.t(), String.t()) :: Explorer.Series.t()
Convert an NX Tensor back to an Explorer Series.
Parameters
tensor- The NX Tensor to convertname- The name for the series (currently unused but kept for API consistency)
Returns
Series.t()- The converted series with :f64 dtype
@spec to_tensor(Explorer.Series.t()) :: Nx.Tensor.t()
Convert an Explorer Series to an NX Tensor.
Parameters
series- The Explorer Series to convert
Returns
Nx.Tensor.t()- The converted tensor with :f64 type
@spec validate_column(Explorer.DataFrame.t(), atom()) :: :ok | {:error, String.t()}
Validate that a column exists in the DataFrame.
Parameters
df- The DataFrame to checkcolumn- The column name (atom) to validate
Returns
:okif column exists{:error, message}if column is missing
@spec validate_column!(Explorer.DataFrame.t(), atom()) :: :ok
Validate that a column exists in the DataFrame and raise if not.
Parameters
df- The DataFrame to checkcolumn- The column name (atom) to validate
Returns
:okif column exists
Raises
ArgumentErrorif column does not exist
@spec validate_dataframe!(any()) :: :ok
Validate that a DataFrame is valid and raise if not.
Parameters
df- The DataFrame to validate
Returns
:okif valid DataFrame
Raises
ArgumentErrorif not a valid DataFrame
Validate that a period is a positive integer and raise if not.
Parameters
period- The period to validatename- Optional name for better error messages (default: "period")
Returns
:okif valid period
Raises
ArgumentErrorif period is not a positive integer
@spec weighted_mean(Nx.Tensor.t(), pos_integer(), Nx.Tensor.t() | nil) :: Nx.Tensor.t()
Calculate weighted moving average over a tensor using a sliding window.
Parameters
tensor- The input NX Tensorperiod- The period for the WMA calculationweights- Optional weight vector. If not provided, uses linear weights [1, 2, 3, ..., period]
Returns
Nx.Tensor.t()- Tensor with WMA values, NaN for insufficient data
Algorithm
- Default weights: Linear sequence [1, 2, 3, ..., period]
- WMA = Σ(price_i × weight_i) / Σ(weight_i)
- Gives more weight to recent prices in the linear case