以EM算法为例介绍坐标上升(Coordinate Ascent)算法:中英双语
中文版
什么是 Coordinate Ascent 算法?
Coordinate Ascent(坐标上升)是一种优化算法,它通过在每次迭代时优化一个变量(或一个坐标),并保持其他变量不变,逐步逼近最优解。与坐标下降(Coordinate Descent)算法相对,坐标上升通常用于求解带有多个变量的优化问题,特别是在优化函数在每个维度上都是可分的情况下。
在每次迭代中,Coordinate Ascent算法通过选择一个变量来最大化该变量对目标函数的贡献,然后再选择下一个变量进行优化。它通过反复更新每个坐标来寻找最优解。虽然这个过程可能不会全局收敛,但它在很多实际问题中表现得很好,特别是在目标函数较为简单且每个坐标可以独立优化时。
以EM算法为例介绍Coordinate Ascent
EM(Expectation-Maximization)算法是一种常见的优化算法,常用于统计学和机器学习中,尤其是当数据中有隐变量时。EM算法的基本思路是通过迭代求解期望步骤(E步)和最大化步骤(M步)来优化参数。
我们可以将EM算法视为一种坐标上升方法。每一轮的E步和M步都可以视为分别优化不同的“坐标”,其中E步优化期望函数,M步最大化该期望函数。
EM算法中的坐标上升
假设我们有一个隐变量模型,其目标是最大化对数似然函数:
L ( θ ) = log P ( X , Z ∣ θ ) \mathcal{L}(\theta) = \log P(X, Z | \theta) L(θ)=logP(X,Z∣θ)
其中:
- ( X X X ) 是观察数据,
- ( Z Z Z ) 是隐变量,
- ( θ \theta θ ) 是模型的参数。
由于隐变量 ( Z Z Z ) 是不可观测的,直接求解对数似然函数是困难的。因此,EM算法通过迭代的方法来解决这个问题。
-
E步(Expectation step):
在E步中,我们计算隐变量的期望值,即给定当前参数估计 ( θ ( t ) \theta^{(t)} θ(t) ) 下,隐变量 ( Z Z Z ) 的条件概率分布:
Q ( θ , θ ( t ) ) = E Z ∣ X , θ ( t ) [ log P ( X , Z ∣ θ ) ] Q(\theta, \theta^{(t)}) = \mathbb{E}_{Z | X, \theta^{(t)}} \left[ \log P(X, Z | \theta) \right] Q(θ,θ(t))=EZ∣X,θ(t)[logP(X,Z∣θ)]
这是一个坐标上升的过程,因为我们通过固定当前的参数 ( θ ( t ) \theta^{(t)} θ(t) ) 来最大化隐变量的期望。 -
M步(Maximization step):
在M步中,我们最大化E步得到的期望函数 ( Q ( θ , θ ( t ) ) Q(\theta, \theta^{(t)}) Q(θ,θ(t)) ) 来更新参数 ( θ \theta θ ):
θ ( t + 1 ) = arg max θ Q ( θ , θ ( t ) ) \theta^{(t+1)} = \arg\max_\theta Q(\theta, \theta^{(t)}) θ(t+1)=argθmaxQ(θ,θ(t))
这也可以视为一次坐标上升,因为我们固定隐变量的期望并优化参数 ( θ \theta θ )。
在EM算法中,E步和M步交替进行,每一轮都像是在“沿着不同的坐标轴上升”,通过迭代更新参数和隐变量的估计值。
数学公式
假设我们有如下的对数似然函数:
L ( θ ) = log P ( X ∣ θ ) = log ∑ Z P ( X , Z ∣ θ ) \mathcal{L}(\theta) = \log P(X | \theta) = \log \sum_{Z} P(X, Z | \theta) L(θ)=logP(X∣θ)=logZ∑P(X,Z∣θ)
由于隐变量 ( Z ) 无法直接观测,我们在E步中计算隐变量的条件期望:
Q ( θ , θ ( t ) ) = E Z ∣ X , θ ( t ) [ log P ( X , Z ∣ θ ) ] Q(\theta, \theta^{(t)}) = \mathbb{E}_{Z | X, \theta^{(t)}} \left[ \log P(X, Z | \theta) \right] Q(θ,θ(t))=EZ∣X,θ(t)[logP(X,Z∣θ)]
然后在M步中最大化这个期望:
θ ( t + 1 ) = arg max θ Q ( θ , θ ( t ) ) \theta^{(t+1)} = \arg\max_\theta Q(\theta, \theta^{(t)}) θ(t+1)=argθmaxQ(θ,θ(t))
这两个步骤交替进行,直到收敛为止。
例子:高斯混合模型(Gaussian Mixture Model, GMM)
高斯混合模型是一种常见的隐变量模型,它假设数据是由多个高斯分布组成的,每个数据点属于某个高斯分布,但我们不知道每个数据点具体属于哪个分布。EM算法可以用来估计模型参数。
假设数据 ( X = { x 1 , x 2 , … , x n } X = \{x_1, x_2, \dots, x_n\} X={x1,x2,…,xn} ) 来自两个高斯分布,每个高斯分布的参数为 ( ( μ 1 , σ 1 2 ) (\mu_1, \sigma_1^2) (μ1,σ12) ) 和 ( ( μ 2 , σ 2 2 ) (\mu_2, \sigma_2^2) (μ2,σ22) )。我们要估计这些参数。
E步:计算每个数据点属于每个高斯分布的概率(即隐变量的期望):
给定当前参数 ( ( μ 1 ( t ) , σ 1 2 ( t ) , μ 2 ( t ) , σ 2 2 ( t ) ) (\mu_1^{(t)}, \sigma_1^{2(t)}, \mu_2^{(t)}, \sigma_2^{2(t)}) (μ1(t),σ12(t),μ2(t),σ22(t)) ),我们计算每个数据点 ( x i x_i xi ) 属于第一个高斯分布的概率(称为责任):
γ i 1 = π 1 N ( x i ∣ μ 1 ( t ) , σ 1 2 ( t ) ) π 1 N ( x i ∣ μ 1 ( t ) , σ 1 2 ( t ) ) + π 2 N ( x i ∣ μ 2 ( t ) , σ 2 2 ( t ) ) \gamma_{i1} = \frac{\pi_1 \mathcal{N}(x_i | \mu_1^{(t)}, \sigma_1^{2(t)})}{\pi_1 \mathcal{N}(x_i | \mu_1^{(t)}, \sigma_1^{2(t)}) + \pi_2 \mathcal{N}(x_i | \mu_2^{(t)}, \sigma_2^{2(t)})} γi1=π1N(xi∣μ1(t),σ12(t))+π2N(xi∣μ2(t),σ22(t))π1N(xi∣μ1(t),σ12(t))
其中, ( π 1 \pi_1 π1 ) 和 ( π 2 \pi_2 π2 ) 是高斯分布的混合系数, ( N ( x i ∣ μ , σ 2 ) \mathcal{N}(x_i | \mu, \sigma^2) N(xi∣μ,σ2) ) 是高斯分布的概率密度函数。
M步:最大化E步得到的期望函数以更新参数:
根据责任 ( γ i 1 \gamma_{i1} γi1 ) 和 ( γ i 2 \gamma_{i2} γi2 ),我们更新高斯分布的均值和方差:
μ 1 ( t + 1 ) = ∑ i = 1 n γ i 1 x i ∑ i = 1 n γ i 1 \mu_1^{(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{i1} x_i}{\sum_{i=1}^{n} \gamma_{i1}} μ1(t+1)=∑i=1nγi1∑i=1nγi1xi
σ 1 2 ( t + 1 ) = ∑ i = 1 n γ i 1 ( x i − μ 1 ( t + 1 ) ) 2 ∑ i = 1 n γ i 1 \sigma_1^{2(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{i1} (x_i - \mu_1^{(t+1)})^2}{\sum_{i=1}^{n} \gamma_{i1}} σ12(t+1)=∑i=1nγi1∑i=1nγi1(xi−μ1(t+1))2
类似地,我们也更新 ( μ 2 \mu_2 μ2 ) 和 ( σ 2 2 \sigma_2^2 σ22 ),并更新混合系数 ( π 1 \pi_1 π1 ) 和 ( π 2 \pi_2 π2 )。
Python代码实现
以下是使用EM算法估计GMM参数的简单Python代码示例:
具体解析见文末
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm# 生成数据:两个高斯分布混合的样本
np.random.seed(42)
n_samples = 500
data1 = np.random.normal(0, 1, n_samples//2) # 均值为0,标准差为1
data2 = np.random.normal(5, 1, n_samples//2) # 均值为5,标准差为1
X = np.concatenate([data1, data2])# 初始化参数
pi = np.array([0.5, 0.5]) # 混合系数
mu = np.array([0, 5]) # 均值
sigma = np.array([1, 1]) # 方差# EM算法迭代
n_iterations = 100
for iteration in range(n_iterations):# E步:计算责任(每个点属于每个分布的概率)resp1 = pi[0] * norm.pdf(X, mu[0], sigma[0])resp2 = pi[1] * norm.pdf(X, mu[1], sigma[1])total_resp = resp1 + resp2resp1 /= total_respresp2 /= total_resp# M步:最大化Q函数,更新参数pi[0] = np.mean(resp1)pi[1] = np.mean(resp2)mu[0] = np.sum(resp1 * X) / np.sum(resp1)mu[1] = np.sum(resp2 * X) / np.sum(resp2)sigma[0] = np.sqrt(np.sum(resp1 * (X - mu[0])**2) / np.sum(resp1))sigma[1] = np.sqrt(np.sum(resp2 * (X - mu[1])**2) / np.sum(resp2))# 打印每次迭代的结果print(f"Iteration {iteration+1}:")print(f" pi: {pi}")print(f" mu: {mu}")print(f" sigma: {sigma}")# 可视化结果
x_vals = np.linspace(-5, 10, 1000)
y_vals = pi[0] * norm.pdf(x_vals, mu[0], sigma[0]) + pi[1] * norm.pdf(x_vals, mu[1], sigma[1])
plt.hist(X, bins=30, density=True, alpha=0.6, color='g')
plt.plot(x_vals, y_vals, color='r')
plt.title('EM Algorithm for GMM')
plt.show()
总结
Coordinate Ascent(坐标上升)算法是一种通过逐个优化坐标(变量)来最大化目标函数的方法。在EM算法中,E步和M步分别优化隐变量的期望和模型参数的最大化,这可以看作是一种坐标上升的过程。通过反复交替执行这两个步骤,EM算法最终能够逼近最优的模型参数。
英文版
What is the Coordinate Ascent Algorithm?
Coordinate Ascent is an optimization algorithm that improves one variable (or coordinate) at a time, while keeping the other variables fixed, gradually approaching the optimal solution. In contrast to Coordinate Descent, which focuses on minimizing the objective function, Coordinate Ascent is used to maximize the objective function, typically in situations where the optimization function is separable across dimensions.
In each iteration, the Coordinate Ascent algorithm selects a variable and maximizes the contribution of that variable to the objective function, then proceeds to the next variable for optimization. The process involves iteratively updating each coordinate to find the optimal solution. While this process may not always converge globally, it performs well in many practical problems, particularly when the objective function is simple, and each coordinate can be independently optimized.
Coordinate Ascent in the Context of the EM Algorithm
The Expectation-Maximization (EM) algorithm is a common optimization method used in statistics and machine learning, especially when dealing with hidden variables in the data. The basic idea of the EM algorithm is to iteratively solve the Expectation (E) step and Maximization (M) step to optimize the model parameters.
We can view the EM algorithm as a form of Coordinate Ascent. Each round of the E-step and M-step can be seen as optimizing different “coordinates,” with the E-step optimizing the expectation function, and the M-step maximizing that expectation.
Coordinate Ascent in the EM Algorithm
Assume we have a latent variable model with the goal of maximizing the log-likelihood function:
L ( θ ) = log P ( X , Z ∣ θ ) \mathcal{L}(\theta) = \log P(X, Z | \theta) L(θ)=logP(X,Z∣θ)
where:
- ( X X X ) represents observed data,
- ( Z Z Z ) represents latent variables,
- ( θ \theta θ ) represents model parameters.
Since the latent variables ( Z Z Z ) are unobserved, directly maximizing the log-likelihood function is difficult. Thus, the EM algorithm solves this problem iteratively.
-
E-step (Expectation step):
In the E-step, we compute the expected value of the latent variables, i.e., given the current parameter estimate ( θ ( t ) \theta^{(t)} θ(t) ), the conditional probability distribution of the latent variables ( Z Z Z ):
Q ( θ , θ ( t ) ) = E Z ∣ X , θ ( t ) [ log P ( X , Z ∣ θ ) ] Q(\theta, \theta^{(t)}) = \mathbb{E}_{Z | X, \theta^{(t)}} \left[ \log P(X, Z | \theta) \right] Q(θ,θ(t))=EZ∣X,θ(t)[logP(X,Z∣θ)]
This is a coordinate ascent process because we maximize the expectation of the latent variables while holding the current parameters ( θ ( t ) \theta^{(t)} θ(t) ) fixed. -
M-step (Maximization step):
In the M-step, we maximize the expected function ( Q ( θ , θ ( t ) ) Q(\theta, \theta^{(t)}) Q(θ,θ(t)) ) computed in the E-step to update the model parameters ( θ \theta θ ):
θ ( t + 1 ) = arg max θ Q ( θ , θ ( t ) ) \theta^{(t+1)} = \arg\max_\theta Q(\theta, \theta^{(t)}) θ(t+1)=argθmaxQ(θ,θ(t))
This is also a coordinate ascent process because we fix the expectation of the latent variables and optimize the parameters ( θ \theta θ ).
The E-step and M-step alternate, with each step resembling an ascent along different coordinates. Through iterative updates of the parameters and the latent variables’ estimates, the EM algorithm moves towards the optimal solution.
Mathematical Formulas
Assume we have the following log-likelihood function:
L ( θ ) = log P ( X ∣ θ ) = log ∑ Z P ( X , Z ∣ θ ) \mathcal{L}(\theta) = \log P(X | \theta) = \log \sum_{Z} P(X, Z | \theta) L(θ)=logP(X∣θ)=logZ∑P(X,Z∣θ)
Since the latent variables ( Z ) are unobserved, in the E-step, we compute the conditional expectation of the latent variables:
Q ( θ , θ ( t ) ) = E Z ∣ X , θ ( t ) [ log P ( X , Z ∣ θ ) ] Q(\theta, \theta^{(t)}) = \mathbb{E}_{Z | X, \theta^{(t)}} \left[ \log P(X, Z | \theta) \right] Q(θ,θ(t))=EZ∣X,θ(t)[logP(X,Z∣θ)]
Then, in the M-step, we maximize this expectation:
θ ( t + 1 ) = arg max θ Q ( θ , θ ( t ) ) \theta^{(t+1)} = \arg\max_\theta Q(\theta, \theta^{(t)}) θ(t+1)=argθmaxQ(θ,θ(t))
These two steps alternate until convergence.
Example: Gaussian Mixture Model (GMM)
A Gaussian Mixture Model (GMM) is a common latent variable model that assumes the data is generated from a mixture of several Gaussian distributions. Each data point belongs to one of the Gaussian distributions, but we don’t know which one. The EM algorithm can be used to estimate the model parameters.
Assume that the data ( X = { x 1 , x 2 , … , x n } X = \{x_1, x_2, \dots, x_n\} X={x1,x2,…,xn} ) comes from two Gaussian distributions, with parameters ( ( μ 1 , σ 1 2 ) (\mu_1, \sigma_1^2) (μ1,σ12) ) and ( ( μ 2 , σ 2 2 ) (\mu_2, \sigma_2^2) (μ2,σ22) ), and we aim to estimate these parameters.
E-step: Calculate the probability (responsibility) of each data point belonging to each Gaussian distribution:
Given the current parameters ( ( μ 1 ( t ) , σ 1 2 ( t ) , μ 2 ( t ) , σ 2 2 ( t ) ) (\mu_1^{(t)}, \sigma_1^{2(t)}, \mu_2^{(t)}, \sigma_2^{2(t)}) (μ1(t),σ12(t),μ2(t),σ22(t)) ), we calculate the probability that data point ( x i x_i xi ) belongs to the first Gaussian distribution (called responsibility):
γ i 1 = π 1 N ( x i ∣ μ 1 ( t ) , σ 1 2 ( t ) ) π 1 N ( x i ∣ μ 1 ( t ) , σ 1 2 ( t ) ) + π 2 N ( x i ∣ μ 2 ( t ) , σ 2 2 ( t ) ) \gamma_{i1} = \frac{\pi_1 \mathcal{N}(x_i | \mu_1^{(t)}, \sigma_1^{2(t)})}{\pi_1 \mathcal{N}(x_i | \mu_1^{(t)}, \sigma_1^{2(t)}) + \pi_2 \mathcal{N}(x_i | \mu_2^{(t)}, \sigma_2^{2(t)})} γi1=π1N(xi∣μ1(t),σ12(t))+π2N(xi∣μ2(t),σ22(t))π1N(xi∣μ1(t),σ12(t))
where ( π 1 \pi_1 π1 ) and ( π 2 \pi_2 π2 ) are the mixing coefficients, and ( N ( x i ∣ μ , σ 2 ) \mathcal{N}(x_i | \mu, \sigma^2) N(xi∣μ,σ2) ) is the Gaussian distribution’s probability density function.
M-step: Maximize the expected function obtained in the E-step to update the parameters:
Using the responsibilities ( γ i 1 \gamma_{i1} γi1 ) and ( γ i 2 \gamma_{i2} γi2 ), we update the Gaussian distribution’s mean and variance:
μ 1 ( t + 1 ) = ∑ i = 1 n γ i 1 x i ∑ i = 1 n γ i 1 \mu_1^{(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{i1} x_i}{\sum_{i=1}^{n} \gamma_{i1}} μ1(t+1)=∑i=1nγi1∑i=1nγi1xi
σ 1 2 ( t + 1 ) = ∑ i = 1 n γ i 1 ( x i − μ 1 ( t + 1 ) ) 2 ∑ i = 1 n γ i 1 \sigma_1^{2(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{i1} (x_i - \mu_1^{(t+1)})^2}{\sum_{i=1}^{n} \gamma_{i1}} σ12(t+1)=∑i=1nγi1∑i=1nγi1(xi−μ1(t+1))2
Similarly, we update ( μ 2 \mu_2 μ2 ), ( σ 2 2 \sigma_2^2 σ22 ), and the mixing coefficients ( π 1 \pi_1 π1 ) and ( π 2 \pi_2 π2 ).
Python Code Implementation
Here is a simple Python implementation of the EM algorithm for estimating GMM parameters:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm# Generate data: samples from a mixture of two Gaussian distributions
np.random.seed(42)
n_samples = 500
data1 = np.random.normal(0, 1, n_samples//2) # Mean 0, Std 1
data2 = np.random.normal(5, 1, n_samples//2) # Mean 5, Std 1
X = np.concatenate([data1, data2])# Initialize parameters
pi = np.array([0.5, 0.5]) # Mixing coefficients
mu = np.array([0, 5]) # Means
sigma = np.array([1, 1]) # Variances# EM algorithm iterations
n_iterations = 100
for iteration in range(n_iterations):# E-step: Calculate responsibilities (probabilities of each point belonging to each distribution)resp1 = pi[0] * norm.pdf(X, mu[0], sigma[0])resp2 = pi[1] * norm.pdf(X, mu[1], sigma[1])total_resp = resp1 + resp2resp1 /= total_respresp2 /= total_resp# M-step: Maximize Q function, update parameterspi[0] = np.mean(resp1)pi[1] = np.mean(resp2)mu[0] = np.sum(resp1 * X) / np.sum(resp1)mu[1] = np.sum(resp2 * X) / np.sum(resp2)sigma[0] = np.sqrt(np.sum(resp1 * (X - mu[0])**2) / np.sum(resp1))sigma[1] = np.sqrt(np.sum(resp2 * (X - mu[1])**2) / np.sum(resp2))# Print results for each iterationprint(f"Iteration {iteration+1}:")print(f" pi: {pi}")print(f" mu: {mu}")print(f" sigma: {sigma}")# Visualize the results
x_vals = np.linspace(-5, 10, 1000)
y_vals = pi[0] * norm.pdf(x_vals, mu[0], sigma[0]) + pi[1] * norm.pdf(x_vals, mu[1], sigma[1])
plt.hist(X, bins=30, density=True, alpha=0.6, color='g')
plt.plot(x_vals, y_vals, color='r')
plt.title('EM Algorithm for GMM')plt.show()
Summary
The Coordinate Ascent method is a useful iterative approach to optimization, where only one parameter (coordinate) is updated at a time while others remain fixed. This method is central to algorithms like Expectation-Maximization (EM), where the E-step and M-step alternately optimize different coordinates (latent variables and model parameters). The Gaussian Mixture Model (GMM) example illustrates how the EM algorithm can be used for model estimation in latent variable models.
python代码中哪一步体现数据的似然最大化?
在EM算法的M步中,“最大化对数似然函数” 这一目标体现在通过更新参数(混合系数 ( π k \pi_k πk )、均值 ( μ k \mu_k μk ) 和方差 ( σ k \sigma_k σk ))来最大化 每个数据点的概率,从而最大化整个模型的 数据似然。下面详细解析这个过程,并明确如何体现最大化似然。
1. 对数似然函数的目标
对数似然函数表示的是数据给定当前模型参数下的 概率。在高斯混合模型(GMM)中,假设我们有一个由 ( K K K ) 个高斯分布组成的混合模型,数据集 ( X = { x 1 , x 2 , … , x N } X = \{x_1, x_2, \dots, x_N\} X={x1,x2,…,xN} ) 由这些高斯分布生成。
模型的对数似然函数是:
L ( θ ) = ∑ i = 1 N log ( ∑ k = 1 K π k N ( x i ; μ k , σ k 2 ) ) \mathcal{L}(\theta) = \sum_{i=1}^{N} \log \left( \sum_{k=1}^{K} \pi_k \mathcal{N}(x_i; \mu_k, \sigma_k^2) \right) L(θ)=i=1∑Nlog(k=1∑KπkN(xi;μk,σk2))
其中:
- ( π k \pi_k πk ) 是第 ( k k k ) 个高斯分布的混合系数(即该高斯分布在总模型中的权重),
- ( N ( x i ; μ k , σ k 2 ) \mathcal{N}(x_i; \mu_k, \sigma_k^2) N(xi;μk,σk2) ) 是第 ( k k k ) 个高斯分布对数据点 ( x i x_i xi ) 的概率密度,
- ( μ k \mu_k μk ) 是第 ( k k k ) 个高斯分布的均值,
- ( σ k 2 \sigma_k^2 σk2 ) 是第 ( k k k ) 个高斯分布的方差。
对数似然函数衡量的是在给定数据的情况下,当前模型参数 ( π k , μ k , σ k \pi_k, \mu_k, \sigma_k πk,μk,σk ) 下数据的 整体似然。我们的目标是最大化这个对数似然函数,找到能够最好地解释数据的模型参数。
2. M步的作用:最大化似然函数
在EM算法中,M步的目标是 最大化对数似然函数,即通过更新参数使得模型的似然值(数据的概率)最大化。如何做到这一点呢?
2.1. E步:计算责任(期望)
在E步中,我们计算每个数据点 ( x i x_i xi ) 属于每个高斯分布 ( k k k ) 的 责任,即数据点 ( x i x_i xi ) 属于第 ( k k k ) 个分布的后验概率:
γ i k = P ( z i = k ∣ x i ) = π k N ( x i ; μ k , σ k 2 ) ∑ k ′ = 1 K π k ′ N ( x i ; μ k ′ , σ k ′ 2 ) \gamma_{ik} = P(z_i = k | x_i) = \frac{\pi_k \mathcal{N}(x_i; \mu_k, \sigma_k^2)}{\sum_{k'=1}^{K} \pi_{k'} \mathcal{N}(x_i; \mu_{k'}, \sigma_{k'}^2)} γik=P(zi=k∣xi)=∑k′=1Kπk′N(xi;μk′,σk′2)πkN(xi;μk,σk2)
这里,( γ i k \gamma_{ik} γik ) 表示数据点 ( x i x_i xi ) 属于高斯分布 ( k k k ) 的概率(责任)。
2.2. M步:更新参数
E步计算得到的责任 ( γ i k \gamma_{ik} γik ) 用来在M步中更新模型的参数。通过更新混合系数 ( π k \pi_k πk )、均值 ( μ k \mu_k μk ) 和方差 ( σ k \sigma_k σk ),我们让每个数据点的似然(由它属于每个高斯分布的概率加权计算的)最大化。
- 更新混合系数 ( π k \pi_k πk ):混合系数表示每个高斯分布的权重。更新规则为:
π k = 1 N ∑ i = 1 N γ i k \pi_k = \frac{1}{N} \sum_{i=1}^{N} \gamma_{ik} πk=N1i=1∑Nγik
这里的更新表示的是:每个高斯分布的权重是所有数据点在该分布下的责任(概率)的平均值。
- 更新均值 ( μ k \mu_k μk ):均值是高斯分布的中心,通过对数据点的加权平均来更新。更新规则为:
μ k = ∑ i = 1 N γ i k x i ∑ i = 1 N γ i k \mu_k = \frac{\sum_{i=1}^{N} \gamma_{ik} x_i}{\sum_{i=1}^{N} \gamma_{ik}} μk=∑i=1Nγik∑i=1Nγikxi
这里的加权平均意味着:每个数据点 ( x i x_i xi ) 对均值的贡献是它属于该高斯分布的概率 ( γ i k \gamma_{ik} γik )。
- 更新方差 ( σ k 2 \sigma_k^2 σk2 ):方差衡量高斯分布的扩散程度,同样通过加权平方差来更新。更新规则为:
σ k 2 = ∑ i = 1 N γ i k ( x i − μ k ) 2 ∑ i = 1 N γ i k \sigma_k^2 = \frac{\sum_{i=1}^{N} \gamma_{ik} (x_i - \mu_k)^2}{\sum_{i=1}^{N} \gamma_{ik}} σk2=∑i=1Nγik∑i=1Nγik(xi−μk)2
同样地,这里加权平方差意味着:每个数据点 ( x i x_i xi ) 对方差的贡献是它属于该高斯分布的概率 ( γ i k \gamma_{ik} γik )。
3. 最大化似然:如何体现
最大化对数似然函数是通过更新参数(( π k , μ k , σ k \pi_k, \mu_k, \sigma_k πk,μk,σk ))使得每个数据点的 似然 最大化来实现的。在M步中:
- 更新混合系数 ( π k \pi_k πk ):通过更新每个高斯分布的权重,使得每个数据点属于各个高斯分布的责任概率最大化。
- 更新均值 ( μ k \mu_k μk ):通过更新每个高斯分布的均值,使得数据点在该分布下的概率密度最大化。
- 更新方差 ( σ k \sigma_k σk ):通过更新每个高斯分布的方差,使得数据点在该分布下的概率密度最大化。
最终,EM算法会通过迭代E步和M步,逐步优化参数,直到对数似然函数收敛,即模型的似然最大化。
M步通过计算每个数据点在E步中得到的责任,更新模型的参数(混合系数 ( π k \pi_k πk )、均值 ( μ k \mu_k μk ) 和方差 ( σ k \sigma_k σk )),使得数据在当前模型下的 似然 最大化。这是通过加权平均和加权平方差的方式来进行的,每次更新都朝着使数据在当前模型下最可能的方向调整参数。
后记
2024年12月28日14点12分于上海,在GPT4o大模型辅助下完成。
相关文章:
以EM算法为例介绍坐标上升(Coordinate Ascent)算法:中英双语
中文版 什么是 Coordinate Ascent 算法? Coordinate Ascent(坐标上升)是一种优化算法,它通过在每次迭代时优化一个变量(或一个坐标),并保持其他变量不变,逐步逼近最优解。与坐标下…...
Spark生态圈
Spark 主要用于替代Hadoop中的 MapReduce 计算模型。存储依然可以使用 HDFS,但是中间结果可以存放在内存中;调度可以使用 Spark 内置的,也可以使用更成熟的调度系统 YARN 等。 Spark有完善的生态圈: Spark Core:实现了…...
CSDN编辑器
这里写自定义目录标题 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个…...
【信息系统项目管理师】高分论文:论信息系统项目的资源管理(智慧储电站系统)
更多内容请见: 备考信息系统项目管理师-专栏介绍和目录 文章目录 论文1、规划资源管理2、估算活动资源3、获取资源4、建设团队5、管理团队6、控制资源论文 根据国家2030年前碳达峰行动方案,提出全面推进风电、太阳能发电大规模开发和高质量发展。XX地国家电网启动了“数字李…...
Web开发:ORM框架之使用Freesql的分表分页写法
一、自动分表(高版本可用) 特性写法 //假如是按月分表:[Table(Name "log_{yyyyMM}", AsTable "createtime2022-1-1(1 month)")]注意:①需包含log_202201这张表 ②递增规律是一个月一次,确保他们…...
Unity功能模块一对话系统(1)前置准备
也许你也曾被游戏中的对话系统深深吸引,那些精心设计的对白、鲜活的角色配音、甚至是简单的文字对话,往往能让玩家产生强烈的代入感和情感共鸣。如果你正在开发一款游戏,或者计划为你的项目加入一个引人入胜的对话系统,那么 Unity…...
strrchr的概念和使用案例
strrchr 是 C 语言标准库中的一个函数,用于在字符串中查找最后一次出现的字符,并返回指向该字符的指针。 概念: strrchr 函数在给定的字符串中从末尾开始搜索指定的字符,返回一个指向该字符最后一次出现的指针。如果字符在字符串…...
缓存管理自动化:JuiceFS 企业版 Cache Group Operator 新特性发布
近期,JuiceFS 企业版推出了 Cache Group Operator,用于自动化创建和管理缓存组集群。Operator 是一种简化 Kubernetes 应用管理的工具,它能够自动化应用程序的生命周期管理任务,使部署、扩展和运维更加高效。 在推出 Operator 之前…...
C++ 并发专题 - 实现一个线程安全的队列
一:概述 本文利用 C 标准库中的多线程、条件变量、互斥锁等工具来实现一个线程安全的队列,并且使用多个线程来向队列中添加和获取数据。 二:实现过程: #include <iostream> #include <queue> #include <mutex&g…...
SQL 基础教程
SQL 是用于访问和处理数据库的标准的计算机语言。 在本教程中,您将学到如何使用 SQL 访问和处理数据系统中的数据,这类数据库包括:Oracle, Sybase, SQL Server, DB2, Access 等等。 SQL 是用于访问和处理数据库的标准的计算机语言。 什么是…...
【源码】Sharding-JDBC源码分析之SQL中影子库ShadowSQLRouter路由的原理
Sharding-JDBC系列 1、Sharding-JDBC分库分表的基本使用 2、Sharding-JDBC分库分表之SpringBoot分片策略 3、Sharding-JDBC分库分表之SpringBoot主从配置 4、SpringBoot集成Sharding-JDBC-5.3.0分库分表 5、SpringBoot集成Sharding-JDBC-5.3.0实现按月动态建表分表 6、【…...
雷池 WAF 搭配阿里云 CDN 使用教程
雷池 WAF(Web Application Firewall)是一款强大的网络安全防护产品,通过实时流量分析和精准规则拦截,有效抵御各种网络攻击。在部署雷池 WAF 的同时,结合阿里云 CDN(内容分发网络)可以显著提升网…...
3.银河麒麟V10 离线安装Nginx
1. 下载nginx离线安装包 前往官网下载离线压缩包 2. 下载3个依赖 openssl依赖,前往 官网下载 pcre2依赖下载,前往Git下载 zlib依赖下载,前往Git下载 下载完成后完整的包如下: 如果网速下载不到请使用网盘下载 通过网盘分享的文件…...
【模块一】kubernetes容器编排进阶实战之kubernetes 资源限制
kubernetes 资源限制 kubernetes中资源限制概括 1.如果运行的容器没有定义资源(memory、CPU)等限制,但是在namespace定义了LimitRange限制,那么该容器会继承LimitRange中的 默认限制。 2.如果namespace没有定义LimitRange限制,那么该容器可…...
【开源】一款基于SpringBoot的智慧小区物业管理系统
一、下载项目文件 项目文件源码链接:https://pan.quark.cn/s/3998d958e182如出现网盘空间不够存的情况!!!解决办法是先用夸克手机app注册,然后保存上方链接,就可以得到1TB空间了!!&…...
Goland:专为Go语言设计的高效IDE
本文还有配套的精品资源,点击获取 简介:Goland是JetBrains公司开发的集成开发环境(IDE),专为Go语言设计,提供了高效的代码编辑、强大的调试工具和丰富的项目管理功能。其智能代码补全、强大的调试与测试支…...
云手机与Temu矩阵:跨境电商运营新引擎
云手机与 Temu 矩阵结合的基础 云手机技术原理 云手机基于先进的 ARM 虚拟化技术,在服务器端运行 APP。通过在服务器上利用容器虚拟化软件技术,能够虚拟出多个独立的手机操作系统实例,每个实例等同于一部单独的手机,可独立运行各…...
仓颉编程笔记1:变量函数定义,常用关键字,实际编写示例
本文就在网页版上体验一下仓颉编程,就先不下载它的SDK了 基本围绕着实际摸索的编程规则来写的 也没心思多看它的文档,写的不太明确,至少我是看的一知半解的 文章提供测试代码讲解、测试效果图: 目录 仓颉编程在线体验网址&…...
Python小括号( )、中括号[ ]和大括号{}代表什么
python语言最常见的括号有三种,分别是:小括号( )、中括号[ ]和大括号也叫做花括号{ },分别用来代表不同的python基本内置数据类型。 小括号():struct结构体,但不能改值 python中的小括号( )&am…...
React里使用lodash工具库
安装 使用命令 npm install lodash 页面引入 常见的引入方式 引入整个lodash对象: import _ from lodash按名称引入特定的函数: import { orderBy } from "lodash"; tips: 这两种引入方式都会引入整个lodash库, 体积大&#x…...
Chapter03-Authentication vulnerabilities
文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
遍历 Map 类型集合的方法汇总
1 方法一 先用方法 keySet() 获取集合中的所有键。再通过 gey(key) 方法用对应键获取值 import java.util.HashMap; import java.util.Set;public class Test {public static void main(String[] args) {HashMap hashMap new HashMap();hashMap.put("语文",99);has…...
无法与IP建立连接,未能下载VSCode服务器
如题,在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈,发现是VSCode版本自动更新惹的祸!!! 在VSCode的帮助->关于这里发现前几天VSCode自动更新了,我的版本号变成了1.100.3 才导致了远程连接出…...
【Go】3、Go语言进阶与依赖管理
前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课,做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程,它的核心机制是 Goroutine 协程、Channel 通道,并基于CSP(Communicating Sequential Processes࿰…...
微服务商城-商品微服务
数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...
JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...
Element Plus 表单(el-form)中关于正整数输入的校验规则
目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入(联动)2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...
蓝桥杯3498 01串的熵
问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798, 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…...
AI,如何重构理解、匹配与决策?
AI 时代,我们如何理解消费? 作者|王彬 封面|Unplash 人们通过信息理解世界。 曾几何时,PC 与移动互联网重塑了人们的购物路径:信息变得唾手可得,商品决策变得高度依赖内容。 但 AI 时代的来…...
