python - 在重现正态分布的最大似然估计时遇到错误
问题描述
我设法复制了这篇文章并试图理解它的逻辑。
这是代码。
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 被认为是位置?谁的位置?
解决方案
该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_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)
推荐阅读
- javascript - 如何解析菜单链接
- xslt - 满足条件时分组数据
- google-cloud-platform - 如何在 Google Dataproc 上安排 Spark 作业?
- vue.js - 带有链接的 VueJs Vuetify 自动完成
- php - 尝试根据运输类别在 Woocommerce 中的产品缩略图中添加“免费送货”徽章
- auth0 - 使用 Auth0 登录成功,但仍然返回 401“拒绝访问”?
- java - 如何通过 retrofit2 发送 ArrayList 和 Multipart?(错误:预期的 begin_object 但是字符串)
- android - 我可以在扩展 Android 中的 AppCompactDialogFragment 的类中调用另一个类构造函数吗?
- java - 使用 Java 原子变量执行方法调用
- powershell - 从 3.6.3 迁移到 3.8.7 或 3.10.2 后,Liquibase 停止工作