Issue 1:
The scipy curve_fit documentation says that xdata can be an object:

xdataarray_like or object

The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.

(emphasis mine)

This is currently not respected. Previously, anything could be passed in, but with 1.2.3, it appears that a numpy array is assumed and xdata is modified to float64.

xdata = xdata.astype(float)

The xdata should not be modified by curve_fit since it's not, in any way, meant for curve_fit to consume. It's meant for our models. These may rely on the data types of the xdata, possibly non-numeric. Even for numeric models, casting to float breaks models that rely on the data types passed to them, or would require possibly lossy conversions back.

This is a regression that breaks all models that use something besides float64 numpy arrays or need to preserve data types for their models to work.

Reproducing code example:

from scipy.optimize import curve_fit

class ExampleObject(object):
def __init__(self, a=5, b=3.0):
self.a = 5.0
self.b = 3.0

def model(xdata, k):
return xdata.a * k

values = range(10)

answer = curve_fit(
model, # Function to fit the data to
xdata=ExampleObject(),
ydata=values,
p0=[1.0],
)

Error message:

File "/usr/local/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 716, in curve_fit
xdata = xdata.astype(float)
AttributeError: 'ExampleObject' object has no attribute 'astype'

Scipy/Numpy/Python version information:

('1.2.3', '1.16.6', sys.version_info(major=2, minor=7, micro=17, releaselevel='final', serial=0))

Issue 2:
In the CZI Proposal, we wrote:

The continuous distributions in SciPy all have a method for fitting the distribution to data using

the method of maximum likelihood estimation (MLE). The proposed enhancements are:

Where possible, use an analytical formula rather than numerical optimization for greater

speed and accuracy

...

This is part of our effort to address the roadmap item "Improve the options for fitting a probability distribution to data".

One of the best sources I've found (via a NIST Engineering Statistics Handbook) is the Parameter Estimation sections from:

Evans, Hastings, and Peacock (2000), Statistical Distributions, 3rd. Ed., John Wiley and Sons.

Any other sources that are easily accessible?

Also, for method of moments (gh-11695), I could write a program that tries to solve for fitting formulas (from the formulas for the moments) using SymPy. I don't think that would work as well for MLE, though. Might not even get anything new for MM.

Given the formulas, this should be pretty straightforward work: just override the fit methods of the distributions defined in /scipy/stats/_continuous_distns.py, following existing examples (e.g. beta_gen) for extending documentation and any other conventions. For tests, I suppose we could mainly compare against the generic implementation. In cases where the generic implementation isn't working very well, we could check the partial derivatives of the likelihood function?

If you have any thoughts / words of wisdom before we get started, please let us know here.

@fletcheaston @swallan I think this is next.

Update:

@swallan @WarrenWeckesser A simple but really useful script for generating first-order conditions is here. Now it can (sometimes) solve for a variable in terms of the rest.

Continuous distributions
Current status of fit override
Page Number of MLE (Evans)

laplace
merged: gh-11988, good
124

rayleigh
merged: gh-12097, gh-17090, good
175

logistic
merged: gh-12738, gh-17117, gh-18177, almost good, but should use root_scalar
130

pareto
merged gh-15567, good
149

invgauss, wald
merged: gh-12514, further investigation needed
121

gumbel_r, gumbel_l
merged: gh-12737, good
101

powerlaw
merged: gh-13053, good
159

nakagami
see gh-10908
-

vonmises
merged: gh-17585, gh-18013, gh-18128. Almost good; see gh-18128.
192

weibull_min, weibull_max
see gh-11806
196

asymmetric laplace
awaiting PR
notes

genextreme
see gh-10446
-

Other distributions that have overridden fit method:

norm - looks good, nothing to do here

beta - investigate fitting loc and scale? Adjust inequality conditions for FitDataError.

expon - works perfectly, AFAICT

weibull_min - should implement analytical solution when loc is fixed

gamma - look into fitting when loc is not fixed. Adjust inequality conditions for FitDataError.

lognorm - merged: gh-16839; works quite well. I'd rather not work on it more, but others are welcome to.

uniform - works perfectly, AFAICT