V1
V1 is a library for mathematical calculations and machine learning. V1 gives you mathematical functions, in the form of ordinary functions that you can use, without writing complex functions.
Provides:
- activation functions (GELU, ReLU, SiLU...)
- loss functions (MSE, MAE, Huber)
- financial math tools (SMA, EMA, CAGR)
Installation
Add to your Cargo.toml:
Quick Start
Activation Functions
use *;
// GELU activation with tanh approximation
let x = 2.0;
let result = gelu_tanh;
// ReLU: zero out negative values
let relu_out = relu;
// SiLU: sigmoid-weighted linear unit
let silu_out = silu;
Loss Functions
use *;
let prediction = 0.8;
let target = 1.0;
// Mean Squared Error
let mse = mse;
let mse_grad = mse_grad;
// Mean Absolute Error
let mae = mae;
// Huber loss (robust to outliers)
let huber = huber;
Weight Initialization
use *;
let n_in = 64;
let n_out = 128;
// Xavier initialization for sigmoid/tanh
let xavier_scale = xavier_normal;
// He initialization for ReLU/GELU
let he_scale = he_normal;
Financial Analysis
use *;
// Simple Moving Average
let mut sma = 0.0;
sma = sma_update; // 20-period SMA
// Exponential Moving Average
let mut ema = 0.0;
ema = ema_update; // 20-period EMA
// CAGR: Compound Annual Growth Rate
let initial = 1000.0;
let final_value = 2000.0;
let years = 5.0;
let cagr = cagr;
Documentation
What kind of math. The functions are included in V1
Activation functions in V1
GELU (Gaussian Error Linear Unit)
A smooth function for neuron activation. There are several ways to calculate it in the V1 library.:
- Approximation via hyperbolic tangent The most accurate and standard approximation option.
Formula $$GELU(x) \approx 0.5x \left( 1 + \tanh\left( \sqrt{\frac{2}{\pi}} \left( x + 0.044715x^3 \right) \right) \right)$$
- Sigmoid approximation A faster alternative that is calculated through a scaled logistic function.
Formula $$GELU(x) \approx x \cdot \sigma(1.702 \cdot x) = \frac{x}{1 + e^{-1.702x}}$$
Where $\sigma(x)$ is a standard logistic function (sigmoid).
ReLU (Rectified Linear Unit)
A popular and fast nonlinear function for neuron activation. It accepts x, and if the number is negative, it converts it to 0. It skips positive numbers unchanged.
Formula $$ReLU(x) = \max(0, x)$$
Softplus
A smooth and continuous alternative to the ReLU function that does not have a sharp break at zero. Allows you to save micro-gradients on negative ranges of values.
Formula $$Softplus(x) = \ln(1 + e^x)$$
SiLU (Sigmoid Linear Unit / Swish)
An activation function that weights the input value by its sigmoidal probability. It is the de facto standard in the architectures of modern language models (LLaMA, Mistral).
Formula $$SiLU(x) = x \cdot \sigma(x) = \frac{x}{1 + e^{-x}}$$
Where $\sigma(x)$ is a standard logistic function (sigmoid).
ELU (Exponential Linear Unit)
An exponential linear unit that smooths out the negative half-plane. This allows the average value of activations on the layer to be closer to zero, which significantly speeds up the convergence and training of the neural network.
Formula $$ELU(x) = \begin{cases} x, & \text{at } x > 0 \ \alpha(e^x - 1), & \text{at} x \le 0 \end{cases}$$
Where $\alpha$ (alpha) is the scaling factor of the negative range (usually 1.0).
Loss Functions Module
Scalar losses and their derivatives (gradients), designed to calculate the error on individual nodes of the computational graph.
MSE (Mean Squared Error)
Quadratic loss. Dramatically increases the penalty for severe deviations.
-
Function $L = (pred - target)^2$
-
The gradient $\frac{\partial L}{\partial pred} = 2 \cdot (pred - target)$
MAE (Mean Absolute Error)
Linear loss. It is resistant to anomalies (outliers) in the training sample.
- Function: $L = |pred - target|$
- The gradient: $\frac{\partial L}{\partial pred} = sign(pred - target)$
Huber Loss
Hybrid steady loss. It combines the best properties of MSE (smoothness near zero) and MAE (linear limit on large errors, protecting against explosion of gradients).
- Function: $$L = \begin{cases} 0.5 \cdot (pred - target)^2, & \text{at } |pred - target| \le \delta \ \delta \cdot (|pred - target| - 0.5 \cdot \delta), & \text{at } |pred - target| > \delta \end{cases}$$
The Weight Initialization module
Scalar functions for calculating the initial scale of the model weights. They protect the network from fading or exploding gradients in the first steps of learning, without requiring built-in random number generators.
Xavier (Glorot) Initialization
It is recommended for networks with smooth activation functions (Sigmoid, Tanh).
- Uniform boundary:$$r= \sqrt{\frac{6}{n_{in} + n_{out}}}$$
- Normal scale: $$\sigma = \sqrt{\frac{2}{n_{in} + n_{out}}}$$
He (Kaiming) Initialization
It is recommended for networks with nonlinear activation functions (ReLU, LeakyReLU, GELU, SiLU).
- Uniform boundary: $$r= \sqrt{\frac{6}{n_{in}}}$$
- Normal scale: $$\sigma = \sqrt{\frac{2}{n_{in}}}$$
Financial Mathematics Module
Scalar functions for technical market analysis, time series filtering, and investment evaluation. Designed without heap allocations for maximum speed in HFT systems.
Trending Filters (Moving Averages)
SMA (Simple Moving Average)
A simple moving average. Smooths out price fluctuations by calculating the arithmetic mean in a sliding window.
- The update formula $$SMA_{new} = SMA_{old} + \frac{price_{new} - price_{old}}{N}$$
EMA (Exponential Moving Average)
Exponential moving average. Gives more weight to the latest prices. A mathematical analogue of the Momentum mechanism in neural network optimizers.
- Formula $$EMA_t = \alpha \cdot price_t + (1 - \alpha) \cdot EMA_{t-1}$$
Where $\alpha = \frac{2}{N + 1}$ is the smoothing coefficient.
Mathematics of Market oscillators (Oscillator Math)
Instead of heavy indicators with history retention, the module provides clean atomic components for normalization of financial ranges.
RS (Relative Strength)
The coefficient of relative strength. It finds a pure mathematical proportion between the average growth and average decline of an asset.
- Formula $$RS = \frac{\text{Average Gain}}{\text{Average Loss}}$$
RSI Squash (Financial Range Compression)
is a scalar normalization function that takes the momentum coefficient $RS$ and non-linearly packages it into tight bounds from 0.0 to 100.0. It is the mathematical analog of the sigmoid for time series.
- Formula $$RSI(RS) = 100 - \frac{100}{1 + RS}$$
Investment Metrics (Value Metrics)
CAGR (Compound Annual Growth Rate)
The cumulative average annual growth rate. It shows the real average return on an investment asset over a period, completely smoothing out the intermediate volatility.
- Formula $$CAGR = \left( \frac{V_{final}}{V_{initial}} \right)^{\frac{1}{n}} - 1.0$$
Where $n$ is the number of years (or periods).