再现正态分布的最大似然估计时遇到错误

时间:2019-04-21 06:40:20

标签: python probability

我设法复制了this post,并试图理解其逻辑。

这是代码。

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视为位置?谁的位置?

1 个答案:

答案 0 :(得分:1)

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

让我们仔细看看这个if / elif块。这样做的目的是跟踪到目前为止所看到的LL的最大值(max_val)和产生该LL最大值的mu的值。在这里使用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_location重命名为max_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)