module Mcmc.Proposal.Generic
( genericContinuous,
genericDiscrete,
)
where
import Mcmc.Proposal
import Numeric.Log
import Statistics.Distribution
genericContinuous ::
(ContDistr d, ContGen d) =>
d ->
(a -> Double -> a) ->
Maybe (Double -> Double) ->
Maybe (a -> Double -> Log Double) ->
ProposalSimple a
genericContinuous :: d
-> (a -> Double -> a)
-> Maybe (Double -> Double)
-> Maybe (a -> Double -> Log Double)
-> ProposalSimple a
genericContinuous d
d a -> Double -> a
f Maybe (Double -> Double)
mInv Maybe (a -> Double -> Log Double)
mJac a
x GenIO
g = do
Double
u <- d -> GenIO -> IO Double
forall d (m :: * -> *).
(ContGen d, PrimMonad m) =>
d -> Gen (PrimState m) -> m Double
genContVar d
d GenIO
g
let r :: Log Double
r = case Maybe (Double -> Double)
mInv of
Maybe (Double -> Double)
Nothing -> Log Double
1.0
Just Double -> Double
fInv ->
let qXY :: Log Double
qXY = Double -> Log Double
forall a. a -> Log a
Exp (Double -> Log Double) -> Double -> Log Double
forall a b. (a -> b) -> a -> b
$ d -> Double -> Double
forall d. ContDistr d => d -> Double -> Double
logDensity d
d Double
u
qYX :: Log Double
qYX = Double -> Log Double
forall a. a -> Log a
Exp (Double -> Log Double) -> Double -> Log Double
forall a b. (a -> b) -> a -> b
$ d -> Double -> Double
forall d. ContDistr d => d -> Double -> Double
logDensity d
d (Double -> Double
fInv Double
u)
in Log Double
qYX Log Double -> Log Double -> Log Double
forall a. Fractional a => a -> a -> a
/ Log Double
qXY
j :: Log Double
j = case Maybe (a -> Double -> Log Double)
mJac of
Maybe (a -> Double -> Log Double)
Nothing -> Log Double
1.0
Just a -> Double -> Log Double
fJac -> a -> Double -> Log Double
fJac a
x Double
u
(a, Log Double, Log Double) -> IO (a, Log Double, Log Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x a -> Double -> a
`f` Double
u, Log Double
r, Log Double
j)
{-# INLINEABLE genericContinuous #-}
genericDiscrete ::
(DiscreteDistr d, DiscreteGen d) =>
d ->
(a -> Int -> a) ->
Maybe (Int -> Int) ->
ProposalSimple a
genericDiscrete :: d -> (a -> Int -> a) -> Maybe (Int -> Int) -> ProposalSimple a
genericDiscrete d
d a -> Int -> a
f Maybe (Int -> Int)
mfInv a
x GenIO
g = do
Int
u <- d -> GenIO -> IO Int
forall d (m :: * -> *).
(DiscreteGen d, PrimMonad m) =>
d -> Gen (PrimState m) -> m Int
genDiscreteVar d
d GenIO
g
let r :: Log Double
r = case Maybe (Int -> Int)
mfInv of
Maybe (Int -> Int)
Nothing -> Log Double
1.0
Just Int -> Int
fInv ->
let qXY :: Log Double
qXY = Double -> Log Double
forall a. a -> Log a
Exp (Double -> Log Double) -> Double -> Log Double
forall a b. (a -> b) -> a -> b
$ d -> Int -> Double
forall d. DiscreteDistr d => d -> Int -> Double
logProbability d
d Int
u
qYX :: Log Double
qYX = Double -> Log Double
forall a. a -> Log a
Exp (Double -> Log Double) -> Double -> Log Double
forall a b. (a -> b) -> a -> b
$ d -> Int -> Double
forall d. DiscreteDistr d => d -> Int -> Double
logProbability d
d (Int -> Int
fInv Int
u)
in Log Double
qYX Log Double -> Log Double -> Log Double
forall a. Fractional a => a -> a -> a
/ Log Double
qXY
(a, Log Double, Log Double) -> IO (a, Log Double, Log Double)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x a -> Int -> a
`f` Int
u, Log Double
r, Log Double
1.0)
{-# INLINEABLE genericDiscrete #-}