packages feed

ppad-eproc 0.5.3 → 0.5.4

raw patch · 8 files changed

+99/−52 lines, 8 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Numeric.Eproc.Common: log_sum_exps :: [Double] -> Double

Files

CHANGELOG view
@@ -1,5 +1,16 @@ # Changelog +- 0.5.4 (2026-11-15)+  * Fixes Numeric.Eproc.Mixture.update turning a component's infinite+    log e-value into NaN: the mixture is infinite too, and rejects.+  * Numeric.Eproc.TwoSample mixes and latches its components through+    Numeric.Eproc.Mixture, as its own copy of that logic did; its+    results are unchanged. An interval null with no clip bounds, which+    mixed no component, is now refused (InvalidComponentCount).+  * One log-sum-exp (Numeric.Eproc.Common.log_sum_exps) under Mixture+    and Merge. Paired's and Mixture's documentation point to TwoSample+    and Merge.+ - 0.5.3 (2026-11-15)   * Adds to Numeric.Eproc.TwoSample the anytime-valid p-value and     each component's supremum so far (log_evalue_sup_sign,
lib/Numeric/Eproc/Common.hs view
@@ -33,9 +33,11 @@   -- * Internal: helpers   , finite   , log_sum_exp+  , log_sum_exps   , log2_dbl   ) where +import qualified Data.List as L import GHC.Float (log1p)  -- | A predictable bettor.@@ -160,6 +162,21 @@   | a >= b    = a + log1p (exp (b - a))   | otherwise = b + log1p (exp (a - b)) {-# INLINE log_sum_exp #-}++-- | The log of a sum of exponentials, of values given as logs:+--   stable against overflow, @+Infinity@ where a term is, and+--   @-Infinity@ of no terms or of terms all @-Infinity@. The sum+--   'Numeric.Eproc.Mixture' latches and 'Numeric.Eproc.Merge'+--   averages.+log_sum_exps :: [Double] -> Double+log_sum_exps les = case les of+  [] -> negate (1 / 0)+  (l : ls) ->+    let !m = L.foldl' max l ls+    in  if isInfinite m+          then m+          else m + log (L.foldl' (\ !acc v -> acc + exp (v - m)) 0 les)+{-# INLINE log_sum_exps #-}  -- | @log 2@ as a shared constant. Used both as the initial value of --   the two-sided running sup-log-sum (since @K^+_0 + K^-_0 = 2@) and
lib/Numeric/Eproc/Merge.hs view
@@ -43,6 +43,8 @@ import qualified Data.List as L import Data.Ord (Down(..), comparing) +import Numeric.Eproc.Common (log_sum_exps)+ -- | The log of the mean of e-values given as logs: an e-value for --   the null they share, whatever their dependence. The mean of no --   e-values is taken to be one.@@ -56,15 +58,7 @@ log_mean :: [Double] -> Double log_mean les = case les of   [] -> 0-  (l : ls) ->-    let !m = L.foldl' max l ls-        !s = L.foldl' (\ !acc v -> acc + exp (v - m)) 0 les-        !k = fromIntegral (length les) :: Double-    -- an infinite e-value, of either sign, is the mean's sign too: an-    -- e-value of zero among no greater, or one infinitely great-    in  if isInfinite m-          then m-          else m + log s - log k+  _ -> log_sum_exps les - log (fromIntegral (length les)) {-# INLINE log_mean #-}  -- | The log of the product of e-values given as logs: an e-value
lib/Numeric/Eproc/Mixture.hs view
@@ -25,7 +25,9 @@ -- the sum dominates the max. Use a mixture when the alternative has -- several qualitatively different faces (a location shift, a shape -- change, a rare-outlier channel, ...) and you want a single test--- with power against their union.+-- with power against their union. "Numeric.Eproc.TwoSample" mixes+-- its components through this module; to merge e-values once, rather+-- than stream them, see "Numeric.Eproc.Merge". -- -- This module does not own or update the components: they may be -- heterogeneous (different test modules, different observation@@ -105,8 +107,8 @@   , samples   ) where -import qualified Data.List as L-import Numeric.Eproc.Common (Verdict(..), ConfigError(..), finite)+import Numeric.Eproc.Common (Verdict(..), ConfigError(..), finite,+  log_sum_exps)  -- types ---------------------------------------------------------------------- @@ -200,14 +202,10 @@ --   >>> let s1 = update cfg s0 [0.1, -0.2, 0.0, 0.4] update :: Config -> State -> [Double] -> State update _ st@State{..} les = case les of-  []       -> st-  (l : ls) ->-    let !m = L.foldl' max l ls-        !s = L.foldl' (\ !acc v -> acc + exp (v - m)) 0 les-        -- all components at e-value zero: the mixture log-sum is-        -- -Infinity, and (m +) would poison it into NaN.-        !cur | isInfinite m && m < 0 = m-             | otherwise             = m + log s+  [] -> st+  _  ->+    -- infinite where a component is, of either sign, rather than NaN+    let !cur = log_sum_exps les     in  State {             st_n           = st_n + 1           , st_log_sum     = cur
lib/Numeric/Eproc/Paired.hs view
@@ -30,7 +30,10 @@ -- Pairing is required: independent two-sample testing without -- alignment would need to bet against a richer alternative (the -- joint distribution rather than the marginal difference) and is--- beyond the scope of this module.+-- beyond the scope of this module. This module tests equality of+-- means, with known sample bounds; for equality of the laws+-- themselves, by a hedge over several statistics of each pair, see+-- "Numeric.Eproc.TwoSample". -- -- == Example --
lib/Numeric/Eproc/TwoSample.hs view
@@ -18,7 +18,8 @@ -- at once, each a component e-process for the null: -- --   * the sign of the difference @d = a - b@ (ties skipped), against---     one law running above the other ("Numeric.Eproc.Bernoulli.TwoSided");+--     one law running above the other+--     ("Numeric.Eproc.Bernoulli.TwoSided"); -- --   * @d@ clipped at each of some bounds, against a shift in mean, --     without letting rare large differences dominate@@ -28,11 +29,12 @@ --     the laws' distribution functions differing at @q@ (a change in --     spread, say, that leaves the mean alone). ----- The hedge's e-value is the components' mean ("Numeric.Eproc.Merge"),--- an e-process for the null; the test rejects when its supremum so--- far reaches @1 \/ alpha@, with probability at most @alpha@ under the--- null, at any stopping time. The hedge's price is the log of its--- number of components, against power on each.+-- The hedge's e-value is the components' mean, mixed and latched by+-- "Numeric.Eproc.Mixture": an e-process for the null; the test rejects+-- when its supremum so far reaches @1 \/ alpha@, with probability at+-- most @alpha@ under the null, at any stopping time. The hedge's+-- price is the log of its number of components, against power on+-- each. -- -- Nothing ties the statistic to a pair. 'update' takes any statistic -- symmetric about zero under the null, and one per cut point bounded@@ -97,19 +99,19 @@ import qualified Numeric.Eproc.Bounded as Magn import Numeric.Eproc.Common (Bettor(..), ConfigError(..), Verdict(..),   finite)-import Numeric.Eproc.Merge (log_mean)+import qualified Numeric.Eproc.Mixture as Mix  -- | Hedge configuration. data Config = Config {-    cfg_alpha      :: {-# UNPACK #-} !Double-  , cfg_log_thresh :: {-# UNPACK #-} !Double  -- log (1 / alpha)-  , cfg_sign       :: !Sign.Config+    cfg_sign       :: !Sign.Config   , cfg_magn       :: ![(Double, Magn.Config)]  -- each clip bound, its                                                 -- test   , cfg_cdf_bound  :: {-# UNPACK #-} !Double   , cfg_cdf        :: !Magn.Config               -- shared by every cut   , cfg_cuts       :: ![Double]   , cfg_interval   :: !Bool                      -- an interval null+  , cfg_mix        :: !Mix.Config                -- the mixture of the+                                                 -- components mixed   }  -- | The hedge after some observations.@@ -118,7 +120,9 @@   , st_sign    :: !Sign.State   , st_magn    :: ![Magn.State]   , st_cdf     :: ![Magn.State]-  , st_sup     :: {-# UNPACK #-} !Double  -- the log e-value's supremum+  , st_mixc    :: !Mix.Config  -- the mixture's, so that its+                               -- accessors need no 'Config'+  , st_mix     :: !Mix.State   }  -- | Build a 'Config' for the sharp null, at a level in @(0, 1)@, from@@ -160,15 +164,19 @@       s <- Sign.config 0.5 alpha Newton       ms <- traverse magn bs       cd <- Magn.config 0 (negate u) u alpha Newton+      -- every component mixed, or the clipped ones alone under an+      -- interval null (which so needs at least one clip bound)+      mx <- Mix.config+        (maybe (1 + length bs + length qs) (const (length bs)) margin)+        alpha       pure Config {-          cfg_alpha      = alpha-        , cfg_log_thresh = log (1 / alpha)-        , cfg_sign       = s+          cfg_sign       = s         , cfg_magn       = ms         , cfg_cdf_bound  = u         , cfg_cdf        = cd         , cfg_cuts       = qs         , cfg_interval   = maybe False (const True) margin+        , cfg_mix        = mx         }   where     magn !b@@ -185,7 +193,8 @@   , st_sign = Sign.initial cfg_sign   , st_magn = strict_map (Magn.initial . snd) cfg_magn   , st_cdf  = strict_map (const (Magn.initial cfg_cdf)) cfg_cuts-  , st_sup  = 0+  , st_mixc = cfg_mix+  , st_mix  = Mix.initial cfg_mix   }  -- a map whose list is whole, spine and elements, when evaluated at@@ -248,14 +257,13 @@ --   /Precondition/: one indicator statistic per cut point, in their --   order; any beyond them are ignored. update :: Config -> State -> Double -> [Double] -> State-update cfg@Config{..} State{..} !d cs =+update Config{..} State{..} !d cs =   let !sg | d == 0 = st_sign           | otherwise = Sign.update cfg_sign st_sign (d > 0)       !mg = step_magn cfg_magn st_magn       !cd = step_cdf cs st_cdf-      st' = State (st_n + 1) sg mg cd st_sup-      !le = log_evalue cfg st'-  in  st' { st_sup = max st_sup le }+      !mx = Mix.update cfg_mix st_mix (mixed cfg_interval sg mg cd)+  in  State (st_n + 1) sg mg cd st_mixc mx   where     step_magn ((b, c) : bt) (s : ss) =       let !s' = Magn.update c s (clip b d)@@ -285,12 +293,19 @@   | otherwise = x {-# INLINE clip #-} +-- the log e-values the hedge mixes: every component's, or the clipped+-- components' alone under an interval null+mixed :: Bool -> Sign.State -> [Magn.State] -> [Magn.State] -> [Double]+mixed interval sg mg cd+  | interval = fmap Magn.log_evalue mg+  | otherwise =+      Sign.log_evalue sg : fmap Magn.log_evalue mg <> fmap Magn.log_evalue cd+{-# INLINE mixed #-}+ -- | 'Reject' iff the hedge's log e-value has reached @log(1 \/ alpha)@ --   at some observation so far. decide :: Config -> State -> Verdict-decide Config{..} State{..}-  | st_sup >= cfg_log_thresh = Reject-  | otherwise = Continue+decide Config{..} State{..} = Mix.decide cfg_mix st_mix {-# INLINE decide #-}  -- inspection ----------------------------------------------------------------@@ -298,22 +313,18 @@ -- | The hedge's current log e-value: the log of its components' mean, --   of the clipped components' alone under an interval null. log_evalue :: Config -> State -> Double-log_evalue Config{..} State{..}-  | cfg_interval = log_mean (fmap Magn.log_evalue st_magn)-  | otherwise = log_mean-      (Sign.log_evalue st_sign-       : fmap Magn.log_evalue st_magn <> fmap Magn.log_evalue st_cdf)+log_evalue Config{..} State{..} = Mix.log_evalue cfg_mix st_mix {-# INLINE log_evalue #-}  -- | The supremum so far of the hedge's log e-value; zero before any --   observation. log_evalue_sup :: State -> Double-log_evalue_sup = st_sup+log_evalue_sup State{..} = Mix.log_evalue_sup st_mixc st_mix {-# INLINE log_evalue_sup #-}  -- | The anytime-valid p-value: @min 1 (exp (- log_evalue_sup))@. p_value :: State -> Double-p_value st = min 1 (exp (negate (st_sup st)))+p_value State{..} = Mix.p_value st_mixc st_mix {-# INLINE p_value #-}  -- | The sign component's current log e-value.
ppad-eproc.cabal view
@@ -1,6 +1,6 @@ cabal-version:      3.0 name:               ppad-eproc-version:            0.5.3+version:            0.5.4 synopsis:           Anytime-valid sequential testing via e-processes. license:            MIT license-file:       LICENSE
test/Main.hs view
@@ -862,7 +862,16 @@  mixture_tests :: TestTree mixture_tests = testGroup "mixture" [-    testCase "fresh mixture sits at log K, p-value 1" $ do+    testCase "an infinite component makes the mixture infinite" $ do+      let cfg = ok (Mix.config 2 1.0e-3)+          s1  = Mix.update cfg (Mix.initial cfg) [1 / 0, 0]+      Mix.log_evalue cfg s1 @?= 1 / 0+      Mix.decide cfg s1 @?= Mix.Reject+  , testCase "no components leave the mixture as it was" $ do+      let cfg = ok (Mix.config 2 1.0e-3)+          s0  = Mix.initial cfg+      Mix.log_evalue cfg (Mix.update cfg s0 []) @?= Mix.log_evalue cfg s0+  , testCase "fresh mixture sits at log K, p-value 1" $ do       let cfg = ok (Mix.config 4 1.0e-3)           s0  = Mix.initial cfg       assertBool "log_wealth is log K" $@@ -1233,6 +1242,10 @@           rate = hedge_rate cfg (geometric 0.5) (geometric 0.35) 4000 50                    757575       assertBool ("rate " ++ show rate) $ rate >= 0.9+  , testCase "an interval null needs a clip bound" $+      assertBool "an interval null without clip bounds was accepted" $+        either (const True) (const False)+          (TS.configInterval 1 1.0e-3 [] 1 [0])   , testCase "a margin inside a clip bound" $       assertBool "a margin at a clip bound was accepted" $         either (const True) (const False)