Geostatex.Variogram (Geostatex v0.6.0)

Copy Markdown View Source

Experimental variograms, after skgstat.Variogram (scikit-gstat).

new/3 measures how the difference between two observations grows with the distance between them: every pair of points is assigned to a distance lag and the lag's semivariance is estimated from the absolute value differences of its pairs.

Options

  • :estimator - :matheron (default), :cressie, :dowd, :genton, :minmax, :percentile (with :percentile, default 50) or :entropy (with :entropy_bins, default 50). An empty lag gives :nan.
  • :n_lags - number of lags for :even and :uniform (default 10).
  • :bin_func - how the lag edges are set: :even (default, n_lags equal widths from 0 to maxlag), :uniform (n_lags lags with the same number of pairs), one of numpy's histogram_bin_edges rules :sturges, :scott, :sqrt, :fd, :doane or :rice (which set the number of lags themselves, over the range of the kept distances), or an ascending list of upper edges (which also sets maxlag to the last edge).
  • :maxlag - the largest lag: nil (default, the largest distance), a number below 1 (a share of the largest distance), a number of at least 1 (a distance), :mean or :median (of all distances).
  • :dist_func - the coordinate distance, with scipy's names and defaults: :euclidean (default), :cityblock, :chebyshev, :sqeuclidean, :seuclidean (each axis divided by its variance), :canberra, :braycurtis, :cosine, :mahalanobis (with the inverse covariance of the coordinates) or :minkowski (p = 2, the Euclidean distance).
  • :direction - only the point pairs in one direction, as skgstat.DirectionalVariogram (see below).
  • :co_values - a co-variable observed at the same points: the result is a cross-variogram (see below).
  • :samples - only the pairs between two random subsets of the points, as scikit-gstat's samples: an integer is the size of each subset, a float in (0, 1] its share of the points (rounded down). Euclidean distances only, and not with :direction.
  • :seed - the seed of :samples (an integer in 0..2^64-1); without one a random seed is drawn and recorded in the result, and the same seed gives the same pairs.

As in scikit-gstat the edges are upper edges and a pair at or beyond the last edge is left out, so a pair exactly at maxlag does not count. :entropy bins the value differences with entropy_bins - 1 equal bins of the distance range, which is scikit-gstat's choice.

Directional variograms

With direction: [...] only the pairs whose separation vector points along an azimuth enter the lags (which are set from those pairs alone; maxlag still from all of them):

  • :azimuth - degrees from east, positive clockwise, in [-180, 180] (default 0); a direction and its opposite are the same;
  • :tolerance - the full opening angle around the azimuth in degrees, [0, 360] (default 45.0);
  • :bandwidth - the largest distance across the azimuth, a number or {:percentile, q} of the pair distances (default {:percentile, 33}, scikit-gstat's "q33");
  • :model - the search area: :triangle (default, the tolerance angle cut by the bandwidth) or :compass (the tolerance angle alone).

The direction of a pair comes from its Euclidean coordinates whatever :dist_func, and coincident points have none (they are left out). The resolved values are in direction.

iex> v = Geostatex.Variogram.new([{0, 0}, {1, 0}, {0, 1}, {2, 0}], [1, 2, 3, 5],
...>   bin_func: [1.5, 2.5], fit_method: nil,
...>   direction: [azimuth: 0, tolerance: 10.0, model: :compass])
iex> {v.bin_count, v.experimental}
{[2, 1], [2.5, 8.0]}

Pair sampling

For many points, samples: k measures only the pairs between two subsets of k points drawn at random (the left one first), as skgstat.Variogram(samples=k, binning_random_state=seed): the draws reproduce numpy's RandomState(MT19937(SeedSequence(seed))).choice bit for bit, so a seed selects the same pairs as in scikit-gstat. A pair (l, r) enters when r < l and the points are apart (duplicate locations are left out).

Cross-variograms

With co_values: b the lags hold the joint variation of values (a) and b, with one of two definitions (:cross):

  • :skgstat (default) - scikit-gstat's: the estimator runs on |a_i - a_j| * |b_i - b_j|, so Matheron gives sum((|a_i - a_j| |b_i - b_j|)^2) / 2N, which is never negative;
  • :classical - the cross-semivariogram of the literature, sum((a_i - a_j) (b_i - b_j)) / 2N, negative when the variables vary in opposite directions (Matheron only).

cross_variograms/3 computes every pair of several variables. The struct's cross is the definition used (nil for a variogram of one variable).

iex> v = Geostatex.Variogram.new([{0, 0}, {1, 0}, {2, 0}, {3, 0}], [1, 2, 4, 7],
...>   co_values: [2, 1, 5, 3], cross: :classical, bin_func: :even, n_lags: 2,
...>   fit_method: nil)
iex> v.experimental
[0.16666666666666666, 4.75]

Summed models

model: [:spherical, :gaussian] fits the sum of several models (scikit-gstat's "spherical+gaussian"): cof holds each component's [range, sill, (shape)] in order and one nugget at the end (with use_nugget: true); parameters is cof with the nugget always last (0 without one). Each component is bounded as alone (range <= max(lag), sill <= max(semivariance)), and model_values/3 evaluates a summed model the same way.

iex> Geostatex.Variogram.model_values([:spherical, :exponential], [10.0, 1.0, 30.0, 2.0, 0.5], [0, 10])
[0.5, 2.7642411176571153]

Harmonized variograms

model: :harmonize replaces the fit by scikit-gstat's harmonization: an increasing isotonic regression of the non-empty lags (pool adjacent violators, as sklearn's IsotonicRegression), interpolated linearly between them and :nan outside. cof is [range, sill, nugget] read off 100 points from 0 to the last non-empty lag: sill is 95% of the model's largest value and range the first of those points to reach it (scikit-gstat records that point's index), and with use_nugget: true the nugget is the model at 0 (:nan unless a lag starts there). :fit_method and :fit_sigma do not apply.

Fitting a model

