<commit_before>{-# LANGUAGE ScopedTypeVariables #-}

module Math.Probably.MCMC (
    continuousSlice
  , discreteSlice
  , hamiltonian
  , mala
  , metropolis
  , nuts
  , nutsDualAveraging
  , trace
  ) where

import Control.Applicative
import Control.Monad
import Control.Monad.State.Strict
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe
import Math.Probably.Sampler
import Math.Probably.Types
import Numeric.LinearAlgebra
import Strategy.Hamiltonian
import Strategy.MALA
import Strategy.Metropolis
import Strategy.NUTS
import Strategy.NUTSDualAveraging
import Strategy.Slice

-- interleave :: [Transition a] -> Transition a
-- interleave = foldl1 (>>) 
-- 
-- oneOfRandomly :: [Transition a] -> Transition a
-- oneOfRandomly ts = do
--   j <- lift $ oneOf [0..(length ts - 1)]
--   ts !! j
-- 
-- frequency :: [(Int, Transition a)] -> Transition a
-- frequency xs = lift (oneOf [1..tot]) >>= (`pick` xs) where
--   tot = sum . map fst $ xs
--   pick n ((k, v):vs)
--     | n <= k    = v
--     | otherwise = pick (n - k) vs
-- 
-- firstWithProb :: Double -> Transition a -> Transition a -> Transition a
-- firstWithProb p t0 t1 = do
--   s <- lift $ bernoulli p
--   if s then t0 else t1

trace :: Int -> Transition t -> Chain t -> Prob (Prob Parameters)<commit_msg>Add sampling-based interleave operators.<commit_after>{-# LANGUAGE ScopedTypeVariables #-}

module Math.Probably.MCMC (
  -- * Strategies
    continuousSlice
  , discreteSlice
  , hamiltonian
  , mala
  , metropolis
  , nuts
  , nutsDualAveraging
  -- * Interleaving
  , interleave
  , polyInterleave
  , frequency
  , firstWithProb
  , oneOfRandomly
  -- * Other
  , ezMC
  , trace
  ) where

import Control.Applicative
import Control.Monad
import Control.Monad.State.Strict
import Math.Probably.Sampler
import Math.Probably.Types
import Strategy.Hamiltonian
import Strategy.MALA
import Strategy.Metropolis
import Strategy.NUTS
import Strategy.NUTSDualAveraging
import Strategy.Slice

interleave :: [Transition a] -> Transition a
interleave = foldl1 (>>) 

oneOfRandomly :: [Transition a] -> Transition a
oneOfRandomly ts = do
  j <- lift $ oneOf [0..(length ts - 1)]
  ts !! j

frequency :: [(Int, Transition a)] -> Transition a
frequency xs = lift (oneOf [1..tot]) >>= (`pick` xs) where
  tot = sum . map fst $ xs
  pick n ((k, v):vs)
    | n <= k    = v
    | otherwise = pick (n - k) vs

firstWithProb :: Double -> Transition a -> Transition a -> Transition a
firstWithProb p t0 t1 = do
  s <- lift $ bernoulli p
  if s then t0 else t1

trace :: Int -> Transition t -> Chain t -> Prob (Prob Parameters)
{'fraction_non_alphanumeric': 0.1218293620292083, 'fraction_numerical': 0.006149116064565719, 'mean_word_length': 3.1186708860759493, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '195566', 'n_tokens_mistral': 915, 'n_tokens_neox': 843, 'n_words': 267}