Skip to content

LogNormal Variance loses precision for small σ and returns 0 below σ≈1e-8 #84

Closed

Description

@matt-edmondson

What's wrong

Essentials.DistributionProviders.LogNormal/LogNormalDistributionProvider.cs:59-66:

double logVariance = LogStandardDeviation * LogStandardDeviation;
return (Math.Exp(logVariance) - 1.0) * Math.Exp((2.0 * LogMean) + logVariance);

Math.Exp(σ²) - 1.0 cancels catastrophically for small σ²: relative error grows like ε/σ², and once σ² < ~1.1e-16 the result is exactly 0.

Measured on net10.0 with the expression copied verbatim (expected = ExpM1(σ²)·exp(2μ+σ²)):

σ Variance expected rel. error
1e-9 0 1e-18 100 %
1e-8 0 1e-16 100 %
1e-7 9.992e-15 1.0000e-14 8e-4
1e-6 1.0000889e-12 1.0000000e-12 8.9e-5

Independent of logMean, so LogNormal(5, 1e-8) reports Variance == 0 and StandardDeviation == 0 while Mean ≈ 148.

Why it matters

Same defect class as the recently fixed #49 (Triangular variance cancellation) and #52 (Exponential CDF near zero). Users modelling tight multiplicative noise (log-scale σ of 1e-6…1e-9) get a variance wrong in the leading digits, or zero. The cure is already in the project: Shared/SpecialFunctions.cs:149 has a cancellation-free ExpM1, and it is already linked into the LogNormal project.

Suggested fix / acceptance criteria

  • Variance = SpecialFunctions.ExpM1(logVariance) * Math.Exp((2.0 * LogMean) + logVariance).
  • Tests: (0, 1e-8).Variance ≈ 1e-16 (rel. 1e-12); (0, 1e-6).Variance ≈ 1.0000000000015e-12; existing LogNormal(0,1) value unchanged.

Activity

  1. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  2. added a commit that references this issue on Oct 14, 2026
    6482ba5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions