MyNixOS website logo
Description

Exact Kantorovich distance between finite probability measures.

This small package allows to compute the exact Kantorovich distance between two finite probability measures. This assumes that the probability masses are rational numbers and that the distance function takes rational values only.

exact-kantorovich

Stack-lts Stack-nightly

Exact Kantorovich distance between finite probability measures.

This small package allows to compute the exact Kantorovich distance between two finite probability measures. This assumes that the probability masses are rational numbers and that the distance function takes rational values only.


Let's say you have the two probability measures with masses $(1/7, 2/7, 4/7)$ and $(1/4, 1/4, 1/2)$, both distributed on the set ${1, 2, 3}$, and you want to get the Kantorovich distance between them when this set is endowed with the distance function $d(i, j) = |i - j|$. To get it with this package, you will use the kantorovich function. It has four arguments. The first two ones are the two random variables corresponding these probability measures, and a random variable is defined as a map from its states set to the rational numbers; this map assigns to each element its probability mass:

import Data.Map.Strict ( fromList )
import Data.Ratio ( (%) )
mu, nu :: RandomVariable Int
mu = fromList $ zip [1, 2, 3] [1%7, 2%7, 4%7]
nu = fromList $ zip [1, 2, 3] [1%4, 1%4, 1%2]

The third one is the distance function; it must return a positive rational number:

dist :: (Int, Int) -> Rational
dist (i, j) = toRational $ abs (i - j)

And the fourth one is a Boolean value that you set to True if you want to print to the stdout stream some details of the simplex algorithm performed by the kantorovich function:

import Math.Optimization.Kantorovich
result <- kantorovich mu nu dist False

The output of the kantorovich function has type IO (Maybe (KantorovichResult a b)) where here a = b = Int and the type KantorovichResult a b is the pair of types (Rational, RandomVariable (a, b)). The first element of an object of a KantorovichResult object represents the value of the Kantorovich distance and the second element represents a solution of the underlying linear programming problem, that is to say a joining of the two probability measures that achieves the Kantorovich distance.

Here is the value of the Kantorovich distance for our example:

import Data.Maybe ( fromJust )
fst $ fromJust result
-- 5 % 28

You can display the solution in the style of a matrix with the help of the prettyKantorovichSolution function:

putStrLn $ prettyKantorovichSolution result

This prints:

┌                      ┐
│  1 % 7  0 % 1  0 % 1 │
│ 3 % 28 5 % 28  0 % 1 │
│  0 % 1 1 % 14  1 % 2 │
└                      ┘

That's all. There is no other function exported by this package.

Metadata

Version

0.1.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows