# `Spatex.Inequality`
[🔗](https://github.com/CountlinkX-Solutions/spatex/blob/v0.2.1/lib/spatex/inequality.ex#L1)

Inequality measures, mirroring the `inequality` package (1.2.0).

  * `gini/1` - Gini coefficient (`inequality.gini.Gini`)
  * `theil/1` - Theil's T (`inequality.theil.Theil`)
  * `theil_d/3` - between / within decomposition of Theil's T by regions, with an optional
    permutation test (`inequality.theil.TheilD` / `TheilDSim`)
  * `spatial_gini/3` - how much inequality lies between neighbors (`inequality.gini.Gini_Spatial`)
  * `polarization/3` - spatial polarization index with a permutation test
    (`inequality.polarization.S`)
  * `atkinson/2` - Atkinson index and equally distributed equivalent
    (`inequality.atkinson.Atkinson`)
  * `schutz/1` - Schutz distance, intersection point and coefficient (`inequality.schutz.Schutz`)
  * `lorenz_curve/1`, `wolfson/1` - Lorenz curve and Wolfson bipolarization index
    (`inequality.wolfson`)

Values are lists of numbers (or anything `Spatex.Input.to_f64_binary/2` accepts); `theil/1` and
`theil_d/3` also take an `n x t` matrix (a list of rows) and then return one value per column.
Invalid input raises `ArgumentError`. Permutation tests take `:permutations` and `:seed`;
without a seed a random one is drawn and stored in the result, and the same seed gives
bit-identical results at any thread count.

# `atkinson`

```elixir
@spec atkinson(term(), number()) :: Spatex.Inequality.Atkinson.t()
```

Atkinson index of strictly positive values (`inequality.atkinson.Atkinson`).

`epsilon >= 0` is the inequality aversion: larger values weigh the lower tail more. `0` gives
an index of 0 and `1` uses the geometric mean. Returns a `Spatex.Inequality.Atkinson` with the
index `a` and the equally distributed equivalent `ede` (PySAL's `A` and `EDE`; the
`atkinson()` function returns only `A`).

Raises `ArgumentError` for empty input, a value that is not positive, or a negative `epsilon`.

## Examples

    iex> a = Spatex.Inequality.atkinson([10, 20, 30, 40, 50], 0.5)
    iex> Float.round(a.a, 5)
    0.06315
    iex> Float.round(a.ede, 4)
    28.1054

# `gini`

```elixir
@spec gini(term()) :: float() | :nan
```

Gini coefficient of non-negative values, `(2 sum_i i x_(i) - (n + 1) sum x) / (n sum x)`.

`:nan` when the values sum to zero.

## Examples

    iex> Spatex.Inequality.gini([1, 1, 1, 1])
    0.0
    iex> Spatex.Inequality.gini([0, 0, 0, 1])
    0.75

# `lorenz_curve`

```elixir
@spec lorenz_curve(term()) :: {[float()], [float() | :nan]}
```

Lorenz curve of a distribution (`inequality.wolfson.lorenz_curve`).

Returns `{population, share}`: the cumulative population share (`n + 1` evenly spaced points
from 0 to 1) and the cumulative share of the sorted values, both starting at 0.

## Examples

    iex> {pop, share} = Spatex.Inequality.lorenz_curve([1, 1, 2])
    iex> pop
    [0.0, 0.3333333333333333, 0.6666666666666666, 1.0]
    iex> share
    [0.0, 0.25, 0.5, 1.0]

# `polarization`

```elixir
@spec polarization(term(), Spatex.Graph.t(), keyword()) ::
  Spatex.Inequality.Polarization.t()
```

Spatial polarization index of a variable over a graph (`inequality.polarization.S`).

The values are split into groups (`:k` quantile classes, or explicit `:bins`) and the index
measures how far like groups form connected blocks of the graph: 1 when each group is one
connected block, towards 0 when the groups are scattered. Only the graph's structure is used:
every stored entry is an edge whatever its weight, and islands are components of their own.
`x` is aligned with the graph's ids (PySAL aligns a data frame's index).

Observations the grouping leaves without a group take no part: with `:bins` (right-closed,
`(a, b]`) a value equal to the first edge, and with constant data under `k > 1`, every
observation. Quantile edges that coincide are merged but the number of groups stays `k`, as in
`pandas.qcut(..., duplicates: "drop")`.

