-
Notifications
You must be signed in to change notification settings - Fork 0
/
Probability.hs
699 lines (504 loc) · 17 KB
/
Probability.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
-- Credits: Erwig, Martin and Kollmannsberger, Steve
-- FUNCTIONAL PEARLS: Probabilistic functional programming in Haskell,
-- JFP, 2006
-- DOI: 10.1017/S0956796805005721
module Probability where
import qualified System.Random
import Data.List (sort,sortBy,transpose)
import Control.Monad
import System.IO.Unsafe (unsafePerformIO)
import ListUtils
import Show
{- TO DO:
* create export list
* extend Dist by a constructor for continuous distributions:
C (Float -> Float)
* prove correctness of |||
-}
--------- jno (to be checked) --------------
import Control.Applicative
instance Applicative Dist where
pure = return
(<*>) = ap
instance Alternative Dist where
empty = D []
(<|>) = (>>)
--------- end jno --------------------------
------------------------------------------------------------------------------
-- CONTENTS:
--
-- 0 AUXILIARY DEFINITIONS
-- 1 DETERMINISTIC AND PROBABILISTIC VALUES
-- 2 RANDOMIZED VALUES
-- 3 DETERMINISTIC AND PROBABILISTIC GENERATORS
-- 4 RANDOMIZED GENERATORS
-- 5 ITERATORS AND SIMULATORS
-- 6 TRACING
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- 0 AUXILIARY DEFINITIONS
--
-- Event
-- Probability
------------------------------------------------------------------------------
--
-- Events
--
type Event a = a -> Bool
oneOf :: Eq a => [a] -> Event a
oneOf = flip elem
just :: Eq a => a -> Event a
just = oneOf . singleton
--
-- Probabilities
--
newtype Probability = P ProbRep
type ProbRep = Float
precision :: Int
precision = 1
showPfix :: ProbRep -> String
showPfix f | precision==0 = showR 3 (round (f*100))++"%"
| otherwise = showR (4+precision) (fromIntegral (round (f*100*d))/d)++"%"
where d = 10^precision
-- -- mixed precision
-- --
-- showP :: ProbRep -> String
-- showP f | f>=0.1 = showR 3 (round (f*100))++"%"
-- | otherwise = show (f*100)++"%"
-- fixed precision
--
showP :: ProbRep -> String
showP = showPfix
instance Show Probability where
show (P p) = showP p
errorMargin :: ProbRep
errorMargin = 0.00001
--
-- Monad composition
--
-- (>@>) binary composition
-- sequ composition of a list of monadic functions
--
(>@>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
f >@> g = (>>= g) . f
sequ :: Monad m => [a -> m a] -> a -> m a
sequ = foldl (>@>) return
------------------------------------------------------------------------------
-- 1 DETERMINISTIC AND PROBABILISTIC VALUES
--
-- Dist probability disribution
-- Spread functions to convert a list of values into a distribution
------------------------------------------------------------------------------
--
-- Distributions
--
newtype Dist a = D {unD :: [(a,ProbRep)]}
instance Monad Dist where
return x = D [(x,1)]
d >>= f = D [(y,q*p) | (x,p) <- unD d, (y,q) <- unD (f x)]
-- note: mzero is a zero for >>= and a unit for mplus
--
instance MonadPlus Dist where
mzero = D []
mplus d d' | isZero d || isZero d' = mzero
| otherwise = unfoldD $ choose 0.5 d d'
isZero :: Dist a -> Bool
isZero (D d) = null d
instance Functor Dist where
fmap f (D d) = D [(f x,p) | (x,p) <- d]
instance (Ord a,Eq a) => Eq (Dist a) where
D xs == D ys = map fst (norm' xs)==map fst (norm' ys) &&
all (\((_,p),(_,q))->abs (p-q)<errorMargin) (zip (norm' xs) (norm' ys))
-- auxiliary functions for constructing and working with distributions
--
onD :: ([(a,ProbRep)] -> [(a,ProbRep)]) -> Dist a -> Dist a
onD f = D . f . unD
sizeD :: Dist a -> Int
sizeD = length . unD
checkD :: Dist a -> Dist a
checkD (D d) | abs (1-sumP d) < errorMargin = D d
| otherwise = error ("Illegal distribution: total probability = "++show (sumP d))
mkD :: [(a,ProbRep)] -> Dist a
mkD = checkD . D
sumP :: [(a,ProbRep)] -> ProbRep
sumP = sum . map snd
sortP :: [(a,ProbRep)] -> [(a,ProbRep)]
sortP = sortBy (\x y->compare (snd y) (snd x))
-- normalization = grouping
--
normBy :: Ord a => (a -> a -> Bool) -> Dist a -> Dist a
normBy f = onD $ accumBy f . sort
accumBy :: Num b => (a -> a -> Bool) -> [(a,b)] -> [(a,b)]
accumBy f ((x,p):ys@((y,q):xs)) | f x y = accumBy f ((x,p+q):xs)
| otherwise = (x,p):accumBy f ys
accumBy _ xs = xs
norm :: Ord a => Dist a -> Dist a
norm = normBy (==)
norm' :: Ord a => [(a,ProbRep)] -> [(a,ProbRep)]
norm' = accumBy (==) . sort
-- pretty printing
--
instance (Ord a,Show a) => Show (Dist a) where
show (D []) = "Impossible"
show (D xs) = concatMap (\(x,p)->showR w x++' ':showP p++"\n") (sortP (norm' xs))
where w = maximum (map (length.show.fst) xs)
--
-- Operations on distributions
--
-- product of independent distributions
--
joinWith :: (a -> b -> c) -> Dist a -> Dist b -> Dist c
joinWith f (D d) (D d') = D [ (f x y,p*q) | (x,p) <- d , (y,q) <- d' ]
prod :: Dist a -> Dist b -> Dist (a,b)
prod = joinWith (,)
-- distribution generators
--
type Spread a = [a] -> Dist a
certainly :: Trans a
certainly = return
impossible :: Dist a
impossible = mzero
choose :: ProbRep -> a -> a -> Dist a
choose p x y = enum [p,1-p] [x,y]
enum :: [ProbRep] -> Spread a
enum ps xs = mkD $ zip xs ps
enumPC :: [ProbRep] -> Spread a
enumPC ps = enum (map (/100) ps)
relative :: [Int] -> Spread a
relative ns = enum (map (\n->fromIntegral n/fromIntegral (sum ns)) ns)
shape :: (Float -> Float) -> Spread a
shape _ [] = impossible
shape f xs = scale (zip xs ps)
where incr = 1 / fromIntegral ((length xs) - 1)
ps = map f (iterate (+incr) 0)
linear :: Float -> Spread a
linear c = shape (c*)
uniform :: Spread a
uniform = shape (const 1)
negexp :: Spread a
negexp = shape (\x -> exp (-x))
normal :: Spread a
normal = shape (normalCurve 0.5 0.5)
normalCurve :: Float -> Float -> Float -> Float
normalCurve mean stddev x = 1 / sqrt (2 * pi) * exp (-1/2 * u^2)
where u = (x - mean) / stddev
-- extracting and mapping the domain of a distribution
--
extract :: Dist a -> [a]
extract = map fst . unD
mapD :: (a -> b) -> Dist a -> Dist b
mapD = fmap
-- unfold a distribution of distributions into one distribution
--
unfoldD :: Dist (Dist a) -> Dist a
unfoldD (D d) = D [ (x,p*q) | (d',q) <- d, (x,p) <- unD d' ]
-- conditional distribution
--
cond :: Dist Bool -> Dist a -> Dist a -> Dist a
cond b d d' = unfoldD $ choose p d d'
where P p = truth b
truth :: Dist Bool -> Probability
truth (D ((b,p):_)) = P (if b then p else 1-p)
-- conditional probability
--
(|||) :: Dist a -> Event a -> Dist a
(|||) = flip filterD
-- filtering distributions
--
data Select a = Case a | Other
deriving (Eq,Ord,Show)
above :: Ord a => ProbRep -> Dist a -> Dist (Select a)
above p (D d) = D (map (\(x,q)->(Case x,q)) d1++[(Other,sumP d2)])
where (d1,d2) = span (\(_,q)->q>=p) (sortP (norm' d))
scale :: [(a,ProbRep)] -> Dist a
scale xs = D (map (\(x,p)->(x,p/q)) xs)
where q = sumP xs
filterD :: (a -> Bool) -> Dist a -> Dist a
filterD p = scale . filter (p . fst) . unD
-- selecting from distributions
--
selectP :: Dist a -> ProbRep -> a
selectP (D d) p = scanP p d
scanP :: ProbRep -> [(a,ProbRep)] -> a
scanP p ((x,q):ps) | p<=q || null ps = x
| otherwise = scanP (p-q) ps
infix 8 ??
(??) :: Event a -> Dist a -> Probability
(??) p = P . sumP . filter (p . fst) . unD
-- TO DO: generalize Float to arbitrary Num type
--
class ToFloat a where
toFloat :: a -> Float
instance ToFloat Float where toFloat = id
instance ToFloat Int where toFloat = fromIntegral
instance ToFloat Integer where toFloat = fromIntegral
class FromFloat a where
fromFloat :: Float -> a
instance FromFloat Float where fromFloat = id
instance FromFloat Int where fromFloat = round
instance FromFloat Integer where fromFloat = round
-- expected :: ToFloat a => Dist a -> Float
-- expected = sum . map (\(x,p)->toFloat x*p) . unD
class Expected a where
expected :: a -> Float
-- instance ToFloat a => Expected a where
-- expected = toFloat
instance Expected Float where expected = id
instance Expected Int where expected = toFloat
instance Expected Integer where expected = toFloat
instance Expected a => Expected [a] where
expected xs = sum (map expected xs) / toFloat (length xs)
instance Expected a => Expected (Dist a) where
expected = sum . map (\(x,p)->expected x*p) . unD
instance Expected a => Expected (IO a) where
expected r = expected (System.IO.Unsafe.unsafePerformIO r)
-- statistical analyses
--
variance :: Expected a => Dist a -> Float
variance d@(D ps) = sum $ map (\(x,p)->p*sqr (expected x - ex)) ps
where sqr x = x * x
ex = expected d
stddev :: Expected a => Dist a -> Float
stddev = sqrt . variance
------------------------------------------------------------------------------
-- 2 RANDOMIZED VALUES
--
-- R random value
-- RDist random distribution
------------------------------------------------------------------------------
--
-- Random values
--
type R a = IO a
printR :: Show a => R a -> R ()
printR = (>>= print)
instance Show (IO a) where
show _ = ""
pick :: Dist a -> R a
-- pick d = do {p <- Random.randomRIO (0,1); return (selectP p d)}
pick d = System.Random.randomRIO (0,1) >>= return . selectP d
--
-- Randomized distributions
--
type RDist a = R (Dist a)
rAbove :: Ord a => ProbRep -> RDist a -> RDist (Select a)
rAbove p rd = do D d <- rd
let (d1,d2) = span (\(_,q)->q>=p) (sortP (norm' d))
return (D (map (\(x,q)->(Case x,q)) d1++[(Other,sumP d2)]))
------------------------------------------------------------------------------
-- 3 DETERMINISTIC AND PROBABILISTIC GENERATORS
--
-- Change deterministic generator
-- Trans probabilistic generator
-- SpreadC functions to convert a list of changes into a transition
-- SpreadT functions to convert a list of transitions into a transition
------------------------------------------------------------------------------
--
-- transitions
--
type Change a = a -> a
type Trans a = a -> Dist a
idT :: Trans a
idT = certainlyT id
-- mapT maps a change function to the result of a transformation
-- (mapT is somehow a lifted form of mapD)
-- The restricted type of f results from the fact that the
-- argument to t cannot be changed to b in the result Trans type.
--
mapT :: Change a -> Trans a -> Trans a
mapT f t = mapD f . t
-- unfold a distribution of transitions into one transition
--
-- NOTE: The argument transitions must be independent
--
unfoldT :: Dist (Trans a) -> Trans a
unfoldT (D d) x = D [ (y,p*q) | (f,p) <- d, (y,q) <- unD (f x) ]
-- spreading changes into transitions
--
type SpreadC a = [Change a] -> Trans a
certainlyT :: Change a -> Trans a
certainlyT f = certainly . f
-- certainlyT = (certainly .)
-- certainlyT = maybeC 1
maybeT :: ProbRep -> Change a -> Trans a
maybeT p f = enumT [p,1-p] [f,id]
liftC :: Spread a -> [Change a] -> Trans a
liftC s cs x = s [f x | f <- cs]
-- liftC s cs x = s $ map ($ x) cs
uniformT = liftC uniform
normalT = liftC normal
linearT c = liftC (linear c)
enumT xs = liftC (enum xs)
-- spreading transitions into transitions
--
type SpreadT a = [Trans a] -> Trans a
liftT :: Spread (Trans a) -> [Trans a] -> Trans a
liftT s = unfoldT . s
uniformTT = liftT uniform
normalTT = liftT normal
linearTT c = liftT (linear c)
enumTT xs = liftT (enum xs)
------------------------------------------------------------------------------
-- 4 RANDOMIZED GENERATORS
--
-- RChange random change
-- RTrans random transition
------------------------------------------------------------------------------
--
-- Randomized changes
--
type RChange a = a -> R a
random :: Trans a -> RChange a
random t = pick . t
-- random = (pick .)
--
-- Randomized transitions
--
type RTrans a = a -> RDist a
type ApproxDist a = R [a]
-- rDist converts a list of randomly generated values into
-- a distribution by taking equal weights for all values
--
rDist :: Ord a => [R a] -> RDist a
rDist = fmap (norm . uniform) . sequence
------------------------------------------------------------------------------
-- 5 ITERATION AND SIMULATION
--
-- Iterate class defining *.
-- Sim class defining ~.
------------------------------------------------------------------------------
{-
Naming convention:
* takes n :: Int and a generator and iterates the generator n times
. produces a single result
.. produces a trace
~ takes k :: Int [and n :: Int] and a generator and simulates
the [n-fold repetition of the] generator k times
n *. t iterates t and produces a distribution
n *.. t iterates t and produces a trace
k ~. t simulates t and produces a distribution
(k,n) ~*. t simulates the n-fold repetition of t and produces a distribution
(k,n) ~.. t simulates the n-fold repetition of t and produces a trace
-}
-- Iteration captures three iteration strategies:
-- iter builds an n-fold composition of a (randomized) transition
-- while and until implement conditional repetitions
--
-- The class Iterate allows the overloading of iteration for different
-- kinds of generators, namely transitions and random changes:
--
-- Trans a = a -> Dist a ==> c = Dist
-- RChange a = a -> R a ==> c = R = IO
--
class Iterate c where
(*.) :: Int -> (a -> c a) -> (a -> c a)
while :: (a -> Bool) -> (a -> c a) -> (a -> c a)
until :: (a -> Bool) -> (a -> c a) -> (a -> c a)
until p = while (not.p)
infix 8 *.
-- iteration of transitions
--
instance Iterate Dist where
n *. t = head . (n *.. t)
while p t x = if p x then t x >>= while p t else certainly x
-- iteration of random changes
--
instance Iterate IO where
n *. r = (>>= return . head) . rWalk n r
while p t x = do {l <- t x; if p l then while p t l else return l}
-- Simulation means to repeat a random chage many times and
-- to accumulate all results into a distribution. Therefore,
-- simulation can be regarded as an approximation of distributions
-- through randomization.
--
-- The Sim class contains two functions:
--
-- ~. returns the final randomized transition
-- ~.. returns the whole trace
--
-- The Sim class allows the overloading of simulation for different
-- kinds of generators, namely transitions and random changes:
--
-- Trans a = a -> Dist a ==> c = Dist
-- RChange a = a -> R a ==> c = R = IO
--
class Sim c where
(~.) :: Ord a => Int -> (a -> c a) -> RTrans a
(~..) :: Ord a => (Int,Int) -> (a -> c a) -> RExpand a
(~*.) :: Ord a => (Int,Int) -> (a -> c a) -> RTrans a
infix 6 ~.
infix 6 ~..
-- simulation for transitions
--
instance Sim Dist where
(~.) x = (~.) x . random
(~..) x = (~..) x . random
(~*.) x = (~*.) x . random
-- simulation for random changes
--
instance Sim IO where
(~.) n t = rDist . replicate n . t
(~..) (k,n) t = mergeTraces . replicate k . rWalk n t
(~*.) (k,n) t = k ~. n *. t
infix 8 ~*.
--(~*.) :: (Iterate c,Sim c,Ord a) => (Int,Int) -> (a -> c a) -> RTrans a
--(k,n) ~*. t =
------------------------------------------------------------------------------
-- 7 TRACING
--
-- (R)Trace
-- (R)Space
-- (R)Walk
-- (R)Expand
------------------------------------------------------------------------------
type Trace a = [a]
type Space a = Trace (Dist a)
type Walk a = a -> Trace a
type Expand a = a -> Space a
-- >>: composes the result of a transition with a space
-- (transition is composed on the left)
--
-- (a -> m a) -> (a -> [m a]) -> (a -> [m a])
(>>:) :: Trans a -> Expand a -> Expand a
f >>: g = \x -> let ds@(D d:_)=g x in
D [ (z,p*q) | (y,p) <- d, (z,q) <- unD (f y)]:ds
infix 6 >>:
-- walk is a bounded version of the predefined function iterate
--
walk :: Int -> Change a -> Walk a
walk n f = take n . iterate f
-- *.. is identical to *., but returns the list of all intermediate
-- distributions
--
(*..) :: Int -> Trans a -> Expand a
0 *.. _ = singleton . certainly
1 *.. t = singleton . t
n *.. t = t >>: (n-1) *.. t
infix 8 *..
type RTrace a = R (Trace a)
type RSpace a = R (Space a)
type RWalk a = a -> RTrace a
type RExpand a = a -> RSpace a
-- (a -> m a) -> (a -> m [a]) -> (a -> m [a])
composelR :: RChange a -> RWalk a -> RWalk a
composelR f g x = do {rs@(r:_) <- g x; s <- f r; return (s:rs)}
-- rWalk computes a list of values by
-- randomly selecting one value from a distribution in each step.
--
rWalk :: Int -> RChange a -> RWalk a
rWalk 0 _ = return . singleton
rWalk 1 t = (>>= return . singleton) . t
rWalk n t = composelR t (rWalk (n-1) t)
-- mergeTraces converts a list of RTraces, into a list of randomized
-- distributions, i.e., an RSpace, by creating a randomized
-- distribution for each list position across all traces
--
mergeTraces :: Ord a => [RTrace a] -> RSpace a
mergeTraces = fmap (zipListWith (norm . uniform)) . sequence
where
zipListWith :: ([a] -> b) -> [[a]] -> [b]
zipListWith f = map f . transpose
{-
LAWS
const . pick = random . const
-}