Skip to content

Commit

Permalink
add sampling dep
Browse files Browse the repository at this point in the history
  • Loading branch information
ocramz committed Dec 30, 2019
1 parent c45ce6c commit 79c2c64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
33 changes: 18 additions & 15 deletions app/stat-rethinking/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ main :: IO ()
main = putStrLn "hello!"


-- | Posterior probability of each hypothesis
--
-- >>> posterior (fromList [1,0,1]) [fromList [0,0,0,0], fromList [1,0,0,0], fromList [1,1,0,0], fromList [1,1,1,0], fromList [1,1,1,1]]
-- [0 % 1,3 % 20,2 % 5,9 % 20,0 % 1]
posterior :: Sample Integer -> [Sample Integer] -> [Ratio Integer]
posterior spl hyps = map (% n) ws
where
n = sum ws
ws = nWays spl hyps



-- | Ratio of observed outcomes to all possible outcomes for samples of length 3
-- | Number of ways in which the observed sample could be produced by each of the "hypotheses"
--
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,1,0,0])
-- 1 % 8
-- >>> pOutcomes (fromList [1,0,1]) (fromList [1,0,0,0])
-- 3 % 64
pOutcomes :: Sample Int -- ^ observed sample
-> Sample Int -- ^ hypothesis
-> Ratio Int
pOutcomes spl hypot = nsps % ntot
-- >>> nWays (fromList [1,0,1]) [fromList [0,0,0,0], fromList [1,0,0,0], fromList [1,1,0,0], fromList [1,1,1,0], fromList [1,1,1,1]]
-- [0,3,8,9,0]
nWays :: Sample Integer -- ^ observed sample
-> [Sample Integer] -- ^ hypothesis space
-> [Integer]
nWays spl hyps = map f hyps
where
xs = samples3 hypot
nsps = length $ filter (== spl) xs
ntot = length xs
f h = fromIntegral $ length $ filter (== spl) $ samples3 h

-- | All possible samples of length 3
samples3 :: Sample Int -> Sample (Sample Int)
samples3 :: Sample a -> Sample (Sample a)
samples3 xs = sample3 <$> xs <*> xs <*> xs


Expand Down
1 change: 1 addition & 0 deletions bayesian-inference.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
, mwc-probability
, mwc-probability-transition
, primitive
, sampling
, text
, transformers
, vector
Expand Down

0 comments on commit 79c2c64

Please sign in to comment.