Certified

Certified answers

A certified answer is a RationalInterval: two fractions with the true value between them. This page says how to read one, how to make it narrower, and what happens when a decision depends on a value that sits on the boundary.

Reading an enclosure

import { normal, show, toNumber, compare } from "@ratmath/stats";

const p = normal().sf("1.96");
p.low; p.high;            // the two rationals; the true value is between them
show(p, 20);              // "0.02499789514822043414": the digits both ends share
show(p, 40);              // "0.0249978951482204341365842690408…": shared digits, then "…"
toNumber(p);              // 0.024997895148220435: the midpoint, as a JS number

show(value, digits) prints a fraction rounded to that many places. For an enclosure it prints the digits on which both ends agree, and marks with … the place where they stop agreeing. Digits it prints are digits of the true value's enclosure, not of a floating-point estimate.

Precision on request

const narrow = normal().sf("1.96", { bits: 300 });
show(narrow, 80);
// "0.02499789514822043413658426904083719002249977906188339108571710412881842600366650"

bits is a target for the width: about 2−bits2^{-\text{bits}}. The work grows with the precision, and there is no limit built in.

Comparisons that do not guess

compare(normal().sf("1.96"), "0.025");               // "below": certainly less than 0.025
compare(normal().sf("1.95"), "0.025");               // "above"

// a p-value computed to only 30 bits, right at the boundary
const z = normal().quantile("0.975", { bits: 20 });  // an enclosure of 1.95996…
compare(normal().sf(z, { bits: 30 }), "0.025");      // "too close to call"

compare(value, level) answers "below" when the whole enclosure is below the level, "above" when it is all above, and "too close to call" when the level is inside. For an exact value it can also answer "equal". A test result's decision(alpha) uses the same rule and returns "reject", "keep" or "too close to call"; reject(alpha) returns true or false and refuses in the third case, telling you to ask for more precision.

In floating point a p-value of 0.05000000000000001 and one of 0.04999999999999999 may be the same number with different rounding. Here the question "is p<αp \lt \alpha?" has an honest third answer, and more bits will usually resolve it.

How a quantile is enclosed

For an increasing distribution function FF and a level pp, the quantile is the xx with F(x)=pF(x) = p. The solver keeps two rationals a<ba \lt b with

F(a)<p<F(b), F(a) \lt p \lt F(b),

where each inequality has been certified: the enclosure of F(a)F(a) lies entirely below pp. Since FF is increasing, the quantile is between aa and bb. Secant steps (with bisection when progress stalls) move the ends together until b−ab - a is about 2−bits2^{-\text{bits}} times the size of the answer. To decide a sign near the crossing the enclosure of FF must be narrower than its distance from pp, so the working precision rises as the bracket closes.

The floating-point guess only sets where the search starts. A bad guess costs time and cannot cost correctness.

Where exact answers come back

Certified does not mean always an interval. exponential(2).quantile("1/2") is an enclosure of 12ln⁡2\tfrac12\ln 2, but uniform(0, 4).quantile("1/4") is the fraction 1. Standard deviations are exact when the variance is a perfect square. Discrete quantiles are whole numbers. The library returns a fraction whenever it has one.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs