Scipy: lognormal fitting

后端 未结 5 2048
渐次进展
渐次进展 2020-12-25 14:39

There have been quite a few posts on handling the lognorm distribution with Scipy but i still dont get the hang of it.

The 2 parameter lognormal is usua

5条回答
  •  情歌与酒
    2020-12-25 15:12

    I made the same observations: a free fit of all parameters fails most of the time. You can help by providing a better initial guess, fixing the parameter is not necessary.

    samp = stats.lognorm(0.5,loc=0,scale=1).rvs(size=2000)
    
    # this is where the fit gets it initial guess from
    print stats.lognorm._fitstart(samp)
    
    (1.0, 0.66628696413404565, 0.28031095750445462)
    
    print stats.lognorm.fit(samp)
    # note that the fit failed completely as the parameters did not change at all
    
    (1.0, 0.66628696413404565, 0.28031095750445462)
    
    # fit again with a better initial guess for loc
    print stats.lognorm.fit(samp, loc=0)
    
    (0.50146296628099118, 0.0011019321419653122, 0.99361128537912125)
    

    You can also make up your own function to calculate the initial guess, e.g.:

    def your_func(sample):
        # do some magic here
        return guess
    
    stats.lognorm._fitstart = your_func
    

提交回复
热议问题