Skip to main content

MultiMin: Multivariate Gaussian fitting

Project description

version license pythonver downloads Powered by SciPy

Introducing MultiMin

MultiMin is a Python package designed to provide numerical tools for fitting composed multivariate distributions to data. It is particularly useful for modelling complex multimodal distributions in N-dimensions.

These are the main features of MultiMin:

  • Multivariate Fitting: Tools for fitting composed multivariate normal distributions (CMND).
  • Visualization: Corner plots and specific visualization utilities.
  • Statistical Analysis: Tools for handling covariance matrices and correlations.

Documentation

Full API documentation is available at https://multimin.readthedocs.io.

Installation

From PyPI

MultiMin will be available on PyPI at https://pypi.org/project/multimin/. Once published, you can install it with:

pip install -U multimin

From Sources

You can also install from the GitHub repository:

git clone https://github.com/seap-udea/multimin
cd multimin
pip install .

For development, use an editable installation:

cd multimin
pip install -e .

In Google Colab

If you use Google Colab, you can install MultiMin by executing:

!pip install -U multimin

Theoretical Background

The core of MultiMin is the Composed Multivariate Normal Distribution (CMND). The theory behind it posits that any multivariate distribution function $p(\tilde U):\Re^{N}\rightarrow\Re$, where $\tilde U:(u_1,u_2,u_3,\ldots,u_N)$ are random variables, can be approximated with arbitrary precision by a normalized linear combination of $M$ Multivariate Normal Distributions (MND):

$$ p(\tilde U) \approx \mathcal{C}_M(\tilde U; {w_k}_M, {\mu_k}_M, {\Sigma_k}M) \equiv \sum{i=1}^{M} w_i;\mathcal{N}(\tilde U; \tilde \mu_i, \Sigma_i) $$

where the multivariate normal $\mathcal{N}(\tilde U; \tilde \mu, \Sigma)$ with mean vector $\tilde \mu$ and covariance matrix $\Sigma$ is given by:

$$ \mathcal{N}(\tilde U; \tilde \mu, \Sigma) = \frac{1}{\sqrt{(2\pi)^{k} \det \Sigma}} \exp\left[-\frac{1}{2}(\tilde U - \tilde \mu)^{\rm T} \Sigma^{-1} (\tilde U - \tilde \mu)\right] $$

The covariance matrix $\Sigma$ elements are defined as $\Sigma_{ij} = \rho_{ij}\sigma_{i}\sigma_{j}$, where $\sigma_i$ is the standard deviation of $u_i$ and $\rho_{ij}$ is the correlation coefficient between variable $u_i$ and $u_j$ ($-1<\rho_{ij}<1$, $\rho_{ii}=1$).

The normalization condition on $p(\tilde U)$ implies that the set of weights ${w_k}_M$ are also normalized, i.e., $\sum_i w_i=1$.

Quick Start

Getting started with MultiMin is straightforward. Import the package:

import multimin as mn

NOTE: If you are working in Google Colab, load the matplotlib backend before producing plots:

%matplotlib inline

Here is a basic example of how to use MultiMin to fit a 3D distribution composed of 2 Multivariate Normals.

1. Define a true distribution

First, we define a distribution from which we will generate synthetic data. We use a Composed Multivariate Normal Distribution (CMND) with 2 Gaussian components (ngauss=2) in 3 dimensions (nvars=3).

import numpy as np
import multimin as mn

# Define parameters for 2 Gaussian components
weights = [0.5, 0.5]
mus = [[1.0, 0.5, -0.5], [1.0, -0.5, +0.5]]
sigmas = [[1, 1.2, 2.3], [0.8, 0.2, 3.3]]
deg = np.pi/180
angles = [
    [10*deg, 30*deg, 20*deg],
    [-20*deg, 0*deg, 30*deg],
] 

# Calculate covariance matrices from rotation angles
Sigmas = mn.Stats.calc_covariance_from_rotation(sigmas, angles)

# Create the CMND object
CMND = mn.ComposedMultiVariateNormal(mus=mus, weights=weights, Sigmas=Sigmas)

2. Generate sample data

We generate 5000 random samples from this distribution to serve as our "observed" data.

np.random.seed(1)
data = CMND.rvs(5000)

3. Visualize the data

We can check the distribution of the generated data using CornerPlot.

import matplotlib.pyplot as plt

# Define properties labels
properties = dict(
    x=dict(label=r"$x$", range=None),
    y=dict(label=r"$y$", range=None),
    z=dict(label=r"$z$", range=None),
)

# Plot the corner plot
G = mn.CornerPlot(properties, figsize=3)
hargs = dict(bins=30, cmap='Spectral_r')
sargs = dict(s=1.2, edgecolor='None', color='r')
hist = G.scatter_plot(data, **sargs)
Data Scatter Plot

4. Initialize the Fitter and Run the Fit

We initialize the FitCMND handler with the expected number of Gaussians (2) and variables (3). We then run the fitting procedure.

# Initialize the fitter
F = mn.FitCMND(ngauss=2, nvars=3)

# Run the fit (using advance=True for better convergence on complex models)
F.fit_data(data, advance=True)

5. Check and Plot Results

Finally, we visualize the fitted distribution compared to the data.

# Plot the fit result
G = F.plot_fit(
    props=["x", "y", "z"],
    hargs=dict(bins=30, cmap='YlGn'),
    sargs=dict(s=0.2, edgecolor='None', color='r'),
    figsize=3
)
Fit Result

Citation

The numerical tools and codes provided in this package have been developed and tested over several years of scientific research.

If you use MultiMin in your research, please cite:

@software{multimin2026,
  author = {Zuluaga, Jorge I.},
  title = {MultiMin: Multivariate Gaussian fitting},
  year = {2026},
  url = {https://github.com/seap-udea/multimin}
}

What's New

For a detailed list of changes and new features, see WHATSNEW.md.

Authors and Licensing

This project is developed by the Solar, Earth and Planetary Physics Group (SEAP) at Universidad de Antioquia, Medellín, Colombia. The main developers are:

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0) - see the LICENSE file for details.

Contributing

We welcome contributions! If you're interested in contributing to MultiMin, please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

Please read the CONTRIBUTING.md file for more information.

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

multimin-0.5.2.tar.gz (4.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multimin-0.5.2-py3-none-any.whl (4.9 MB view details)

Uploaded Python 3

File details

Details for the file multimin-0.5.2.tar.gz.

File metadata

  • Download URL: multimin-0.5.2.tar.gz
  • Upload date:
  • Size: 4.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for multimin-0.5.2.tar.gz
Algorithm Hash digest
SHA256 9d1040e46cf883aed00988603c2a9e2fec4d1e223bd1d336d043bf06ca66d2fb
MD5 a88e9caf978ccd3f12892a1f24e1fe3a
BLAKE2b-256 a338ce8a23b2ff54e3b5c5e606c6aa2c73d3eff1b4249e92f44d4d88291927e1

See more details on using hashes here.

File details

Details for the file multimin-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: multimin-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for multimin-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5057f389dc14387407e191647ed06a2aafa869fe97f7772055fd64e851bb0e4a
MD5 9e19542046a13f7951dafeb09994eef5
BLAKE2b-256 c7cdcc9896ed7574f5fca9cadae369bd300cf62ab19059d9eaeb02c39802b9d4

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