Fitting a Weibull distribution using Scipy

前端 未结 7 726
误落风尘
误落风尘 2020-12-04 09:22

I am trying to recreate maximum likelihood distribution fitting, I can already do this in Matlab and R, but now I want to use scipy. In particular, I would like to estimate

7条回答
  •  离开以前
    2020-12-04 09:57

    There have been a few answers to this already here and in other places. likt in Weibull distribution and the data in the same figure (with numpy and scipy)

    It still took me a while to come up with a clean toy example so I though it would be useful to post.

    from scipy import stats
    import matplotlib.pyplot as plt
    
    #input for pseudo data
    N = 10000
    Kappa_in = 1.8
    Lambda_in = 10
    a_in = 1
    loc_in = 0 
    
    #Generate data from given input
    data = stats.exponweib.rvs(a=a_in,c=Kappa_in, loc=loc_in, scale=Lambda_in, size = N)
    
    #The a and loc are fixed in the fit since it is standard to assume they are known
    a_out, Kappa_out, loc_out, Lambda_out = stats.exponweib.fit(data, f0=a_in,floc=loc_in)
    
    #Plot
    bins = range(51)
    fig = plt.figure() 
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(bins, stats.exponweib.pdf(bins, a=a_out,c=Kappa_out,loc=loc_out,scale = Lambda_out))
    ax.hist(data, bins = bins , normed=True, alpha=0.5)
    ax.annotate("Shape: $k = %.2f$ \n Scale: $\lambda = %.2f$"%(Kappa_out,Lambda_out), xy=(0.7, 0.85), xycoords=ax.transAxes)
    plt.show()
    

提交回复
热议问题