<p dir="auto">Hello, I have some data where inadvertently the x and y data were of different types, eventually after much troubleshooting I discovered that one was <code class="notranslate">np.float32</code> and the other was <code class="notranslate">np.float64</code>. When trying to use least-squares minimization to fit to the data, either <code class="notranslate">scipy.optimize.curve_fit</code> or <code class="notranslate">scipy.optimize.leastsq</code> failed in a weird quasi-silent way. The failure mode is that the parameters are barely changed from the initial guess and the optimization returns even though the result is clearly not good. Below is a minimum example demonstrating the bug (from a jupyter notebook):</p>
<h3 dir="auto">Reproducing code example:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="%pylab inline
import numpy as np
from scipy.optimize import curve_fit


def gauss(x, A, mu, sigma):
    return A/(sigma*np.sqrt(2*np.pi))*np.exp(-(np.power(x-mu,2)/(2*np.power(sigma,2))))

xdata = np.linspace(-1, 1, 20)
p_act = [1.4, 0.2, 0.2]
ydata = gauss(xdata, *p_act) + np.random.normal(loc=0, scale=0.05, size=len(xdata))
p0 = [1, 0, 0.1]

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float64)
fit1, pcov1 = curve_fit(gauss, xdata, ydata, p0=p0)

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float32)
fit2, pcov2 = curve_fit(gauss, xdata, ydata, p0=p0)

print(fit1)
print(fit2)

plot(xdata, ydata, 'bo')
x2 = np.linspace(xdata[0], xdata[-1], 100)
plot(x2, gauss(x2, *fit1), 'r--')
plot(x2, gauss(x2, *fit2), 'm--')"><pre class="notranslate"><code class="notranslate">%pylab inline
import numpy as np
from scipy.optimize import curve_fit


def gauss(x, A, mu, sigma):
    return A/(sigma*np.sqrt(2*np.pi))*np.exp(-(np.power(x-mu,2)/(2*np.power(sigma,2))))

xdata = np.linspace(-1, 1, 20)
p_act = [1.4, 0.2, 0.2]
ydata = gauss(xdata, *p_act) + np.random.normal(loc=0, scale=0.05, size=len(xdata))
p0 = [1, 0, 0.1]

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float64)
fit1, pcov1 = curve_fit(gauss, xdata, ydata, p0=p0)

xdata = xdata.astype(dtype=np.float32)
ydata = ydata.astype(dtype=np.float32)
fit2, pcov2 = curve_fit(gauss, xdata, ydata, p0=p0)

print(fit1)
print(fit2)

plot(xdata, ydata, 'bo')
x2 = np.linspace(xdata[0], xdata[-1], 100)
plot(x2, gauss(x2, *fit1), 'r--')
plot(x2, gauss(x2, *fit2), 'm--')
</code></pre></div>
<p dir="auto">The output of the print statements are:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[1.         0.05021768 0.10987875]
[1.37911003 0.19699727 0.19586094]"><pre class="notranslate"><code class="notranslate">[1.         0.05021768 0.10987875]
[1.37911003 0.19699727 0.19586094]
</code></pre></div>
<h3 dir="auto">Error message:</h3>

<p dir="auto">The only signature is that there is a covariance warning when fitting with the different data types:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/usr/local/lib/python3.7/site-packages/scipy/optimize/minpack.py:794: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)"><pre class="notranslate"><code class="notranslate">/usr/local/lib/python3.7/site-packages/scipy/optimize/minpack.py:794: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)
</code></pre></div>
<h3 dir="auto">Scipy/Numpy/Python version information:</h3>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.1.0 1.15.1 sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">1.1.0 1.15.1 sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)
</code></pre></div>
<p dir="auto">I recently ran into an issue whereby curve_fit was failing to fit a function, even though the guesses were reasonable. The warning reported by Scipy was "/usr/local/depot/Python-3.5.1/lib/python3.5/site-packages/scipy/optimize/minpack.py:690: OptimizeWarning: Covariance of the parameters could not be estimated".</p>
<p dir="auto">After hours of searching, I discovered that the problem was that I was using np.float32 arrays for the x and y data. Simply using x.astype(np.float64) resolved the issue. Though the warning provided no help or hints as to the source of the problem.</p>
<p dir="auto">This is similar to what was reported in this (rather old) email thread: <a href="http://scipy-user.scipy.narkive.com/wSafjCiz/curve-fit-with-float32-values" rel="nofollow">http://scipy-user.scipy.narkive.com/wSafjCiz/curve-fit-with-float32-values</a>, which proved to be very difficult to find. It would be useful to warn in the curve_fit documentation (and other minimisation functions) about using float32 inputs, particularly as the problem only manifests itself with certain data sets.</p>