ppad-eproc-0.5.5: Anytime-valid sequential testing via e-processes.
Copyright(c) 2026 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellSafe-Inferred
LanguageHaskell2010

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 < hi
True
Synopsis

Configuration and state

data Config Source #

Confidence-sequence configuration.

data State Source #

The trials so far, as their counts.

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 (0, 1)

InvalidBounds !Double !Double

sample bounds violate lo < hi

InvalidNullMean !Double !Double !Double

null mean outside (lo, hi) (strict, to avoid div-by-zero in the safe-bet ceilings)

InvalidNullInterval !Double !Double !Double !Double

null-interval endpoints violate lo < m_lo <= m_hi < hi (strict at the sample bounds, to avoid div-by-zero in the safe-bet ceilings)

InvalidBaselineRate !Double

baseline rate outside (0, 1)

InvalidComponentCount !Int

component count not positive

InvalidGridSize !Int

grid size below 1

Instances

Instances details
Show ConfigError Source # 
Instance details

Defined in Numeric.Eproc.Common

Eq ConfigError Source # 
Instance details

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

initial :: Config -> State Source #

The state before any trial.

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)

samples :: State -> Int Source #

The number of trials so far.

successes :: State -> Int Source #

The number of successes so far.