Skip to content

Commit

Permalink
BUG: optimize: return a matrix if indeterminate variance
Browse files Browse the repository at this point in the history
The documentation says pcov is a matrix, so it may be confusing to have
it be a scalar in some pathological cases.
  • Loading branch information
pv committed Nov 29, 2013
1 parent 977b138 commit fffae00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 7 additions & 3 deletions scipy/optimize/minpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,16 @@ def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, **kw):
msg = "Optimal parameters not found: " + errmsg
raise RuntimeError(msg)

if not absolute_sigma:
if len(ydata) > len(p0) and pcov is not None:
if pcov is None:
# indeterminate covariance
pcov = zeros((len(popt), len(popt)), dtype=float)
pcov.fill(inf)
elif not absolute_sigma:
if len(ydata) > len(p0):
s_sq = (asarray(func(popt, *args))**2).sum() / (len(ydata) - len(p0))
pcov = pcov * s_sq
else:
pcov = inf
pcov.fill(inf)

if return_full:
return popt, pcov, infodict, errmsg, ier
Expand Down
13 changes: 13 additions & 0 deletions scipy/optimize/tests/test_minpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,19 @@ def f(x, a, b):
perr = np.sqrt(np.diag(pcov))
assert_allclose(perr, [3*0.30714756, 3*0.85045308], rtol=1e-3)

# infinite variances

def f_flat(x, a, b):
return a*x

popt, pcov = curve_fit(f_flat, xdata, ydata, p0=[2, 0], sigma=sigma)
assert_(pcov.shape == (2, 2))
assert_array_equal(pcov, np.inf)

popt, pcov = curve_fit(f, xdata[:2], ydata[:2], p0=[2, 0])
assert_(pcov.shape == (2, 2))
assert_array_equal(pcov, np.inf)


class TestFixedPoint(TestCase):

Expand Down

0 comments on commit fffae00

Please sign in to comment.