[SciPy-user] distribution fit()

Miguel Dias Costa mcosta at fc.up.pt
Mon Jan 24 07:52:48 EST 2005


Hello all,

I'm trying to fit a Frechet distribution to some data and have had some 
success by comparing the histogram to the pdf and minimizing the sum of 
squared differences, but only after spending some time trying to get the 
distribution.fit() method to work, without success.

This seems to take the actual data, not the histogram, and minimize a 
"negative log likelihood function" (nnlf(?) in the code). The fit() 
method only rearranges the arguments and keywords and calls optimize.fmin:

    def fit(self, data, *args, **kwds):
        loc0, scale0 = map(kwds.get, ['loc', 'scale'],[0.0, 1.0])
        Narg = len(args)
        if Narg != self.numargs:
            if Narg > self.numargs:
                raise ValueError, "Too many input arguments."
            else:
                args += (1.0,)*(self.numargs-Narg)
        # location and scale are at the end               
        x0 = args + (loc0, scale0)
        return optimize.fmin(self.nnlf,x0,args=(ravel(data),),disp=0)

which should allow me to call distribution.fit(data) since it fills the 
missing parameters.
But then optimize.fmin passes to nnlf the following arguments

    fsim[0] = apply(func,(x0,)+args)

that is (x0,args), which in my case would amount to ((c, loc, 
scale),ravel(data)) or, by default, ((1.0,0.0,1.0),ravel(data)).

However, nnlf expects:

    def nnlf(self, *args):
        # - sum (log pdf(x, theta))
        #   where theta are the parameters (including loc and scale)
        #
        try:
            x = args[-1]
            loc = args[-2]
            scale = args[-3]
            args = args[:-3]
        except IndexError:
            raise ValueError, "Not enough input arguments."

which fails, but it seems to me that it should expect something like

    def nnlf(self, *args):
        # - sum (log pdf(x, theta))
        #   where theta are the parameters (including loc and scale)
        #
        try:
            x = args[-1]
            loc = args[-2][-2]
            scale = args[-2][-1]
            args = args[-2]
        except IndexError:
            raise ValueError, "Not enough input arguments."

which works at first but the method still fails to find the parameters.

Am I completely missing the point here?

Thanks in advance.
Miguel Costa

P.S. (Almost) unrelated, there seems to be a missing  "from math import 
*" in /scipy/optimize/anneal.py, at least I needed it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20050124/7e118b5b/attachment.html>


More information about the SciPy-User mailing list