Go · Probability Metrics

Probability Metrics

import "github.com/ashvardanian/NumKong" 8.0.0-rc.1 · b1f866ab

Kullback-Leibler divergence and Jensen-Shannon distance for probability distributions.

package main

import (
	"fmt"

	nk "github.com/ashvardanian/NumKong/golang"
)

func main() {
	p := []float64{0.25, 0.25, 0.25, 0.25}
	q := []float64{0.1, 0.2, 0.3, 0.4}

	fmt.Println(nk.KullbackLeiblerF64(p, q)) // KL divergence
	fmt.Println(nk.JensenShannonF64(p, q))   // JS distance (symmetric)
}

KullbackLeiblerF64 and JensenShannonF64 take []float64 and return float64. KullbackLeiblerF32 and JensenShannonF32 take []float32 and return float64 (widened).

API

Functions

JensenShannonF32JensenShannonF32 computes the Jensen-Shannon distance between two float32 distributions.
numkong.go:56 ↗
func JensenShannonF32(a, b []float32) float64

Both slices must have the same length.

JensenShannonF64JensenShannonF64 computes the Jensen-Shannon distance between two float64 distributions.
numkong.go:42 ↗
func JensenShannonF64(a, b []float64) float64

Both slices must have the same length.

KullbackLeiblerF32KullbackLeiblerF32 computes the Kullback-Leibler divergence between two float32 distributions.
numkong.go:28 ↗
func KullbackLeiblerF32(a, b []float32) float64

Both slices must have the same length.

KullbackLeiblerF64KullbackLeiblerF64 computes the Kullback-Leibler divergence between two float64 distributions.
numkong.go:14 ↗
func KullbackLeiblerF64(a, b []float64) float64

Both slices must have the same length.