Title: numpy polyfit not giving best linear fit according to chi squared
Tags: python;numpy;data-analysis
Question: I have to find a model fit for some data and then calculate its chi squared value to see how good a fit it is. So I used numpy's polyfit function to find the fit, then calculated chi squared and plotted it. It looks like a pretty good fit, however, I have managed to get a better fit by optimizing for minimum chi squared. I would have thought that numpy's polyfit would try to do the same, no?

Here's my code:

```import numpy as np
from matplotlib import pyplot as plt
from scipy import stats

data = np.loadtxt('MW_Cepheids.dat', usecols=(1,2,3,4,5,6), dtype=float)

plx = data[:,0]                     # Parallax data
plx_err = data[:,1]                 # Error in plx.

P = data[:,2]                       # Period data
log_P = np.log10(P)                 # Logarithm of P.

m = data[:,3]                       # Apparent magnitude data

A = data[:,4]                       # Extinction data
A_err = data[:,5]                   # Error in ext.

dst = 1000/plx                      # Distance calculation
dst_err = 1000*(plx_err/plx**2)     # Error in dist. calc.


# Calculating Absolute Magnitude and its error
# This would correspond to y and the error in y
M = m - 5*np.log10(dst) + 5 - A
M_err = np.sqrt((5*dst_err/(np.log(10)*dst))**2 + A_err**2)


# Using numpy's polyfit function
a,b = np.polyfit(log_P, M, 1)

M_m = a*log_P + b

# Calculating chi squared from the polyfit result
chi2 = np.sum(((M-M_m)/(M_err))**2)
print(a, b, chi2)


def lin_fit (x, y, y_err):
    """ Cycles through probable values of gradient and intercept, calculating chi2 for each.
    Returns best values for a and b, ie when chi2 is smallest."""

    chi2_min = 999999
    a_range = np.arange(-2.5, -2.3, 0.001)
    b_range = np.arange(-1.7, -1.4, 0.001)

    for a in a_range:
        for b in b_range:
            y_m = a*x + b
            chi2 = np.sum(((y-y_m)/(y_err))**2)

            if chi2 < chi2_min:
                chi2_min = chi2
                a_fit = a
                b_fit = b
    return a_fit, b_fit, chi2_min


a, b, chi2 = lin_fit(log_P, M, M_err)
print(a, b, chi2)
```

And the data is in this file: We transfer link

I know the simple choice here to just stick with the other method of finding the fit, but it's a lot messier, and I would like to make sure I am calculating chi2 properly and understand why polyfit isn't finding the 'best' fit.

Many thanks!

Comment: @rpoleski sorry, hopefully that's more helpful now. Thank you.

Comment: Please show [minimal working example](https://stackoverflow.com/help/minimal-reproducible-example) - in this case your data are needed.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/13/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><numpy><data-analysis>', 'Title': 'numpy polyfit not giving best linear fit according to chi squared', 'ViewCount': 149, 'id': 61772645, 'user_AboutMe': None, 'user_AccountId': 8824422.0, 'user_DisplayName': 'Surak', 'user_Reputation': 23.0, 'user_Views': 3.0, 'user_id': 6593108.0}}