-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbabilities.hs
53 lines (43 loc) · 1.54 KB
/
Probabilities.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
module Chanel.BEC.Probabilities
( probability,
toProbability,
frozenBits,
)
where
import Data.List (sort)
data Tree a
= Branch (Tree a) (Tree a)
| Leaf a
newtype Probability a = P a
-- | Wrap the value to @Probability@,
-- and check that the value is in the range [0, 1].
toProbability :: (Fractional a, Ord a) => a -> Probability a
toProbability v
| (0 <= v) && (v <= 1) = P v
| otherwise = error "Probability should be in the range [0, 1]"
growTree :: (Fractional a) => Tree a -> Tree a
growTree (Leaf v) = Branch (Leaf (v ^^ (2 :: Int))) (Leaf (2 * v - v ^^ (2 :: Int)))
growTree (Branch l r) = Branch (growTree l) (growTree r)
toList :: (Fractional a) => Tree a -> [a]
toList (Leaf v) = [v]
toList (Branch l r) = toList l ++ toList r
probabilityTree :: (Fractional a) => Int -> a -> Tree a
probabilityTree 0 = Leaf
probabilityTree n = growTree . probabilityTree (n - 1)
-- | Returns the erasure probabilities for each bit.
probability :: (Fractional a) => Probability a -> Int -> [a]
probability (P prob) n = toList (probabilityTree n prob)
getKMin :: (Ord a) => [a] -> Int -> [a]
getKMin list k = take k (reverse . sort $ list)
-- | Returns the numbers of frozen bits.
frozenBits ::
(Ord a, Fractional a) =>
Probability a -> -- Probability of erasure
Int -> -- N - code length
Int -> -- K - dimension
[Int] --
frozenBits prob n k =
let lengthFrozen = (2 ^ n - 2 ^ k)
probabilities = reverse (probability prob n) `zip` ([0 ..] :: [Int])
frozenNums = map snd (getKMin probabilities lengthFrozen)
in sort frozenNums