| Copyright | (c) 2026 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Numeric.Eproc.ConfSeq.Bernoulli
Description
Anytime-valid confidence sequence for a Bernoulli rate, by Robbins' method of mixtures.
For trials x_t in {0, 1}, i.i.d. with rate r, after s
successes in n trials, the likelihood ratio of the uniform
mixture over rates against r,
M_n(r) = B(s + 1, n - s + 1) / (r^s (1 - r)^(n - s))
(B the beta function), is a nonnegative martingale under r
with M_0(r) = 1. By Ville's inequality it ever reaches
1 / alpha with probability at most alpha, so the rates it has
not reached that against,
C_n = { r : M_n(r) < 1 / alpha },
cover the true rate at every n at once with probability at least
1 - alpha: inspect after every trial and stop when you like. As
B(s + 1, n - s + 1) = 1 / ((n + 1) C(n, s)),
log M_n(r) = - log(n + 1) - log P(s; n, r),
P the binomial probability, computed by Loader's saddle-point
expansion (as R's dbinom): stable at any count, where a
difference of log-factorials of size n log n is not.
log M_n is convex in r and at most zero at s / n, so C_n
is an interval about s / n, its ends found by bisection; its
half-width is about
sqrt(2 r (1 - r) (log(1 / alpha) + log(tau n r (1 - r)) / 2) / n)
(tau = 2 pi), the price of anytime validity a term in log n.
Unlike Numeric.Eproc.ConfSeq, which bets over a finite grid of
candidate means of any bounded variable, this is exact for a rate,
however small, and depends on the trials only through their
counts: from_counts resumes from a tally kept elsewhere.
Example
>>>let Right cfg = config 0.05>>>let Just st = from_counts 30 100>>>let (lo, hi) = interval cfg st>>>lo < 0.3 && 0.3 < hiTrue
Configuration and state
data ConfigError Source #
Reasons that a test-configuration smart constructor can reject
its inputs. Returned by config,
configInterval,
config,
config,
config, and
config.
Constructors
| InvalidAlpha !Double | significance level outside |
| InvalidBounds !Double !Double | sample bounds violate |
| InvalidNullMean !Double !Double !Double | null mean outside |
| InvalidNullInterval !Double !Double !Double !Double | null-interval endpoints violate |
| InvalidBaselineRate !Double | baseline rate outside |
| InvalidComponentCount !Int | component count not positive |
| InvalidGridSize !Int | grid size below |
Instances
| Show ConfigError Source # | |
Defined in Numeric.Eproc.Common Methods showsPrec :: Int -> ConfigError -> ShowS # show :: ConfigError -> String # showList :: [ConfigError] -> ShowS # | |
| Eq ConfigError Source # | |
Defined in Numeric.Eproc.Common | |
Construction
config :: Double -> Either ConfigError Config Source #
Build a Config at a level in (0, 1).
>>>either (const False) (const True) (config 0.05)True
from_counts :: Int -> Int -> Maybe State Source #
The state after some successes of some trials, where there are no more successes than trials and neither is negative.
>>>maybe False (const True) (from_counts 3 2)False
Streaming
update :: Config -> State -> Bool -> State Source #
The state after one more trial, a success or not.
Inspection
interval :: Config -> State -> (Double, Double) Source #
The confidence interval after the trials so far, (0, 1) before
any. Each end is the outside one of the bracket a bisection
leaves it in, to the precision of a double, so that the interval
errs wide.
>>>let Right cfg = config 0.05>>>interval cfg (initial cfg)(0.0,1.0)