module Control.Monad.Bayes.Inference.RMSMC
( rmsmc,
rmsmcLocal,
rmsmcBasic,
)
where
import Control.Monad.Bayes.Class
import Control.Monad.Bayes.Helpers
import Control.Monad.Bayes.Population
import Control.Monad.Bayes.Sequential as Seq
import Control.Monad.Bayes.Traced as Tr
import qualified Control.Monad.Bayes.Traced.Basic as TrBas
import qualified Control.Monad.Bayes.Traced.Dynamic as TrDyn
rmsmc ::
MonadSample m =>
Int ->
Int ->
Int ->
Sequential (Traced (Population m)) a ->
Population m a
rmsmc :: Int
-> Int
-> Int
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmc k :: Int
k n :: Int
n t :: Int
t =
Traced (Population m) a -> Population m a
forall (m :: * -> *) a. Monad m => Traced m a -> m a
marginal
(Traced (Population m) a -> Population m a)
-> (Sequential (Traced (Population m)) a
-> Traced (Population m) a)
-> Sequential (Traced (Population m)) a
-> Population m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Int
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sis (Int
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall a. Int -> (a -> a) -> a -> a
composeCopies Int
t Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a. MonadSample m => Traced m a -> Traced m a
mhStep (Traced (Population m) x -> Traced (Population m) x)
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
hoistT forall x. Population m x -> Population m x
forall (m :: * -> *) a.
MonadSample m =>
Population m a -> Population m a
resampleSystematic) Int
k
(Sequential (Traced (Population m)) a -> Traced (Population m) a)
-> (Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a)
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a
forall (m :: * -> *) a. (forall x. m x -> m x) -> S m a -> S m a
hoistS ((forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
hoistT (Int -> Population m ()
forall (m :: * -> *). Monad m => Int -> Population m ()
spawn Int
n Population m () -> Population m x -> Population m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>))
rmsmcBasic ::
MonadSample m =>
Int ->
Int ->
Int ->
Sequential (TrBas.Traced (Population m)) a ->
Population m a
rmsmcBasic :: Int
-> Int
-> Int
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmcBasic k :: Int
k n :: Int
n t :: Int
t =
Traced (Population m) a -> Population m a
forall (m :: * -> *) a. Monad m => Traced m a -> m a
TrBas.marginal
(Traced (Population m) a -> Population m a)
-> (Sequential (Traced (Population m)) a
-> Traced (Population m) a)
-> Sequential (Traced (Population m)) a
-> Population m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Int
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sis (Int
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall a. Int -> (a -> a) -> a -> a
composeCopies Int
t Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a. MonadSample m => Traced m a -> Traced m a
TrBas.mhStep (Traced (Population m) x -> Traced (Population m) x)
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
TrBas.hoistT forall x. Population m x -> Population m x
forall (m :: * -> *) a.
MonadSample m =>
Population m a -> Population m a
resampleSystematic) Int
k
(Sequential (Traced (Population m)) a -> Traced (Population m) a)
-> (Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a)
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a
forall (m :: * -> *) a. (forall x. m x -> m x) -> S m a -> S m a
hoistS ((forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
TrBas.hoistT (Int -> Population m ()
forall (m :: * -> *). Monad m => Int -> Population m ()
spawn Int
n Population m () -> Population m x -> Population m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>))
rmsmcLocal ::
MonadSample m =>
Int ->
Int ->
Int ->
Sequential (TrDyn.Traced (Population m)) a ->
Population m a
rmsmcLocal :: Int
-> Int
-> Int
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmcLocal k :: Int
k n :: Int
n t :: Int
t =
Traced (Population m) a -> Population m a
forall (m :: * -> *) a. Monad m => Traced m a -> m a
TrDyn.marginal
(Traced (Population m) a -> Population m a)
-> (Sequential (Traced (Population m)) a
-> Traced (Population m) a)
-> Sequential (Traced (Population m)) a
-> Population m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Int
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sis (Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a. Monad m => Traced m a -> Traced m a
TrDyn.freeze (Traced (Population m) x -> Traced (Population m) x)
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall a. Int -> (a -> a) -> a -> a
composeCopies Int
t Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a. MonadSample m => Traced m a -> Traced m a
TrDyn.mhStep (Traced (Population m) x -> Traced (Population m) x)
-> (Traced (Population m) x -> Traced (Population m) x)
-> Traced (Population m) x
-> Traced (Population m) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
TrDyn.hoistT forall x. Population m x -> Population m x
forall (m :: * -> *) a.
MonadSample m =>
Population m a -> Population m a
resampleSystematic) Int
k
(Sequential (Traced (Population m)) a -> Traced (Population m) a)
-> (Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a)
-> Sequential (Traced (Population m)) a
-> Traced (Population m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Traced (Population m) x -> Traced (Population m) x)
-> Sequential (Traced (Population m)) a
-> Sequential (Traced (Population m)) a
forall (m :: * -> *) a. (forall x. m x -> m x) -> S m a -> S m a
hoistS ((forall x. Population m x -> Population m x)
-> Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
TrDyn.hoistT (Int -> Population m ()
forall (m :: * -> *). Monad m => Int -> Population m ()
spawn Int
n Population m () -> Population m x -> Population m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>))
composeCopies :: Int -> (a -> a) -> (a -> a)
composeCopies :: Int -> (a -> a) -> a -> a
composeCopies k :: Int
k f :: a -> a
f = ((a -> a) -> (a -> a) -> a -> a) -> (a -> a) -> [a -> a] -> a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) a -> a
forall a. a -> a
id (Int -> (a -> a) -> [a -> a]
forall a. Int -> a -> [a]
replicate Int
k a -> a
f)