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) \(f(x)\) satisfying:

\[f(x) \geq 0 \quad \text{for all } x\]
\[\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.


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:

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 \([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 \(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 \(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

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<x<=2 else 0

Cauchy(x₀=0, γ=1)

-inf

inf

1 / (pi * (1 + x**2))

Note

The Cauchy distribution has no finite mean or variance. ToFUL will compute the integrals but they will not converge to finite values. This is expected and correct behaviour.


Precision Settings for CRVs

The Calc precision setting controls the tolerance passed to the quadrature routine:

Precision 4   →  tolerance ~1e-6   (fast, sufficient for most uses)
Precision 8   →  tolerance ~1e-10  (default, high accuracy)
Precision 12  →  tolerance ~1e-14  (near float64 limit)
Precision 13+ →  switches to mpmath tanh-sinh (arbitrary precision)

For the Normal distribution, the SymPy path gives exact results regardless of the precision setting.


Tips for Continuous Distributions

  • Always include a guard (if ... else 0) when the PDF is only defined on part of the real line. Without it, the integrator may evaluate the expression outside the support and produce wrong results.

  • For the Normal distribution, enter the full real line (-inf, inf) even if you expect most probability to live in (-5, 5). The integrator handles this correctly and SymPy will solve it exactly.

  • For Beta and other distributions on (0, 1), the default Gauss- Kronrod quadrature works well. There is no need to adjust settings.

  • If integration fails with “PDF integrates to 0”, check that the function evaluates to positive values at a few test points manually. A common mistake is an else 0 that shadows the intended expression.

See also