Spatial dynamics, mirroring giddy 2.3.9.
Markov chains
markov/2- classic Markov chain on a panel of classes (giddy.markov.Markov)spatial_markov/3- transitions conditioned on the class of the spatial lag (giddy.markov.Spatial_Markov) with the homogeneity testslisa_markov/3- Markov chain on local Moran quadrants (giddy.markov.LISA_Markov)kullback/2- Kullback's information test of Markov homogeneity across strata (giddy.markov.kullback)steady_state/2,mfpt/2,sojourn_time/2- the ergodic summaries of any transition probability matrix (giddy.ergodic)
Panels are n x t matrices: one row per location, one column per period, given as a list of
lists or a rank-2 Nx.Tensor. markov/2 takes class ids (integers) or, with k:, continuous
values that are classified into quantiles period by period. Matrices come back as lists of rows.
Mean first passage and sojourn times can be :infinity.
Rank mobility
kendall_tau/2- Kendall's tau-b (giddy.rank.Tau)spatial_tau/4- tau over neighbor pairs with a permutation test (giddy.rank.SpatialTau)theta/3- regime-wise direction of rank changes (giddy.rank.Theta)
Permutations and seeds
Permutation tests take :permutations and :seed. Without a seed a random one is drawn and
stored in the result, so a run can be reproduced; the same seed gives bit-identical results
at any thread count (ChaCha8 streams, so draws differ from numpy's while the distribution
agrees).
Invalid input raises ArgumentError.
Summary
Functions
Markov chain of the rank of every area (giddy.markov.FullRank_Markov).
Markov chain of the area holding every rank (giddy.markov.GeoRank_Markov).
Kendall's tau-b of two variables with its asymptotic p-value (giddy.rank.Tau).
Kullback's information test of Markov homogeneity across strata (giddy.markov.kullback).
Markov chain of local Moran quadrants (giddy.markov.LISA_Markov).
Spill-over of significant LISA clusters between consecutive periods
(giddy.markov.LISA_Markov.spillover).
Markov chain of a panel of classes (giddy.markov.Markov).
Mean first passage times of a transition probability matrix (giddy.ergodic.mfpt).
Sojourn time 1 / (1 - p_ii) of every state (giddy.markov.sojourn_time); :infinity for
absorbing states. Empty rows are treated as absorbing.
Spatial Markov (giddy.markov.Spatial_Markov): the transition probabilities of y conditioned
on the class of each location's spatial lag in the initial period, with tests of whether the
conditional matrices differ from the overall one.
Spatial tau (giddy.rank.SpatialTau): Kendall's tau restricted to pairs of neighbors, with a
permutation test of its value under random relabelling of locations.
Steady-state distribution of a transition probability matrix (giddy.ergodic.steady_state).
Local Kendall tau (giddy.rank.Tau_Local) of x and y.
Neighbor-set local Kendall tau (giddy.rank.Tau_Local_Neighbor) over graph.
Neighborhood local Kendall tau (giddy.rank.Tau_Local_Neighborhood) over graph.
Regional Kendall tau (giddy.rank.Tau_Regional): the mean concordance between every pair of
regimes (and within each, on the diagonal).
Regime mobility measure theta (giddy.rank.Theta).
Functions
@spec full_rank_markov(term(), keyword()) :: Spatex.Dynamics.Markov.t()
Markov chain of the rank of every area (giddy.markov.FullRank_Markov).
y is an n x t matrix of numbers. In each period the areas are ranked from 1 (largest)
to n (smallest); ties keep their order of appearance (scipy's rankdata(method="ordinal")
on the ascending order). The ranks are the states of markov/2, kept in ranks. Options:
:fill_empty_classes as in markov/2. At most 1000 areas (the class limit of markov/2).
Examples
iex> m = Spatex.Dynamics.full_rank_markov([[1, 3], [2, 1], [3, 2]])
iex> m.ranks
[[3, 1], [2, 3], [1, 2]]
@spec geo_rank_markov(term(), keyword()) :: Spatex.Dynamics.Markov.t()
Markov chain of the area holding every rank (giddy.markov.GeoRank_Markov).
y is an n x t matrix of numbers. In each period, state r (1-based, ascending ranks as in
giddy) is the 1-based index of the area holding rank r, with ties in order of appearance;
these states are kept in ranks and fed to markov/2. Options: :fill_empty_classes.
Examples
iex> Spatex.Dynamics.geo_rank_markov([[1, 3], [2, 1], [3, 2]]).ranks
[[1, 2], [2, 3], [3, 1]]
@spec kendall_tau(term(), term()) :: Spatex.Dynamics.Tau.t()
Kendall's tau-b of two variables with its asymptotic p-value (giddy.rank.Tau).
Examples
iex> Spatex.Dynamics.kendall_tau([1, 2, 3, 4], [2, 4, 6, 8]).tau
1.0
@spec kullback(term(), keyword()) :: Spatex.Dynamics.Kullback.t()
Kullback's information test of Markov homogeneity across strata (giddy.markov.kullback).
source holds the transition counts (not probabilities) of s strata over the same r
classes: a list of s square r x r matrices, a rank-3 Nx.Tensor of shape {s, r, r}, or a
Spatex.Dynamics.SpatialMarkov (its conditional count matrices t, one per spatial-lag class).
It tests whether the transition probabilities are the same in every stratum; a large
statistic (small p_value) says they differ. Returns a Spatex.Dynamics.Kullback.
Options
:giddy_compatible- reproduce giddy's arithmetic (defaultfalse). Kullback's statistic is2 [sum f_sij ln f_sij - sum f_si. ln f_si. - sum f_.ij ln f_.ij + sum f_.i. ln f_.i.], withf_si.the row totals of each stratum. giddy sums that term over the wrong axis (the column totalsf_s.j), which gives the same value only when every stratum has equal row and column totals (as in the table of its docstring) and otherwise a wrong one, even a negative statistic for identical strata.truereproduces giddy's value.
Examples
iex> k = Spatex.Dynamics.kullback([[[10, 5], [4, 12]], [[10, 5], [4, 12]]])
iex> {k.dof, Float.round(abs(k.statistic), 9)}
{2, 0.0}
@spec lisa_markov(term(), Spatex.Graph.t(), keyword()) :: Spatex.Dynamics.LisaMarkov.t()
Markov chain of local Moran quadrants (giddy.markov.LISA_Markov).
y is an n x t matrix aligned with the graph's ids. Each period gets a
Spatex.ESDA.moran_local/3; the quadrants (1..4) are the states.
Options
:permutations- conditional randomizations per period (default0: no p-values),:seed(periodsusesseed + s).:significance_level- threshold forsignificant_moves(default0.05).:geoda_quads- GeoDa's quadrant numbering (defaultfalse),:transform- the weights transform of the local statistics (default:r).
The independence test (chi_2) lags y with the graph as given. Returns a
Spatex.Dynamics.LisaMarkov.
@spec lisa_spillover(Spatex.Dynamics.LisaMarkov.t(), Spatex.Graph.t(), keyword()) :: %{components: [[non_neg_integer()]], spill_over: [[0 | 1]]} | nil
Spill-over of significant LISA clusters between consecutive periods
(giddy.markov.LISA_Markov.spillover).
For every period, the significant sites of :quadrant (q == quadrant and
p_values <= significance_level) are joined to their neighbors in graph (and, with
neighbors_on: true, to their neighbors' neighbors), and the connected components of those
links are taken. A site that belongs to a component in period t + 1, to none in period t,
and whose t + 1 component meets a period-t component spills over.
Returns %{components: n x t, spill_over: n x (t - 1)} (lists of rows): components[i][t]
is the 1-based component of site i in period t (0 outside every component; the last
period stays 0, as in giddy) and spill_over[i][t] is 1 when site i spills over between
t and t + 1. Components are numbered by their smallest site index; giddy follows Python's
set order, which agrees for integer ids. nil when lisa_markov ran without permutations.
Options: :quadrant (1..4, default 1) and :neighbors_on (default false).
@spec markov(term(), keyword()) :: Spatex.Dynamics.Markov.t()
Markov chain of a panel of classes (giddy.markov.Markov).
data is an n x t matrix of integer class ids, or of numbers when :k is given (each
period is then classified into k quantile classes, like mapclassify.Quantiles(y_s, k)).
Options
:k- number of quantile classes for continuous values (defaultnil:dataholds class ids).:classes- the class labels in row order (default: the sorted distinct labels).:fill_empty_classes- give rows without observed transitions a self-transition of 1 (defaultfalse;steady_state,mfptandsojourn_timealways do, as in giddy).
Examples
iex> m = Spatex.Dynamics.markov([[0, 1, 1], [1, 0, 0]])
iex> m.transitions
[[1.0, 1.0], [1.0, 1.0]]
iex> m.steady_state
[0.5, 0.5]
Mean first passage times of a transition probability matrix (giddy.ergodic.mfpt).
result[i][j] is the expected number of steps from i to first reach j (the recurrence
time when i == j), :infinity where passage is not certain. Same options as steady_state/2.
Sojourn time 1 / (1 - p_ii) of every state (giddy.markov.sojourn_time); :infinity for
absorbing states. Empty rows are treated as absorbing.
@spec spatial_markov(term(), Spatex.Graph.t(), keyword()) :: Spatex.Dynamics.SpatialMarkov.t()
Spatial Markov (giddy.markov.Spatial_Markov): the transition probabilities of y conditioned
on the class of each location's spatial lag in the initial period, with tests of whether the
conditional matrices differ from the overall one.
y is an n x t matrix aligned with the graph's ids. The lag W y uses the graph's current
weights (row-standardize it first, as in PySAL practice).
Options
:k/:m- number of classes ofy/ of its lag (default4each).:fixed- classify all periods together with one set of quantile cut points (defaulttrue) or each period on its own.:discrete-yalready holds class labels; the lag is the weighted mode of the neighbors' labels (ties go to the focal label, then the lowest class).:kand:mthen equal the number of labels.:cutoffs/:lag_cutoffs- ascending class upper bounds (without the final infinity).:permutations- rows ofyshuffled to testx2(default0),:seed.:fill_empty_classes- make rows without transitions absorbing inpandp_cond(defaultfalse).
Returns a Spatex.Dynamics.SpatialMarkov.
@spec spatial_tau(term(), term(), Spatex.Graph.t(), keyword()) :: Spatex.Dynamics.SpatialTau.t()
Spatial tau (giddy.rank.SpatialTau): Kendall's tau restricted to pairs of neighbors, with a
permutation test of its value under random relabelling of locations.
x and y are aligned with the graph's ids; edge weights are ignored (binary). Options:
:permutations (default 0) and :seed.
Steady-state distribution of a transition probability matrix (giddy.ergodic.steady_state).
A list of k floats for an irreducible chain, otherwise one list per recurrent class. A
matrix with all-zero rows raises unless fill_empty_classes: true, which makes those states
absorbing.
Examples
iex> Spatex.Dynamics.steady_state([[0.5, 0.25, 0.25], [0.5, 0.0, 0.5], [0.25, 0.25, 0.5]])
...> |> Enum.map(&Float.round(&1, 6))
[0.4, 0.2, 0.4]
@spec tau_local(term(), term()) :: Spatex.Dynamics.TauLocal.t()
Local Kendall tau (giddy.rank.Tau_Local) of x and y.
tau_local_i is the mean concordance sign((x_i - x_j)(y_i - y_j)) of site i with every
other site and tau the mean over all ordered pairs. O(n^2) time, O(n) memory.
Examples
iex> t = Spatex.Dynamics.tau_local([1, 2, 3, 4, 5], [2, 1, 4, 3, 5])
iex> {t.tau, t.tau_local}
{0.6, [0.5, 0.5, 0.5, 0.5, 1.0]}
@spec tau_local_neighbor(term(), term(), Spatex.Graph.t(), keyword()) :: Spatex.Dynamics.TauLocalNeighbor.t()
Neighbor-set local Kendall tau (giddy.rank.Tau_Local_Neighbor) over graph.
tau_ln_i is the mean concordance of site i with its neighbors (binary weights, self pairs
ignored; :nan for islands). Options: :permutations (default 0, as in giddy), :seed
and :keep_simulations (default true; false or :tensor, an Nx.Tensor of shape
{n, permutations}, are also accepted). A permutation gives every site distinct random other
sites as neighbors (conditional randomization); p_sim is giddy's
(min(#sim >= obs, #sim <= obs) + 1) / (P + 1). Returns a Spatex.Dynamics.TauLocalNeighbor.
@spec tau_local_neighborhood(term(), term(), Spatex.Graph.t(), keyword()) :: Spatex.Dynamics.TauLocalNeighborhood.t()
Neighborhood local Kendall tau (giddy.rank.Tau_Local_Neighborhood) over graph.
tau_lnhood_i is the tau of the neighborhood {i} + N(i): the sum of the concordances of all
its ordered pairs over m (m - 1) (:nan for islands). Options and inference as in
tau_local_neighbor/4. Returns a Spatex.Dynamics.TauLocalNeighborhood.
@spec tau_regional(term(), term(), [term()], keyword()) :: Spatex.Dynamics.TauRegional.t()
Regional Kendall tau (giddy.rank.Tau_Regional): the mean concordance between every pair of
regimes (and within each, on the diagonal).
regime holds one label per observation (a list, Nx vector or Explorer.Series; see Spatex.Input.to_labels/2), sorted for the row order. Options:
:permutations (default 0), :seed and :keep_simulations (default true; false or
:tensor, a {permutations, k, k} Nx.Tensor, are also accepted); permutations
shuffle all observations over the fixed regimes. Returns a Spatex.Dynamics.TauRegional.
Examples
iex> Spatex.Dynamics.tau_regional([1, 2, 3, 4, 5], [2, 1, 4, 3, 5], [1, 1, 2, 2, 2]).tau_reg
[[-1.0, 1.0], [1.0, 0.3333333333333333]]
@spec theta(term(), [term()], keyword()) :: Spatex.Dynamics.Theta.t()
Regime mobility measure theta (giddy.rank.Theta).
y is an n x t matrix (t >= 2), regime a list of n labels. Options: :permutations
(default 999, as in giddy; 0 skips the test) and :seed.
Examples
iex> Spatex.Dynamics.theta([[1, 4], [2, 3], [3, 2], [4, 1]], [0, 0, 1, 1], permutations: 0).theta
[1.0]