# Probability Metrics

> NumKong for JavaScript

```ts
import { kullbackleibler, jensenshannon } from "numkong";

const p = new Float32Array([0.2, 0.3, 0.5]);
const q = new Float32Array([0.1, 0.3, 0.6]);

console.log(kullbackleibler(p, q));
console.log(jensenshannon(p, q));
```

These call the underlying SIMD divergence kernels directly.

`import * as numkong from 'numkong';`

## kullbackleibler

Computes the Kullback-Leibler divergence between two probability distributions.

```typescript
function kullbackleibler(
    a: Float64Array | Float32Array,
    b: Float64Array | Float32Array,
): number
```

```typescript
function kullbackleibler(
    a: Float64Array | Float32Array | Uint16Array,
    b: Float64Array | Float32Array | Uint16Array,
    dtype: DType,
): number
```

```typescript
function kullbackleibler(a: TensorBase, b: TensorBase): number
```

Both vectors must represent valid probability distributions, non-negative and summing to 1.
Supports f64 and f32, auto-detected, and f16 and bf16 with an explicit dtype.

- `a`: The first probability distribution, as Float32Array, Float64Array, or TensorBase.
- `b`: The second probability distribution, matching the type and length of `a`.
- `dtype`: Optional dtype string for custom types, e.g. 'f16', 'bf16'.

## jensenshannon

Computes the Jensen-Shannon distance between two probability distributions.

```typescript
function jensenshannon(
    a: Float64Array | Float32Array,
    b: Float64Array | Float32Array,
): number
```

```typescript
function jensenshannon(
    a: Float64Array | Float32Array | Uint16Array,
    b: Float64Array | Float32Array | Uint16Array,
    dtype: DType,
): number
```

```typescript
function jensenshannon(a: TensorBase, b: TensorBase): number
```

Both vectors must represent valid probability distributions, non-negative and summing to 1.
Supports f64 and f32, auto-detected, and f16 and bf16 with an explicit dtype. JSD is the square
root of the symmetrized KL divergence:
d_JS(a, b) = √(0.5 × (KL(a‖m) + KL(b‖m))), where m = (a + b) / 2.

- `a`: The first probability distribution, as Float32Array, Float64Array, or TensorBase.
- `b`: The second probability distribution, matching the type and length of `a`.
- `dtype`: Optional dtype string for custom types, e.g. 'f16', 'bf16'.

