您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:15 python 字数 2999 阅读 511 来源 www.jb51.cc/python

我正在从exponential distribution中抽取一些样本.在我的第一个实验中,我正在绘制1000个样本,而在第二个实验中,我正在从该分布中抽取10,000个样本. (使用numpy.random.exponential)我想直观地比较两次实验的最大似然估计的差异. (因为这是指数分布,MLE将只是样本均值,所以在我的第二个实验中,MLE应该更接

概述

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

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

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

import numpy as np
import matplotlib.pyplot as plt

def plot_exponential_density(mu,xmax,fmt,label):
        x = np.arange(0,0.1)
        y = 1/mu * np.exp(-x/mu)
        plt.plot(x,y,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,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()

我使用了100和10,000个样本,因为有1,估计已经相当不错了.但是仍然只有100个样本,我有点惊讶于平均值和密度的估计有多好.鉴于直方图没有知道样本是从指数分布中提取的,我不确定我会在这里识别出指数分布……

总结

以上是编程之家为你收集整理的如何在Python中绘制最大似然估计值全部内容,希望文章能够帮你解决如何在Python中绘制最大似然估计值所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