-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLSA_linear.py
More file actions
52 lines (46 loc) · 1.05 KB
/
Copy pathLSA_linear.py
File metadata and controls
52 lines (46 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Least Squares Analysis (LSA) on linear fit
#
# Author: Oscar A. Nieves
# Last update: January 19 2021
import matplotlib.pyplot as plt
import numpy as np
import statistics as st
plt.close('all')
np.random.seed(0) # Set seed
# Generate dataset
x = np.linspace(0,10,100)
Nx = len(x)
noise = np.random.normal(0,1,Nx)
S = 2*x + noise
# Use LSA to find line of best fit
n = len(S)
sum_x = sum(x)
sum_x2 = sum(x**2)
sum_S = sum(S)
sum_xS = sum(x*S)
A = np.array([ [n, sum_x],
[sum_x, sum_x2] ])
RHS = np.array([[sum_S],
[sum_xS]])
Ainv = np.linalg.inv(A)
b = np.dot(Ainv,RHS)
b0 = b[0]
b1 = b[1]
# Fit straight line to data and generate plots
y = b0 + b1*x
print('b0 = ' + str(b0))
print('b1 = ' + str(b1))
# R^2 value
SStot = sum( (S - st.mean(S))**2 )
SSres = sum( (S - y)**2 )
R2 = 1 - SSres/SStot
print('R^2 = ' + str(R2))
# PLOTS
plt.figure(1)
plt.scatter(x,S,color='r',label='Data')
plt.plot(x,y,color='b',label='Fit')
plt.xlabel('x', fontsize=16)
plt.ylabel('y', fontsize=16)
plt.title("Linear Fit Least Squares")
plt.show()
plt.legend()