@@ -13,7 +13,7 @@ kernelspec:
1313
1414+++ {"user_expressions": [ ] }
1515
16- # Markov Chains II
16+ # Markov Chains: Irreducibility and Ergodicity
1717
1818In addition to what's in Anaconda, this lecture will need the following libraries:
1919
@@ -262,7 +262,7 @@ This is one aspect of the concept of ergodicity.
262262### Example 2
263263
264264
265- Another example is Hamilton {cite}` Hamilton2005 ` dynamics {ref}` discussed above <mc_eg2> ` .
265+ Another example is Hamilton {cite}` Hamilton2005 ` dynamics {ref}` discussed before <mc_eg2> ` .
266266
267267The diagram of the Markov chain shows that it is ** irreducible** .
268268
@@ -298,6 +298,61 @@ plt.show()
298298
299299### Example 3
300300
301+ Let's look at one more example with six states {ref}` discussed before <mc_eg3> ` .
302+
303+
304+ $$
305+ $$
306+ P :=
307+ \left(
308+ \begin{array}{cccccc}
309+ 0.86 & 0.11 & 0.03 & 0.00 & 0.00 & 0.00 \\
310+ 0.52 & 0.33 & 0.13 & 0.02 & 0.00 & 0.00 \\
311+ 0.12 & 0.03 & 0.70 & 0.11 & 0.03 & 0.01 \\
312+ 0.13 & 0.02 & 0.35 & 0.36 & 0.10 & 0.04 \\
313+ 0.00 & 0.00 & 0.09 & 0.11 & 0.55 & 0.25 \\
314+ 0.00 & 0.00 & 0.09 & 0.15 & 0.26 & 0.50
315+ \end{array}
316+ \right)
317+ $$
318+ $$
319+
320+
321+ The graph for the chain shows states are densely connected indicating that it is ** irreducible** .
322+
323+ Similar to previous examples, the sample path averages for each state converges to the stationary distribution
324+
325+ ``` {code-cell} ipython3
326+ P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00],
327+ [0.52, 0.33, 0.13, 0.02, 0.00, 0.00],
328+ [0.12, 0.03, 0.70, 0.11, 0.03, 0.01],
329+ [0.13, 0.02, 0.35, 0.36, 0.10, 0.04],
330+ [0.00, 0.00, 0.09, 0.11, 0.55, 0.25],
331+ [0.00, 0.00, 0.09, 0.15, 0.26, 0.50]]
332+
333+ ts_length = 10_000
334+ mc = qe.MarkovChain(P)
335+ ψ_star = mc.stationary_distributions[0]
336+ fig, ax = plt.subplots(figsize=(9, 6))
337+ X = mc.simulate(ts_length)
338+ # Center the plot at 0
339+ ax.set_ylim(-0.25, 0.25)
340+ ax.axhline(0, linestyle='dashed', lw=2, color = 'black', alpha=0.4)
341+
342+
343+ for x0 in range(6):
344+ # Calculate the fraction of time for each state
345+ X_bar = (X == x0).cumsum() / (1 + np.arange(ts_length, dtype=float))
346+ ax.plot(X_bar - ψ_star[x0], label=f'$X = {x0+1} $')
347+ ax.set_xlabel('t')
348+ ax.set_ylabel(r'fraction of time spent in a state $- \psi^* (x)$')
349+
350+ ax.legend()
351+ plt.show()
352+ ```
353+
354+ ### Example 4
355+
301356Let's look at another example with two states: 0 and 1.
302357
303358
@@ -444,16 +499,14 @@ In this exercise,
444499Use the technique we learnt before, we can take the power of the transition matrix
445500
446501``` {code-cell} ipython3
447- P = [
448- [0.222, 0.222, 0.215, 0.187, 0.081, 0.038, 0.029, 0.006],
449- [0.221, 0.22, 0.215, 0.188, 0.082, 0.039, 0.029, 0.006],
450- [0.207, 0.209, 0.21, 0.194, 0.09, 0.046, 0.036, 0.008],
451- [0.198, 0.201, 0.207, 0.198, 0.095, 0.052, 0.04, 0.009],
452- [0.175, 0.178, 0.197, 0.207, 0.11, 0.067, 0.054, 0.012],
453- [0.182, 0.184, 0.2, 0.205, 0.106, 0.062, 0.05, 0.011],
454- [0.123, 0.125, 0.166, 0.216, 0.141, 0.114, 0.094, 0.021],
455- [0.084, 0.084, 0.142, 0.228, 0.17, 0.143, 0.121, 0.028]
456- ]
502+ P = [[0.222, 0.222, 0.215, 0.187, 0.081, 0.038, 0.029, 0.006],
503+ [0.221, 0.22, 0.215, 0.188, 0.082, 0.039, 0.029, 0.006],
504+ [0.207, 0.209, 0.21, 0.194, 0.09, 0.046, 0.036, 0.008],
505+ [0.198, 0.201, 0.207, 0.198, 0.095, 0.052, 0.04, 0.009],
506+ [0.175, 0.178, 0.197, 0.207, 0.11, 0.067, 0.054, 0.012],
507+ [0.182, 0.184, 0.2, 0.205, 0.106, 0.062, 0.05, 0.011],
508+ [0.123, 0.125, 0.166, 0.216, 0.141, 0.114, 0.094, 0.021],
509+ [0.084, 0.084, 0.142, 0.228, 0.17, 0.143, 0.121, 0.028]]
457510
458511P = np.array(P)
459512codes_B = ('1','2','3','4','5','6','7','8')
@@ -476,11 +529,9 @@ ts_length = 1000
476529mc = qe.MarkovChain(P)
477530fig, ax = plt.subplots(figsize=(9, 6))
478531X = mc.simulate(ts_length)
479- # Center the plot at 0
480532ax.set_ylim(-0.25, 0.25)
481533ax.axhline(0, linestyle='dashed', lw=2, color = 'black', alpha=0.4)
482534
483-
484535for x0 in range(8):
485536 # Calculate the fraction of time for each worker
486537 X_bar = (X == x0).cumsum() / (1 + np.arange(ts_length, dtype=float))
0 commit comments