forked from mikeizbicki/hmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMMTest.hs
67 lines (52 loc) · 1.98 KB
/
HMMTest.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import HMM
import Debug.Trace
-- | utility functions
--
-- | takes the cross product of a list multiple times
listCPExp :: [a] -> Int -> [[a]]
listCPExp language order = listCPExp' order [[]]
where
listCPExp' order list
| order == 0 = list
| otherwise = listCPExp' (order-1) [symbol:l | l <- list, symbol <- language]
-- | tests
-- these should equal ~1 if our recurrence if alpha and beta are correct
forwardtest hmm x = sum [forward hmm e | e <- listCPExp (events hmm) x]
backwardtest hmm x = sum [backward hmm e | e <- listCPExp (events hmm) x]
fbtest hmm events = "fwd: " ++ show (forward hmm events) ++ " bkwd:" ++ show (backward hmm events)
verifyhmm hmm = do
check "initProbs" ip
check "transMatrix" tm
check "outMatrix" om
where check str var = do
putStrLn $ str++" tollerance check: "++show var
{- if abs(var-1)<0.0001
then putStrLn "True"
else putStrLn "False"-}
ip = sum $ [initProbs hmm s | s <- states hmm]
tm = (sum $ [transMatrix hmm s1 s2 | s1 <- states hmm, s2 <- states hmm]) -- (length $ states hmm)
om = sum $ [outMatrix hmm s e | s <- states hmm, e <- events hmm] -- / length $ states hmm
-- Test HMMs
newHMM = HMM { states=[1,2]
, events=['A','G','C','T']
, initProbs = ipTest
, transMatrix = tmTest
, outMatrix = omTest
}
ipTest s
| s == 1 = 0.1
| s == 2 = 0.9
tmTest s1 s2
| s1==1 && s2==1 = 0.9
| s1==1 && s2==2 = 0.1
| s1==2 && s2==1 = 0.5
| s1==2 && s2==2 = 0.5
omTest s e
| s==1 && e=='A' = 0.4
| s==1 && e=='G' = 0.1
| s==1 && e=='C' = 0.1
| s==1 && e=='T' = 0.4
| s==2 && e=='A' = 0.1
| s==2 && e=='G' = 0.4
| s==2 && e=='C' = 0.4
| s==2 && e=='T' = 0.1