Learn R Programming

OVL.CI (version 0.1.1)

EM: EM algorithm for a univariate Gaussian mixture

Description

Fits a univariate Gaussian mixture model using the Expectation-Maximization (EM) algorithm. The function is intended as a lightweight fallback implementation (e.g., when mixtools is unavailable or fails).

Usage

EM(X, K = 2, max_iter = 100, tol = 1e-05)

Value

A list with the following components:

mu

Numeric vector of estimated component means (length K).

sigma

Numeric vector of estimated component standard deviations (length K).

pi

Numeric vector of estimated mixing proportions (length K).

num_iteraciones

Number of iterations performed.

posterior

Matrix of posterior probabilities (responsibilities) with dimension length(X) by K.

Arguments

X

Numeric vector of observations.

K

Integer. Number of mixture components.

max_iter

Integer. Maximum number of EM iterations.

tol

Positive numeric. Convergence tolerance for the absolute change in the log-likelihood.

Details

The algorithm is initialized using the k-means clustering procedure and then alternates between:

  1. E-step: computing the expectation of the complete log-likelihood function.

  2. M-step: maximizing the expectation of the complete log-likelihood function.

Examples

Run this code
set.seed(1)
x <- c(rnorm(100, -2, 1), rnorm(100, 2, 1))
fit <- EM(x, K = 2)
fit$mu
fit$pi

Run the code above in your browser using DataLab