title: numpy.polyfit versus scipy.odr
body: <p>I have a data set which in theory is described by a polynomial of the second degree. I would like to fit this data and I have used <code>numpy.polyfit</code> to do this. However, the down side is that the error on the returned coefficients is not available. Therefore I decided to also fit the data using <code>scipy.odr</code>. The weird thing was that the coefficients for the polynomial deviated from each other.</p>
  <p>I do not understand this and therefore decided to test both fitting routines on a set of data that I produce my self:</p>
  <pre><code>import numpy
  import scipy.odr
  import matplotlib.pyplot as plt
  
  x = numpy.arange(-20, 20, 0.1)
  y = 1.8 * x**2 -2.1 * x + 0.6 + numpy.random.normal(scale = 100, size = len(x))
  
  #Define function for scipy.odr
  def fit_func(p, t):
    return p[0] * t**2 + p[1] * t + p[2]
  
  #Fit the data using numpy.polyfit
  fit_np = numpy.polyfit(x, y, 2)
  
  #Fit the data using scipy.odr
  Model = scipy.odr.Model(fit_func)
  Data = scipy.odr.RealData(x, y)
  Odr = scipy.odr.ODR(Data, Model, [1.5, -2, 1], maxit = 10000)
  output = Odr.run()
  #output.pprint()
  beta = output.beta
  betastd = output.sd_beta
  
  print &quot;poly&quot;, fit_np
  print &quot;ODR&quot;, beta
  
  plt.plot(x, y, &quot;bo&quot;)
  plt.plot(x, numpy.polyval(fit_np, x), &quot;r--&quot;, lw = 2)
  plt.plot(x, fit_func(beta, x), &quot;g--&quot;, lw = 2)
  
  plt.tight_layout()
  
  plt.show()
  </code></pre>
  <p>An example of an outcome is as follows:</p>
  <pre><code>poly [ 1.77992643 -2.42753714  3.86331152]
  ODR [   3.8161735   -23.08952492 -146.76214989]
  </code></pre>
  <p><img src="https://i.stack.imgur.com/LkC8g.png" alt="enter image description here" /></p>
  <p>In the included image, the solution of <code>numpy.polyfit</code> (red dashed line) corresponds pretty well. The solution of <code>scipy.odr</code> (green dashed line) is basically completely off. I do have to note that the difference between <code>numpy.polyfit</code> and <code>scipy.odr</code> was less in the actual data set I wanted to fit. However, I do not understand where the difference between the two comes from, why in my own testing example the difference is extremely big, and which fitting routine is better?</p>
  <p>I hope you can provide answers that might help me give a better understanding between the two fitting routines and in the process provide answers to the questions I have.</p>