{-# LANGUAGE FlexibleContexts #-}
module ELynx.Data.MarkovProcess.RateMatrix
( RateMatrix,
ExchangeabilityMatrix,
StationaryDistribution,
isValid,
normalizeSD,
totalRate,
totalRateWith,
normalize,
normalizeWith,
setDiagonal,
toExchangeabilityMatrix,
fromExchangeabilityMatrix,
getStationaryDistribution,
exchFromListLower,
exchFromListUpper,
)
where
import qualified Data.Vector.Storable as V
import Numeric.LinearAlgebra hiding (normalize)
import Numeric.SpecFunctions
import Prelude hiding ((<>))
type RateMatrix = Matrix R
type ExchangeabilityMatrix = Matrix R
type StationaryDistribution = Vector R
epsRelaxed :: Double
epsRelaxed :: Double
epsRelaxed = Double
1e-5
isValid :: StationaryDistribution -> Bool
isValid :: StationaryDistribution -> Bool
isValid StationaryDistribution
d = Double
epsRelaxed Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double -> Double
forall a. Num a => a -> a
abs (StationaryDistribution -> Double
forall a. Normed a => a -> Double
norm_1 StationaryDistribution
d Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1.0)
normalizeSD :: StationaryDistribution -> StationaryDistribution
normalizeSD :: StationaryDistribution -> StationaryDistribution
normalizeSD StationaryDistribution
d = StationaryDistribution
d StationaryDistribution
-> StationaryDistribution -> StationaryDistribution
forall a. Fractional a => a -> a -> a
/ Double -> StationaryDistribution
forall (c :: * -> *) e. Container c e => e -> c e
scalar (StationaryDistribution -> Double
forall a. Normed a => a -> Double
norm_1 StationaryDistribution
d)
matrixSetDiagToZero :: Matrix R -> Matrix R
matrixSetDiagToZero :: Matrix Double -> Matrix Double
matrixSetDiagToZero Matrix Double
m = Matrix Double
m Matrix Double -> Matrix Double -> Matrix Double
forall a. Num a => a -> a -> a
- StationaryDistribution -> Matrix Double
forall a. (Num a, Element a) => Vector a -> Matrix a
diag (Matrix Double -> StationaryDistribution
forall t. Element t => Matrix t -> Vector t
takeDiag Matrix Double
m)
{-# INLINE matrixSetDiagToZero #-}
totalRateWith :: StationaryDistribution -> RateMatrix -> Double
totalRateWith :: StationaryDistribution -> Matrix Double -> Double
totalRateWith StationaryDistribution
d Matrix Double
m = StationaryDistribution -> Double
forall a. Normed a => a -> Double
norm_1 (StationaryDistribution -> Double)
-> StationaryDistribution -> Double
forall a b. (a -> b) -> a -> b
$ StationaryDistribution
d StationaryDistribution -> Matrix Double -> StationaryDistribution
forall t. Numeric t => Vector t -> Matrix t -> Vector t
<# Matrix Double -> Matrix Double
matrixSetDiagToZero Matrix Double
m
totalRate :: RateMatrix -> Double
totalRate :: Matrix Double -> Double
totalRate Matrix Double
m = StationaryDistribution -> Matrix Double -> Double
totalRateWith (Matrix Double -> StationaryDistribution
getStationaryDistribution Matrix Double
m) Matrix Double
m
normalize :: RateMatrix -> RateMatrix
normalize :: Matrix Double -> Matrix Double
normalize Matrix Double
m = StationaryDistribution -> Matrix Double -> Matrix Double
normalizeWith (Matrix Double -> StationaryDistribution
getStationaryDistribution Matrix Double
m) Matrix Double
m
normalizeWith :: StationaryDistribution -> RateMatrix -> RateMatrix
normalizeWith :: StationaryDistribution -> Matrix Double -> Matrix Double
normalizeWith StationaryDistribution
d Matrix Double
m = Double -> Matrix Double -> Matrix Double
forall t (c :: * -> *). Linear t c => t -> c t -> c t
scale (Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ StationaryDistribution -> Matrix Double -> Double
totalRateWith StationaryDistribution
d Matrix Double
m) Matrix Double
m
setDiagonal :: RateMatrix -> RateMatrix
setDiagonal :: Matrix Double -> Matrix Double
setDiagonal Matrix Double
m = Matrix Double
diagZeroes Matrix Double -> Matrix Double -> Matrix Double
forall a. Num a => a -> a -> a
- StationaryDistribution -> Matrix Double
forall a. (Num a, Element a) => Vector a -> Matrix a
diag ([Double] -> StationaryDistribution
forall a. Storable a => [a] -> Vector a
fromList [Double]
rowSums)
where
diagZeroes :: Matrix Double
diagZeroes = Matrix Double -> Matrix Double
matrixSetDiagToZero Matrix Double
m
rowSums :: [Double]
rowSums = (StationaryDistribution -> Double)
-> [StationaryDistribution] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map StationaryDistribution -> Double
forall a. Normed a => a -> Double
norm_1 ([StationaryDistribution] -> [Double])
-> [StationaryDistribution] -> [Double]
forall a b. (a -> b) -> a -> b
$ Matrix Double -> [StationaryDistribution]
forall t. Element t => Matrix t -> [Vector t]
toRows Matrix Double
diagZeroes
toExchangeabilityMatrix ::
RateMatrix -> StationaryDistribution -> ExchangeabilityMatrix
toExchangeabilityMatrix :: Matrix Double -> StationaryDistribution -> Matrix Double
toExchangeabilityMatrix Matrix Double
m StationaryDistribution
f = Matrix Double
m Matrix Double -> Matrix Double -> Matrix Double
forall t. Numeric t => Matrix t -> Matrix t -> Matrix t
<> StationaryDistribution -> Matrix Double
forall a. (Num a, Element a) => Vector a -> Matrix a
diag StationaryDistribution
oneOverF
where
oneOverF :: StationaryDistribution
oneOverF = (Double -> Double)
-> StationaryDistribution -> StationaryDistribution
forall b (c :: * -> *) e.
(Element b, Container c e) =>
(e -> b) -> c e -> c b
cmap (Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/) StationaryDistribution
f
fromExchangeabilityMatrix ::
ExchangeabilityMatrix -> StationaryDistribution -> RateMatrix
fromExchangeabilityMatrix :: Matrix Double -> StationaryDistribution -> Matrix Double
fromExchangeabilityMatrix Matrix Double
em StationaryDistribution
d = Matrix Double -> Matrix Double
setDiagonal (Matrix Double -> Matrix Double) -> Matrix Double -> Matrix Double
forall a b. (a -> b) -> a -> b
$ Matrix Double
em Matrix Double -> Matrix Double -> Matrix Double
forall t. Numeric t => Matrix t -> Matrix t -> Matrix t
<> StationaryDistribution -> Matrix Double
forall a. (Num a, Element a) => Vector a -> Matrix a
diag StationaryDistribution
d
eps :: Double
eps :: Double
eps = Double
1e-12
normalizeSumVec :: V.Vector Double -> V.Vector Double
normalizeSumVec :: StationaryDistribution -> StationaryDistribution
normalizeSumVec StationaryDistribution
v = (Double -> Double)
-> StationaryDistribution -> StationaryDistribution
forall a b.
(Storable a, Storable b) =>
(a -> b) -> Vector a -> Vector b
V.map (Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
s) StationaryDistribution
v
where
s :: Double
s = StationaryDistribution -> Double
forall a. (Storable a, Num a) => Vector a -> a
V.sum StationaryDistribution
v
{-# INLINE normalizeSumVec #-}
getStationaryDistribution :: RateMatrix -> StationaryDistribution
getStationaryDistribution :: Matrix Double -> StationaryDistribution
getStationaryDistribution Matrix Double
m =
if Double
eps Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double -> Double
forall a. Num a => a -> a
abs (Complex Double -> Double
forall a. RealFloat a => Complex a -> a
magnitude (Vector (Complex Double)
eVals Vector (Complex Double) -> Int -> Complex Double
forall c t. Indexable c t => c -> Int -> t
! Int
i))
then StationaryDistribution -> StationaryDistribution
normalizeSumVec StationaryDistribution
distReal
else [Char] -> StationaryDistribution
forall a. HasCallStack => [Char] -> a
error [Char]
"getStationaryDistribution: Could not retrieve stationary distribution."
where
(Vector (Complex Double)
eVals, Matrix (Complex Double)
eVecs) = Matrix Double -> (Vector (Complex Double), Matrix (Complex Double))
forall t.
Field t =>
Matrix t -> (Vector (Complex Double), Matrix (Complex Double))
eig (Matrix Double -> Matrix Double
forall m mt. Transposable m mt => m -> mt
tr Matrix Double
m)
i :: IndexOf Vector
i = Vector (Complex Double) -> IndexOf Vector
forall (c :: * -> *) e. Container c e => c e -> IndexOf c
minIndex Vector (Complex Double)
eVals
distComplex :: Vector (Complex Double)
distComplex = Matrix (Complex Double) -> [Vector (Complex Double)]
forall t. Element t => Matrix t -> [Vector t]
toColumns Matrix (Complex Double)
eVecs [Vector (Complex Double)] -> Int -> Vector (Complex Double)
forall a. [a] -> Int -> a
!! Int
i
distReal :: StationaryDistribution
distReal = (Complex Double -> Double)
-> Vector (Complex Double) -> StationaryDistribution
forall b (c :: * -> *) e.
(Element b, Container c e) =>
(e -> b) -> c e -> c b
cmap Complex Double -> Double
forall a. Complex a -> a
realPart Vector (Complex Double)
distComplex
ijToKLower :: Int -> Int -> Int
ijToKLower :: Int -> Int -> Int
ijToKLower Int
i Int
j
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
j = Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Int
i Int -> Int -> Double
`choose` Int
2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
j
| Bool
otherwise = [Char] -> Int
forall a. HasCallStack => [Char] -> a
error [Char]
"ijToKLower: not defined for upper triangular matrix."
ijToKUpper :: Int -> Int -> Int -> Int
ijToKUpper :: Int -> Int -> Int -> Int
ijToKUpper Int
n Int
i Int
j
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
j = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Int
i Int -> Int -> Double
`choose` Int
2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
| Bool
otherwise = [Char] -> Int
forall a. HasCallStack => [Char] -> a
error [Char]
"ijToKUpper: not defined for lower triangular matrix."
fromListBuilderLower :: RealFrac a => [a] -> a -> a -> a
fromListBuilderLower :: [a] -> a -> a -> a
fromListBuilderLower [a]
es a
i a
j
| a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
j = [a]
es [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int -> Int -> Int
ijToKLower Int
iI Int
jI
| a
i a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
j = a
0.0
| a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
j = [a]
es [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int -> Int -> Int
ijToKLower Int
jI Int
iI
| Bool
otherwise =
[Char] -> a
forall a. HasCallStack => [Char] -> a
error
[Char]
"Float indices could not be compared during matrix creation."
where
iI :: Int
iI = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round a
i :: Int
jI :: Int
jI = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round a
j :: Int
fromListBuilderUpper :: RealFrac a => Int -> [a] -> a -> a -> a
fromListBuilderUpper :: Int -> [a] -> a -> a -> a
fromListBuilderUpper Int
n [a]
es a
i a
j
| a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
j = [a]
es [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int -> Int -> Int -> Int
ijToKUpper Int
n Int
iI Int
jI
| a
i a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
j = a
0.0
| a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
j = [a]
es [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int -> Int -> Int -> Int
ijToKUpper Int
n Int
jI Int
iI
| Bool
otherwise =
[Char] -> a
forall a. HasCallStack => [Char] -> a
error
[Char]
"Float indices could not be compared during matrix creation."
where
iI :: Int
iI = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round a
i :: Int
jI :: Int
jI = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round a
j :: Int
checkEs :: RealFrac a => Int -> [a] -> [a]
checkEs :: Int -> [a] -> [a]
checkEs Int
n [a]
es
| [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
es Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
nExp = [a]
es
| Bool
otherwise = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error [Char]
eStr
where
nExp :: Int
nExp = Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Int
n Int -> Int -> Double
`choose` Int
2)
eStr :: [Char]
eStr =
[[Char]] -> [Char]
unlines
[ [Char]
"exchFromListlower: the number of exchangeabilities does not match the matrix size",
[Char]
"matrix size: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n,
[Char]
"expected number of exchangeabilities: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
nExp,
[Char]
"received number of exchangeabilities: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
es)
]
exchFromListLower :: (RealFrac a, Container Vector a) => Int -> [a] -> Matrix a
exchFromListLower :: Int -> [a] -> Matrix a
exchFromListLower Int
n [a]
es = (Int, Int) -> (a -> a -> a) -> Matrix a
forall d f (c :: * -> *) e. Build d f c e => d -> f -> c e
build (Int
n, Int
n) ([a] -> a -> a -> a
forall a. RealFrac a => [a] -> a -> a -> a
fromListBuilderLower (Int -> [a] -> [a]
forall a. RealFrac a => Int -> [a] -> [a]
checkEs Int
n [a]
es))
exchFromListUpper :: (RealFrac a, Container Vector a) => Int -> [a] -> Matrix a
exchFromListUpper :: Int -> [a] -> Matrix a
exchFromListUpper Int
n [a]
es = (Int, Int) -> (a -> a -> a) -> Matrix a
forall d f (c :: * -> *) e. Build d f c e => d -> f -> c e
build (Int
n, Int
n) (Int -> [a] -> a -> a -> a
forall a. RealFrac a => Int -> [a] -> a -> a -> a
fromListBuilderUpper Int
n (Int -> [a] -> [a]
forall a. RealFrac a => Int -> [a] -> [a]
checkEs Int
n [a]
es))