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

Ordinary cokriging with a linear model of coregionalisation (R `gstat`'s multivariable
prediction, each variable with a constant mean `~ 1`), and the model's fit (`gstat::fit.lmc`).

A linear model of coregionalisation (LMC) of `n` variables is a list of structures, each a
model, a range and a symmetric, positive semi-definite `n x n` matrix of partial sills: every
direct and cross variogram is `sum sills[a][b] * unit(h)`. Models: `:nugget`, `:spherical`,
`:exponential` and `:gaussian` with Geostatex's (PyKrige's) ranges (gstat's `Exp` and `Gau`
take `range / 3` and `4 / 7 * range`).

The variables may be observed at different points (heterotopic data). The system holds the
covariances between every observation, bordered by one unbiasedness constraint per variable;
`execute/5` gives each variable's estimate, its cokriging variance and the error covariances
between variables.

## Examples

    iex> zn = {[{0, 0}, {10, 0}, {0, 10}, {10, 10}], [1.0, 2.0, 1.5, 3.0]}
    iex> cu = {[{0, 0}, {10, 0}, {0, 10}, {10, 10}], [10.0, 12.0, 11.0, 14.0]}
    iex> ck = Geostatex.Cokriging.new([zn, cu],
    ...>   structures: [
    ...>     %{model: :nugget, sills: [[0.05, 0.02], [0.02, 0.1]]},
    ...>     %{model: :spherical, range: 20.0, sills: [[0.5, 0.6], [0.6, 1.2]]}
    ...>   ])
    iex> %{estimates: [[z1], [z2]]} = Geostatex.Cokriging.execute(ck, :points, [0.0], [0.0])
    iex> {Float.round(z1, 9), Float.round(z2, 9)}
    {1.0, 10.0}

# `t`

```elixir
@type t() :: %Geostatex.Cokriging{n: term(), structures: term(), variables: term()}
```

# `execute`

```elixir
@spec execute(t(), :points | :grid, [number()], [number()], keyword()) :: map()
```

Predictions at `:points` (`{xs[i], ys[i]}`) or on the `:grid` of `xs` and `ys` (rows of y):
`%{estimates:, variances:, covariances:}`, with one list per variable for the estimates and
variances and `covariances` keyed by `{i, j}` (`i < j`, 0-based).

# `fit_lmc`

```elixir
@spec fit_lmc(map(), [map()], keyword()) :: [map()]
```

gstat's `fit.lmc` with the ranges fixed: fits the partial sills of `structures` (a list of
`%{model:, range:}`) to the sample `samples`, a map from `{i, j}` (`i <= j`, 0-based, every
pair once) to `%{lags:, gamma:, counts:}` (as gstat's `variogram()` gives them: `dist`,
`gamma`, `np`). Each direct and cross variogram is fitted by weighted least squares (weights
`counts / lags^2`, gstat's `fit.method = 7`); each structure's sill matrix is then replaced by
its closest positive semi-definite matrix and its diagonal multiplied by
`:correct_diagonal` (default `1.0`). Returns the structures with their `sills:`.

# `new`

```elixir
@spec new([{term(), term()}], keyword()) :: t()
```

Sets up ordinary cokriging of `variables`, a list of `{points, values}` (2D points), with the
LMC `structures:` (a list of `%{model:, range:, sills:}`; `:nugget` needs no range). Raises if
the model is not a valid LMC.

---

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