{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE RecordWildCards #-}

-- |
-- Module      : Control.Monad.Bayes.Inference.RMSMC
-- Description : Resample-Move Sequential Monte Carlo (RM-SMC)
-- Copyright   : (c) Adam Scibior, 2015-2020
-- License     : MIT
-- Maintainer  : leonhard.markert@tweag.io
-- Stability   : experimental
-- Portability : GHC
--
-- Resample-move Sequential Monte Carlo (RM-SMC) sampling.
--
-- Walter Gilks and Carlo Berzuini. 2001. Following a moving target - Monte Carlo inference for dynamic Bayesian models. /Journal of the Royal Statistical Society/ 63 (2001), 127-146. <http://www.mathcs.emory.edu/~whalen/Papers/BNs/MonteCarlo-DBNs.pdf>
module Control.Monad.Bayes.Inference.RMSMC
  ( rmsmc,
    rmsmcDynamic,
    rmsmcBasic,
  )
where

import Control.Monad.Bayes.Class (MonadDistribution)
import Control.Monad.Bayes.Inference.MCMC (MCMCConfig (..))
import Control.Monad.Bayes.Inference.SMC
import Control.Monad.Bayes.Population
  ( Population,
    spawn,
    withParticles,
  )
import Control.Monad.Bayes.Sequential.Coroutine as Seq
import Control.Monad.Bayes.Sequential.Coroutine qualified as S
import Control.Monad.Bayes.Traced.Basic qualified as TrBas
import Control.Monad.Bayes.Traced.Dynamic qualified as TrDyn
import Control.Monad.Bayes.Traced.Static as Tr
  ( Traced,
    marginal,
    mhStep,
  )
import Control.Monad.Bayes.Traced.Static qualified as TrStat
import Data.Monoid (Endo (..))

-- | Resample-move Sequential Monte Carlo.
rmsmc ::
  MonadDistribution m =>
  MCMCConfig ->
  SMCConfig m ->
  -- | model
  Sequential (Traced (Population m)) a ->
  Population m a
rmsmc :: forall (m :: * -> *) a.
MonadDistribution m =>
MCMCConfig
-> SMCConfig m
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmc (MCMCConfig {Int
Proposal
proposal :: Proposal
numMCMCSteps :: Int
numBurnIn :: Int
proposal :: MCMCConfig -> Proposal
numMCMCSteps :: MCMCConfig -> Int
numBurnIn :: MCMCConfig -> Int
..}) (SMCConfig {Int
forall x. Population m x -> Population m x
resampler :: forall x. Population m x -> Population m x
numSteps :: Int
numParticles :: Int
resampler :: forall (m :: * -> *).
SMCConfig m -> forall x. Population m x -> Population m x
numSteps :: forall (m :: * -> *). SMCConfig m -> Int
numParticles :: forall (m :: * -> *). SMCConfig m -> Int
..}) =
  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
S.sequentially (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
numMCMCSteps Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
MonadDistribution 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
TrStat.hoist Population m x -> Population m x
forall x. Population m x -> Population m x
resampler) Int
numSteps
    (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) -> Sequential m a -> Sequential m a
S.hoistFirst ((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
TrStat.hoist (Int -> Population m ()
forall (m :: * -> *). Monad m => Int -> Population m ()
spawn Int
numParticles Population m () -> Population m x -> Population m x
forall a b. Population m a -> Population m b -> Population m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>))

-- | Resample-move Sequential Monte Carlo with a more efficient
-- tracing representation.
rmsmcBasic ::
  MonadDistribution m =>
  MCMCConfig ->
  SMCConfig m ->
  -- | model
  Sequential (TrBas.Traced (Population m)) a ->
  Population m a
rmsmcBasic :: forall (m :: * -> *) a.
MonadDistribution m =>
MCMCConfig
-> SMCConfig m
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmcBasic (MCMCConfig {Int
Proposal
proposal :: MCMCConfig -> Proposal
numMCMCSteps :: MCMCConfig -> Int
numBurnIn :: MCMCConfig -> Int
proposal :: Proposal
numMCMCSteps :: Int
numBurnIn :: Int
..}) (SMCConfig {Int
forall x. Population m x -> Population m x
resampler :: forall (m :: * -> *).
SMCConfig m -> forall x. Population m x -> Population m x
numSteps :: forall (m :: * -> *). SMCConfig m -> Int
numParticles :: forall (m :: * -> *). SMCConfig m -> Int
resampler :: forall x. Population m x -> Population m x
numSteps :: Int
numParticles :: Int
..}) =
  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
S.sequentially (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
numMCMCSteps Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
MonadDistribution 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.hoist Population m x -> Population m x
forall x. Population m x -> Population m x
resampler) Int
numSteps
    (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) -> Sequential m a -> Sequential m a
S.hoistFirst ((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.hoist (Int -> Population m x -> Population m x
forall (m :: * -> *) a.
Monad m =>
Int -> Population m a -> Population m a
withParticles Int
numParticles))

-- | A variant of resample-move Sequential Monte Carlo
-- where only random variables since last resampling are considered
-- for rejuvenation.
rmsmcDynamic ::
  MonadDistribution m =>
  MCMCConfig ->
  SMCConfig m ->
  -- | model
  Sequential (TrDyn.Traced (Population m)) a ->
  Population m a
rmsmcDynamic :: forall (m :: * -> *) a.
MonadDistribution m =>
MCMCConfig
-> SMCConfig m
-> Sequential (Traced (Population m)) a
-> Population m a
rmsmcDynamic (MCMCConfig {Int
Proposal
proposal :: MCMCConfig -> Proposal
numMCMCSteps :: MCMCConfig -> Int
numBurnIn :: MCMCConfig -> Int
proposal :: Proposal
numMCMCSteps :: Int
numBurnIn :: Int
..}) (SMCConfig {Int
forall x. Population m x -> Population m x
resampler :: forall (m :: * -> *).
SMCConfig m -> forall x. Population m x -> Population m x
numSteps :: forall (m :: * -> *). SMCConfig m -> Int
numParticles :: forall (m :: * -> *). SMCConfig m -> Int
resampler :: forall x. Population m x -> Population m x
numSteps :: Int
numParticles :: Int
..}) =
  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
S.sequentially (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
numMCMCSteps Traced (Population m) x -> Traced (Population m) x
forall (m :: * -> *) a.
MonadDistribution 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.hoist Population m x -> Population m x
forall x. Population m x -> Population m x
resampler) Int
numSteps
    (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) -> Sequential m a -> Sequential m a
S.hoistFirst ((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.hoist (Int -> Population m x -> Population m x
forall (m :: * -> *) a.
Monad m =>
Int -> Population m a -> Population m a
withParticles Int
numParticles))

-- | Apply a function a given number of times.
composeCopies :: Int -> (a -> a) -> (a -> a)
composeCopies :: forall a. Int -> (a -> a) -> a -> a
composeCopies Int
k = (Endo a -> Endo a) -> (a -> a) -> a -> a
forall a b. (Endo a -> Endo b) -> (a -> a) -> b -> b
withEndo ([Endo a] -> Endo a
forall a. Monoid a => [a] -> a
mconcat ([Endo a] -> Endo a) -> (Endo a -> [Endo a]) -> Endo a -> Endo a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Endo a -> [Endo a]
forall a. Int -> a -> [a]
replicate Int
k)

withEndo :: (Endo a -> Endo b) -> (a -> a) -> b -> b
withEndo :: forall a b. (Endo a -> Endo b) -> (a -> a) -> b -> b
withEndo Endo a -> Endo b
f = Endo b -> b -> b
forall a. Endo a -> a -> a
appEndo (Endo b -> b -> b) -> ((a -> a) -> Endo b) -> (a -> a) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Endo a -> Endo b
f (Endo a -> Endo b) -> ((a -> a) -> Endo a) -> (a -> a) -> Endo b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> Endo a
forall a. (a -> a) -> Endo a
Endo