[Scipy-svn] r6576 - in trunk/scipy/stats: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Mon Jun 28 19:14:33 EDT 2010


Author: oliphant
Date: 2010-06-28 18:14:32 -0500 (Mon, 28 Jun 2010)
New Revision: 6576

Modified:
   trunk/scipy/stats/distributions.py
   trunk/scipy/stats/tests/test_distributions.py
Log:
Allow optimizer to be changed in .fit method of distributions.

Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py	2010-06-27 15:02:57 UTC (rev 6575)
+++ trunk/scipy/stats/distributions.py	2010-06-28 23:14:32 UTC (rev 6576)
@@ -1626,9 +1626,14 @@
             Starting values for the location and scale parameters 
             Special keyword arguments are recognized as holding certain 
               parameters fixed:
-            f1..fn : hold respective shape paramters fixed
-            floc : hold location parameter fixed to specified value
-            fscale : hold scale parameter fixed to specified value
+               f0..fn : hold respective shape paramters fixed
+               floc : hold location parameter fixed to specified value
+               fscale : hold scale parameter fixed to specified value
+            optimizer : The optimizer to use.  The optimizer must take func, 
+                         and starting position as the first two arguments, 
+                         plus args (for extra arguments to pass to the 
+                         function to be optimized) and disp=0 to suppress
+                         output as keyword arguments.
               
         Return
         ------
@@ -1647,7 +1652,19 @@
         scale = kwds.get('scale', start[-1])
         args += (loc, scale)
         x0, func, restore, args = self._reduce_func(args, kwds)
-        vals = optimize.fmin(func,x0,args=(ravel(data),),disp=0)
+
+        optimizer = kwds.get('optimizer', optimize.fmin)
+        # convert string to function in scipy.optimize
+        if not callable(optimizer) and isinstance(optimizer, (str, unicode)):
+            if not optimizer.startswith('fmin_'):
+                optimizer = "fmin_"+optimizer
+            if optimizer == 'fmin_': 
+                optimizer = 'fmin'
+            try:
+                optimizer = getattr(optimize, optimizer)
+            except AttributeError:
+                raise ValueError, "%s is not a valid optimizer" % optimizer
+        vals = optimizer(func,x0,args=(ravel(data),),disp=0)
         vals = tuple(vals)
         if restore is not None:
             vals = restore(args, vals)

Modified: trunk/scipy/stats/tests/test_distributions.py
===================================================================
--- trunk/scipy/stats/tests/test_distributions.py	2010-06-27 15:02:57 UTC (rev 6575)
+++ trunk/scipy/stats/tests/test_distributions.py	2010-06-28 23:14:32 UTC (rev 6576)
@@ -394,10 +394,17 @@
             distfunc = getattr(stats, dist)
             res = distfunc.rvs(*args, **{'size':200})
             vals = distfunc.fit(res)
+            vals2 = distfunc.fit(res, optimizer='powell')
+            # Only check the length of the return
+            # FIXME: should check the actual results to see if we are 'close'
+            #   to what was created --- but what is 'close' enough
             if dist in ['erlang', 'frechet']:
                 assert(len(vals)==len(args))
+                assert(len(vals2)==len(args))
             else:
                 assert(len(vals) == 2+len(args))
+                assert(len(vals2)==2+len(args))
+                
 
     def test_fix_fit(self):
         for func, dist, args, alpha in test_all_distributions():




More information about the Scipy-svn mailing list