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

Geographically weighted regression (Gaussian, Poisson and binomial families), bandwidth
selection and multiscale GWR (MGWR), after `mgwr` (PySAL).

`fit/4` estimates one weighted regression per observation, with weights that decay with
distance, giving spatially varying coefficients, standard errors, local R-squared and the
diagnostics of `mgwr.gwr.GWRResults`. `select_bandwidth/4` picks the bandwidth by
golden-section (or equal-interval) search over AICc, AIC, BIC or the cross-validation score.
`mgwr/4` lets every covariate have its own bandwidth (backfitting) and computes standard
errors from the per-covariate hat matrices. Results are checked against mgwr 2.2.1 on the
Georgia example and on seeded synthetic data.

## Families

`family: :gaussian` (the default) is weighted least squares. `family: :poisson` (log link,
optional `offset:` exposure) and `family: :binomial` (logit link, 0/1 responses) are fitted
at every location by iteratively re-weighted least squares exactly as `spglm.iwls` does,
including its starting values and its stopping rule (the smallest coefficient change is
compared with `iwls_tol`, default `1.0e-5`, at most `iwls_max_iter` = 20 iterations). Their
information criteria are deviance based (`AIC = deviance + 2 tr(S)`), and results carry
`d2`, `adj_d2` (percent deviance explained) and the local `p_dev`.

## Bandwidths and kernels

With `fixed: false` (the default, as in mgwr) the bandwidth is a number of nearest neighbors,
the focal point included, and the kernel adapts to the local point density. With `fixed: true`
it is a distance in the units of the coordinates (kilometers with `spherical: true`, where
points are `{longitude, latitude}` in degrees). The kernels are `:bisquare` (the default,
zero beyond the bandwidth), `:gaussian` and `:exponential` (never zero).

## Memory and cost

Neither the hat matrix nor the weight matrix is ever stored: each location's weights are
recomputed on the fly and only per-location vectors are kept, so memory is `O(n * k)`. A fit
costs `O(n^2 * k^2)` time (in parallel over locations; a GLM multiplies that by the IWLS
iterations) and a bandwidth search repeats that a few dozen times. Both raise for more than
`max_n` observations (default `10_000`) unless you raise the limit explicitly.

MGWR inference needs one `n x n` hat matrix per covariate. They are never held in full: they
are built in column blocks (`chunk_size`, default 256), so memory is about
`8 * (k + 2) * n * chunk_size` bytes, but the time is `O(iterations * k * n^3)`. `mgwr/4`
therefore refuses inference above `max_n` observations (default `5_000`); see its docs.

