Skip to main content

PyLint Check code style Code style Unit Tests

Abstract

This package contains realization of em algorithm for solving the parameter estimation of mixture distribution problem:

$$p(x | \Omega, F, \Theta) = \sum_{j=1}^k \omega_j f_j(x | \theta_j)$$

  • $p(x | \Omega, F, \Theta)$ - mixture distribution
  • $f_j(x | \theta_j) \in F$ - $j$ distribution
  • $\omega_j \in \Omega$ - prior probability of $j$ distribution
  • $\theta_j \in \Theta$ - parameters of $j$ distribution

The problem is to find $\Omega$ and $\Theta$ params of custom mixture distribution with known (or guessed) $k$, $F$ and guessed or randomized initial approximation.

This package uses EM algorithm tuned to work with different distributions models and optimizers, which could match the given interfaces. This allows using this package even for mixture distribution of different distributions classes. For example mixture distribution of both Gaussian and Exponential distributions.

Usage

The package can work with mixture distribution of any combination of models, which implements AModel abstract class. EM algorithm result can be calculated by using EM class with AOptimizer implementation and guessed or random initial approximation.

Given samples should be wrapped in MixtureDistribution then by using EM.solve the result will be calculated. EM class depends on TOptimizer, ABreakpointer and ADistributionChecker objects. :

  • ABreakpointer class $-$ the EM algorithm breakpointer function. There are few basic realizations of that abstract class in that package.
  • ADistributionChecker class $-$ sometimes because of using math optimizers in M-step of EM algorithm, some distributions inside mixture distribution can become degenerated. Such distributions may be detected and removed from calculations. There are few basic realizations of that abstract class in that package.
  • AOptimizer/AOptimizerJacobian classes $-$ math optimizers for M step of algorithm. There are few SciPy optimizers made follow the given interfaces.

Code example

import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

from mpest import Distribution, MixtureDistribution, Problem
from mpest.models import WeibullModelExp, GaussianModel
from mpest.optimizers import ScipyTNC
from mpest.em.breakpointers import StepCountBreakpointer
from mpest.em.distribution_checkers import FiniteChecker
from mpest.em import EM

x = np.concatenate(
    (
        WeibullModelExp().generate(np.array([0.5, 1.0]), 100),
        GaussianModel().generate(np.array([5.0, 1.0]), 200),
    )
)
np.random.shuffle(x)

base_mixture_distribution = MixtureDistribution.from_distributions(
    [
        Distribution.from_params(WeibullModelExp, [0.5, 1.0]),
        Distribution.from_params(GaussianModel, [5.0, 1.0]),
    ]
)

problem = Problem(
    x,
    MixtureDistribution.from_distributions(
        [
            Distribution.from_params(WeibullModelExp, [1.0, 2.0]),
            Distribution.from_params(GaussianModel, [0.0, 5.0]),
        ]
    ),
)

em = EM(StepCountBreakpointer(max_step=8), FiniteChecker(), ScipyTNC())

result = em.solve(problem)


fig, axs = plt.subplots()
axs.set_xlabel("x")

sns.histplot(x, color="lightsteelblue")

axs_ = axs.twinx()
axs_.set_ylabel("p(x)")
axs_.set_yscale("log")

X = np.linspace(0.001, max(x), 2048)
axs_.plot(X, [base_mixture_distribution.pdf(x) for x in X], color="green", label="base")
axs_.plot(X, [result.content.pdf(x) for x in X], color="red", label="result")

plt.legend()
plt.show()

plot

Requirements

  • python 3.11
  • numpy
  • scipy

Release files for mpest 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 mpest 0.1.0
File Size Uploaded
mpest-0.1.0.tar.gz 18.4 kB Details

Built distribution (wheel)

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

Total release size: 43.2 kB

Release files / mpest-0.1.0.tar.gz

Download URL mpest-0.1.0.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3f957ca5eb033ef3306b172836650eac01c802471b8cbd7afc450f4ec87f7d2e
BLAKE2b-256 checksum
How to use checksums
5eae486e74467a9f46dd9b2fa81f8eeca5fd36ce70256abf3af8767b1e58974a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

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

Download URL mpest-0.1.0-py3-none-any.whl
Size 24.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f77e2896d3ef940591f6b97fc5add6fa35f2bfb2aba4884e579f6a096933177
BLAKE2b-256 checksum
How to use checksums
d85372a63143e704be9f3c76a4496b0676f8bb6c00f37bab695ed2a2fe0af367
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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