circuits-inference
Safe HaskellNone
LanguageGHC2024

Circuit.Inference.SMC

Description

Sequential Monte Carlo on a discrete hidden Markov model.

A 2-state HMM with binary observations. Exact inference by enumeration of all 2^T state sequences. SMC particle filter approximates the same posterior. Oracle: L1 distance between SMC and exact marginal < 0.1.

Synopsis

Model

data State Source #

Constructors

StateA 
StateB 

Instances

Instances details
Eq State Source # 
Instance details

Defined in Circuit.Inference.SMC

Methods

(==) :: State -> State -> Bool #

(/=) :: State -> State -> Bool #

Ord State Source # 
Instance details

Defined in Circuit.Inference.SMC

Methods

compare :: State -> State -> Ordering #

(<) :: State -> State -> Bool #

(<=) :: State -> State -> Bool #

(>) :: State -> State -> Bool #

(>=) :: State -> State -> Bool #

max :: State -> State -> State #

min :: State -> State -> State #

Show State Source # 
Instance details

Defined in Circuit.Inference.SMC

Methods

showsPrec :: Int -> State -> ShowS #

show :: State -> String #

showList :: [State] -> ShowS #

transProb :: State -> State -> Double Source #

Transition: P(next | current).

obsProb :: Int -> State -> Double Source #

Observation likelihood: P(o | state). o ∈ {0, 1}.

initialP :: State -> Double Source #

Initial distribution: P(S₁=S).

Exact inference

exactFiltering :: [Int] -> [(State, Double)] Source #

Exact filtering P(S_T | O_{1:T}) by enumeration. Returns [(S, normalised probability)].

SMC particle filter

particleFilter :: [Int] -> Int -> [(State, Double)] Source #

Run a particle filter and return empirical final filtering distribution. n particles, deterministic RNG (seed = 42), systematic resampling.

Polynomial shape oracle

type SMCPoly = 'Prod ('Prod ('Const State) ('Exp ())) ('Const Double) Source #

SMC polynomial: particle stream paired with a weight annotation.

  • Mono () State — the particle is an output position; the input direction is trivial (()) because the particle is sampled, not supplied.
  • Const Double — the weight is an output position with no direction, so it cannot be fed back as an input.

Written directly in Poly constructors because Mono is a type synonym.

smcIn :: () -> Dir SMCPoly Source #

Canonical input direction for the SMC polynomial: advance one step.

smcSystem :: Int -> System (Prob (->) Double) State SMCPoly Source #

One-step SMC kernel for a fixed observation.

Given current hidden state s and observation o, transition to s' according to transProb and emit the particle s' together with weight obsProb o s'. The weight is part of the output position, not an input direction, which is exactly the instance-table claim.

smcTotalWeight :: Int -> State -> Double Source #

Total expected weight emitted by one SMC step starting from s for observation o. This is the marginal likelihood P(o | s).

Oracle

trace5 :: [Int] Source #

5-step trace, 2⁵ = 32 sequences for exact enumeration.

l1Distance :: [(State, Double)] -> [(State, Double)] -> Double Source #

L1 distance between two filtering distributions.