Consider a markovian process with state spaces {0,1} with the following state-transition matrix:
$\pi =$ \begin{bmatrix}
Pr(s_{t+1} = 0|s_{t}= 0) & Pr(s_{t+1} = 1|s_{t}= 0) \\
Pr(s_{t+1} = 0|s_{t}= 1) & Pr(s_{t+1} = 1|s_{t}= 1) 
\end{bmatrix}.

Assume that $Pr(s_{t+1} = 0|s_{t}= 0) = Pr(s_{t+1} = 1|s_{t}= 1) = 2/3$ and $Pr(s_{t+1} = 0|s_{t}= 1) = 
Pr(s_{t+1} = 1|s_{t} = 0) = 1/3$. Consider that the initial distribution for the states as: $\Pi_{0} = [5/6 1/6]'$ 
and answer the following itens:

a) Right a Python function that iterates the states until the stationary state distribution for a given initial condition. 
Like: $\Pi_{t+1} = \pi'\Pi_{t}$

Answer:

Importing modules


```python
import numpy as np
from scipy import linalg
```

Defining the state-transition matrix and the initial condition:


```python
T = np.array([[2/3, 1/3], [1/3 , 2/3]])
pi_0 = np.array([[5/6, 1/6]])
```

Creating the function:


```python
#Returns the stationary distribution
def markov_int(T, pi_0):
    pi_t = np.copy(pi_0)
    norm, tol = 1, 1e-6
    while norm > tol:
        pi_t1 = np.dot(pi_t, T)
        norm = np.max(abs(pi_t1 - pi_t))
        pi_t = np.copy(pi_t1)
    return pi_t
```


```python
pi_t = markov_int(T, pi_0)
print(f'The stationary distribution is {pi_t}')
```

    The stationary distribution is [[0.50000021 0.49999979]]


b) Create a function in Python that returns the stationary distribution using the 
left normalized eigenvector of $\pi$ associated to the eignvalue $1$. 

I will deactivate the warnings because sometimes the function to find the eignvalues/vectors divides by zero


```python
import warnings
warnings.filterwarnings('ignore')
```


```python
# returns the normalized eignvectors and eignvalues
# the first line calculates the eignvector(its normalized already) and eignvalues
# the second line copies the eignvectors
# the third line normalizes it(just because the exercise asks to do it)
def markov_matr(T):
    w, v = linalg.eig(T, left = True, right = False)
    pi = v[:, :]
    pi /= pi.sum(axis = 0) 
    return pi, w
```


```python
v, w = markov_matr(T)
print(v)
print(abs(w))
print(v[:,0].reshape(2,1))
print('The first column of the eignvectors matrix is the eignvector associated to the first eignvalue and so on.')
print('The normalized eignvectors matrix is:')
print(f'{v}')
print(f'The eignvalues associated are:')
print(f'{abs(w)}')
```

    [[ 0.5 -inf]
     [ 0.5  inf]]
    [1.         0.33333333]
    [[0.5]
     [0.5]]
    The first column of the eignvectors matrix is the eignvector associated to the first eignvalue and so on.
    The normalized eignvectors matrix is:
    [[ 0.5 -inf]
     [ 0.5  inf]]
    The eignvalues associated are:
    [1.         0.33333333]


Consider a vector of possible incomes $y = [y_{1} \space y_{2}]$ and a vector of three possible values for the assets
$a = [a_{1} \space a_{2} \space a_{3}]$. The joint stationary distribution of endowments of assets and income is given by:
$\phi_{\infty} = [Pr(y_{1}, a_{1}) \space Pr(y_{1}, a_{2}) \space Pr(y_{1}, a_{3}) \space Pr(y_{2}, a_{1})
 \space Pr(y_{2}, a_{2}) \space Pr(y_{2}, a_{3})]$.

Suppose that the policy function is given by $a'(a,y) = a$. Suppose that $Pr(y_{1}, a_{i}) = 0.1$ for all
$i \in {1,2,3}$ and $Pr(y_{2}, a_{1}) = Pr(y_{2}, a_{2}) = 0.2$ and that $Pr(y_{1}, a_{3}) = 0.3$.
Compute the aggregate demand for assets, that is:

\begin{equation}
E[a] = \int a'(a,y)d\phi_{\infty}
\end{equation} 

Defining the vectors:


```python
phi = np.array([[.1, .1, .1], [.2, .2, .3]])
```

Note that the initial income(y) and asset(a) distribution were not defined.We need to do it.

Defining state dimensions:


```python
n_y = 2
n_a = 3
```

Defining state space:


```python
y_grid = np.linspace(0,1, n_y)
a_grid = np.linspace(0,1, n_a)
```

Creating a policy function as defined by the exercise:


```python
def f_poli(a_grid):
    a_line = np.zeros((n_y, n_a))
    a_line[0,:] = a_grid
    a_line[1,:] = a_grid
    
    return a_line
a_line = f_poli(a_grid)
a_line
```




    array([[0. , 0.5, 1. ],
           [0. , 0.5, 1. ]])



Now we need loop through all income and assets(a line) states defined by the policy functio, 
but first we will define the initial expected asset distribution as zero and the phi given.


```python
Ea = 0 #Expected asset demand distribution
phi = np.array([.1, .1, .1, .2, .2, .3]) #Creating phi
#Now looping
for i_y in range(n_y):
    for i_a in range(n_a):
        idx = i_y * n_a + i_a
        print(idx)
        Ea += a_line[i_y, i_a]*phi[idx]
print(Ea)
```

    0.55


501133eff10ea2a1cd491401eadd1b461dd2d744
ipynb
Jupyter Notebook
Markov Chain and Asset Distribution.ipynb
valcareggi/Macroeconomics
e6b1165aeacf2369b70f2d710a198962c3390864
MIT
Markov Chain and Asset Distribution.ipynb
valcareggi/Macroeconomics
e6b1165aeacf2369b70f2d710a198962c3390864
MIT
Markov Chain and Asset Distribution.ipynb
valcareggi/Macroeconomics
e6b1165aeacf2369b70f2d710a198962c3390864
MIT
Qwen/Qwen-72B
1. YES
2. YES