{-# LANGUAGE RankNTypes #-}
module Mcmc.Monitor.ParameterBatch
(
MonitorParameterBatch (..),
(>$<),
monitorBatchMean,
monitorBatchMeanF,
monitorBatchMeanS,
)
where
import qualified Data.ByteString.Builder as BB
import Data.Functor.Contravariant
import qualified Data.Vector as VB
data MonitorParameterBatch a = MonitorParameterBatch
{
forall a. MonitorParameterBatch a -> String
mbpName :: String,
forall a. MonitorParameterBatch a -> Vector a -> Builder
mbpFunc :: VB.Vector a -> BB.Builder
}
instance Contravariant MonitorParameterBatch where
contramap :: forall a' a.
(a' -> a) -> MonitorParameterBatch a -> MonitorParameterBatch a'
contramap a' -> a
f (MonitorParameterBatch String
n Vector a -> Builder
m) = forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (Vector a -> Builder
m forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> Vector a -> Vector b
VB.map a' -> a
f)
mean :: Real a => VB.Vector a -> Double
mean :: forall a. Real a => Vector a -> Double
mean Vector a
xs = forall a b. (Real a, Fractional b) => a -> b
realToFrac (forall a. Num a => Vector a -> a
VB.sum Vector a
xs) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Vector a -> Int
VB.length Vector a
xs)
{-# SPECIALIZE mean :: VB.Vector Double -> Double #-}
{-# SPECIALIZE mean :: VB.Vector Int -> Double #-}
monitorBatchMean ::
Real a =>
String ->
MonitorParameterBatch a
monitorBatchMean :: forall a. Real a => String -> MonitorParameterBatch a
monitorBatchMean String
n = forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (FloatFormat -> Double -> Builder
BB.formatDouble (Int -> FloatFormat
BB.standard Int
8) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => Vector a -> Double
mean)
{-# SPECIALIZE monitorBatchMean :: String -> MonitorParameterBatch Int #-}
{-# SPECIALIZE monitorBatchMean :: String -> MonitorParameterBatch Double #-}
monitorBatchMeanF ::
Real a =>
String ->
MonitorParameterBatch a
monitorBatchMeanF :: forall a. Real a => String -> MonitorParameterBatch a
monitorBatchMeanF String
n = forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (Double -> Builder
BB.doubleDec forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => Vector a -> Double
mean)
{-# SPECIALIZE monitorBatchMeanF :: String -> MonitorParameterBatch Int #-}
{-# SPECIALIZE monitorBatchMeanF :: String -> MonitorParameterBatch Double #-}
monitorBatchMeanS ::
Real a =>
String ->
MonitorParameterBatch a
monitorBatchMeanS :: forall a. Real a => String -> MonitorParameterBatch a
monitorBatchMeanS String
n = forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (FloatFormat -> Double -> Builder
BB.formatDouble FloatFormat
BB.scientific forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => Vector a -> Double
mean)
{-# SPECIALIZE monitorBatchMeanS :: String -> MonitorParameterBatch Int #-}
{-# SPECIALIZE monitorBatchMeanS :: String -> MonitorParameterBatch Double #-}