首页 > 解决方案 > 在重现正态分布的最大似然估计时遇到错误

问题描述

我设法复制了这篇文章并试图理解它的逻辑。

这是代码。

x = [4, 5, 7, 8, 8, 9, 10, 5, 2, 3, 5, 4, 8, 9]
# Plot the Maximum Likelihood Functions for different values of mu 
# and sigma
def plot_ll(x):
    plt.figure(figsize=(5,8))
    plt.title("Maximim Likelihood Functions")
    plt.xlabel("Mean Estimate")
    plt.ylabel("Log Likelihood")
    plt.ylim(-40, -30)
    plt.xlim(0, 12)
    mu_set = np.linspace(0, 16, 1000)
    sd_set = [.5, 1, 1.5, 2.5, 3, 3.5]
    max_val = max_val_location = None
    for sd_hat in sd_set:
        ll_array = []
        for mu_hat in mu_set:
            temp_mm = 0
            for smp in x:
                temp_mm += np.log(norm.pdf(smp, mu_hat, sd_hat)) # The LL function
            ll_array.append(temp_mm)
            if (max_val is None):
                max_val = max(ll_array)
            elif max(ll_array) > max_val:
                max_val = max(ll_array)
                max_val_location = mu_hat
        # Plot the results
        plt.plot(mu_set, ll_array, label="sd: %.1f" % sd_hat)
        print("The max LL for sd %.2f is %.2f" % (sd_hat, max(ll_array)))
    plt.axvline(x=max_val_location, color='black', ls='-.')
    plt.legend(loc='lower left')
plot_ll(x)

我已经掌握了 norm.pdf,对数似然实现。

temp_mm 用于缓存 mu = mu_hat 和 sd = sd_hat 的 x 可能性。

ll_array 是缓存样本 x 中每个元素的所有可能性。

max(ll_array) 是找到最大似然。

为什么 mu_hat 被认为是位置?谁的位置?

标签: pythonprobability

解决方案


max_val_location变量指的mu是对应于最大对数似然的值,因此它是mu_set产生最大对数似然的“位置”。我发现这个实现有点过于复杂了。

让我们仔细看看这个 if/elif 块。这样做的目的是跟踪迄今为止看到的 LL 的最大值 ( max_val),以及mu产生 LL 的最大值的那个值。这里的使用max是不必要且低效的,因为我们正在取一个运行最大值,只需要检查 LL 的最新值是否大于我们迄今为止看到的最大值。max_val_location = mu_hat此外,块下也应该有一个if。更好的是,这可能是if结合了这两个条件的一个。

if (max_val is None):
    max_val = max(ll_array)
elif max(ll_array) > max_val:
    max_val = max(ll_array)
    max_val_location = mu_hat

我已经稍微重写了这个例子,希望让它更清楚,包括重命名max_val_locationmax_val_parameters.

x = [4, 5, 7, 8, 8, 9, 10, 5, 2, 3, 5, 4, 8, 9]
# Plot the Maximum Likelihood Functions for different values of mu 
# and sigma
def plot_ll(x):
    plt.figure(figsize=(5,8))
    plt.title("Maximim Likelihood Functions")
    plt.xlabel("Mean Estimate")
    plt.ylabel("Log Likelihood")
    plt.ylim(-40, -30)
    plt.xlim(0, 12)
    mu_set = np.linspace(0, 16, 1000)
    sd_set = [.5, 1, 1.5, 2.5, 3, 3.5]
    max_val = None
    max_val_parameters = None # Keeps track of the (mu, sigma) that produces
                              # the maximum value of the LL
    for sd_hat in sd_set:
        ll_array = []
        for mu_hat in mu_set:
            temp_mm = 0
            for smp in x:
                temp_mm += np.log(norm.pdf(smp, mu_hat, sd_hat)) # The LL function
            ll_array.append(temp_mm)
            if max_val is None or temp_mm > max_val:
                # This `temp_mm` is the largest value of the LL that we've
                # seen so far, so keep track of its value and the (mu, sd)
                # that produced this value.
                max_val = temp_mm
                max_val_parameters = (mu_hat, sd_hat)
        # Plot the results
        plt.plot(mu_set, ll_array, label="sd: %.1f" % sd_hat)
        print("The max LL for sd %.2f is %.2f" % (sd_hat, max(ll_array)))
    plt.axvline(x=max_val_parameters[0], color='black', ls='-.')
    plt.legend(loc='lower left')
plot_ll(x)

推荐阅读