## Options

  * `:k` - number of quantile classes, an integer in `1..n` (default `2`); ignored with `:bins`.
  * `:bins` - at least two strictly increasing edges covering the data (default `nil`).
  * `:permutations` - shuffle the group labels over the nodes this many times (default `999`, as
    in PySAL); `0` skips the test. `:seed` - see the module documentation.
  * `:keep_simulations` - keep the simulated indices in `sim` (default `true`, the global
    statistic convention; PySAL's `keep_sim` defaults to `False`). `true` keeps a list, `:tensor`
    a `{permutations}` `Nx.Tensor` of type `{:f, 64}` (optional `:nx` dependency), `false` drops
    them.

Returns a `Spatex.Inequality.Polarization`. Permutation draws come from Spatex's seeded ChaCha8
streams, so `p_value` agrees with PySAL's statistically, not bit for bit.

## Examples

    iex> g = Spatex.Graph.from_arrays([0, 1, 1, 2], [1, 0, 2, 1], nil, ids: [0, 1, 2])
    iex> s = Spatex.Inequality.polarization([1, 2, 9], g, permutations: 0)
    iex> {s.statistic, s.n_i_components}
    {1.0, 2}

# `schutz`

```elixir
@spec schutz(term()) :: Spatex.Inequality.Schutz.t()
```

Schutz measures of a distribution (`inequality.schutz.Schutz`).

`y` is a vector of values (PySAL takes a data frame and a column name). Returns a
`Spatex.Inequality.Schutz`: the Schutz `distance` between the Lorenz curve and the line of
equality, the cumulative population share (`intersection_point`) where it is first reached
and the original Schutz `coefficient`. Raises `ArgumentError` for empty input or values that
sum to zero (PySAL fails with an `IndexError` there).

## Examples

    iex> s = Spatex.Inequality.schutz([1000, 2000, 1500, 3000, 2500])
    iex> {Float.round(s.distance, 2), Float.round(s.intersection_point, 1)}
    {0.15, 0.6}
    iex> Float.round(s.coefficient, 1)
    7.5

# `spatial_gini`

```elixir
@spec spatial_gini(term(), Spatex.Graph.t(), keyword()) ::
  Spatex.Inequality.SpatialGini.t()
```

Spatial Gini (`inequality.gini.Gini_Spatial`): splits the total absolute difference between
observations into pairs of neighbors and non-neighbors and tests the non-neighbor part by
permutation.

`x` is aligned with the graph's ids. The graph is read as binary: every non-zero edge makes two
observations neighbors, whatever its weight.

## Options

  * `:permutations` - default `99` (as in PySAL); `0` skips the test.
  * `:seed` - see the module documentation.
  * `:keep_simulations` - keep `wcgp` and `polarization_sim` in the result (default `true`).
    `true` keeps them as lists, `:tensor` as `{permutations}` `Nx.Tensor`s of type `{:f, 64}`
    (optional `:nx` dependency), `false` drops them.

# `theil`

```elixir
@spec theil(term()) :: float() | [float()]
```

Theil's T, `sum_i s_i ln(n s_i)` with `s_i = y_i / sum y` (zeros are lifted to the smallest
positive float, like the Python package).

A vector gives a float, an `n x t` matrix a list of `t` floats.

## Examples

    iex> Spatex.Inequality.theil([2, 2, 2])
    0.0

# `theil_d`

```elixir
@spec theil_d(term(), [term()], keyword()) :: Spatex.Inequality.TheilD.t()
```

Decomposition of Theil's T by a partition of the observations into regions
(`inequality.theil.TheilD`).

`partition` has one region label per observation (a list, `Nx` vector or `Explorer.Series`; see `Spatex.Input.to_labels/2`). Returns a
`Spatex.Inequality.TheilD`.

## Options

  * `:permutations` - shuffle the rows of `y` across observations this many times to test the
    between-group term (default `0`, `inequality.TheilDSim`), `:seed`.

# `wolfson`

```elixir
@spec wolfson(term()) :: float() | :nan | :infinity | :neg_infinity
```

Wolfson bipolarization index (`inequality.wolfson.wolfson`),
`W = (2 D50 - G) mean / median` with `D50 = 0.5 - L(0.5)`, `L` the Lorenz curve and `G` the
Gini coefficient.

## Examples

    iex> Spatex.Inequality.wolfson([6, 6, 8, 8, 10, 10, 12, 12]) |> Float.round(4)
    0.0833

---

*Consult [api-reference.md](api-reference.md) for complete listing*
