{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}

-- |
-- Module: Numeric.Eproc.TwoSample
-- Copyright: (c) 2026 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Anytime-valid two-sample test of equality in law, by a hedge of
-- e-processes on an antisymmetric statistic.
--
-- Draw pairs @(a_t, b_t)@, one from each of two laws, independently.
-- Under the null that the laws are equal the pair is exchangeable,
-- so any antisymmetric function of it, @f(a, b) = -f(b, a)@, is
-- symmetric about zero, whatever the law. The hedge bets on several
-- 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");
--
--   * @d@ clipped at each of some bounds, against a shift in mean,
--     without letting rare large differences dominate
--     ("Numeric.Eproc.Bounded");
--
--   * at each of some cut points @q@, @1[a <= q] - 1[b <= q]@, against
--     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, 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
-- by a given bound: the energy of a quartet (two draws from each of
-- two laws), say, which swapping two of its draws negates. 'update_pair'
-- makes the statistics of a pair.
--
-- Under an interval null ('configInterval'), that the clipped
-- difference's mean lies within a margin of zero, the clipped
-- components bet against the interval and alone make the hedge's
-- e-value; the sign and indicator components test the sharp null,
-- and are stepped beside it but not mixed.
--
-- The clip bounds and cut points must not depend on the data the
-- test sees: fix them from a warmup the test then discards
-- ('clip_bound', 'cut_points').
--
-- == Example
--
-- >>> let Right cfg = config 1.0e-3 [1.0, 4.0] 1 [0.5]
-- >>> let ps = take 2000 (cycle [(1, 0), (1, 0), (0, 0), (1, 1)])
-- >>> decide cfg (foldl' (update_pair cfg) (initial cfg) ps)
-- Reject

module Numeric.Eproc.TwoSample (
  -- * Configuration and state
    Config
  , State
  , Verdict(..)
  , ConfigError(..)

  -- * Construction
  , config
  , configInterval
  , initial

  -- * Calibration
  , clip_bound
  , cut_points

  -- * Streaming
  , update
  , update_pair
  , decide

  -- * Inspection
  , log_evalue
  , log_evalue_sup
  , p_value
  , log_evalue_sign
  , log_evalue_magnitude
  , log_evalue_cdf
  , log_evalue_sup_sign
  , log_evalue_sup_magnitude
  , log_evalue_sup_cdf
  , samples
  ) where

import qualified Data.List as L

import qualified Numeric.Eproc.Bernoulli.TwoSided as Sign
import qualified Numeric.Eproc.Bounded as Magn
import Numeric.Eproc.Common (Bettor(..), ConfigError(..), Verdict(..),
  finite)
import qualified Numeric.Eproc.Mixture as Mix

-- | Hedge configuration.
data Config = Config {
    Config -> Config
cfg_sign       :: !Sign.Config
  , Config -> [(Double, Config)]
cfg_magn       :: ![(Double, Magn.Config)]  -- each clip bound, its
                                                -- test
  , Config -> Double
cfg_cdf_bound  :: {-# UNPACK #-} !Double
  , Config -> Config
cfg_cdf        :: !Magn.Config               -- shared by every cut
  , Config -> [Double]
cfg_cuts       :: ![Double]
  , Config -> Bool
cfg_interval   :: !Bool                      -- an interval null
  , Config -> Config
cfg_mix        :: !Mix.Config                -- the mixture of the
                                                 -- components mixed
  }

-- | The hedge after some observations.
data State = State {
    State -> Int
st_n       :: {-# UNPACK #-} !Int
  , State -> State
st_sign    :: !Sign.State
  , State -> [State]
st_magn    :: ![Magn.State]
  , State -> [State]
st_cdf     :: ![Magn.State]
  , State -> Config
st_mixc    :: !Mix.Config  -- the mixture's, so that its
                               -- accessors need no 'Config'
  , State -> State
st_mix     :: !Mix.State
  }

-- | Build a 'Config' for the sharp null, at a level in @(0, 1)@, from
--   positive clip bounds, the positive bound of the indicator
--   statistics (one for a pair's), and the cut points.
--
--   >>> either (const False) (const True) (config 1.0e-3 [1, 4] 1 [0])
--   True
config
  :: Double    -- ^ significance level @alpha@
  -> [Double]  -- ^ clip bounds
  -> Double    -- ^ indicator statistics' bound
  -> [Double]  -- ^ cut points
  -> Either ConfigError Config
config :: Double
-> [Double] -> Double -> [Double] -> Either ConfigError Config
config = Maybe Double
-> Double
-> [Double]
-> Double
-> [Double]
-> Either ConfigError Config
mk Maybe Double
forall a. Maybe a
Nothing
{-# INLINE config #-}

-- | Build a 'Config' for the interval null that the clipped
--   difference's mean lies within a positive margin of zero; each
--   clip bound must exceed the margin.
configInterval
  :: Double    -- ^ margin
  -> Double    -- ^ significance level @alpha@
  -> [Double]  -- ^ clip bounds
  -> Double    -- ^ indicator statistics' bound
  -> [Double]  -- ^ cut points
  -> Either ConfigError Config
configInterval :: Double
-> Double
-> [Double]
-> Double
-> [Double]
-> Either ConfigError Config
configInterval Double
m = Maybe Double
-> Double
-> [Double]
-> Double
-> [Double]
-> Either ConfigError Config
mk (Double -> Maybe Double
forall a. a -> Maybe a
Just Double
m)
{-# INLINE configInterval #-}

mk
  :: Maybe Double -> Double -> [Double] -> Double -> [Double]
  -> Either ConfigError Config
mk :: Maybe Double
-> Double
-> [Double]
-> Double
-> [Double]
-> Either ConfigError Config
mk Maybe Double
margin !Double
alpha [Double]
bs !Double
u [Double]
qs
  | Bool -> Bool
not (Double -> Bool
finite Double
alpha Bool -> Bool -> Bool
&& Double
alpha Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
alpha Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
1) =
      ConfigError -> Either ConfigError Config
forall a b. a -> Either a b
Left (Double -> ConfigError
InvalidAlpha Double
alpha)
  | Bool -> Bool
not (Double -> Bool
finite Double
u Bool -> Bool -> Bool
&& Double
u Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) = ConfigError -> Either ConfigError Config
forall a b. a -> Either a b
Left (Double -> Double -> ConfigError
InvalidBounds (Double -> Double
forall a. Num a => a -> a
negate Double
u) Double
u)
  | Bool
otherwise = do
      s <- Double -> Double -> Bettor -> Either ConfigError Config
Sign.config Double
0.5 Double
alpha Bettor
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_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 :: Double -> Either ConfigError (Double, Config)
magn !Double
b
      | Bool -> Bool
not (Double -> Bool
finite Double
b Bool -> Bool -> Bool
&& Double
b Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) = ConfigError -> Either ConfigError (Double, Config)
forall a b. a -> Either a b
Left (Double -> Double -> ConfigError
InvalidBounds (Double -> Double
forall a. Num a => a -> a
negate Double
b) Double
b)
      | Bool
otherwise = (Config -> (Double, Config))
-> Either ConfigError Config -> Either ConfigError (Double, Config)
forall a b.
(a -> b) -> Either ConfigError a -> Either ConfigError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,) Double
b) (Either ConfigError Config -> Either ConfigError (Double, Config))
-> Either ConfigError Config -> Either ConfigError (Double, Config)
forall a b. (a -> b) -> a -> b
$ case Maybe Double
margin of
          Maybe Double
Nothing -> Double
-> Double
-> Double
-> Double
-> Bettor
-> Either ConfigError Config
Magn.config Double
0 (Double -> Double
forall a. Num a => a -> a
negate Double
b) Double
b Double
alpha Bettor
Newton
          Just Double
m -> Double
-> Double
-> Double
-> Double
-> Double
-> Bettor
-> Either ConfigError Config
Magn.configInterval (Double -> Double
forall a. Num a => a -> a
negate Double
m) Double
m (Double -> Double
forall a. Num a => a -> a
negate Double
b) Double
b Double
alpha
            Bettor
Newton

-- | The hedge before any observation, its e-value one.
initial :: Config -> State
initial :: Config -> State
initial Config{Bool
Double
[Double]
[(Double, Config)]
Config
Config
Config
cfg_sign :: Config -> Config
cfg_magn :: Config -> [(Double, Config)]
cfg_cdf_bound :: Config -> Double
cfg_cdf :: Config -> Config
cfg_cuts :: Config -> [Double]
cfg_interval :: Config -> Bool
cfg_mix :: Config -> Config
cfg_sign :: Config
cfg_magn :: [(Double, Config)]
cfg_cdf_bound :: Double
cfg_cdf :: Config
cfg_cuts :: [Double]
cfg_interval :: Bool
cfg_mix :: Config
..} = State {
    st_n :: Int
st_n    = Int
0
  , st_sign :: State
st_sign = Config -> State
Sign.initial Config
cfg_sign
  , st_magn :: [State]
st_magn = ((Double, Config) -> State) -> [(Double, Config)] -> [State]
forall a b. (a -> b) -> [a] -> [b]
strict_map (Config -> State
Magn.initial (Config -> State)
-> ((Double, Config) -> Config) -> (Double, Config) -> State
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Config) -> Config
forall a b. (a, b) -> b
snd) [(Double, Config)]
cfg_magn
  , st_cdf :: [State]
st_cdf  = (Double -> State) -> [Double] -> [State]
forall a b. (a -> b) -> [a] -> [b]
strict_map (State -> Double -> State
forall a b. a -> b -> a
const (Config -> State
Magn.initial Config
cfg_cdf)) [Double]
cfg_cuts
  , st_mixc :: Config
st_mixc = Config
cfg_mix
  , st_mix :: State
st_mix  = Config -> State
Mix.initial Config
cfg_mix
  }

-- a map whose list is whole, spine and elements, when evaluated at
-- all, as update's are
strict_map :: (a -> b) -> [a] -> [b]
strict_map :: forall a b. (a -> b) -> [a] -> [b]
strict_map a -> b
f = [a] -> [b]
go
  where
    go :: [a] -> [b]
go (a
x : [a]
xs) = let !y :: b
y = a -> b
f a
x
                      !ys :: [b]
ys = [a] -> [b]
go [a]
xs
                  in  b
y b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b]
ys
    go [] = []
{-# INLINE initial #-}

-- calibration ---------------------------------------------------------------

-- | A clip bound from a warmup's statistics: twice the nearest-rank
--   p99 of their magnitudes (the @ceiling (0.99 m)@-th smallest of
--   @m@), and at least a floor.
--
--   >>> clip_bound 1 [0.1, 0.2, -0.3]
--   1.0
--   >>> clip_bound 0 (replicate 99 1 ++ [1.0e9])
--   2.0
clip_bound :: Double -> [Double] -> Double
clip_bound :: Double -> [Double] -> Double
clip_bound !Double
floor_ [Double]
ds = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
floor_ (Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> [Double] -> Double
nearest Double
0.99 ([Double] -> [Double]
forall a. Ord a => [a] -> [a]
L.sort ((Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Double -> Double
forall a. Num a => a -> a
abs [Double]
ds)))
{-# INLINE clip_bound #-}

-- | Cut points from a warmup's pooled values: their nearest-rank
--   quantiles at 0.10, 0.25, 0.50, 0.75 and 0.90, ascending, alike
--   ones once. None from no values.
--
--   >>> cut_points [1, 2, 3, 4]
--   [1.0,2.0,3.0,4.0]
cut_points :: [Double] -> [Double]
cut_points :: [Double] -> [Double]
cut_points [Double]
xs = case [Double] -> [Double]
forall a. Ord a => [a] -> [a]
L.sort [Double]
xs of
  [] -> []
  [Double]
sorted -> [Double] -> [Double]
forall {a}. Eq a => [a] -> [a]
dedup [ Double -> [Double] -> Double
nearest Double
q [Double]
sorted | Double
q <- [Double
0.10, Double
0.25, Double
0.50, Double
0.75, Double
0.90] ]
  where
    dedup :: [a] -> [a]
dedup (a
a : a
b : [a]
rest)
      | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
b = [a] -> [a]
dedup (a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
rest)
      | Bool
otherwise = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
dedup (a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
rest)
    dedup [a]
ys = [a]
ys

-- the nearest-rank order statistic at a level of a sorted list: its
-- ceiling (q m)-th smallest of m, zero of an empty list
nearest :: Double -> [Double] -> Double
nearest :: Double -> [Double] -> Double
nearest !Double
q [Double]
xs = case Int -> [Double] -> [Double]
forall a. Int -> [a] -> [a]
drop Int
i [Double]
xs of
  Double
x : [Double]
_ -> Double
x
  [] -> Double
0
  where
    !m :: Int
m = [Double] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Double]
xs
    !i :: Int
i = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double
q Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))

-- streaming -----------------------------------------------------------------

-- | The hedge after one more observation: a statistic symmetric about
--   zero under the null, and one indicator statistic per cut point,
--   within the indicators' bound (clipped to it otherwise).
--
--   /Precondition/: one indicator statistic per cut point, in their
--   order; any beyond them are ignored.
update :: Config -> State -> Double -> [Double] -> State
update :: Config -> State -> Double -> [Double] -> State
update Config{Bool
Double
[Double]
[(Double, Config)]
Config
Config
Config
cfg_sign :: Config -> Config
cfg_magn :: Config -> [(Double, Config)]
cfg_cdf_bound :: Config -> Double
cfg_cdf :: Config -> Config
cfg_cuts :: Config -> [Double]
cfg_interval :: Config -> Bool
cfg_mix :: Config -> Config
cfg_sign :: Config
cfg_magn :: [(Double, Config)]
cfg_cdf_bound :: Double
cfg_cdf :: Config
cfg_cuts :: [Double]
cfg_interval :: Bool
cfg_mix :: Config
..} State{Int
[State]
State
State
Config
st_n :: State -> Int
st_sign :: State -> State
st_magn :: State -> [State]
st_cdf :: State -> [State]
st_mixc :: State -> Config
st_mix :: State -> State
st_n :: Int
st_sign :: State
st_magn :: [State]
st_cdf :: [State]
st_mixc :: Config
st_mix :: State
..} !Double
d [Double]
cs =
  let !sg :: State
sg | Double
d Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 = State
st_sign
          | Bool
otherwise = Config -> State -> Bool -> State
Sign.update Config
cfg_sign State
st_sign (Double
d Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0)
      !mg :: [State]
mg = [(Double, Config)] -> [State] -> [State]
step_magn [(Double, Config)]
cfg_magn [State]
st_magn
      !cd :: [State]
cd = [Double] -> [State] -> [State]
step_cdf [Double]
cs [State]
st_cdf
      !mx :: State
mx = Config -> State -> [Double] -> State
Mix.update Config
cfg_mix State
st_mix (Bool -> State -> [State] -> [State] -> [Double]
mixed Bool
cfg_interval State
sg [State]
mg [State]
cd)
  in  Int -> State -> [State] -> [State] -> Config -> State -> State
State (Int
st_n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) State
sg [State]
mg [State]
cd Config
st_mixc State
mx
  where
    step_magn :: [(Double, Config)] -> [State] -> [State]
step_magn ((Double
b, Config
c) : [(Double, Config)]
bt) (State
s : [State]
ss) =
      let !s' :: State
s' = Config -> State -> Double -> State
Magn.update Config
c State
s (Double -> Double -> Double
clip Double
b Double
d)
          !rest :: [State]
rest = [(Double, Config)] -> [State] -> [State]
step_magn [(Double, Config)]
bt [State]
ss
      in  State
s' State -> [State] -> [State]
forall a. a -> [a] -> [a]
: [State]
rest
    step_magn [(Double, Config)]
_ [State]
_ = []
    step_cdf :: [Double] -> [State] -> [State]
step_cdf (Double
x : [Double]
xs) (State
s : [State]
ss) =
      let !s' :: State
s' = Config -> State -> Double -> State
Magn.update Config
cfg_cdf State
s (Double -> Double -> Double
clip Double
cfg_cdf_bound Double
x)
          !rest :: [State]
rest = [Double] -> [State] -> [State]
step_cdf [Double]
xs [State]
ss
      in  State
s' State -> [State] -> [State]
forall a. a -> [a] -> [a]
: [State]
rest
    step_cdf [Double]
_ [State]
_ = []

-- | The hedge after one more pair: its difference, and at each cut
--   point whether its first value lies at or below it, less whether
--   its second does (the indicators' bound being one).
update_pair :: Config -> State -> (Double, Double) -> State
update_pair :: Config -> State -> (Double, Double) -> State
update_pair Config
cfg State
st (!Double
a, !Double
b) =
  Config -> State -> Double -> [Double] -> State
update Config
cfg State
st (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b) [ Double -> Double -> Double
forall {a} {a}. (Ord a, Num a) => a -> a -> a
below Double
a Double
q Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
forall {a} {a}. (Ord a, Num a) => a -> a -> a
below Double
b Double
q | Double
q <- Config -> [Double]
cfg_cuts Config
cfg ]
  where
    below :: a -> a -> a
below a
x a
q = if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
q then a
1 else a
0
{-# INLINE update_pair #-}

clip :: Double -> Double -> Double
clip :: Double -> Double -> Double
clip !Double
b !Double
x
  | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
b = Double
b
  | Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double -> Double
forall a. Num a => a -> a
negate Double
b = Double -> Double
forall a. Num a => a -> a
negate Double
b
  | Bool
otherwise = Double
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 :: Bool -> State -> [State] -> [State] -> [Double]
mixed Bool
interval State
sg [State]
mg [State]
cd
  | Bool
interval = (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue [State]
mg
  | Bool
otherwise =
      State -> Double
Sign.log_evalue State
sg Double -> [Double] -> [Double]
forall a. a -> [a] -> [a]
: (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue [State]
mg [Double] -> [Double] -> [Double]
forall a. Semigroup a => a -> a -> a
<> (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue [State]
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 -> Verdict
decide Config{Bool
Double
[Double]
[(Double, Config)]
Config
Config
Config
cfg_sign :: Config -> Config
cfg_magn :: Config -> [(Double, Config)]
cfg_cdf_bound :: Config -> Double
cfg_cdf :: Config -> Config
cfg_cuts :: Config -> [Double]
cfg_interval :: Config -> Bool
cfg_mix :: Config -> Config
cfg_sign :: Config
cfg_magn :: [(Double, Config)]
cfg_cdf_bound :: Double
cfg_cdf :: Config
cfg_cuts :: [Double]
cfg_interval :: Bool
cfg_mix :: Config
..} State{Int
[State]
State
State
Config
st_n :: State -> Int
st_sign :: State -> State
st_magn :: State -> [State]
st_cdf :: State -> [State]
st_mixc :: State -> Config
st_mix :: State -> State
st_n :: Int
st_sign :: State
st_magn :: [State]
st_cdf :: [State]
st_mixc :: Config
st_mix :: State
..} = Config -> State -> Verdict
Mix.decide Config
cfg_mix State
st_mix
{-# INLINE decide #-}

-- inspection ----------------------------------------------------------------

-- | 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 -> Double
log_evalue Config{Bool
Double
[Double]
[(Double, Config)]
Config
Config
Config
cfg_sign :: Config -> Config
cfg_magn :: Config -> [(Double, Config)]
cfg_cdf_bound :: Config -> Double
cfg_cdf :: Config -> Config
cfg_cuts :: Config -> [Double]
cfg_interval :: Config -> Bool
cfg_mix :: Config -> Config
cfg_sign :: Config
cfg_magn :: [(Double, Config)]
cfg_cdf_bound :: Double
cfg_cdf :: Config
cfg_cuts :: [Double]
cfg_interval :: Bool
cfg_mix :: Config
..} State{Int
[State]
State
State
Config
st_n :: State -> Int
st_sign :: State -> State
st_magn :: State -> [State]
st_cdf :: State -> [State]
st_mixc :: State -> Config
st_mix :: State -> State
st_n :: Int
st_sign :: State
st_magn :: [State]
st_cdf :: [State]
st_mixc :: Config
st_mix :: State
..} = Config -> State -> Double
Mix.log_evalue Config
cfg_mix State
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 :: State -> Double
log_evalue_sup State{Int
[State]
State
State
Config
st_n :: State -> Int
st_sign :: State -> State
st_magn :: State -> [State]
st_cdf :: State -> [State]
st_mixc :: State -> Config
st_mix :: State -> State
st_n :: Int
st_sign :: State
st_magn :: [State]
st_cdf :: [State]
st_mixc :: Config
st_mix :: State
..} = Config -> State -> Double
Mix.log_evalue_sup Config
st_mixc State
st_mix
{-# INLINE log_evalue_sup #-}

-- | The anytime-valid p-value: @min 1 (exp (- log_evalue_sup))@.
p_value :: State -> Double
p_value :: State -> Double
p_value State{Int
[State]
State
State
Config
st_n :: State -> Int
st_sign :: State -> State
st_magn :: State -> [State]
st_cdf :: State -> [State]
st_mixc :: State -> Config
st_mix :: State -> State
st_n :: Int
st_sign :: State
st_magn :: [State]
st_cdf :: [State]
st_mixc :: Config
st_mix :: State
..} = Config -> State -> Double
Mix.p_value Config
st_mixc State
st_mix
{-# INLINE p_value #-}

-- | The sign component's current log e-value.
log_evalue_sign :: State -> Double
log_evalue_sign :: State -> Double
log_evalue_sign = State -> Double
Sign.log_evalue (State -> Double) -> (State -> State) -> State -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> State
st_sign
{-# INLINE log_evalue_sign #-}

-- | Each clipped component's current log e-value, in the order of the
--   clip bounds.
log_evalue_magnitude :: State -> [Double]
log_evalue_magnitude :: State -> [Double]
log_evalue_magnitude = (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue ([State] -> [Double]) -> (State -> [State]) -> State -> [Double]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> [State]
st_magn
{-# INLINE log_evalue_magnitude #-}

-- | Each indicator component's current log e-value, in the order of
--   the cut points.
log_evalue_cdf :: State -> [Double]
log_evalue_cdf :: State -> [Double]
log_evalue_cdf = (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue ([State] -> [Double]) -> (State -> [State]) -> State -> [Double]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> [State]
st_cdf
{-# INLINE log_evalue_cdf #-}

-- | The supremum so far of the sign component's log e-value: which
--   component's evidence peaked is the shape of a difference found.
log_evalue_sup_sign :: State -> Double
log_evalue_sup_sign :: State -> Double
log_evalue_sup_sign = State -> Double
Sign.log_evalue_sup (State -> Double) -> (State -> State) -> State -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> State
st_sign
{-# INLINE log_evalue_sup_sign #-}

-- | The supremum so far of each clipped component's log e-value, in
--   the order of the clip bounds.
log_evalue_sup_magnitude :: State -> [Double]
log_evalue_sup_magnitude :: State -> [Double]
log_evalue_sup_magnitude = (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue_sup ([State] -> [Double]) -> (State -> [State]) -> State -> [Double]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> [State]
st_magn
{-# INLINE log_evalue_sup_magnitude #-}

-- | The supremum so far of each indicator component's log e-value, in
--   the order of the cut points.
log_evalue_sup_cdf :: State -> [Double]
log_evalue_sup_cdf :: State -> [Double]
log_evalue_sup_cdf = (State -> Double) -> [State] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap State -> Double
Magn.log_evalue_sup ([State] -> [Double]) -> (State -> [State]) -> State -> [Double]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> [State]
st_cdf
{-# INLINE log_evalue_sup_cdf #-}

-- | How many observations the hedge has seen.
samples :: State -> Int
samples :: State -> Int
samples = State -> Int
st_n
{-# INLINE samples #-}