{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts, DeriveGeneric, DeriveAnyClass,
DerivingStrategies, GeneralizedNewtypeDeriving, FlexibleInstances, TypeOperators #-}
module Markov.Examples ( FromMatrix (..)
, Simple (..)
, Urn (..)
, Extinction (..)
, Tidal (..)
, Room (..)
, FillBin
, initial
, expectedLoss
) where
import Markov
import Generics.Deriving (Generic)
import Data.Discrimination (Grouping)
newtype FromMatrix = FromMatrix Char
deriving Generic
deriving newtype (Eq, Show)
deriving anyclass Grouping
instance Markov (Product Double) FromMatrix where
transition = let mat = [ [0.4, 0.3, 0.3]
, [0.2, 0.1, 0.7]
, [0.9, 0.1, 0.0] ]
chars = map FromMatrix ['a','t','l']
in fromLists mat chars
newtype Simple = Simple Int
deriving Generic
deriving newtype (Num, Enum, Eq, Ord, Show)
deriving anyclass Grouping
instance Markov0 Simple where
transition0 _ = [pred, succ]
instance Markov (Product Double) Simple where
transition _ = [ 0.5 >*< pred
, 0.5 >*< succ ]
instance Markov (Product Int) Simple where
transition _ = [ 1 >*< pred
, 1 >*< succ ]
instance Markov (Sum Int) Simple where
transition _ = [ 1 >*< pred
, 0 >*< id
, 0 >*< succ ]
newtype Urn = Urn (Int,Int)
deriving Generic
deriving newtype (Eq, Ord, Show)
deriving anyclass Grouping
instance Markov (Product Double) Urn where
transition x = [ probLeft x >*< addLeft
, 1 - probLeft x >*< addRight ]
addLeft :: Urn -> Urn
addLeft (Urn (a,b)) = Urn (a+1,b)
addRight :: Urn -> Urn
addRight (Urn (a,b)) = Urn (a,b+1)
probLeft :: Fractional a => Urn -> a
probLeft (Urn (a,b)) = (fromIntegral a)/(fromIntegral $ a + b)
newtype Extinction = Extinction Int
deriving Generic
deriving newtype (Eq, Num, Show)
deriving anyclass Grouping
instance Markov (Sum Int, Product Rational) Extinction where
transition x = case x of
0 -> [ 0 >*< (q+r) >*< id
, 0 >*< s >*< (+1) ]
_ -> [ 1 >*< q >*< const 0
, 0 >*< r >*< id
, 0 >*< s >*< (+1) ]
where q = 0.1; r = 0.3; s = 0.6
instance Combine Extinction where
combine = const
instance Semigroup Extinction where
(<>) = flip const
instance MultiMarkov (Sum Int :* Product Rational :* Extinction) where
multiTransition _ = [trans]
where trans ((_,_),z) = case z of
0 -> [ 0 >*< (q+r) >*< 0
, 0 >*< s >*< 1 ]
x -> [ 1 >*< q >*< 0
, 0 >*< r >*< x
, 0 >*< s >*< x+1 ]
where q = 0.1; r = 0.3; s = 0.6
data Tidal = Tidal { time :: Double
, position :: Int }
deriving (Eq, Ord, Show, Generic)
deriving anyclass Grouping
instance Markov (Product Double) Tidal where
transition tw = [ probRight tw >*< stepPos (+1)
, 1 - (probRight tw) >*< stepPos (flip (-) 1) ]
stepPos :: (Int -> Int) -> Tidal -> Tidal
stepPos f tw = Tidal (time tw + 1) (f $ position tw)
probRight :: Tidal -> Product Double
probRight tw = Product $ timeBias * positionBias
where timeBias = (1 + sin (2 * pi * (time tw) / stepsPerCycle))/2
positionBias
| position tw >= 0 = 1 / steepness
| otherwise = 1
stepsPerCycle = 10
steepness = 1.3
newtype Room = Room Int
deriving (Generic, Show)
deriving newtype (Eq, Num)
deriving anyclass Grouping
instance Semigroup Room where
(<>) = flip const
instance Combine Room where
combine = const
instance MultiMarkov (Product Rational :* Merge String :* Room) where
multiTransition _ = [giveToken, changeState]
where changeState ((_,_),z) = case z of
1 -> [ 0.3 >*< mempty >*< 1
, 0.6 >*< mempty >*< 2
, 0.1 >*< mempty >*< 3 ]
2 -> [ 1.0 >*< mempty >*< 3 ]
3 -> [ 0.3 >*< mempty >*< 1
, 0.6 >*< mempty >*< 2
, 0.1 >*< mempty >*< 3 ]
_ -> error "State out of bounds in transitionk"
giveToken ((_,_),z) = case z of
1 -> [ 0.5 >*< Merge "a" >*< 1
, 0.5 >*< Merge "b" >*< 1 ]
2 -> [ 0.3 >*< Merge "a" >*< 2
, 0.7 >*< Merge "b" >*< 2 ]
3 -> [ 0.4 >*< Merge "a" >*< 3
, 0.4 >*< Merge "b" >*< 3
, 0.2 >*< Merge "c" >*< 3 ]
_ -> error "State out of bounds in transitionk"
type Bin = (Open,Full)
type Index = Int
type Gap = Int
type Full = Int
type Open = Int
type Trans = FillBin -> FillBin
data FillBin = End Gap | Ext Gap Bin FillBin deriving (Eq, Ord, Generic, Grouping)
instance Show FillBin where
show (Ext g b s) = show g ++ " " ++ show b ++ " " ++ show s
show (End g) = show g
instance Markov (Product Double) FillBin where
transition x = case probId x of
0 -> filter (\(Product y,_) -> y /= 0)
$ [probAdd i x >*< addItem i | i <- indices]
++ [probGrowL i x >*< addItem i . growLeft j i
| i <- indices, j <- [1..gapN (i-1) x]]
++ [probGrowR i x >*< addItem i . growRight j i
| i <- indices, j <- [1..gapN i x]]
1 -> [pure id]
_ -> error "Pattern not matched in transition"
where indices = [1..size x]
fBFromLists :: [Gap] -> [Bin] -> FillBin
fBFromLists gaps bins = case (gaps,bins) of
(g:_ , [] ) -> End g
([g] , _ ) -> End g
(g:gs , b:bs) -> Ext g b $ fBFromLists gs bs
([] , _ ) -> End 0
initial :: [Int] -> FillBin
initial gs = fBFromLists gs $ repeat (0,0)
size :: FillBin -> Int
size x = case x of
End _ -> 0
Ext _ _ s -> 1 + size s
getBins :: FillBin -> [Bin]
getBins x = case x of
End _ -> []
Ext _ b s -> b:getBins s
getOpen :: FillBin -> [Open]
getOpen x = map fst $ getBins x
openN :: Index -> FillBin -> Open
openN i x = (getOpen x)!!(i-1)
getFull :: FillBin -> [Full]
getFull x = map snd $ getBins x
fullN :: Index -> FillBin -> Full
fullN i x = (getFull x)!!(i-1)
getGap :: FillBin -> [Gap]
getGap x = case x of
End g -> [g]
Ext g _ s -> g:getGap s
gapN :: Index -> FillBin -> Gap
gapN i x = (getGap x)!!i
iApply :: Trans -> Index -> Trans
iApply f idx x = case (idx,x) of
(1, y) -> f y
(i, Ext g b s) -> Ext g b $ iApply f (i-1) s
_ -> error "Pattern not matched in iApply"
addItem :: Index -> Trans
addItem = iApply h
where h (Ext g (o,f) s) = Ext g (o-1,f+1) s
h _ = error "pattern not matched in h in addItem"
growLeft :: Int -> Index -> Trans
growLeft j = iApply h
where h (Ext g (o,f) s) = Ext (g-j) (o+j,f) s
h _ = error "pattern not matched in h in growLeft"
growRight :: Int -> Index -> Trans
growRight j = iApply h
where h (Ext g (o,f) s) = Ext g (o+j,f) (shrink s)
h _ = error "pattern not matched in h in growRight"
shrink s = case s of
End g -> End (g-j)
Ext g b t -> Ext (g-j) b t
slots :: FillBin -> Int
slots x = sum $ getGap x ++ getOpen x
probId :: Num a => FillBin -> a
probId x = case slots x == 0 of
True -> 1
False -> 0
divInt :: (Integral a, Integral b, Fractional c) => a -> b -> c
divInt x y = (fromIntegral x)/(fromIntegral y)
probAdd :: Fractional a => Index -> FillBin -> a
probAdd i x = openN i x `divInt` slots x
probGrowL :: Fractional a => Index -> FillBin -> a
probGrowL i x = case test of
True -> 1 `divInt` slots x
False -> 0
where test = i == 1 || fullN i x < fullN (i-1) x
probGrowR :: Fractional a => Index -> FillBin -> a
probGrowR i x = case test of
True -> 1 `divInt` slots x
False -> 0
where test = i == size x || fullN i x <= fullN (i+1) x
individualLoss :: Fractional a => FillBin -> a
individualLoss x = sum . map f . getFull $ x
where f y = (fromIntegral y - ideal)^2
ideal = sum (getFull x) `divInt` size x
probLoss :: Fractional a => (Product a, FillBin) -> a
probLoss (Product x, y) = x * individualLoss y
expectedLoss :: (Fractional a, Markov (Product a) FillBin) => [Product a :* FillBin] -> a
expectedLoss xs = sum . map probLoss $ (chain xs) !! idx
where idx = slots . snd . head $ xs