<jupyter_start><jupyter_text>Examples of performing linear least squares fitting First we import numpy and matplotlib as usual<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Now lets generate some random date about a trend lineNeed to have random number generator, want to make sure data isnt changing, RMS width is sigma, fill array, where each data point has an uncertainty of 2=full function in last line<jupyter_code>#set a random number seed
np.random.seed(119)

#set number of data points
npoints = 50

#set x
x = np.linspace(0,10.,npoints)

#set slope, intercept, and scatter rms
m = 2.0
b = 1.0
sigma = 2.0

#generate y points
y = m*x + b +np.random.normal(scale=sigma,size=npoints)
y_err = np.full(npoints,sigma)<jupyter_output><empty_output><jupyter_text>Lets plot the data first<jupyter_code>f = plt.figure(figsize=(7,7))
plt.errorbar(x,y,sigma,fmt='o')
plt.xlabel('x')
plt.ylabel('y')<jupyter_output><empty_output><jupyter_text>Poly fit, takes x and y values, and makes a fit with an order 1, and makes a slope and intercept fit value  Method 1, polyfit()<jupyter_code>m_fit, b_fit = np.poly1d(np.polyfit(x, y, 1, w=1./y_err))   #weigh with uncertainties 
print(m_fit, b_fit)

y_fit = m_fit * x + b_fit<jupyter_output><empty_output><jupyter_text>Plot Result<jupyter_code>f = plt.figure(figsize=(7,7))
plt.errorbar(x,y,yerr=y_err,fmt='o',label='data')
plt.plot(x,y_fit,label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=2,frameon=False)<jupyter_output><empty_output><jupyter_text>Method 2, scipy + optimize<jupyter_code>#import optimize from scipy
from scipy import optimize

#define the fxn to fit
def f_line(x, m, b):
    return m*x + b

#perform the fit
params, params_cov = optimize.curve_fit(f_line,x,y,sigma=y_err)

m_fit = params[0]
b_fit = params[1]
print(m_fit,b_fit)

#cant use polyfit for uncertainties with curves
#scipy is a new module, that provides us with a optimize module
#it can minimize a function that can fit a curve<jupyter_output><empty_output><jupyter_text>Plot Result<jupyter_code>f = plt.figure(figsize=(7,7))
plt.errorbar(x,y,yerr=y_err,fmt='o',label='data')
plt.plot(x,y_fit,label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=2,frameon=False)<jupyter_output><empty_output><jupyter_text>We can perform much more complicated fits<jupyter_code>#redefine x and y
npoints = 50
x = np.linspace(0.,2*np.pi,npoints)

#make y a complicated function
a = 3.4
b = 2.1
c = 0.27
d = -1.3
sig = 0.6

y = a * np.sin(b*x + c) + d +np.random.normal(scale=sig,size=npoints)
y_err = np.full(npoints,sig)

f = plt.figure(figsize=(7,7))
plt.errorbar(x,y,yerr=y_err,fmt='o')
plt.xlabel('x')
plt.ylabel('y')<jupyter_output><empty_output><jupyter_text>Perform a fit using scipy.optimize.curve_fit()<jupyter_code>from scipy import optimize

#define the fxn to fit
def f_line(x, a, b, c, d):
    return a * np.sin(b*x + c) + d

#perform the fit
params, params_cov = optimize.curve_fit(f_line,x,y,sigma=y_err,p0=[1,2.,0.1,-0.1])

a_fit = params[0]
b_fit = params[1]
c_fit = params[2]
d_fit = params[3]

print(a_fit,b_fit,c_fit,d_fit)

y_fit = a_fit * np.sin(b_fit * x +c_fit) + d_fit<jupyter_output><empty_output><jupyter_text>Plot the fit<jupyter_code>f = plt.figure(figsize=(7,7))
plt.errorbar(x,y,yerr=y_err,fmt='o',label='data')
plt.plot(x,y_fit,label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc=2,frameon=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14227994227994228, 'fraction_numerical': 0.017893217893217895, 'mean_word_length': 6.600877192982456, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '360118', 'license': 'MIT', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1427, 'n_words': 317, 'path': 'line-fitting-examples.ipynb', 'repo_name': 'npurtell/astr-119-session-7', 'size': '3465'}