# `Geostatex.ClassificationKriging`
[🔗](https://github.com/CountlinkX-Solutions/geostatex/blob/v0.6.0/lib/geostatex/classification_kriging.ex#L1)

Classification kriging, after `pykrige.ck.ClassificationKriging` (PyKrige): simplicial
indicator kriging of a classifier's residuals.

`fit/4` fits a classifier of the labels on predictors observed at the points, then takes the
isometric log-ratio (ILR) coordinates of the one-hot labels and of the classifier's
probabilities, and krige each coordinate of their difference over the points
(`Geostatex.Kriging`). `predict_proba/3` adds the kriged residual to the ILR of the
classifier's probabilities at new points and transforms back; `predict/3` gives the most
probable class.

## Options of `fit/4`

  * `:classification` (required) - `{fit, predict_proba}`, two functions:
    `fit.(predictors, labels)` returns a model and `predict_proba.(model, predictors)` a list
    of rows, one probability per class in the order of the sorted classes (Scholar, Axon, ...).
  * `:method` - `:ordinary` (default) or `:universal` kriging of the residuals.
  * `:n_closest_points` - the moving window at prediction (default `10`, as PyKrige; `nil`
    uses every point; universal kriging uses every point).
  * Any option of `Geostatex.Kriging.ordinary/3` (or `universal/3`), with PyKrige's defaults
    `variogram_model: :linear`, `nlags: 6`; `:variogram_parameters` may also be a list with
    one set per ILR coordinate (one fewer than the classes).

`ilr/1` and `inverse_ilr/1` are PyKrige's transforms: `log(max(p, eps))` times the negated
Helmert contrasts (scipy's `helmert` without its first row), and back by `exp` and closure.
PyKrige's `predict` returns the index of the most probable class; Geostatex returns the class.

    iex> points = for x <- 0..3, y <- 0..3, do: {x * 10.0, y * 10.0}
    iex> labels = for {x, _} <- points, do: if(x < 20, do: :a, else: :b)
    iex> classifier = {fn _p, _l -> nil end, fn nil, rows -> for _ <- rows, do: [0.5, 0.5] end}
    iex> ck = Geostatex.ClassificationKriging.fit(Enum.map(points, &[elem(&1, 0)]), points, labels,
    ...>   classification: classifier,
    ...>   variogram_model: :spherical,
    ...>   variogram_parameters: %{psill: 1.0, range: 25.0, nugget: 0.0})
    iex> Geostatex.ClassificationKriging.predict(ck, [[0.0], [30.0]], [{0.0, 0.0}, {30.0, 30.0}])
    [:a, :b]

# `t`

```elixir
@type t() :: %Geostatex.ClassificationKriging{
  classes: term(),
  classification: term(),
  krigings: term(),
  method: term(),
  model: term(),
  n_closest_points: term()
}
```

# `fit`

```elixir
@spec fit(list(), term(), list(), keyword()) :: t()
```

Fits the classifier and krige the ILR residuals. See the module documentation.

# `ilr`

```elixir
@spec ilr([[number()]]) :: [[float()]]
```

PyKrige's isometric log-ratio transform of compositions (rows of `D` parts).

# `inverse_ilr`

```elixir
@spec inverse_ilr([[number()]]) :: [[float()]]
```

PyKrige's inverse isometric log-ratio transform (rows of `D - 1` coordinates).

# `krige_residual`

```elixir
@spec krige_residual(t(), term()) :: [[float()]]
```

The kriged ILR residuals at `points`: one row per point, one value per ILR coordinate.

# `predict`

```elixir
@spec predict(t(), list(), term()) :: list()
```

The most probable class at `points`.

# `predict_proba`

```elixir
@spec predict_proba(t(), list(), term()) :: [[float()]]
```

The class probabilities at `points`: the classifier's corrected by the kriged residuals.

# `score`

```elixir
@spec score(t(), list(), term(), list()) :: float()
```

The share of `labels` that `predict/3` gets right.

---

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