Skip to main content

Parameter estimation of mixture distribution problem solver, based on EM algorithm

Project description

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

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

mpest-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

mpest-0.1.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file mpest-0.1.0.tar.gz.

File metadata

  • Download URL: mpest-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for mpest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3f957ca5eb033ef3306b172836650eac01c802471b8cbd7afc450f4ec87f7d2e
MD5 fc67a3de8d14832982ef3d282aabbce3
BLAKE2b-256 5eae486e74467a9f46dd9b2fa81f8eeca5fd36ce70256abf3af8767b1e58974a

See more details on using hashes here.

File details

Details for the file mpest-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mpest-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for mpest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f77e2896d3ef940591f6b97fc5add6fa35f2bfb2aba4884e579f6a096933177
MD5 d18ba41ecb1413d77a90f8324a189d00
BLAKE2b-256 d85372a63143e704be9f3c76a4496b0676f8bb6c00f37bab695ed2a2fe0af367

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