<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Full Rank Markov and Geographic Rank Markov
Author
Step1:  Full Rank Markov
Step2:  Full rank Markov transition probability matrix
Step3:  Full rank first mean passage times
Step4:  Geographic Rank Markov
Step5:  Visualizing first mean passage time from/to California/Mississippi
Step6:  Visualizing sojourn time for each US state
<ASSISTANT_TASK:>
Python Code:
import pysal.lib as ps
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import pandas as pd
import geopandas as gpd
Explanation: Full Rank Markov and Geographic Rank Markov
Author: Wei Kang &#119;&#101;&#105;&#107;&#97;&#110;&#103;&#57;&#48;&#48;&#57;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;
End of explanation
from pysal.explore.giddy.markov import FullRank_Markov
income_table = pd.read_csv(ps.examples.get_path("usjoin.csv"))
income_table.head()
pci = income_table[list(map(str,range(1929,2010)))].values
pci
m = FullRank_Markov(pci)
m.ranks
m.transitions
Explanation: Full Rank Markov
End of explanation
m.p
Explanation: Full rank Markov transition probability matrix
End of explanation
m.fmpt
m.sojourn_time
df_fullrank = pd.DataFrame(np.c_[m.p.diagonal(),m.sojourn_time], columns=["Staying Probability","Sojourn Time"], index = np.arange(m.p.shape[0])+1)
df_fullrank.head()
df_fullrank.plot(subplots=True, layout=(1,2), figsize=(15,5))
sns.distplot(m.fmpt.flatten(),kde=False)
Explanation: Full rank first mean passage times
End of explanation
from pysal.explore.giddy.markov import GeoRank_Markov, Markov, sojourn_time
gm = GeoRank_Markov(pci)
gm.transitions
gm.p
gm.sojourn_time[:10]
gm.sojourn_time
gm.fmpt
income_table["geo_sojourn_time"] = gm.sojourn_time
i = 0
for state in income_table["Name"]:
    income_table["geo_fmpt_to_" + state] = gm.fmpt[:,i]
    income_table["geo_fmpt_from_" + state] = gm.fmpt[i,:]
    i = i + 1
income_table.head()
geo_table = gpd.read_file(ps.examples.get_path('us48.shp'))
# income_table = pd.read_csv(pysal.lib.examples.get_path("usjoin.csv"))
complete_table = geo_table.merge(income_table,left_on='STATE_NAME',right_on='Name')
complete_table.head()
complete_table.columns
Explanation: Geographic Rank Markov
End of explanation
fig, axes = plt.subplots(nrows=2, ncols=2,figsize = (15,7))
target_states = ["California","Mississippi"]
directions = ["from","to"]
for i, direction in enumerate(directions):
    for j, target in enumerate(target_states):
        ax = axes[i,j]
        col = direction+"_"+target
        complete_table.plot(ax=ax,column = "geo_fmpt_"+ col,cmap='OrRd', 
                    scheme='quantiles', legend=True)
        ax.set_title("First Mean Passage Time "+direction+" "+target)
        ax.axis('off')
        leg = ax.get_legend()
        leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
plt.tight_layout()
Explanation: Visualizing first mean passage time from/to California/Mississippi:
End of explanation
fig, axes = plt.subplots(nrows=1, ncols=2,figsize = (15,7))
schemes = ["Quantiles","Equal_Interval"]
for i, scheme in enumerate(schemes):
    ax = axes[i]
    complete_table.plot(ax=ax,column = "geo_sojourn_time",cmap='OrRd', 
                scheme=scheme, legend=True)
    ax.set_title("Rank Sojourn Time ("+scheme+")")
    ax.axis('off')
    leg = ax.get_legend()
    leg.set_bbox_to_anchor((0.8, 0.15, 0.16, 0.2))
plt.tight_layout()
Explanation: Visualizing sojourn time for each US state:
End of explanation
<END_TASK>