gpt4 book ai didi

python - Scipy正常的pdf评估给出了矛盾的值

转载 作者:行者123 更新时间:2023-12-01 08:52:28 25 4
gpt4 key购买 nike

我试图写下一个图,表明无偏估计量并不总是最好的估计量。这是我为获得漂亮的图片而编写的代码:

# Set up the plot
fig, ax = plt.subplots()
# Create some data
y = np.linspace(-10, 10, 1000)
# Plot two normals, one centered around 0 but with large variance
ax.plot(y, norm.pdf(y, scale=3), 'k-', label=r"pdf of $\hat{\theta_1}$")
# One centered around 1 with small variance
ax.plot(y, norm.pdf(y, loc=1, scale=1), 'r--', label=r"pdf of $\hat{\theta_2}$")
ax.legend()
# Remove left, right and top axis, remove y axis labels and ticks
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.tick_params(axis='y', which='both', left=False, right=False, labelleft=False)
# Remove x axis ticks and labels, keep only one at x=0
ax.set_xticks([0])
ax.set_xticklabels([r"$\theta$"])
# Plot vertical line at x=0 from y=0 to the value of the first pdf
ax.axvline(x=0, ymin=0, ymax=norm.pdf(0, scale=3), linestyle=":")
# Plot second vertical line for second normal distribution
ax.axvline(x=1, ymin=0, ymax=norm.pdf(1, loc=1, scale=1), linestyle=":")
# Remove margins so that pdfs lie on the axis
ax.margins(0)
plt.show(block=True)

基本上我想绘制两个正态分布,一个以 0 为中心但方差较大,另一个以 1 为中心且方差较小。然后我想添加两条垂直线。一条线经过 x=0 并在 x=0 处达到第一正态分布的值,第二条线经过 x=1 并在 x=1 处达到第二正态分布的值。然而,这些线要小得多,我不知道为什么!

plot

我唯一的猜测是,由于这些是连续的 pdf,如果我在某个时刻评估它们,scipy 会做一些奇怪的事情。

导入

我忘了提及我的导入,我将它们包含在这里,以便您可以拥有 MWE

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm

最佳答案

查看 axvline 的文档:

ymax : scalar, optional, default: 1
Should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.

您的线条是在数据坐标中定义的,而不是在轴坐标中定义的。您需要使用vlines相反。

# Plot vertical line at x=0 from y=0 to the value of the first pdf
ax.vlines(x=0, ymin=0, ymax=norm.pdf(0, scale=3), linestyle=":")
# Plot second vertical line for second normal distribution
ax.vlines(x=1, ymin=0, ymax=norm.pdf(1, loc=1, scale=1), linestyle=":")

fixed plot

关于python - Scipy正常的pdf评估给出了矛盾的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com