Skip to main content

mlx-fisher

GPU-accelerated Fisher Information Matrix computation on Apple Silicon via MLX.

Author: Sheng-Kai Huang (akai@fawstudio.com)

Features

  • Fisher Information Matrix from log-likelihood, model predictions, or samples
  • CMB C_l Fisher matrix for cosmological parameter estimation
  • KL divergence (classical) and quantum relative entropy D(rho||sigma)
  • Natural gradient descent optimizer with online Fisher estimation
  • All matrix operations (eigendecomposition, matrix multiply, log) on Apple GPU

Installation

pip install -e .

Requires Python 3.10+ and Apple Silicon (M1/M2/M3/M4).

Quick Start

import mlx.core as mx
from mlx_fisher import FisherMatrix, kl_divergence, quantum_relative_entropy

# --- Gaussian Fisher matrix from a model ---
x = mx.linspace(-3.0, 3.0, 1000)

def model(theta):
    return theta[0] * x**2 + theta[1] * x + theta[2]

theta0 = mx.array([1.0, -0.5, 2.0])
sigma = mx.ones((1000,)) * 0.5

F = FisherMatrix.from_model(model, theta0, sigma)
print(F.marginal_errors())   # 1-sigma errors on each parameter

# --- KL divergence ---
p = mx.array([0.4, 0.3, 0.2, 0.1])
q = mx.array([0.25, 0.25, 0.25, 0.25])
print(kl_divergence(p, q))   # D_KL(p || q)

# --- Quantum relative entropy ---
d = 4
rho = mx.zeros((d, d)); rho[0, 0] = 1.0       # pure state |0><0|
sigma_dm = mx.eye(d) / d                        # maximally mixed
print(quantum_relative_entropy(rho, sigma_dm))   # = ln(d)

CMB Fisher Matrix

from mlx_fisher import fisher_matrix_cl

def cl_fn(theta):
    """Map cosmological parameters to C_l power spectrum."""
    # Your Boltzmann solver here (e.g., CLASS wrapper)
    ...

theta_fid = mx.array([0.022, 0.12, 0.06, 0.96, 3.04, 67.4])
F = fisher_matrix_cl(cl_fn, theta_fid, f_sky=0.7, l_min=2, l_max=2500)
print(F.marginal_errors())

Natural Gradient Descent

from mlx_fisher import NaturalGradientOptimizer

opt = NaturalGradientOptimizer(lr=1e-2, damping=1e-4)

for step in range(100):
    grad = compute_gradient(theta)
    theta = opt.step(theta, grad, fisher_estimator=compute_fisher)

API Reference

FisherMatrix

  • FisherMatrix.from_model(model_fn, theta, sigma) -- Gaussian Fisher matrix
  • FisherMatrix.from_loglikelihood(log_lik, theta) -- from log-likelihood function
  • FisherMatrix.from_samples(log_prob, theta, samples) -- empirical Fisher
  • .inverse(reg=0.0) -- covariance matrix (regularised inversion)
  • .marginal_errors(reg=0.0) -- 1-sigma marginal errors
  • .eigenvalues() -- eigenvalue spectrum
  • .condition_number() -- matrix condition number

fisher_matrix_cl(cl_fn, theta, f_sky, noise_cl, l_min, l_max)

CMB power spectrum Fisher matrix with cosmic variance.

kl_divergence(p, q)

Classical KL divergence D_KL(p || q).

quantum_relative_entropy(rho, sigma)

Quantum relative entropy D(rho || sigma) = Tr[rho(ln rho - ln sigma)].

NaturalGradientOptimizer(lr, damping, fisher_update_interval, ema_decay)

Natural gradient descent: theta_new = theta - lr * F^{-1} @ grad.

Benchmarks (M1 Max)

Operation Scale MLX (ms) NumPy (ms) Speedup
KL divergence 1M bins 0.52 5.22 10x
KL divergence 10M bins 2.23 51.40 23x
Eigendecomposition 512x512 17.25 58.07 3.4x
Matrix multiply (Fisher) 32768x512 4.33 169.96 39x

See benchmark_results.md for full results.

License

MIT

Metadata

Release files for mlx-fisher 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlx-fisher 0.1.0
File Size Uploaded Provenance Actions
mlx_fisher-0.1.0.tar.gz 16.3 kB Without provenance Details for mlx_fisher-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-fisher 0.1.0
File Interpreter ABI Platform Actions
mlx_fisher-0.1.0-py3-none-any.whl Python 3 none any Details for mlx_fisher-0.1.0-py3-none-any.whl

Total release size: 29.3 kB

Release files / mlx_fisher-0.1.0.tar.gz

Download URL mlx_fisher-0.1.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
707ca46fbd3f1c3567fcf97dbe9d479b620c0d4b41f8d936cc260fc9be969823
BLAKE2b-256 checksum
How to use checksums
b51972d475d0e48c962d97219903f817d497eb607281c9be0cffbc5a957bb98a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / mlx_fisher-0.1.0-py3-none-any.whl

Download URL mlx_fisher-0.1.0-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8dad6d6da66d7ab5257a81d41275f1474a7aff7bb55d1f6a25828a71aef8338
BLAKE2b-256 checksum
How to use checksums
6ca1d295f3d64acd4377caef369b36a2bf8a1d4a507ee2034c991dc9d70a7a6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for mlx-fisher 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page