Author: Wei Kang weikang9009@gmail.com
In [1]:
import libpysal 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
In [2]:
from giddy.markov import FullRank_Markov
In [3]:
income_table = pd.read_csv(ps.examples.get_path("usjoin.csv"))
income_table.head()
Out[3]:
In [4]:
pci = income_table[list(map(str,range(1929,2010)))].values
pci
Out[4]:
In [5]:
m = FullRank_Markov(pci)
m.ranks
Out[5]:
In [6]:
m.transitions
Out[6]:
Full rank Markov transition probability matrix
In [7]:
m.p
Out[7]:
Full rank first mean passage times
In [8]:
m.fmpt
Out[8]:
In [9]:
m.sojourn_time
Out[9]:
In [10]:
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()
Out[10]:
In [11]:
df_fullrank.plot(subplots=True, layout=(1,2), figsize=(15,5))
Out[11]:
In [12]:
sns.distplot(m.fmpt.flatten(),kde=False)
Out[12]:
In [13]:
from giddy.markov import GeoRank_Markov, Markov, sojourn_time
gm = GeoRank_Markov(pci)
In [21]:
gm.transitions
Out[21]:
In [24]:
gm.p
Out[24]:
In [28]:
gm.sojourn_time[:10]
Out[28]:
In [14]:
gm.sojourn_time
Out[14]:
In [15]:
gm.fmpt
Out[15]:
In [16]:
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()
Out[16]:
In [17]:
geo_table = gpd.read_file(ps.examples.get_path('us48.shp'))
# income_table = pd.read_csv(libpysal.examples.get_path("usjoin.csv"))
complete_table = geo_table.merge(income_table,left_on='STATE_NAME',right_on='Name')
complete_table.head()
Out[17]:
In [18]:
complete_table.columns
Out[18]:
Visualizing first mean passage time from/to California/Mississippi:
In [19]:
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()
Visualizing sojourn time for each US state:
In [20]:
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()