Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python fit K distribution (defined by me) on data

I have a sample distribution of data which I would like to fit with some not Python embedded statistics in scipy.stats, such as the K pdf. Is it then possible to do so? Are there, by chance other modules that have the k distribution or other not gaussian pdfs available?

Thanks for your help!

like image 501
user3284140 Avatar asked Jul 15 '26 11:07

user3284140


1 Answers

To follow up on @Robert Dodier 's comment, reading http://arxiv.org/pdf/1207.6002.pdf you'll find a recipe for Maximum Likelihood estimation, which I adapt here:

import scipy
import scipy.stats as sciStat
import scipy.optimize as sciOpt

def myMleEstimate(myFunc, par, data):
    def lnL_av(x, par):
        N = len(x)
        lnL = 0.
        for i in range(N):
            lnL += scipy.log(myFunc(par, x[i]))
        return lnL/N

    objFunc = lambda s: -lnL_av(data, s)
    par_mle = sciOpt.fmin(objFunc, par, disp=0)
    return par_mle

If you'd want to model a Rayleigh, you'd:

from scipy.stats import rayleigh
Rayleigh = lambda par, x: sciStat.rayleigh.pdf(x, loc=par[0], scale=par[1])

And estimate from your data:

estimated = myMleEstimate(Rayleigh, [0, 1], data)

(Here I chose 0, 1 starting parameters).

To test the last line, you could first sample a thousand data points using:

# parameters
params = {
    'loc': 1,
    'scale': 2
}
data = rayleigh.rvs(loc=params['loc'], scale=params['scale'], size=1000)

And yes, I understand that a K-distro is a compound of two gammas, not a Rayleigh. But sources such as Estimating the Parameters of the K Distribution in the Intensity Domain point out that it is quite difficult with ML estimation.

So you got what you asked, a Python recipe, but this may not be what you need.

like image 152
Hugues Fontenelle Avatar answered Jul 18 '26 01:07

Hugues Fontenelle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!