{-# LANGUAGE RankNTypes #-}
module Mcmc.Monitor.ParameterBatch
(
MonitorParameterBatch (..),
(>$<),
monitorBatchMean,
monitorBatchMeanF,
monitorBatchMeanE,
)
where
import qualified Data.ByteString.Builder as BB
import qualified Data.Double.Conversion.ByteString as BC
import Data.Functor.Contravariant
import qualified Data.Vector as VB
data MonitorParameterBatch a = MonitorParameterBatch
{
MonitorParameterBatch a -> String
mbpName :: String,
MonitorParameterBatch a -> Vector a -> Builder
mbpFunc :: VB.Vector a -> BB.Builder
}
instance Contravariant MonitorParameterBatch where
contramap :: (a -> b) -> MonitorParameterBatch b -> MonitorParameterBatch a
contramap a -> b
f (MonitorParameterBatch String
n Vector b -> Builder
m) = String -> (Vector a -> Builder) -> MonitorParameterBatch a
forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (Vector b -> Builder
m (Vector b -> Builder)
-> (Vector a -> Vector b) -> Vector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> Vector a -> Vector b
forall a b. (a -> b) -> Vector a -> Vector b
VB.map a -> b
f)
mean :: Real a => VB.Vector a -> Double
mean :: Vector a -> Double
mean Vector a
xs = a -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Vector a -> a
forall a. Num a => Vector a -> a
VB.sum Vector a
xs) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector a -> Int
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 :: String -> MonitorParameterBatch a
monitorBatchMean String
n = String -> (Vector a -> Builder) -> MonitorParameterBatch a
forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (ByteString -> Builder
BB.byteString (ByteString -> Builder)
-> (Vector a -> ByteString) -> Vector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double -> ByteString
BC.toFixed Int
8 (Double -> ByteString)
-> (Vector a -> Double) -> Vector a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> Double
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 :: String -> MonitorParameterBatch a
monitorBatchMeanF String
n = String -> (Vector a -> Builder) -> MonitorParameterBatch a
forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (ByteString -> Builder
BB.byteString (ByteString -> Builder)
-> (Vector a -> ByteString) -> Vector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ByteString
BC.toShortest (Double -> ByteString)
-> (Vector a -> Double) -> Vector a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> Double
forall a. Real a => Vector a -> Double
mean)
{-# SPECIALIZE monitorBatchMeanF :: String -> MonitorParameterBatch Int #-}
{-# SPECIALIZE monitorBatchMeanF :: String -> MonitorParameterBatch Double #-}
monitorBatchMeanE ::
Real a =>
String ->
MonitorParameterBatch a
monitorBatchMeanE :: String -> MonitorParameterBatch a
monitorBatchMeanE String
n = String -> (Vector a -> Builder) -> MonitorParameterBatch a
forall a.
String -> (Vector a -> Builder) -> MonitorParameterBatch a
MonitorParameterBatch String
n (ByteString -> Builder
BB.byteString (ByteString -> Builder)
-> (Vector a -> ByteString) -> Vector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double -> ByteString
BC.toExponential Int
8 (Double -> ByteString)
-> (Vector a -> Double) -> Vector a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> Double
forall a. Real a => Vector a -> Double
mean)
{-# SPECIALIZE monitorBatchMeanE :: String -> MonitorParameterBatch Int #-}
{-# SPECIALIZE monitorBatchMeanE :: String -> MonitorParameterBatch Double #-}