```python
from sympy.stats import DiscreteMarkovChain, TransitionMatrixOf
from sympy import Matrix, MatrixSymbol, Eq
from sympy.stats import P
```


```python
T = Matrix([[0.5, 0.2, 0.3],[0.2, 0.5, 0.3],[0.2, 0.3, 0.5]]) #Create a transition matrix (columns must add to one)
T
```




$\displaystyle \left[\begin{matrix}0.5 & 0.2 & 0.3\\0.2 & 0.5 & 0.3\\0.2 & 0.3 & 0.5\end{matrix}\right]$




```python
Y = DiscreteMarkovChain("Y", [0, 1, 2], T) #add a set of state labels 0,1,2
Y
```




$\displaystyle DiscreteMarkovChain\left(Y, \left\{0, 1, 2\right\}, \left[\begin{matrix}0.5 & 0.2 & 0.3\\0.2 & 0.5 & 0.3\\0.2 & 0.3 & 0.5\end{matrix}\right]\right)$




```python
YS = DiscreteMarkovChain("YS") #initializes an empty markov chain with symbolic name YS
YS

```




$\displaystyle DiscreteMarkovChain\left(YS, \mathbb{R}, \text{None}\right)$




```python
Y.state_space #print the state space of the Markov Chain
```




$\displaystyle \left\{0, 1, 2\right\}$




```python
Y.transition_probabilities #print the transition matrix
```




$\displaystyle \left[\begin{matrix}0.5 & 0.2 & 0.3\\0.2 & 0.5 & 0.3\\0.2 & 0.3 & 0.5\end{matrix}\right]$




```python
TS = MatrixSymbol('T', 3, 3)
Matrix(TS) #create 3 x 3 matrix that is a copy of matrix T
```




$\displaystyle \left[\begin{array}{ccc}T_{0, 0} & T_{0, 1} & T_{0, 2}\\T_{1, 0} & T_{1, 1} & T_{1, 2}\\T_{2, 0} & T_{2, 1} & T_{2, 2}\end{array}\right]$




```python
trans_of_YS = TransitionMatrixOf(YS, TS) #Associates to Markov Chain YS the transition matrix TS
```


```python
#what is the formula to compute the probability that being at state 1 at cycle 1 (YS[1]), we will be at state 2 at cycle 3 (YS(3)])?
P(Eq(YS[3], 2),( Eq(YS[1], 1) & trans_of_YS ) ) 

```




$\displaystyle T_{0, 2} T_{1, 0} + T_{1, 1} T_{1, 2} + T_{1, 2} T_{2, 2}$




```python
#excuting the multiplication we obtain
P(Eq(Y[3], 2), Eq(Y[1], 1)).round(2)

```




$\displaystyle 0.36$




```python
#what is formula for computing the probability that: being at state 1 at cycle 1 (YS[1]), we will be at state 2 at cycle 3 (YS(4)])?
P(Eq(YS[5], 2),( Eq(YS[1], 1) & trans_of_YS ) ) 

```




$\displaystyle \left(\left(T_{0, 0} T_{1, 0} + T_{1, 0} T_{1, 1} + T_{1, 2} T_{2, 0}\right) T_{0, 0} + \left(T_{0, 1} T_{1, 0} + T_{1, 1}^{2} + T_{1, 2} T_{2, 1}\right) T_{1, 0} + \left(T_{0, 2} T_{1, 0} + T_{1, 1} T_{1, 2} + T_{1, 2} T_{2, 2}\right) T_{2, 0}\right) T_{0, 2} + \left(\left(T_{0, 0} T_{1, 0} + T_{1, 0} T_{1, 1} + T_{1, 2} T_{2, 0}\right) T_{0, 1} + \left(T_{0, 1} T_{1, 0} + T_{1, 1}^{2} + T_{1, 2} T_{2, 1}\right) T_{1, 1} + \left(T_{0, 2} T_{1, 0} + T_{1, 1} T_{1, 2} + T_{1, 2} T_{2, 2}\right) T_{2, 1}\right) T_{1, 2} + \left(\left(T_{0, 0} T_{1, 0} + T_{1, 0} T_{1, 1} + T_{1, 2} T_{2, 0}\right) T_{0, 2} + \left(T_{0, 1} T_{1, 0} + T_{1, 1}^{2} + T_{1, 2} T_{2, 1}\right) T_{1, 2} + \left(T_{0, 2} T_{1, 0} + T_{1, 1} T_{1, 2} + T_{1, 2} T_{2, 2}\right) T_{2, 2}\right) T_{2, 2}$




```python
P(Eq(Y[5], 2), Eq(Y[1], 1)).round(2) #compute the probability
```




$\displaystyle 0.37$




```python
P((Eq(Y[1], 1) & Eq(Y[2 + 1], 2)),    Eq(YS[0], 0))
```




$\displaystyle 0.072$




```python
P(Eq(YS[0], 0) & Eq(YS[1], 1) & Eq(YS[2], 0), Eq(Y[0], 0) & trans_of_YS)

```




$\displaystyle T_{0, 1} T_{1, 0}$




```python
P(Eq(Y[1], 1) & Eq(Y[2], 0), Eq(Y[0], 0))
```




$\displaystyle 0.04$




```python
s = 0
for x in range(1, 9):
    s += P((Eq(Y[x], 1) & Eq(Y[x + 1], 2) & Eq(Y[x+2], 0)), Eq(Y[0], 0))
s
```




$\displaystyle 0.1501159866$



6bb4721682439df75a3a77d4388e0fde91d64479
ipynb
Jupyter Notebook
projects/DTMC/DTMC.ipynb
hpi-sam/HMM-Learning4SAS
ff0dc0cc7af6cd71b4d34cdd39c67a4888f9983c
MIT
projects/DTMC/DTMC.ipynb
hpi-sam/HMM-Learning4SAS
ff0dc0cc7af6cd71b4d34cdd39c67a4888f9983c
MIT
projects/DTMC/DTMC.ipynb
hpi-sam/HMM-Learning4SAS
ff0dc0cc7af6cd71b4d34cdd39c67a4888f9983c
MIT
Qwen/Qwen-72B
1. YES
2. YES