# Kernel Weights

Kernel Weights applies kernel function to determine the distance decay in the derived continuous weights kernel. The kernel weights are defined as a function K(z) of the ratio between the distance dij from i to j, and the bandwidth *hi*, with z=dij/*hi*. The kernel functions include *{triangular ,* uniform, *quadratic ,*  epanechnikov, *quartic,* gaussian}.

* Uniform, K(z)=1/2 for |z|<1,
* Triangular, K(z)=(1−|z|) for |z|<1,
* Quadratic or Epanechnikov, K(z)=(3/4)(1−z^2) for |z|<1,
* Quartic, K(z)=(15/16)(1−z^2)^2 for |z|<1|z|<1, and
* Gaussian. K(z) = (2π)^{1/2}exp(-z^2/2)

For more information, please read: <https://geodacenter.github.io/workbook/4c_distance_functions/lab4c.html#kernel-weights>

There are two types of fixed bandwidths for kernel weights. One is the max-min distance used earlier (the largest of the nearest-neighbor distances): ***kernel\_weights**(). The other is the maximum distance for a given specification of k-nearest neighbors: **kernel\_knn\_*****weights**().

### 1. getKernelWeights()

```sql
function getKernelWeights(
    String mapUid,
    Number distBand, 
    String kernel,
    Number power, 
    Boolean isInverse,
    Boolean isArc,
    Boolean isMile,
    Boolean useKernelDiagonals)
```

**Arguments**

| Input Arguments      | Type    | Description                                                                                                               |
| -------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| mapUid               | String  | the unique map id                                                                                                         |
| distBand             | Number  | the distance band/threshold that makes sure each observation has at least one neighbor                                    |
| <p></p><p>kernel</p> | String  | a varchar value of kernel method, which has to be one of {'triangular', 'uniform', 'epanechnikov', 'quartic', 'gaussian'} |
| useKernelDiagonals   | Boolean | if apply kernel on the diagonal of weights matrix. Default: FALSE.                                                        |
| power                | Number  | the power/exponent corresponds to the number of times the base (dist\_band) is used as a factor. Default: 1.              |
| isInverse            | Boolean | if apply inverse on distance value. Default: False.                                                                       |
| isArc                | Boolean | if compute arc distance between two observations. Default: FALSE.                                                         |
| isMile               | Boolean | if convert distance unit from mile to kilometer(KM). Default: TRUE.                                                       |

**Return**

| **Value**     | Description                                                  |
| ------------- | ------------------------------------------------------------ |
| WeightsResult | the weights structure for each observation in binary format. |

### 2. getKernelKnnWeights()&#x20;

With knn set to a given value, the maximum distance between the selected k-nearest neighbors' pairs is used as a "fixed" bandwidth. However, a drawback of fixed bandwidth kernel weights is that the number of non-zero weights can vary considerably, especially when the density of the point locations is not uniform throughout space. The argument `adaptive_bandwidth` is provided to allow adaptive bandwidth in knn kernel weights: instead of a fixed distance bandwidth,  the distance to the k-th nearest neighbor is used in the kernel function for each observation.&#x20;

**API**

```sql
function getKernelKnnWeights(
    String map_uid, 
    integer k, 
    String kernel,
    Boolean adaptive_bandwidth,
    Number power, 
    Boolean is_inverse,
    Boolean is_arc,
    Boolean is_mile,
    Boolean use_kernel_diagonals)
```

**Arguments**

| Input Arguments        | Type    | Description                                                                                                                                      |
| ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| map\_uid               | String  | the unique map id                                                                                                                                |
| k                      | Number  | the k nearest neighbors                                                                                                                          |
| <p></p><p>kernel</p>   | String  | a varchar value of kernel method, which has to be one of {'triangular', 'uniform', 'epanechnikov', 'quartic', 'gaussian'}                        |
| adaptive\_bandwidth    | Boolean | if use adaptive bandwidth (distance to k-th nearest neighbor for each observation), or use max knn distance of all observations. Default: FALSE. |
| use\_kernel\_diagonals | Boolean | if apply kernel on the diagonal of weights matrix. Default: FALSE.                                                                               |
| power                  | Number  | the power/exponent corresponds to the number of times the base (dist\_band) is used as a factor. Default: 1.                                     |
| is\_inverse            | Boolean | if apply inverse on distance value. Default: False.                                                                                              |
| is\_arc                | Boolean | if compute arc distance between two observations. Default: FALSE.                                                                                |
| is\_mile               | Boolean | if convert distance unit from mile to kilometer(KM). Default: TRUE.                                                                              |

**Return**

| **Value**    | Description                                 |
| ------------ | ------------------------------------------- |
| WeightResult | the weights structure for each observation. |

**Try it yourself in the playground (jsgeoda + deck.gl):**

{% embed url="<https://codesandbox.io/s/4spatial-weights-owi84>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xunli.gitbook.io/jsgeoda/spatial-weights/kernel-weights.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
