Skip to main content

Compute Mutual Information using the KSG estimator (1st algo)

Project description

libKSG

A performant implementation of the KSG mutual information estimator (first algorithm). The algorithm is written in C and packaged in Python for convenience. Currently, only works on Linux 🙃

example output

Original publication:

Kraskov, A., Stögbauer, H., & Grassberger, P. (2004). Estimating mutual information. Physical Review E—Statistical, Nonlinear, and Soft Matter Physics, 69(6), 066138.

https://journals.aps.org/pre/abstract/10.1103/PhysRevE.69.066138

Installation

The GNU Scientific Library is required for compilation, which comes shipped with most linux distros. If you have trouble compiling, make sure that this library is installed using your distro's package manager.

Python

Easiest way is to install the PyPI distribution with pip:

pip install libKSG

Or compile and install manually:

git clone https://github.com/LelouchLamperougeVI/libKSG.git
cd libKSG
pip install .

C

If you only need the shared library, compile the C code:

git clone https://github.com/LelouchLamperougeVI/libKSG.git
cd libKSG
make

This will compile into a shared library libksg.so. Additionally, you can compile and run the test programme to make sure everything works:

make test
./test

MATLAB

maybe in the future...

Usage

Python

libKSG provides the class KSG with a single method mi():

libKSG.KSG.mi(x: np.ndarray, y: np.ndarray, k: int = 5, axis: int = 0, keepdims: bool = False)
Inputs:

x, y: np.ndarray
  Input vectors or matrices. The features must have the same lengths.

k: int
  Number of nearest neighbours (see publication).

axis: int
  Axis that contains individual features to be compared.
  All other axes will be flattened based on the 'C' ordering rule.

keepdims: bool
  Whether to keep the singleton dimensions.

Returns:

I: np.ndarray
  Matrix of shape (x.shape[axis], y.shape[axis]) containing mutual information values.
  If keepdims == False, singleton dimensions will be squeezed.

Example

from libKSG import KSG # import the library
import numpy as np

n = 1_000;

theta = np.linspace(0, 2*np.pi, n) # generate test data
phi = np.random.randn(n) + 10
x = phi * np.cos(theta)
y = phi * np.sin(theta)

ksg = KSG() # create instance of KSG object
I = ksg.mi(x, y) # calculate mutual info between x and y

print(I)

C

The main function takes a pointer x to an array of doubles of length N * 2.

double ksg(double *x, size_t N, int k)

The array is assumed to be a two-columns matrix presented in row-major form (i.e. the first two entries correspond to the first elements on the two separate columns and so on). The two columns represent the x/y coordinates of data points for a total of N points. k is the k-th nearest neighbour, as described in the publication.

NOTE: Mutual information may be returned as negative.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libksg-0.0.1.tar.gz (44.3 kB view details)

Uploaded Source

File details

Details for the file libksg-0.0.1.tar.gz.

File metadata

  • Download URL: libksg-0.0.1.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for libksg-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a4da0276c8348bfbf0b3866ef299e7b0474fde142a1cc0159b7773532295d578
MD5 5e658f5cd98df23e3e063b6bb67f1714
BLAKE2b-256 ea23f7784076cb7a70a83d625af0b11a692ffa2d61c93257a0348f0ba8eb526b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page