Debian Codegraph @

tests/prob_prog/coal_mining/Coal.hs

raw
1 module Coal where
2
3 import Probability
4 import Data.Frame
5 import Data.Array
6
7 -- When we get polymorphism, maybe we can create an temporary array as a view on xs'
8 -- I presume that using n directly is more efficient that using [length xs']
9 orderedSample n dist = do
10 xs' <- sample $ iid n dist
11 let xs = sort xs'
12 return $ listArray n xs
13
14 get_intervals (r:[]) (t:[]) tend = [(r,t,tend)]
15 get_intervals (r:rs) (t1:ts@(t2:_)) tend = (r,t1,t2):get_intervals rs ts tend
16
17 model (t1,t2) times = do
18 let lam = 3; nMax = 30; a = 1; b = 365/200
19
20 -- n change points, n+1 intervals
21 n <- min nMax <$> prior (poisson lam)
22
23 -- even numbered order statistics over the interval [t1,t2]
24 s' <- orderedSample (2*n+1) (uniform t1 t2)
25 let s = t1:[s'!(2*i-1) | i <- [1..n]]
26
27 -- n+1 rates
28 g <- prior $ iid (n+1) (gamma a b)
29
30 let intervals = get_intervals g s t2
31
32 observe times $ poisson_processes intervals
33
34 return [ "n" %=% n, "s" %=% s, "g" %=% g, "intervals" %=% intervals]
35
36
37 main dirname = do
38
39 frame <- readTable "coal-times.csv"
40
41 let times = frame $$ "time" :: [Double]
42
43 return $ model (1851, 1963) times