如何在Python中绘制最大似然估计

发布于 2024-12-11 19:05:40 字数 362 浏览 0 评论 0原文

我正在从指数分布中抽取一些样本。在第一个实验中,我抽取了 1000 个样本,而在第二个实验中,我从该分布中抽取了 10,000 个样本。 (使用 numpy.random.exponential)

我想直观地比较我的两个实验的最大似然估计的差异。 (由于这是指数分布,MLE 只是样本平均值,因此在我的第二个实验中,MLE 应该更接近真实密度)。

我怎样才能在Python中进行这样的比较?我知道如何在 matplotlib 中绘制图形,但在这里我不知道应该使用什么类型的图形。

I am drawing some samples from an exponential distribution. In my first experiment, I am drawing 1000 samples and for the second, I am drawing 10,000 samples from this distribution. (with numpy.random.exponential)

I would like to visually compare the difference of the maximum likelihood estimate of my two experiments. (since this is exponential distribution, the MLE will be just sample mean, so with my second experiment, the MLE should be closer to the true density).

How can I do such a comparison in Python? I know how to plot graphics in matplotlib, but here I don't know what type of graphic I should use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-12-18 19:05:40

鉴于评论中的评论,我想您正在寻找类似以下内容的内容:

import numpy as np
import matplotlib.pyplot as plt

def plot_exponential_density(mu, xmax, fmt, label):
        x = np.arange(0, xmax, 0.1)
        y = 1/mu * np.exp(-x/mu)
        plt.plot(x, y, fmt, label=label)

def sample_and_plot(N, color):
        # first sample N valus
        samples = np.zeros( (N,1) )
        for i in range(0,N):
                samples[i] = np.random.exponential()

        # determine the mean
        mu = np.mean(samples)
        print("N = %d  ==> mu = %f" % (N, mu))

        # plot a histogram of the samples
        (n, bins) = np.histogram(samples, bins=int(np.sqrt(N)), density=True)
        plt.step(bins[:-1], n, color=color, label="samples N = %d" % N)

        xmax = max(bins)

        # plot the density according to the estimated mean
        plot_exponential_density(mu, xmax, color + "--", label="estimated density N = %d" % N)

        return xmax


# sample 100 values, draw a histogram, and the density according to
# the estimated mean
xmax1 = sample_and_plot(100, 'r')
# do the same for 1000 samples
xmax2 = sample_and_plot(10000, 'b')

# finally plot the true density
plot_exponential_density(1, max(xmax1, xmax2), 'k', "true density")

# add a legend
plt.legend()

# and show the plot
plt.show()

在此处输入图像描述

I使用了 100 和 10,000 个样本,因为使用 1,000 个样本,估计值已经相当不错了。但仍然只有 100 个样本,我对平均值以及密度的估计如此之好感到有些惊讶。仅给出直方图,而不知道样本是从指数分布中抽取的,我不确定我是否会在这里识别指数分布......

Given the remarks in the comments, I guess something like the following is what you're looking for:

import numpy as np
import matplotlib.pyplot as plt

def plot_exponential_density(mu, xmax, fmt, label):
        x = np.arange(0, xmax, 0.1)
        y = 1/mu * np.exp(-x/mu)
        plt.plot(x, y, fmt, label=label)

def sample_and_plot(N, color):
        # first sample N valus
        samples = np.zeros( (N,1) )
        for i in range(0,N):
                samples[i] = np.random.exponential()

        # determine the mean
        mu = np.mean(samples)
        print("N = %d  ==> mu = %f" % (N, mu))

        # plot a histogram of the samples
        (n, bins) = np.histogram(samples, bins=int(np.sqrt(N)), density=True)
        plt.step(bins[:-1], n, color=color, label="samples N = %d" % N)

        xmax = max(bins)

        # plot the density according to the estimated mean
        plot_exponential_density(mu, xmax, color + "--", label="estimated density N = %d" % N)

        return xmax


# sample 100 values, draw a histogram, and the density according to
# the estimated mean
xmax1 = sample_and_plot(100, 'r')
# do the same for 1000 samples
xmax2 = sample_and_plot(10000, 'b')

# finally plot the true density
plot_exponential_density(1, max(xmax1, xmax2), 'k', "true density")

# add a legend
plt.legend()

# and show the plot
plt.show()

enter image description here

I used 100 and 10,000 samples, since with 1,000 samples the estimate is already pretty good. But still with just 100 samples I'm somewhat surprised how good the estimate of the mean and thus of the density is. Given just the histogram without the knowledge that the samples are drawn from an exponential distribution, I'm not sure I would recognize an exponential distribution here ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文