.. _user-guide-continuous: Continuous Random Variables ============================ A **continuous random variable** (CRV) takes values from an interval of the real line. Its distribution is characterised by a **probability density function** (PDF) :math:`f(x)` satisfying: .. math:: f(x) \geq 0 \quad \text{for all } x .. math:: \int_{-\infty}^{\infty} f(x)\, dx = 1 ToFUL computes moments for distributions on finite, semi-infinite, or doubly-infinite intervals using domain-aware numerical integration. .. contents:: On this page :local: :depth: 2 ---- Defining a Continuous Distribution ------------------------------------ Select **Continuous (CRV)** in the sidebar. Enter: * **Lower bound** — the left endpoint of the support * **Upper bound** — the right endpoint of the support * **PDF** — the probability density function ``f(x)`` Use ``inf`` and ``-inf`` (or ``∞`` and ``-∞``) for unbounded intervals. **Example — Exponential distribution:** .. code-block:: text Lower bound: 0 Upper bound: inf PDF: 2 * exp(-2*x) if x >= 0 else 0 ---- Quadrature Methods ------------------- ToFUL selects the integration method automatically based on the domain: Gauss-Laguerre quadrature — ``[0, ∞)`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For semi-infinite integrals on :math:`[0, \infty)`, ToFUL uses Gauss- Laguerre nodes with 64 points. These nodes are purpose-built for integrals with exponential-decay character (the Laguerre weight function is :math:`e^{-x}`), making them highly efficient for distributions like Exponential, Gamma, and Chi-squared. The result is cross-checked against SciPy's adaptive ``quad``; if they agree to four decimal places the Gauss-Laguerre estimate is returned. Gauss-Hermite quadrature — ``(-∞, ∞)`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For doubly-infinite integrals, ToFUL uses Gauss-Hermite nodes with 64 points. These are optimal for distributions with Gaussian character (the Hermite weight function is :math:`e^{-x^2}`), such as the Normal and Student-t distributions. The result is cross-checked against adaptive ``quad`` as above. Adaptive Gauss-Kronrod — all other domains ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For finite intervals ``[a, b]`` and semi-infinite intervals not starting at 0, ToFUL uses SciPy's ``quad`` function (Gauss-Kronrod adaptive integration) with tolerances matched to your chosen precision setting. mpmath tanh-sinh — high precision ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ When **Calc precision** is set above 12 decimal places, ToFUL automatically switches to mpmath's tanh-sinh (double-exponential) quadrature, which achieves near-arbitrary precision and handles endpoint singularities exceptionally well. SymPy symbolic — exact ~~~~~~~~~~~~~~~~~~~~~~~ For PDFs without ``if/else`` guards (e.g. ``exp(-x**2/2) / sqrt(2*pi)``), ToFUL first attempts an exact symbolic integral via SymPy. When successful, the result carries no numerical error. The **method** shown in the Moments tab will read ``sympy-exact``. ---- Validation ----------- Before moments are computed, ToFUL: 1. Evaluates the PDF at 100 test points to detect negative values. 2. Integrates the PDF over the full domain using the selected quadrature. 3. Checks that the integral equals 1.0 within the precision tolerance. A failure at step 2 or 3 shows the computed integral value and deviation from 1.0 to help diagnose the issue. ---- Common Continuous Distributions --------------------------------- .. list-table:: :header-rows: 1 :widths: 22 15 15 48 * - Distribution - Lower - Upper - PDF expression * - Uniform(a=0, b=1) - ``0`` - ``1`` - ``1 if 0 <= x <= 1 else 0`` * - Exponential(λ=2) - ``0`` - ``inf`` - ``2 * exp(-2*x) if x >= 0 else 0`` * - Normal(μ=0, σ=1) - ``-inf`` - ``inf`` - ``exp(-(x**2)/2) / sqrt(2*pi)`` * - Normal(μ=3, σ=2) - ``-inf`` - ``inf`` - ``exp(-((x-3)**2)/8) / (2*sqrt(2*pi))`` * - Gamma(α=2, β=1) - ``0`` - ``inf`` - ``x * exp(-x) if x >= 0 else 0`` * - Beta(α=2, β=3) - ``0`` - ``1`` - ``12 * x * (1-x)**2 if 0 <= x <= 1 else 0`` * - Triangular(0, 1, 2) - ``0`` - ``2`` - ``x if 0<=x<=1 else (2-x) if 1