## Examples

    iex> pts = for r <- 0..3, c <- 0..3, do: {c * 1.0, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 1.0 + 2.0 * xv + rem(i, 3) * 0.1
    iex> r = Spatex.GWR.fit(pts, y, [x], bw: :infinity)
    iex> {r.n, r.k, Float.round(r.tr_s, 6)}
    {16, 2, 2.0}

# `bandwidth_intervals`

```elixir
@spec bandwidth_intervals(
  Spatex.GWR.Bandwidth.t() | Spatex.GWR.MGWRResult.t(),
  keyword()
) ::
  {number(), number()} | [{number(), number()}]
```

Confidence intervals for the bandwidth (`GWRResults.get_bws_intervals`, `MGWRResults.
get_bws_intervals`), after Li et al. (2020).

The evaluations `{bw, score}` of the bandwidth search are sorted by score, turned into Akaike
weights `exp(-0.5 * (score - best))` (normalized to sum to one) and accumulated; the interval
spans the smallest and largest bandwidth among the `n` best evaluations, `n` being the number
of cumulative weights below `level` plus one. The result therefore depends on the search that
produced the history: a coarse or short search gives a coarse interval.

`source` is a `Spatex.GWR.Bandwidth` (from `select_bandwidth/4`; returns one `{lower, upper}`)
or a `Spatex.GWR.MGWRResult` (from `mgwr/4`; returns one `{lower, upper}` per column, from the
searches of the last backfitting iteration that searched, as mgwr does). An adaptive
bandwidth gives neighbor counts, a fixed one distances. `search: :scipy` keeps no history,
as in mgwr, so it raises `ArgumentError`.

## Options

  * `:level` - cumulative weight, in `(0, 1]` (default `0.95`).

## Examples

    iex> b = %Spatex.GWR.Bandwidth{history: [{10, 100.0}, {20, 101.0}, {30, 102.0}, {40, 110.0}]}
    iex> Spatex.GWR.bandwidth_intervals(b, level: 0.9)
    {10, 30}

# `critical_tval`

```elixir
@spec critical_tval(Spatex.GWR.Result.t() | Spatex.GWR.MGWRResult.t(), number() | nil) ::
  float() | [float()]
```

The t critical value `t.ppf(1 - alpha / 2, n - 1)` for a fitted model.

Without `alpha` it is the value for the corrected 0.05 level (`result.critical_tval`), like
mgwr's `critical_tval()`.

## Examples

    iex> pts = for r <- 0..4, c <- 0..4, do: {c * 1.0, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 2.0 * xv + rem(i, 4) * 0.1
    iex> r = Spatex.GWR.fit(pts, y, x, bw: :infinity)
    iex> Float.round(Spatex.GWR.critical_tval(r, 0.05), 4)
    2.0639

# `filter_tvals`

```elixir
@spec filter_tvals(Spatex.GWR.Result.t() | Spatex.GWR.MGWRResult.t(), keyword()) :: [
  [float() | atom()]
]
```

The t-values with those that are not significant set to `0.0` (mgwr's `filter_tvals`).

A t-value is kept when `abs(t)` reaches the critical value (the strict inequality of mgwr).
For an MGWR result the default critical value is the per-covariate list `critical_tval`.

## Options

  * `:critical_t` - the critical value to use directly (a list per column for MGWR).
  * `:alpha` - a plain significance level (two-sided) to derive it from.
  * Without either, the multiple-testing corrected 0.05 level (`result.critical_tval`).

# `fit`

```elixir
@spec fit(term(), term(), term(), keyword()) :: Spatex.GWR.Result.t()
```

Fits Gaussian GWR at every observation.

`points` are `{x, y}` pairs (or anything `Spatex.Input.to_points_binary/1` accepts), `y` the
response and `x` the regressors: a list of columns, a single column, a `name => column` map
(ordered by name) or an `{n, k}` `Nx.Tensor`. Like mgwr, constant columns of `x` are dropped
and an intercept is prepended unless `constant: false`.

## Options

  * `:bw` (required) - the bandwidth: a neighbor count (adaptive), a distance (`fixed: true`)
    or `:infinity` for the global model. A fractional adaptive count is truncated, with a
    warning, like mgwr. An adaptive count must be between 2 and `n`.
  * `:kernel` - `:bisquare` (default), `:gaussian` or `:exponential`.
  * `:fixed` - fixed distance bandwidth (default `false`, adaptive).
  * `:constant` - add an intercept (default `true`).
  * `:spherical` - great-circle distances in km for `{lon, lat}` degrees (default `false`).
  * `:sigma2_v1` - residual variance `resid_ss / (n - tr(S))` (default `true`, mgwr's default)
    instead of `resid_ss / (n - 2 tr(S) + tr(S'S))`; `enp` follows.
  * `:max_n` - refuse more observations than this (default 10000).
  * `:family` - `:gaussian` (default), `:poisson` or `:binomial`. Poisson needs a
    non-negative `y`; binomial a `y` of `0`s and `1`s.
  * `:offset` - Poisson exposure (one strictly positive number per observation; default
    ones). Not allowed for the other families.
  * `:iwls_tol`, `:iwls_max_iter` - IWLS stopping rule of the GLM families (defaults
    `1.0e-5` and `20`, mgwr's).
  * `:hat_matrix` - keep the full `n x n` hat matrix `S` in `result.hat_matrix` (mgwr's
    `hat_matrix=True`; `S y` are the fitted values and `diag(S)` is `influ`): `true` for a list
    of `n` rows, `:tensor` for an `{n, n}` `Nx.Tensor` of type `{:f, 64}` (needs the optional
    `:nx` dependency), `false` (default) to keep the `O(n k)` memory of an ordinary fit. For the
    GLM families the entries are the final IWLS weighted operator, as mgwr's.
  * `:max_cells` - with `hat_matrix`, refuse more than this many cells `n^2` (default
    4000000, `n = 2000`).

GLM results leave `local_r2`, `r2` and `adj_r2` as `nil` and fill `deviance`,
`resid_deviance`, `p_dev` and `iterations` instead; for a zero Poisson count Spatex uses
`0 * ln(0) = 0` in the local deviance where mgwr returns `NaN`.

Raises `ArgumentError` on invalid input or a singular local design matrix.

## Examples

    iex> pts = for r <- 0..4, c <- 0..4, do: {c * 1.0, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 2.0 * xv + rem(i, 4) * 0.1
    iex> r = Spatex.GWR.fit(pts, y, x, bw: 12, kernel: :gaussian)
    iex> {length(r.params), length(hd(r.params))}
    {25, 2}

With `hat_matrix: true` the full hat matrix is kept; `S y` are the fitted values:

    iex> pts = for r <- 0..4, c <- 0..4, do: {c * 1.0, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 2.0 * xv + rem(i, 4) * 0.1
    iex> r = Spatex.GWR.fit(pts, y, x, bw: 12, kernel: :gaussian, hat_matrix: true)
    iex> {length(r.hat_matrix), length(hd(r.hat_matrix))}
    {25, 25}

# `local_collinearity`

```elixir
@spec local_collinearity(term(), term(), keyword()) :: Spatex.GWR.Collinearity.t()
```

Local collinearity diagnostics of the geographically weighted design matrix
(`GWRResults.local_collinearity` and `MGWRResults.local_collinearity` of mgwr).

`points` and `x` are as in `fit/4` (the response plays no part, so none is passed; constant
columns of `x` are dropped and an intercept is added unless `constant: false`). For every
location the kernel weights are normalized and give

  * the local correlation coefficients and variance inflation factors of the explanatory
    variables (the intercept left out) - with `:bw` only;
  * the local condition number and the variance-decomposition proportions of the smallest
    singular value of the column-scaled weighted design (Belsley, Kuh and Welsch).

With `:bws`, one bandwidth per design column as an MGWR has (pass `mgwr/4`'s `bws`; the
design must be the one the MGWR was fitted on, standardized if it was), only the condition
number and the decomposition proportions exist, as in mgwr.

At least two explanatory variables are needed (mgwr crashes inside numpy with fewer; Spatex
raises). A singular local correlation matrix raises `ArgumentError`.

## Options

  * `:bw` - the bandwidth: a neighbor count (adaptive), a distance (`fixed: true`) or
    `:infinity`. Exactly one of `:bw` and `:bws` is required.
  * `:bws` - a list with one such bandwidth per design column (the intercept first).
  * `:kernel`, `:fixed`, `:constant`, `:spherical`, `:max_n` - as in `fit/4`.

Returns a `Spatex.GWR.Collinearity`.

## Examples

    iex> pts = for r <- 0..4, c <- 0..4, do: {c * 1.0, r * 1.0}
    iex> x1 = for {cx, _} <- pts, do: cx
    iex> x2 = for {_, cy} <- pts, do: cy
    iex> x3 = for {{cx, cy}, i} <- Enum.with_index(pts), do: cx * cy + rem(i, 3)
    iex> r = Spatex.GWR.local_collinearity(pts, [x1, x2, x3], bw: 18)
    iex> {r.n, r.k, length(hd(r.vif)), length(hd(r.vdp))}
    {25, 4, 3, 4}

# `mgwr`

```elixir
@spec mgwr(term(), term(), term(), keyword()) :: Spatex.GWR.MGWRResult.t()
```

Multiscale GWR (Gaussian): a separate bandwidth for every covariate.

The bandwidths come from backfitting (`mgwr.search.multi_bw`): starting from a GWR with one
bandwidth (`init_bw`, searched when not given), each covariate's partial residual is
smoothed by a univariate GWR whose bandwidth is searched with the same golden-section (or
interval) search as `select_bandwidth/4`, cycling through the covariates until the relative
change of the fitted surfaces drops below `:tol_multi`. Then the effective number of
parameters per covariate (`enp_j`) and the standard errors follow from the hat matrices of
the model (Li and Fotheringham, 2020). Arguments are as in `fit/4`.

mgwr expects `y` and the regressors to be standardized (mean 0, standard deviation 1) so the
bandwidths are comparable; nothing enforces it. Pass `standardize: true` to have Spatex do it
(coefficients are then on the standardized scale).

## Options

  * `:kernel`, `:fixed`, `:constant`, `:spherical` - as in `fit/4`. With `:constant` the
    intercept is the first of the `k` columns, so per-column lists start with it.
  * `:criterion`, `:search`, `:interval`, `:tol`, `:max_iter` - the bandwidth search settings
    of `select_bandwidth/4`, used for the initial and every univariate search.
  * `:multi_bw_min`, `:multi_bw_max` - search bounds per column: a number (all columns) or a
    list of `k` numbers or `nil`s. The `bw_min`/`bw_max` of `select_bandwidth/4` do not exist
    here (mgwr ignores them for MGWR as well).
  * `:init_bw` - starting bandwidth (default: searched with a one-bandwidth GWR).
  * `:tol_multi` (`1.0e-5`), `:max_iter_multi` (`200`) - backfitting convergence.
  * `:rss_score` - converge on the relative change of the residual sum of squares instead of
    mgwr's change-of-surface score (default `false`).
  * `:bws_same_times` - stop searching bandwidths after this many iterations without change
    (default `5`).
  * `:standardize` - standardize `y` and the non-intercept columns first (default `false`).
  * `:inference` - compute `enp_j`, standard errors and the criteria (default `true`). With
    `false` only the bandwidths, coefficients, fitted values and `resid_ss`/`r2` are returned.
  * `:chunk_size` - hat-matrix block width (default 256); it changes memory, not results.
  * `:max_n` - inference is refused for more observations than this (default `5_000`): it
    costs `O(iterations * k * n^3)` time and about `8 * (k + 2) * n * chunk_size` bytes. Raise
    it deliberately or pass `inference: false`.

Returns a `Spatex.GWR.MGWRResult`. Like mgwr's, the standard errors divide by the regressor
values, so a regressor with zero entries (a dummy) yields non-finite standard errors; a
warning names such columns. The fields mgwr cannot compute for MGWR (`influ`, `std_res`,
`cooks_d`, `cv`, local R-squared) are `nil`.

## Examples

    iex> pts = for r <- 0..7, c <- 0..7, do: {c + 0.05 * rem(r * 7 + c, 5), r + 0.04 * rem(c * 3 + r, 7)}
    iex> x1 = for {px, py} <- pts, do: :math.sin(px) + :math.cos(py)
    iex> x2 = for {px, py} <- pts, do: :math.cos(2 * px) - 0.3 * py
    iex> y = for {{px, _}, a, b} <- Enum.zip([pts, x1, x2]), do: 1.0 + 0.2 * px * a + 0.5 * b
    iex> r = Spatex.GWR.mgwr(pts, y, [x1, x2], fixed: true, standardize: true)
    iex> {r.n, r.k, length(r.bws), r.converged}
    {64, 3, 3, true}

# `predict`

```elixir
@spec predict(term(), term(), term(), term(), term(), keyword()) ::
  Spatex.GWR.Prediction.t()
```

Predicts at new locations (`mgwr.gwr.GWR.predict`).

The model is calibrated on `points`, `y`, `x` exactly as in `fit/4`; at every point of
`new_points` the weighted regression of the calibration sample (weights from the distances
to the calibration locations) gives local coefficients, their standard errors and the
prediction `x_new . params`. `x_new` has the same columns as `x` (a list of columns, a single
column, a `name => column` map or an `{m, k}` tensor with one row per new point); constant
columns of `x` are dropped from `x_new` too and the intercept is added unless `constant:
false`.

Nothing of size `m x n` is stored: weights are recomputed per prediction point, in parallel,
so memory is `O(m k)` plus `O(n)` per worker. The calibration sample is limited by `max_n`.

## Options

As in `fit/4` (`:bw` is required) plus

  * `:exog_scale` and `:exog_resid` - the residual variance and the response residuals of the
    calibration sample (`n` values) to use for the standard errors and the local R-squared.
    Both or neither: by default (mgwr's behavior) a model is fitted at the calibration
    locations first and its `scale` and `resid` are used. Giving them skips that fit.

For `family: :poisson` and `:binomial` the predictions are the linear predictor (log mean or
log-odds), as in mgwr, and there is no local R-squared. Unlike mgwr, more prediction points
than calibration observations are supported (mgwr fails with an `IndexError`).

Returns a `Spatex.GWR.Prediction`. Raises `ArgumentError` on invalid input or when a local
design matrix is singular, for example a fixed bandwidth with no calibration point in range
of a prediction point.

## Examples

    iex> pts = for r <- 0..4, c <- 0..4, do: {c * 1.0, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 1.0 + 2.0 * xv + rem(i, 4) * 0.1
    iex> p = Spatex.GWR.predict(pts, y, x, [{1.5, 1.5}], [[1.5]], bw: :infinity)
    iex> {p.m, length(hd(p.params)), Float.round(hd(p.predictions), 3)}
    {1, 2, 4.142}

# `select_bandwidth`

```elixir
@spec select_bandwidth(term(), term(), term(), keyword()) :: Spatex.GWR.Bandwidth.t()
```

Selects the bandwidth that minimizes an information criterion (`mgwr.sel_bw.Sel_BW`).

Arguments are as in `fit/4`. Returns a `Spatex.GWR.Bandwidth`.

## Options

  * `:criterion` - `:aicc` (default), `:aic`, `:bic` or `:cv`.
  * `:kernel`, `:fixed`, `:constant`, `:spherical`, `:max_n`, `:family`, `:offset`,
    `:iwls_tol`, `:iwls_max_iter` - as in `fit/4`. For the GLM families the criteria are the
    deviance based ones (`:cv` is computed from the response residuals).
  * `:search` - `:golden_section` (default), `:interval` or `:scipy`.
  * `:bw_min`, `:bw_max` - search bounds. By default adaptive searches run from
    `40 + 2 * (columns + 1)` neighbors to `n`, which therefore needs `n` of at least that size
    (mgwr crashes inside numpy for smaller samples; Spatex raises an `ArgumentError`), and
    fixed searches from half the smallest inter-point distance to twice the largest.
  * `:include_global` - also evaluate the global model (`bw: :infinity`) and keep it when it
    is at least as good (mgwr's `bw_max=np.inf`). Cannot be combined with a numeric `:bw_max`.
  * `:x_glob` - mgwr's `X_glob`: extra columns (any shape accepted for `x`) that only change
    the default search bounds. Like mgwr's `Sel_BW`, which has no semi-parametric fit, Spatex
    uses only their number: the default adaptive lower bound becomes
    `40 + 2 * (local columns + x_glob columns + 1)` neighbors. The values are never read (each
    column must still have `n` numbers) and explicit `:bw_min`/`:bw_max` take precedence.
  * `:interval` - step of the `:interval` search, which evaluates `bw_min, bw_min + interval,
    ...` up to `bw_max` (all three are required).
  * `:tol` - golden-section tolerance on the change of the criterion (default `1.0e-6`);
    `:max_iter` - iteration cap (default `200`).

`search: :scipy` is mgwr's `search_method='scipy'`: SciPy's bounded Brent minimizer
(`minimize_scalar(method='bounded')`, `xatol = 1.0e-5`, at most 500 evaluations, a literal port)
over the same default bounds. As in mgwr the result `bw` is the optimizer's float (an adaptive
fit truncates it to a neighbor count, so `fit/4` warns about it), the fixed bandwidth is not
rounded, `history` is empty, and `nfev` and `converged` come from SciPy's result. The lower and
upper bounds must differ, and `:include_global` is not available with it (mgwr ignores
`bw_max=np.inf` here).

The golden-section search follows mgwr literally: the probes of adaptive searches are rounded
to integers, convergence is on the criterion difference between the two probes, and the
reported fixed bandwidth is rounded to two decimals.

## Examples

    iex> pts = for r <- 0..5, c <- 0..5, do: {c * 1.0 + 0.01 * r, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 1.0 + xv + :math.sin(i)
    iex> b = Spatex.GWR.select_bandwidth(pts, y, [x], fixed: true, kernel: :gaussian)
    iex> b.bw > 0
    true

The SciPy search returns the optimizer's float and its evaluation count:

    iex> pts = for r <- 0..5, c <- 0..5, do: {c * 1.0 + 0.01 * r, r * 1.0}
    iex> x = for {cx, _} <- pts, do: cx
    iex> y = for {xv, i} <- Enum.with_index(x), do: 1.0 + xv + :math.sin(i)
    iex> b = Spatex.GWR.select_bandwidth(pts, y, [x], fixed: true, kernel: :gaussian, search: :scipy)
    iex> {b.history, b.converged, b.nfev > 3}
    {[], true, true}

# `spatial_variability`

```elixir
@spec spatial_variability(term(), term(), term(), keyword()) ::
  Spatex.GWR.SpatialVariability.t()
```

Monte Carlo test of the spatial variability of each coefficient surface
(`GWRResults.spatial_variability`).

The bandwidth is selected and the model fitted as in `select_bandwidth/4` and `fit/4`; the
standard deviation of every coefficient surface is the statistic. Each permutation re-orders
the coordinates among the observations (breaking the spatial pattern, keeping `y` and `x`),
searches the bandwidth again and refits; a surface's p-value is the share of permutations
whose standard deviation exceeds the observed one. A small p-value means the surface varies
in space more than chance would.

**This test is expensive**: every permutation repeats a bandwidth search (dozens of fits).
`:max_n` therefore defaults to `2_000` here. Memory stays `O(n k)` per fit.

Arguments are as in `fit/4`. Returns a `Spatex.GWR.SpatialVariability`.

## Options

All options of `select_bandwidth/4` (kernel, search, criterion, bounds, family, ...), whose
search is repeated for every permutation, and

  * `:permutations` - number of permutations (mgwr's `n_iters`; default `1000`).
  * `:seed` - an integer in `0..2^64-1`, or `nil` (default) to draw one; the seed used is
    recorded in the result. Replicate `r` depends only on `(seed, r)`. Unlike mgwr, which
    uses numpy's generator, Spatex uses its ChaCha8 streams, so p-values agree with mgwr's up
    to Monte Carlo error; the observed statistic agrees exactly.
  * `:keep_simulations` - keep the simulated standard deviations: `true` (default) as lists,
    `:tensor` as a `{permutations, k}` `Nx.Tensor` (optional `:nx` dependency), `false` to
    drop them.

## Examples

    iex> pts = for r <- 0..5, c <- 0..5, do: {c + 0.07 * rem(r * 5 + c, 4), r + 0.05 * c}
    iex> x = for {px, _} <- pts, do: :math.sin(px)
    iex> y = for {{px, py}, a} <- Enum.zip(pts, x), do: 1.0 + 0.2 * px * a + 0.1 * py + :math.cos(px * py)
    iex> r = Spatex.GWR.spatial_variability(pts, y, [x], fixed: true, kernel: :gaussian, permutations: 3, seed: 1)
    iex> {r.k, length(r.p_values), length(r.simulated_sd)}
    {2, 2, 3}

---

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