GitHub

@@ -255,6 +255,8 @@ Then we can address a range of questions, such as

255255256256

We'll cover such applications below.

257257258+259+258260

### Defining Markov Chains

259261260262

So far we've given examples of Markov chains but now let's define them more

@@ -306,6 +308,9 @@ chain $\{X_t\}$ as follows:

306308307309

By construction, the resulting process satisfies {eq}`mpp`.

308310311+312+313+309314

## Simulation

310315311316

```{index} single: Markov Chains; Simulation

@@ -859,8 +864,10 @@ Importantly, the result is valid for any choice of $\psi_0$.

859864860865

Notice that the theorem is related to the law of large numbers.

861866867+

TODO -- link to our undergrad lln and clt lecture

868+862869

It tells us that, in some settings, the law of large numbers sometimes holds even when the

863-

sequence of random variables is [not IID](iid_violation).

870+

sequence of random variables is not IID.

864871865872866873

(mc_eg1-2)=

@@ -905,15 +912,15 @@ n_state = P.shape[1]

905912

fig, axes = plt.subplots(nrows=1, ncols=n_state)

906913

ψ_star = mc.stationary_distributions[0]

907914

plt.subplots_adjust(wspace=0.35)

908-909915

for i in range(n_state):

910916

axes[i].grid()

911-

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

912-

label = fr'$\psi^*({i})$')

917+

axes[i].set_ylim(ψ_star[i]-0.2, ψ_star[i]+0.2)

918+

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

919+

label = fr'$\psi^*(X={i})$')

913920

axes[i].set_xlabel('t')

914-

axes[i].set_ylabel(f'fraction of time spent at {i}')

921+

axes[i].set_ylabel(fr'average time spent at X={i}')

915922916-

# Compute the fraction of time spent, starting from different x_0s

923+

# Compute the fraction of time spent, for each X=x

917924

for x0, col in ((0, 'blue'), (1, 'green'), (2, 'red')):

918925

# Generate time series that starts at different x0

919926

X = mc.simulate(n, init=x0)

@@ -942,8 +949,6 @@ $$

942949

The diagram of the Markov chain shows that it is **irreducible**

943950944951

```{code-cell} ipython3

945-

:tags: [hide-input]

946-947952

dot = Digraph(comment='Graph')

948953

dot.attr(rankdir='LR')

949954

dot.node("0")

@@ -971,16 +976,15 @@ mc = MarkovChain(P)

971976

n_state = P.shape[1]

972977

fig, axes = plt.subplots(nrows=1, ncols=n_state)

973978

ψ_star = mc.stationary_distributions[0]

974-975979

for i in range(n_state):

976980

axes[i].grid()

977981

axes[i].set_ylim(0.45, 0.55)

978-

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

979-

label = fr'$\psi^*({i})$')

982+

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

983+

label = fr'$\psi^*(X={i})$')

980984

axes[i].set_xlabel('t')

981-

axes[i].set_ylabel(f'fraction of time spent at {i}')

985+

axes[i].set_ylabel(fr'average time spent at X={i}')

982986983-

# Compute the fraction of time spent, for each x

987+

# Compute the fraction of time spent, for each X=x

984988

for x0 in range(n_state):

985989

# Generate time series starting at different x_0

986990

X = mc.simulate(n, init=x0)

@@ -1074,7 +1078,6 @@ In the case of Hamilton's Markov chain, the distribution $\psi P^t$ converges to

10741078

P = np.array([[0.971, 0.029, 0.000],

10751079

[0.145, 0.778, 0.077],

10761080

[0.000, 0.508, 0.492]])

1077-10781081

# Define the number of iterations

10791082

n = 50

10801083

n_state = P.shape[0]

@@ -1094,8 +1097,8 @@ for i in range(n):

10941097

# Loop through many initial values

10951098

for x0 in x0s:

10961099

x = x0

1097-

X = np.zeros((n, n_state))

1098-1100+

X = np.zeros((n,n_state))

1101+10991102

# Obtain and plot distributions at each state

11001103

for t in range(0, n):

11011104

x = x @ P

@@ -1104,10 +1107,10 @@ for x0 in x0s:

11041107

axes[i].plot(range(0, n), X[:,i], alpha=0.3)

1105110811061109

for i in range(n_state):

1107-

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

1108-

label = fr'$\psi^*({i})$')

1110+

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',

1111+

label = fr'$\psi^*(X={i})$')

11091112

axes[i].set_xlabel('t')

1110-

axes[i].set_ylabel(fr'$\psi({i})$')

1113+

axes[i].set_ylabel(fr'$\psi(X={i})$')

11111114

axes[i].legend()

1112111511131116

plt.show()

@@ -1144,9 +1147,9 @@ for x0 in x0s:

11441147

axes[i].plot(range(20, n), X[20:,i], alpha=0.3)

1145114811461149

for i in range(n_state):

1147-

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black', label = fr'$\psi^*({i})$')

1150+

axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black', label = fr'$\psi^* (X={i})$')

11481151

axes[i].set_xlabel('t')

1149-

axes[i].set_ylabel(fr'$\psi({i})$')

1152+

axes[i].set_ylabel(fr'$\psi(X={i})$')

11501153

axes[i].legend()

1151115411521155

plt.show()

@@ -1292,7 +1295,7 @@ In this exercise,

1292129512931296

1. show this process is asymptotically stationary and calculate the stationary distribution using simulations.

129412971295-

1. use simulations to demonstrate ergodicity of this process.

1298+

1. use simulation to show ergodicity.

1296129912971300

````

12981301

@@ -1320,7 +1323,7 @@ codes_B = ( '1','2','3','4','5','6','7','8')

13201323

np.linalg.matrix_power(P_B, 10)

13211324

```

132213251323-

We find that rows of the transition matrix converge to the stationary distribution

1326+

We find rows transition matrix converge to the stationary distribution

1324132713251328

```{code-cell} ipython3

13261329

mc = qe.MarkovChain(P_B)

@@ -1341,17 +1344,17 @@ ax.axhline(0, linestyle='dashed', lw=2, color = 'black', alpha=0.4)

134113441342134513431346

for x0 in range(8):

1344-

# Calculate the fraction of time for each worker

1347+

# Calculate the average time for each worker

13451348

X_bar = (X == x0).cumsum() / (1 + np.arange(N, dtype=float))

13461349

ax.plot(X_bar - ψ_star[x0], label=f'$X = {x0+1} $')

13471350

ax.set_xlabel('t')

1348-

ax.set_ylabel(r'fraction of time spent in a state $- \psi^* (x)$')

1351+

ax.set_ylabel(fr'average time spent in a state $- \psi^* (X=x)$')

1349135213501353

ax.legend()

13511354

plt.show()

13521355

```

135313561354-

Note that the fraction of time spent at each state quickly converges to the probability assigned to that state by the stationary distribution.

1357+

We can see that the time spent at each state quickly converges to the stationary distribution.

1355135813561359

```{solution-end}

13571360

```

@@ -1449,9 +1452,10 @@ However, another way to verify irreducibility is by checking whether $A$ satisfi

1449145214501453

Assume A is an $n \times n$ $A$ is irreducible if and only if $\sum_{k=0}^{n-1}A^k$ is a positive matrix.

145114541452-

(see more: {cite}`zhao_power_2012` and [here](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix))

1455+

(see more at \cite{zhao_power_2012} and [here](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix))

1453145614541457

Based on this claim, write a function to test irreducibility.

1458+14551459

```

1456146014571461

```{solution-start} mc_ex3

Read the original on github.com ↗