Suppose you have some data points
x = numpy.array([0.0, 1.0, 2.0, 3.0]) y = numpy.array([3.6, 1.3, 0.2, 0.9])
To set the parabola to these points, use numpy.polyfit() :
p = numpy.polyfit(x, y, 2)
To get the chi-squared value for this fit, evaluate the polynomial in the x values ββof your data points, subtract the y , square and sum values:
chi_squared = numpy.sum((numpy.polyval(p, x) - y) ** 2)
You can divide this number by the number of degrees of freedom if you want.
Sven marnach
source share