def test___init__(self): f = ps.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) res = np.array( [0.08988764, 0.21468144, 0.21125, 0.20194986, 0.07259074]) np.testing.assert_array_almost_equal(prais(m.p), res)
def test___init__(self): import numpy as np f = ps.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"), 0.19758992000997844)
def test___init__(self): import numpy as np f = libpysal.open(libpysal.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = gapi.Markov(q5) res = np.matrix( [[0.08988764, 0.21468144, 0.21125, 0.20194986, 0.07259074]]) np.testing.assert_array_almost_equal(gapi.prais(m.p), res)
def test___init__(self): pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1]) f = ps.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = Markov(q5) np.testing.assert_array_almost_equal(markov_mobility(m.p, measure="P"), 0.19758992000997844) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="D"), 0.60684854623695594) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="L2"), 0.039782002308159647) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="B1", ini=pi), 0.2277675878319787) np.testing.assert_array_almost_equal( markov_mobility(m.p, measure="B2", ini=pi), 0.046366601194789261)
def test___init__(self): # markov = Markov(class_ids, classes) f = ps.open(ps.examples.get_path('usjoin.csv')) pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]) q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() m = 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.array( [[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, expected) expected = np.array( [0.20774716, 0.18725774, 0.20740537, 0.18821787, 0.20937187]) np.testing.assert_array_almost_equal(m.steady_state, expected)
names2009 = names[order2009[::-1]] first_last = np.vstack((names[order1929[::-1]], names[order2009[::-1]])) rcParams['figure.figsize'] = 15, 10 plt.plot(years, rpci) for i in range(48): plt.text(1915, 1.91 - (i * 0.041), names1929[i], fontsize=12) plt.text(2010.5, 1.91 - (i * 0.041), names2009[i], fontsize=12) plt.xlim((years[0], years[-1])) plt.ylim((0, 1.94)) plt.ylabel(r"$y_{i,t}/\bar{y}_t$", fontsize=14) plt.xlabel('Years', fontsize=12) plt.title('Relative Dynamics', fontsize=18) # In[11]: q5 = np.array([mc.Quantiles(y, k=5).yb for y in pci]).transpose() q5[:, 0] # In[12]: f.by_col("Name") # A number of things need to be noted here. First, we are relying on the classification methods in [**mapclassify**](https://github.com/pysal/mapclassify) for defining our quintiles. The class *Quantiles* uses quintiles ($k=5$) as the default and will create an instance of this class that has multiple attributes, the one we are extracting in the first line is $yb$ - the class id for each observation. The second thing to note is the transpose operator which gets our resulting array $q5$ in the proper structure required for use of Markov. Thus we see that the first spatial unit (Alabama with an income of 323) fell in the first quintile in 1929, while the last unit (Wyoming with an income of 675) fell in the fourth quintile. # # So now we have a time series for each state of its quintile membership. For example, Colorado’s quintile time series is: # In[13]: q5[4, :] # indicating that it has occupied the 3rd, 4th and 5th quintiles in the distribution at the first 3 periods. To summarize the transition dynamics for all units, we instantiate a Markov object:
def base_choropleth_classif(map_obj, values, classification='quantiles', k=5, cmap='hot_r', sample_fisher=False): ''' Set coloring based based on different classification methods ... Arguments --------- map_obj : Poly/Line collection Output from map_X_shp values : array Numpy array with values to map classification : str Classificatio method to use. Options supported: * 'quantiles' (default) * 'fisher_jenks' * 'equal_interval' k : int Number of bins to classify values in and assign a color to cmap : str Matplotlib coloring scheme sample_fisher : Boolean Defaults to False, controls whether Fisher-Jenks classification uses a sample (faster) or the entire array of values. Ignored if 'classification'!='fisher_jenks' The percentage of the sample that takes at a time is 10% Returns ------- map : PatchCollection Map object with the polygons from the shapefile and unique value coloring ''' if classification == 'quantiles': classification = mc.Quantiles(values, k) boundaries = classification.bins.tolist() if classification == 'equal_interval': classification = mc.Equal_Interval(values, k) boundaries = classification.bins.tolist() if classification == 'fisher_jenks': if sample_fisher: classification = mc.Fisher_Jenks_Sampled(values, k) else: classification = mc.Fisher_Jenks(values, k) boundaries = classification.bins[:] map_obj.set_alpha(0.4) cmap = cm.get_cmap(cmap, k + 1) map_obj.set_cmap(cmap) boundaries = np.insert(boundaries, 0, values.min()) norm = clrs.BoundaryNorm(boundaries, cmap.N) map_obj.set_norm(norm) if isinstance(map_obj, mpl.collections.PolyCollection): pvalues = _expand_values(values, map_obj.shp2dbf_row) map_obj.set_array(pvalues) map_obj.set_edgecolor('k') elif isinstance(map_obj, mpl.collections.LineCollection): pvalues = _expand_values(values, map_obj.shp2dbf_row) map_obj.set_array(pvalues) elif isinstance(map_obj, mpl.collections.PathCollection): if not hasattr(map_obj, 'shp2dbf_row'): map_obj.shp2dbf_row = np.arange(values.shape[0]) map_obj.set_array(values) return map_obj