As scikit-gstat does, new/3 also fits a theoretical model to the experimental values (at the upper lag edges, empty lags left out), unless fit_method: nil:

  • :model - :spherical (default), :exponential, :gaussian, :cubic, :stable (with a shape in (0, 2]) or :matern (smoothness in (0, 20]). range is the effective range. GSTools' models (M17) :integral, :rational, :circular, :superspherical, :jbessel, :tpl_gaussian, :tpl_exponential and :tpl_simple: range is GSTools' len_scale (rescale = 1) and the shape is bounded by GSTools' default bounds in 2D. :harmonize is not fitted but harmonized (see below).
  • :fit_method - :trf (default, bounded least squares: 0 <= range <= max(lag), 0 <= sill <= max(semivariance), nugget below 0.99 * max(semivariance)), :lm (unbounded), :ml (scikit-gstat's maximum likelihood: Gaussian errors of unit variance weighted by 1 / sigma, which is least squares within the :trf bounds with each residual divided by sqrt(sigma)) or :manual (with :fit_range, :fit_sill and optionally :fit_nugget, :fit_shape, default 2.0).
  • :fit_bounds - {lower, upper}, lists in the order of cof ([range, sill, (shape), (nugget)], the nugget with use_nugget: true), replacing the default box of :trf (scikit-gstat's fit_bounds; :lm is unbounded and :ml uses (0, fit_p0)).
  • :fit_p0 - the initial guess in the same order (scikit-gstat's fit_p0, default the upper bounds); for :ml it is also the upper bounds, its half the start. The fit still tries its other starts and keeps the lowest cost. Not for summed models.
  • :use_nugget - fit a nugget (default false).
  • :fit_sigma - weights: nil, :linear, :exp, :sqrt, :sq (functions of the lag over the largest lag), :entropy (1 / h, h the Shannon entropy of each lag's value differences over Scott's bins of all the pair distances, as scikit-gstat) or one value per lag; each residual is divided by its sigma.

The result holds cof ([range, sill, (shape), (nugget)], as scikit-gstat), parameters ([range, sill, (shape), nugget]), model_at_bins and the goodness of fit rmse, mse, mae, nrmse, nrmse_r, r (Pearson) and nse (Nash-Sutcliffe). transform/2 evaluates the fitted model.

The optimiser differs from scikit-gstat's (scipy's TRF from the upper bounds): the range (and shape) is scanned on a grid with the sill and nugget solved exactly at each point, and the best points are refined by a bounded Levenberg-Marquardt. It reaches scikit-gstat's optimum where scipy finds it and a lower one where scipy stops in a local minimum (see docs/parity.md).

Examples

iex> points = [{0, 0}, {1, 0}, {2, 0}, {3, 0}]
iex> v = Geostatex.Variogram.new(points, [1, 2, 4, 7], n_lags: 2)
iex> {v.bins, v.bin_count}
{[1.5, 3.0], [3, 2]}

Summary

Functions

The variograms of every pair of variables (a list of value lists, each observed at points), after skgstat.util.cross_variograms: row i holds the cross-variograms of variable i with every other one (co_values) and its own variogram on the diagonal. opts are those of new/3 (:cross picks the definition) without :co_values.

The variogram model model with coefficients cof ([range, sill, nugget], or [range, sill, shape, nugget] for :stable and :matern; the nugget may be left out) at the lags h.

The experimental variogram of values observed at points ([{x, y}], [[x, y]], an {n, 2} tensor or a {xs, ys} pair; see Geostatex.Input). See the module documentation for the options.

The fitted model evaluated at the lags h (skgstat.Variogram.transform).

Types

t()

@type t() :: %Geostatex.Variogram{
  bin_count: [non_neg_integer()],
  bin_func: atom() | [float()],
  bins: [float()],
  cof: [float()] | nil,
  cross: :skgstat | :classical | nil,
  direction: map() | nil,
  dist_func: atom(),
  estimator: atom(),
  experimental: [float() | :nan],
  fit_method: atom() | nil,
  fit_sigma: atom() | [float()] | nil,
  mae: float() | :nan | nil,
  maxlag: float() | nil,
  model: atom() | [atom()],
  model_at_bins: [float()] | nil,
  mse: float() | :nan | nil,
  n: pos_integer(),
  n_lags: pos_integer(),
  nrmse: float() | nil,
  nrmse_r: float() | nil,
  nse: float() | nil,
  parameters: [float()] | nil,
  r: float() | nil,
  rmse: float() | nil,
  samples: pos_integer() | float() | nil,
  seed: non_neg_integer() | nil,
  use_nugget: boolean()
}

Functions

cross_variograms(points, variables, opts \\ [])

@spec cross_variograms(term(), [term()], keyword()) :: [[t()]]

The variograms of every pair of variables (a list of value lists, each observed at points), after skgstat.util.cross_variograms: row i holds the cross-variograms of variable i with every other one (co_values) and its own variogram on the diagonal. opts are those of new/3 (:cross picks the definition) without :co_values.

model_values(model, cof, h)

@spec model_values(atom() | [atom()], [number()], [number()]) :: [float()]

The variogram model model with coefficients cof ([range, sill, nugget], or [range, sill, shape, nugget] for :stable and :matern; the nugget may be left out) at the lags h.

iex> Geostatex.Variogram.model_values(:spherical, [10.0, 2.0, 0.5], [0, 5, 10, 20])
[0.5, 1.875, 2.5, 2.5]

new(points, values, opts \\ [])

@spec new(term(), term(), keyword()) :: t()

The experimental variogram of values observed at points ([{x, y}], [[x, y]], an {n, 2} tensor or a {xs, ys} pair; see Geostatex.Input). See the module documentation for the options.

transform(v, h)

@spec transform(t(), [number()]) :: [float()]

The fitted model evaluated at the lags h (skgstat.Variogram.transform).