Original link:tecdat.cn/?p=17549 

Original source:Tuo End number according to the tribe public number

 

This example shows how to create and visualize the structure and evolution of Markov chain models. A four-state Markov chain is considered to create a Markov chain from a random transition matrix. The model simulates the dynamics of gross domestic product (GDP).

Create a Markov chain model of real GDP. Specify the status name.

[0.5 0.5 0.0 0.0; P = 0.5 0.0 0.5 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0]; stateNames = ["Regime 1" "Regime 2" "Regime 3" "Regime 4"]; mc = mc(P,'StateNames'Copy the code

One way to visualize a Markov chain is to draw a heat map of the transition matrix.

figure;
imagesc(P);
colormap(jet);
colorbar;
axis squar
Copy the code

Directed graph

A directed graph shows the states in a chain as nodes and the feasible transitions between states as directed edges.

Draws the default directed graph of a Markov chain.

figure;
plot(mc);
Copy the code

Compare the transition probability by specifying the edge color based on the transition probability.

figure;
plot(mc,'ColorEdges'
Copy the code

Identify cyclic and transient states by specifying node colors and tags based on the state type.

figure;
h = hplot(mc,'ColorEdges',true,
Copy the code

 

The low mean state is transient and eventually moves to the recursive high mean state.

The default font size for node labels is 8. Reduce the font size to 7.

FontSize = 7;
Copy the code

 

Mixed figure

This can be visualized by plotting the target probability and expected first hit time in a directed graph.

Calculates the probability of hitting a specified subset of the target states, starting with each state in the Markov chain. The node color indicates the hit probability.

Draw a directed graph of a Markov chain, where the node colors represent the probability of hitting scheme 1.

htp(mc,"Regime 1",'Graph
Copy the code

 

Calculates the expected first hit time for a specified subset of the target state, starting with each state in the Markov chain. The node color indicates the hit time.

Draws a directed graph of a Markov chain whose node colors represent the expected first hit times of the target subclasses containing states 3 and 4.

target = ["Regime 3" "Regime 4"];
htime(mc,target
Copy the code

Starting with scheme 1, the expected first hit time for this subclass is 6 time steps.

Eigenvalue figure

The eigenvalue diagram shows the eigenvalues on the complex plane. Eigenvalue diagram and identification:

  • Perron-frobenius eigenvalues, using bold asterisks, are non-negative matrices.
  • The spectral gap is the area between the radius of length equal to the amplitude of the second largest eigenvalue (SLEM) and the radius of length 1. The spectral gap determines the mixing time of markov chains. Large gaps indicate faster mixing, while small gaps indicate slower mixing.

Draws and returns the eigenvalues of the transformation matrix on the complex plane.

figure;
eigplt(mc)
Copy the code

 

EVals = 4×1 0.8090-0.3090 1.0000-1.0000Copy the code

The modulus of the two eigenvalues is 1, indicating that the period of the Markov chain is 2.

Redistribution diagram

A redistribution diagram plots a state redistribution from an initial distribution. Use the data generated by the Markov chain object to plot the redistribution. Redistributions can be plotted as static heat maps or animated histograms or digraphs.

Generate a 10-step redistribution from the initial distribution.

redis(mc,numSteps,'X0',x0);
Copy the code

Plot the redistribution as a heat map.

 

Since states 1 and 2 are transient, the Markov chain ultimately concentrates the probabilities on states 3 and 4. In addition, states 3 and 4 have periods of 2 as shown in the eigenvalue graph.

Draw the animation histogram. Set the frame rate to one second.

 

The simulation diagram

The simulation diagram draws the random walk diagram of markov chain starting from a specific initial state.

Generate 100 10-step random walks in which each state initializes 25 walks.

simu(mc,numSteps,
Copy the code

Draw the simulation as a heat map to show the proportion of states achieved at each step.

 

Draw a heat map of the implemented transition matrix.

figure;
simp(mc,X,'Type','transition');
Copy the code

The implemented transition matrix looks similar to the theoretical transition matrix.


Most welcome insight

1. Use R language to simulate the mixed queuing random service queuing system

2. Queuing theory is used in R language to predict waiting time

3. Realize markov chain Monte Carlo MCMC model in R language

4. Markov regime switching model in R language

5. Matlab Bayesian hidden Markov HMM model

6. Use R language to simulate the mixed queuing random service queuing system

7. Portfolio optimization in Python based on particle swarm optimization

8. Research on accident prediction of traffic casualties by R Language Markov Transformation model

9. Identifying changing Stock Market Conditions with Machine learning: Application of Hidden Markov Models