{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-deprecations #-}
module Control.Monad.Bayes.Class
( MonadDistribution,
random,
uniform,
normal,
gamma,
beta,
bernoulli,
categorical,
logCategorical,
uniformD,
geometric,
poisson,
dirichlet,
MonadFactor,
score,
factor,
condition,
MonadMeasure,
discrete,
normalPdf,
Bayesian (..),
poissonPdf,
posterior,
priorPredictive,
posteriorPredictive,
independent,
mvNormal,
Histogram,
histogram,
histogramToList,
Distribution,
Measure,
Kernel,
Log (ln, Exp),
)
where
import Control.Arrow (Arrow (second))
import Control.Monad (replicateM, when)
import Control.Monad.Cont (ContT)
import Control.Monad.Except (ExceptT, lift)
import Control.Monad.Identity (IdentityT)
import Control.Monad.List (ListT)
import Control.Monad.Reader (ReaderT)
import Control.Monad.State (StateT)
import Control.Monad.Writer (WriterT)
import Data.Histogram qualified as H
import Data.Histogram.Fill qualified as H
import Data.Matrix
( Matrix,
cholDecomp,
colVector,
getCol,
multStd,
)
import Data.Vector qualified as V
import Data.Vector.Generic as VG (Vector, map, mapM, null, sum, (!))
import Numeric.Log (Log (..))
import Statistics.Distribution
( ContDistr (logDensity, quantile),
DiscreteDistr (logProbability, probability),
)
import Statistics.Distribution.Beta (betaDistr)
import Statistics.Distribution.Gamma (gammaDistr)
import Statistics.Distribution.Geometric (geometric0)
import Statistics.Distribution.Normal (normalDistr)
import Statistics.Distribution.Poisson qualified as Poisson
import Statistics.Distribution.Uniform (uniformDistr)
class Monad m => MonadDistribution m where
random ::
m Double
uniform ::
Double ->
Double ->
m Double
uniform Double
a Double
b = forall d (m :: * -> *).
(ContDistr d, MonadDistribution m) =>
d -> m Double
draw (Double -> Double -> UniformDistribution
uniformDistr Double
a Double
b)
normal ::
Double ->
Double ->
m Double
normal Double
m Double
s = forall d (m :: * -> *).
(ContDistr d, MonadDistribution m) =>
d -> m Double
draw (Double -> Double -> NormalDistribution
normalDistr Double
m Double
s)
gamma ::
Double ->
Double ->
m Double
gamma Double
shape Double
scale = forall d (m :: * -> *).
(ContDistr d, MonadDistribution m) =>
d -> m Double
draw (Double -> Double -> GammaDistribution
gammaDistr Double
shape Double
scale)
beta ::
Double ->
Double ->
m Double
beta Double
a Double
b = forall d (m :: * -> *).
(ContDistr d, MonadDistribution m) =>
d -> m Double
draw (Double -> Double -> BetaDistribution
betaDistr Double
a Double
b)
bernoulli ::
Double ->
m Bool
bernoulli Double
p = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Ord a => a -> a -> Bool
< Double
p) forall (m :: * -> *). MonadDistribution m => m Double
random
categorical ::
Vector v Double =>
v Double ->
m Int
categorical v Double
ps = if forall (v :: * -> *) a. Vector v a => v a -> Bool
VG.null v Double
ps then forall a. HasCallStack => [Char] -> a
error [Char]
"empty input list" else forall (m :: * -> *).
MonadDistribution m =>
(Int -> Double) -> m Int
fromPMF (v Double
ps forall (v :: * -> *) a. Vector v a => v a -> Int -> a
!)
logCategorical ::
(Vector v (Log Double), Vector v Double) =>
v (Log Double) ->
m Int
logCategorical = forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map (forall a. Floating a => a -> a
exp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Log a -> a
ln)
uniformD ::
[a] ->
m a
uniformD [a]
xs = do
let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
xs
Int
i <- forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> Vector a
V.replicate Int
n (Double
1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
forall (m :: * -> *) a. Monad m => a -> m a
return ([a]
xs forall a. [a] -> Int -> a
!! Int
i)
geometric ::
Double ->
m Int
geometric = forall d (m :: * -> *).
(DiscreteDistr d, MonadDistribution m) =>
d -> m Int
discrete forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> GeometricDistribution0
geometric0
poisson ::
Double ->
m Int
poisson = forall d (m :: * -> *).
(DiscreteDistr d, MonadDistribution m) =>
d -> m Int
discrete forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> PoissonDistribution
Poisson.poisson
dirichlet ::
Vector v Double =>
v Double ->
m (v Double)
dirichlet v Double
as = do
v Double
xs <- forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a, Vector v b) =>
(a -> m b) -> v a -> m (v b)
VG.mapM (forall (m :: * -> *).
MonadDistribution m =>
Double -> Double -> m Double
`gamma` Double
1) v Double
as
let s :: Double
s = forall (v :: * -> *) a. (Vector v a, Num a) => v a -> a
VG.sum v Double
xs
let ys :: v Double
ys = forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
VG.map (forall a. Fractional a => a -> a -> a
/ Double
s) v Double
xs
forall (m :: * -> *) a. Monad m => a -> m a
return v Double
ys
draw :: (ContDistr d, MonadDistribution m) => d -> m Double
draw :: forall d (m :: * -> *).
(ContDistr d, MonadDistribution m) =>
d -> m Double
draw d
d = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall d. ContDistr d => d -> Double -> Double
quantile d
d) forall (m :: * -> *). MonadDistribution m => m Double
random
fromPMF :: MonadDistribution m => (Int -> Double) -> m Int
fromPMF :: forall (m :: * -> *).
MonadDistribution m =>
(Int -> Double) -> m Int
fromPMF Int -> Double
p = forall {m :: * -> *}. MonadDistribution m => Int -> Double -> m Int
f Int
0 Double
1
where
f :: Int -> Double -> m Int
f Int
i Double
r = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
r forall a. Ord a => a -> a -> Bool
< Double
0) forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => [Char] -> a
error [Char]
"fromPMF: total PMF above 1"
let q :: Double
q = Int -> Double
p Int
i
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
q forall a. Ord a => a -> a -> Bool
< Double
0 Bool -> Bool -> Bool
|| Double
q forall a. Ord a => a -> a -> Bool
> Double
1) forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => [Char] -> a
error [Char]
"fromPMF: invalid probability value"
Bool
b <- forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli (Double
q forall a. Fractional a => a -> a -> a
/ Double
r)
if Bool
b then forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
i else Int -> Double -> m Int
f (Int
i forall a. Num a => a -> a -> a
+ Int
1) (Double
r forall a. Num a => a -> a -> a
- Double
q)
discrete :: (DiscreteDistr d, MonadDistribution m) => d -> m Int
discrete :: forall d (m :: * -> *).
(DiscreteDistr d, MonadDistribution m) =>
d -> m Int
discrete = forall (m :: * -> *).
MonadDistribution m =>
(Int -> Double) -> m Int
fromPMF forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. DiscreteDistr d => d -> Int -> Double
probability
class Monad m => MonadFactor m where
score ::
Log Double ->
m ()
factor ::
MonadFactor m =>
Log Double ->
m ()
factor :: forall (m :: * -> *). MonadFactor m => Log Double -> m ()
factor = forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
type Distribution a = forall m. MonadDistribution m => m a
type Measure a = forall m. MonadMeasure m => m a
type Kernel a b = forall m. MonadMeasure m => a -> m b
condition :: MonadFactor m => Bool -> m ()
condition :: forall (m :: * -> *). MonadFactor m => Bool -> m ()
condition Bool
b = forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score forall a b. (a -> b) -> a -> b
$ if Bool
b then Log Double
1 else Log Double
0
independent :: Applicative m => Int -> m a -> m [a]
independent :: forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
independent = forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM
class (MonadDistribution m, MonadFactor m) => MonadMeasure m
normalPdf ::
Double ->
Double ->
Double ->
Log Double
normalPdf :: Double -> Double -> Double -> Log Double
normalPdf Double
mu Double
sigma Double
x = forall a. a -> Log a
Exp forall a b. (a -> b) -> a -> b
$ forall d. ContDistr d => d -> Double -> Double
logDensity (Double -> Double -> NormalDistribution
normalDistr Double
mu Double
sigma) Double
x
poissonPdf :: Double -> Integer -> Log Double
poissonPdf :: Double -> Integer -> Log Double
poissonPdf Double
rate Integer
n = forall a. a -> Log a
Exp forall a b. (a -> b) -> a -> b
$ forall d. DiscreteDistr d => d -> Int -> Double
logProbability (Double -> PoissonDistribution
Poisson.poisson Double
rate) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n)
mvNormal :: MonadDistribution m => V.Vector Double -> Matrix Double -> m (V.Vector Double)
mvNormal :: forall (m :: * -> *).
MonadDistribution m =>
Vector Double -> Matrix Double -> m (Vector Double)
mvNormal Vector Double
mu Matrix Double
bigSigma = do
let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length Vector Double
mu
[Double]
ss <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (forall (m :: * -> *).
MonadDistribution m =>
Double -> Double -> m Double
normal Double
0 Double
1)
let bigL :: Matrix Double
bigL = forall a. Floating a => Matrix a -> Matrix a
cholDecomp Matrix Double
bigSigma
let ts :: Matrix Double
ts = (forall a. Vector a -> Matrix a
colVector Vector Double
mu) forall a. Num a => a -> a -> a
+ Matrix Double
bigL forall a. Num a => Matrix a -> Matrix a -> Matrix a
`multStd` (forall a. Vector a -> Matrix a
colVector forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Vector a
V.fromList [Double]
ss)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> Matrix a -> Vector a
getCol Int
1 Matrix Double
ts
data Bayesian m z o = Bayesian
{ forall (m :: * -> *) z o. Bayesian m z o -> m z
prior :: m z,
forall (m :: * -> *) z o. Bayesian m z o -> z -> m o
generative :: z -> m o,
forall (m :: * -> *) z o. Bayesian m z o -> z -> o -> Log Double
likelihood :: z -> o -> Log Double
}
posterior :: (MonadMeasure m, Foldable f, Functor f) => Bayesian m z o -> f o -> m z
posterior :: forall (m :: * -> *) (f :: * -> *) z o.
(MonadMeasure m, Foldable f, Functor f) =>
Bayesian m z o -> f o -> m z
posterior Bayesian {m z
z -> m o
z -> o -> Log Double
likelihood :: z -> o -> Log Double
generative :: z -> m o
prior :: m z
likelihood :: forall (m :: * -> *) z o. Bayesian m z o -> z -> o -> Log Double
generative :: forall (m :: * -> *) z o. Bayesian m z o -> z -> m o
prior :: forall (m :: * -> *) z o. Bayesian m z o -> m z
..} f o
os = do
z
z <- m z
prior
forall (m :: * -> *). MonadFactor m => Log Double -> m ()
factor forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (z -> o -> Log Double
likelihood z
z) f o
os
forall (m :: * -> *) a. Monad m => a -> m a
return z
z
priorPredictive :: Monad m => Bayesian m a b -> m b
priorPredictive :: forall (m :: * -> *) a b. Monad m => Bayesian m a b -> m b
priorPredictive Bayesian m a b
bm = forall (m :: * -> *) z o. Bayesian m z o -> m z
prior Bayesian m a b
bm forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) z o. Bayesian m z o -> z -> m o
generative Bayesian m a b
bm
posteriorPredictive ::
(MonadMeasure m, Foldable f, Functor f) =>
Bayesian m a b ->
f b ->
m b
posteriorPredictive :: forall (m :: * -> *) (f :: * -> *) a b.
(MonadMeasure m, Foldable f, Functor f) =>
Bayesian m a b -> f b -> m b
posteriorPredictive Bayesian m a b
bm f b
os = forall (m :: * -> *) (f :: * -> *) z o.
(MonadMeasure m, Foldable f, Functor f) =>
Bayesian m z o -> f o -> m z
posterior Bayesian m a b
bm f b
os forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) z o. Bayesian m z o -> z -> m o
generative Bayesian m a b
bm
type Histogram = H.Histogram H.BinD Double
histogram :: Int -> [(Double, Log Double)] -> Histogram
histogram :: Int -> [(Double, Log Double)] -> Histogram
histogram Int
n [(Double, Log Double)]
v = forall (f :: * -> *) a b. Foldable f => HBuilder a b -> f a -> b
H.fillBuilder HBuilder (Double, Double) Histogram
buildr forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (forall a. Log a -> a
ln forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Floating a => a -> a
exp)) [(Double, Log Double)]
v
where
v1 :: [Double]
v1 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst [(Double, Log Double)]
v
mi :: Double
mi = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
Prelude.minimum [Double]
v1
ma :: Double
ma = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
Prelude.maximum [Double]
v1
bins :: BinD
bins = Double -> Int -> Double -> BinD
H.binD Double
mi Int
n Double
ma
buildr :: HBuilder (BinValue BinD, Double) Histogram
buildr = forall bin val.
(Bin bin, Unbox val, Num val) =>
bin -> HBuilder (BinValue bin, val) (Histogram bin val)
H.mkWeighted BinD
bins
histogramToList :: Histogram -> [(Double, Double)]
histogramToList :: Histogram -> [(Double, Double)]
histogramToList = forall a bin.
(Unbox a, Bin bin) =>
Histogram bin a -> [(BinValue bin, a)]
H.asList
instance MonadDistribution m => MonadDistribution (IdentityT m) where
random :: IdentityT m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> IdentityT m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
instance MonadFactor m => MonadFactor (IdentityT m) where
score :: Log Double -> IdentityT m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (IdentityT m)
instance MonadDistribution m => MonadDistribution (ExceptT e m) where
random :: ExceptT e m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
uniformD :: forall a. [a] -> ExceptT e m a
uniformD = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadDistribution m => [a] -> m a
uniformD
instance MonadFactor m => MonadFactor (ExceptT e m) where
score :: Log Double -> ExceptT e m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (ExceptT e m)
instance MonadDistribution m => MonadDistribution (ReaderT r m) where
random :: ReaderT r m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> ReaderT r m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
instance MonadFactor m => MonadFactor (ReaderT r m) where
score :: Log Double -> ReaderT r m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (ReaderT r m)
instance (Monoid w, MonadDistribution m) => MonadDistribution (WriterT w m) where
random :: WriterT w m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> WriterT w m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
categorical :: forall (v :: * -> *).
Vector v Double =>
v Double -> WriterT w m Int
categorical = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical
instance (Monoid w, MonadFactor m) => MonadFactor (WriterT w m) where
score :: Log Double -> WriterT w m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance (Monoid w, MonadMeasure m) => MonadMeasure (WriterT w m)
instance MonadDistribution m => MonadDistribution (StateT s m) where
random :: StateT s m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> StateT s m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
categorical :: forall (v :: * -> *). Vector v Double => v Double -> StateT s m Int
categorical = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical
uniformD :: forall a. [a] -> StateT s m a
uniformD = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadDistribution m => [a] -> m a
uniformD
instance MonadFactor m => MonadFactor (StateT s m) where
score :: Log Double -> StateT s m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (StateT s m)
instance MonadDistribution m => MonadDistribution (ListT m) where
random :: ListT m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> ListT m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
categorical :: forall (v :: * -> *). Vector v Double => v Double -> ListT m Int
categorical = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical
instance MonadFactor m => MonadFactor (ListT m) where
score :: Log Double -> ListT m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (ListT m)
instance MonadDistribution m => MonadDistribution (ContT r m) where
random :: ContT r m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
instance MonadFactor m => MonadFactor (ContT r m) where
score :: Log Double -> ContT r m ()
score = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score
instance MonadMeasure m => MonadMeasure (ContT r m)