Files
QuanTAlib/lib/numerics/betadist/Betadist.md
T
Miha Kralj 90d5638008 Add new moving average implementations: LTMA, MCNMA, NLMA, NMA, NYQMA, RAIN, and TRAMA
- LTMA (Linear Trend Moving Average): Introduces a predictive moving average using dual cascaded EMAs for trend estimation.
- MCNMA (McNicholl EMA): Implements a zero-lag TEMA using a cascaded EMA structure for enhanced responsiveness.
- NLMA (Non-Lag Moving Average): Utilizes a damped cosine kernel to achieve reduced lag in moving averages.
- NMA (Natural Moving Average): Adapts smoothing based on volatility profiles using a square-root kernel.
- NYQMA (Nyquist Moving Average): Applies the Nyquist-Shannon theorem to prevent aliasing in cascaded moving averages.
- RAIN (Rainbow Moving Average): Combines multiple SMA layers with weighted averages for multi-scale smoothing.
- TRAMA (Trend Regularity Adaptive Moving Average): Adapts smoothing based on the frequency of new highs and lows in price data.
2026-02-20 21:40:32 -08:00

3.6 KiB

BETADIST: Beta Distribution CDF

BETADIST computes the cumulative distribution function of the Beta distribution applied to a min-max normalized price series. The source price is first normalized to [0, 1] over a lookback window, then passed through the regularized incomplete beta function I_x(\alpha, \beta) to produce a probability-mapped oscillator. The two shape parameters \alpha and \beta control the nonlinear mapping: symmetric parameters (\alpha = \beta) produce a sigmoid-like transformation centered at 0.5, while asymmetric parameters skew the mapping to emphasize extremes in either direction.

Historical Context

The Beta distribution is one of the fundamental distributions in Bayesian statistics, serving as the conjugate prior for Bernoulli and binomial processes. Its application to financial time series normalization leverages the distribution's unique property of being defined on the bounded interval [0, 1], making it a natural fit for min-max normalized price data. The CDF transformation converts a uniformly-distributed normalized price into a probability-weighted oscillator where the shape parameters control sensitivity to price levels within the range. When \alpha = \beta = 1, the Beta distribution reduces to the uniform distribution (no transformation); when \alpha = \beta = 2, it produces a smooth S-curve that compresses extremes and expands the midrange. The regularized incomplete beta function required for the CDF has no elementary closed form and requires numerical methods — this implementation uses Lentz's continued fraction algorithm, the standard approach in numerical libraries (NAG, CEPHES, Numerical Recipes).

Architecture & Physics

Three-Stage Pipeline

  1. Min-Max Normalization: Scans the lookback window to find minimum and maximum values, then maps the current source to x \in [0, 1]. If the range is zero (flat price), defaults to 0.5.

  2. Lanczos Log-Gamma: The Lanczos approximation with g = 7 and 9 coefficients computes \ln\Gamma(z) for any positive z. This is used internally by the continued fraction to compute the prefactor of the incomplete beta function.

  3. Lentz Continued Fraction: The regularized incomplete beta function I_x(a, b) is evaluated via the modified Lentz algorithm. A symmetry flip is applied when x > (a+1)/(a+b+2) to ensure convergence of the continued fraction from the correct side. Convergence typically requires 10-20 iterations for standard parameter ranges.

Mathematical Foundation

Min-max normalization:

x_t = \frac{S_t - \min_{i \in [t-n, t]} S_i}{\max_{i \in [t-n, t]} S_i - \min_{i \in [t-n, t]} S_i}

Beta CDF (regularized incomplete beta function):

I_x(\alpha, \beta) = \frac{B(x; \alpha, \beta)}{B(\alpha, \beta)} = \frac{\int_0^x t^{\alpha-1}(1-t)^{\beta-1}\,dt}{B(\alpha, \beta)}

Lentz continued fraction for I_x(a, b):

I_x(a,b) = \frac{x^a (1-x)^b}{a \cdot B(a,b)} \cdot \cfrac{1}{1+\cfrac{d_1}{1+\cfrac{d_2}{1+\cdots}}}

where d_{2m} = \frac{m(b-m)x}{(a+2m-1)(a+2m)} and d_{2m+1} = \frac{-(a+m)(a+b+m)x}{(a+2m)(a+2m+1)}

Symmetry flip: If x > \frac{a+1}{a+b+2}, compute I_x(a,b) = 1 - I_{1-x}(b,a)

Lanczos log-gamma (g = 7, 9 coefficients):

\ln\Gamma(z) = \frac{1}{2}\ln(2\pi) + (z - \tfrac{1}{2})\ln(t) - t + \ln\left(\sum_{k=0}^{8} \frac{c_k}{z+k}\right)

where t = z + g - \frac{1}{2}

Default parameters: period = 50, alpha = 2.0, beta = 2.0.

Resources

  • Abramowitz, M. & Stegun, I. (1964). Handbook of Mathematical Functions, Chapter 26
  • Press, W. et al. (2007). Numerical Recipes, 3rd ed. Cambridge, §6.4 (Incomplete Beta Function)
  • PineScript reference: betadist.pine