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

Gaussian spatial random fields by the randomisation method (`gstools.SRF`), unconditional or
conditioned on data by kriging (`gstools.CondSRF`).

A field is `mean + sqrt(psill / mode_no) * sum_j (z1_j cos(k_j . x) + z2_j sin(k_j . x))` plus
white noise of variance `nugget`, with normal amplitudes `z1`, `z2` and wave vectors `k_j`
drawn from the variogram model's spectral density: its covariance is the model's.

The random numbers are GSTools' (numpy's legacy `RandomState`, a master stream seeding each
use), so for 2D exponential and gaussian models a seed gives GSTools' field (verified to 1e-9).
For 3D and spherical models GSTools samples the spectrum by MCMC; Geostatex samples it exactly
instead, so the realisations differ from GSTools' for a seed but follow the same distribution.

Models: `:spherical`, `:exponential` and `:gaussian` with `variogram_parameters`
`%{psill:, range:, nugget:}` as in `Geostatex.Kriging`. GSTools' models map onto these with
`rescale=1`: `Exponential(len_scale: range / 3)`, `Gaussian(len_scale: 4 / 7 * range)`,
`Spherical(len_scale: range)`, `var: psill`, `anis: 1 / anisotropy_scaling`.

Results are `%{field: values, seed: seed}`: `:points` gives one value per point, `:grid` rows of
y as `Geostatex.Kriging.execute/5` (3D: planes of z), and `seed` is the seed used (`seed: nil`
draws one).

## Examples

    iex> %{field: [a, b], seed: 7} =
    ...>   Geostatex.Simulation.unconditional(:points, [0.0, 50.0], [0.0, 0.0],
    ...>     variogram_model: :exponential,
    ...>     variogram_parameters: %{psill: 1.0, range: 30.0, nugget: 0.0},
    ...>     seed: 7
    ...>   )
    iex> is_float(a) and is_float(b)
    true

# `conditional`

```elixir
@spec conditional(
  Geostatex.Kriging.t(),
  :points | :grid,
  [number()],
  [number()],
  keyword()
) :: map()
```

A 2D field conditioned on the data of `kriging` (`gstools.CondSRF`), a simple or ordinary
kriging model (`Geostatex.Kriging.simple/3`, `ordinary/3`) with a spherical, exponential or
gaussian variogram: the kriging estimate plus the model's field scaled by the kriging standard
deviation, `estimate + sqrt(max(v - nugget, 0) / psill) * field + sqrt(min(v, nugget) / nugget)
* noise` (`v` the kriging variance). It reproduces the data where the kriging variance is 0
(exact values; at a data point the variance is rounding noise, so the data are matched to
about 1e-8, as in GSTools) and has the kriging estimate and variance as its pointwise mean and
variance.
The field uses the kriging's model and anisotropy. Options: `:mode_no`, `:seed`.

# `conditional_3d`

```elixir
@spec conditional_3d(
  Geostatex.Kriging.t(),
  :points | :grid,
  [number()],
  [number()],
  [number()],
  keyword()
) :: map()
```

A 3D field conditioned on a 3D simple or ordinary kriging model, as `conditional/5`.

# `unconditional`

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

An unconditional 2D field (`gstools.SRF`) at `:points` (`{xs[i], ys[i]}`) or on the `:grid` of
`xs` and `ys`. Options:

  * `:variogram_model` - `:spherical` (default), `:exponential` or `:gaussian`;
  * `:variogram_parameters` - `%{psill:, range:, nugget:}` (required);
  * `:mean` - the field's mean (default `0.0`);
  * `:mode_no` - the number of modes (default `1000`);
  * `:seed` - an integer in `0..4294967295`, or `nil` (default) to draw one;
  * `:anisotropy_scaling`, `:anisotropy_angle` - as in `Geostatex.Kriging.ordinary/3`,
    applied about the origin (GSTools' `anis` and `angles`).

# `unconditional_3d`

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

An unconditional 3D field at `:points` or on the `:grid` of `xs`, `ys` and `zs`: the options of
`unconditional/4` with the 3D anisotropy of `Geostatex.Kriging.ordinary_3d/3`.

---

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