{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}
module Numeric.Eproc.TwoSample (
Config
, State
, Verdict(..)
, ConfigError(..)
, config
, configInterval
, initial
, clip_bound
, cut_points
, update
, update_pair
, decide
, 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
data Config = Config {
Config -> Config
cfg_sign :: !Sign.Config
, Config -> [(Double, Config)]
cfg_magn :: ![(Double, Magn.Config)]
, Config -> Double
cfg_cdf_bound :: {-# UNPACK #-} !Double
, Config -> Config
cfg_cdf :: !Magn.Config
, Config -> [Double]
cfg_cuts :: ![Double]
, Config -> Bool
cfg_interval :: !Bool
, Config -> Config
cfg_mix :: !Mix.Config
}
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
, State -> State
st_mix :: !Mix.State
}
config
:: Double
-> [Double]
-> Double
-> [Double]
-> 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 #-}
configInterval
:: Double
-> Double
-> [Double]
-> Double
-> [Double]
-> 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
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
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
}
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 #-}
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 :: [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
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))
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]
_ = []
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
samples :: State -> Int
samples :: State -> Int
samples = State -> Int
st_n
{-# INLINE samples #-}