def __init__(self, y, w, permutations=0, significance_level=0.05): y = y.transpose() pml = pysal.Moran_Local ################################################################# # have to optimize conditional spatial permutations over a # time series - this is a place holder for the foreclosure paper ml = [pml(yi, w, permutations=permutations) for yi in y] ################################################################# q = np.array([mli.q for mli in ml]).transpose() classes = np.arange(1, 5) # no guarantee all 4 quadrants are visited Markov.__init__(self, q, classes) self.q = q self.w = w n, k = q.shape k -= 1 self.significance_level = significance_level move_types = np.zeros((n, k), int) sm = np.zeros((n, k), int) self.significance_level = significance_level if permutations > 0: p = np.array([mli.p_z_sim for mli in ml]).transpose() self.p_values = p pb = p <= significance_level else: pb = np.zeros_like(y.T) for t in range(k): origin = q[:, t] dest = q[:, t + 1] p_origin = pb[:, t] p_dest = pb[:, t] for r in range(n): move_types[r, t] = TT[origin[r], dest[r]] key = (origin[r], dest[r], p_origin[r], p_dest[r]) sm[r, t] = MOVE_TYPES[key] if permutations > 0: self.significant_moves = sm self.move_types = move_types # null of own and lag moves being independent ybar = y.mean(axis=0) r = y / ybar ylag = np.array([pysal.lag_spatial(w, yt) for yt in y]) rlag = ylag / ybar rc = r < 1. rlagc = rlag < 1. markov_y = pysal.Markov(rc) markov_ylag = pysal.Markov(rlagc) A = np.matrix([[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]]) kp = A * np.kron(markov_y.p, markov_ylag.p) * A.T trans = self.transitions.sum(axis=1) t1 = np.diag(trans) * kp t2 = self.transitions t1 = t1.getA() self.chi_2 = pysal.spatial_dynamics.markov.chi2(t1, t2) self.expected_t = t1 self.permutations = permutations
def test___init__(self): import numpy as np f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([pysal.Quantiles(y).yb for y in pci]).transpose() m = pysal.Markov(q5) np.testing.assert_array_almost_equal(markov.shorrock(m.p), 0.19758992000997844)
def test___init__(self): import numpy as np f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([pysal.Quantiles(y).yb for y in pci]).transpose() m = pysal.Markov(q5) res = np.matrix( [[0.08988764, 0.21468144, 0.21125, 0.20194986, 0.07259074]]) np.testing.assert_array_almost_equal(markov.prais(m.p), res)
def test___init__(self): # markov = Markov(class_ids, classes) import pysal f = pysal.open(pysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([pysal.Quantiles(y).yb for y in pci]).transpose() m = pysal.Markov(q5) expected = np.array([[729., 71., 1., 0., 0.], [72., 567., 80., 3., 0.], [0., 81., 631., 86., 2.], [0., 3., 86., 573., 56.], [0., 0., 1., 57., 741.]]) np.testing.assert_array_equal(m.transitions, expected) expected = np.matrix( [[0.91011236, 0.0886392, 0.00124844, 0., 0.], [0.09972299, 0.78531856, 0.11080332, 0.00415512, 0.], [0., 0.10125, 0.78875, 0.1075, 0.0025], [0., 0.00417827, 0.11977716, 0.79805014, 0.07799443], [0., 0., 0.00125156, 0.07133917, 0.92740926]]) np.testing.assert_array_almost_equal(m.p.getA(), expected.getA()) expected = np.matrix([[0.20774716], [0.18725774], [0.20740537], [0.18821787], [0.20937187]]).getA() np.testing.assert_array_almost_equal(m.steady_state.getA(), expected)
def markov(observations, w=None, numQuints=5, method="regular"): result = None s = None if method == "regular": # non spatial analysis quintiles = np.array([pysal.Quantiles(y, k=numQuints).yb for y in observations]).transpose() result = pysal.Markov(quintiles) #s = result.steady_state else: observations = observations.transpose() if method == "spatial": # standardize observations for smoother calculations: observations = observations / (observations.mean(axis=0)) result = pysal.Spatial_Markov(observations, w, fixed=True, k=numQuints) #s = result.S else: # method == lisa result = pysal.LISA_Markov(observations, w) #s = result.steady_state return result.transitions, result.p, s, pysal.ergodic.fmpt(result.p)
import numpy as np import pysal c = np.array([['b', 'a', 'c'], ['c', 'c', 'a'], ['c', 'b', 'c'], ['a', 'a', 'b'], ['a', 'b', 'c']]) print(c) m = pysal.Markov(c) print(m.classes) print(m.transitions) #################################################################Classic Markov f = pysal.open( r'C:\Anaconda\Lib\site-packages\pysal\examples\us_income\usjoin.csv') pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) print(pci.shape) print(pci) q5 = np.array([pysal.Quantiles(y).yb for y in pci]).transpose() print(q5.shape) print(q5[:, 0]) print(q5[4, :]) m5 = pysal.Markov(q5) print(m5.transitions) print(m5.p) print(m5.steady_state) print(pysal.ergodic.fmpt(m5.p))
plt.scatter(range(1, 32), q5[:, 0]) plt.title('GDP Class in 1993') plt.ylabel('Class') plt.xlabel('Province') plt.show() # 1993年各省GDP类别分布 print(f'GDP in 1993:\n{q5[:,0]}') # Beijing’s quintile time series print(f'Beijing’s quintile time series:\n{q5[0,:]}') plt.scatter(range(1993, 2014), q5[0, :]) plt.show() # instantiate a Markov object m5 = pysal.Markov(q5) print(f'instantiate a Markov object:\n{m5.transitions}') print(f'the transition probabilities:\n{m5.p}') print(f'the long run steady state distribution:\n{m5.steady_state}') print(f'the first mean passage time:\n{pysal.ergodic.fmpt(m5.p)}') # Spatial Markov###################################################### pci = pci.transpose() rpci = pci / (pci.mean(axis=0)) w = pysal.open(r"D:\pysal_task\data\GDP_China.swm").read()