<commit_before>module Language.PP where

import Control.Monad
import Control.Monad.Free
import Language.PP.Types
import Language.PP.Dist
import Language.PP.Eval
import Language.PP.Infer

import Data.Random
import Data.Random.Distribution.Normal

import Data.List

type Y = Double

data S = A | B deriving(Eq, Read, Show, Ord)

switch :: S -> S
switch A = B
switch B = A

ptrans :: MonadRandom m => Double -> S -> PP m S
ptrans theta s =
  liftF $ categorical [(theta, s), (1 - theta, switch s)]

py :: S -> Y -> Double
py s y = logPdf (Normal (mu s) 1) y

mu :: S -> Double
mu A = 0
mu B = 5

go :: MonadRandom m => Double -> Y -> S -> PP m S
go theta y s = do
  s' <- liftF $ categorical [(theta, s), (1 - theta, switch s)]
  observe (py s' y)
  return s'

ys :: [Double]
{-ys = [0, 5, 0, 5, 0, 5, 0, 5]-}
ys = [0, 0, 0, 5, 0, 0, 5]

model :: MonadRandom m => Double -> PP m [S]
model theta = sequence . scanl (>>=) (pure A) $ map (go theta) ys

-- Util to get result
vote :: Ord a => [[a]] -> [a]
vote xxs =  map (snd . f) (transpose xxs)
  where
    f = maximum . map (\g -> (length g, head g))
      . groupBy (==) . sort

-- Run bootstrap with 100 particles over model
main = eval (bootstrap 100 (model 0.5)) >>= print . vote . fmap snd . snd<commit_msg>fixed main to use new types for bootstrap<commit_after>module Language.PP where

import Control.Monad
import Control.Monad.Free
import Language.PP.Types
import Language.PP.Dist
import Language.PP.Eval
import Language.PP.Infer

import Data.Random
import Data.Random.Distribution.Normal

import Data.List

type Y = Double

data S = A | B deriving(Eq, Read, Show, Ord)

switch :: S -> S
switch A = B
switch B = A

ptrans :: MonadRandom m => Double -> S -> PP m S
ptrans theta s =
  liftF $ categorical [(theta, s), (1 - theta, switch s)]

py :: S -> Y -> Double
py s y = logPdf (Normal (mu s) 1) y

mu :: S -> Double
mu A = 0
mu B = 3

go :: MonadRandom m => Double -> Y -> S -> PP m S
go theta y s = do
  s' <- liftF $ categorical [(theta, s), (1 - theta, switch s)]
  observe (py s' y)
  return s'

ys :: [Double]
ys = [3, 0, 3, 0]

model :: MonadRandom m => Double -> PP m [S]
model theta = sequence . scanl (>>=) (pure A) $ map (go theta) ys

-- Util to get result
vote :: Ord a => [[a]] -> [a]
vote xxs =  map (snd . f) (transpose xxs)
  where
    f = maximum . map (\g -> (length g, head g))
      . groupBy (==) . sort

-- Run bootstrap with 100 particles over model
main =
  eval (bootstrap 50 (model 0.5)) >>= print . vote . map snd . snd

m2 :: PP IO Double
m2 = liftM2 (+) g g
  where g = liftF (gaussian 0 1)
{'fraction_non_alphanumeric': 0.1498447204968944, 'fraction_numerical': 0.019021739130434784, 'mean_word_length': 2.778592375366569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '2042017', 'n_tokens_mistral': 990, 'n_tokens_neox': 921, 'n_words': 392}