-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrend_Line.py
More file actions
61 lines (47 loc) · 1.77 KB
/
Trend_Line.py
File metadata and controls
61 lines (47 loc) · 1.77 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
53
54
55
56
57
58
59
60
61
# Author: Jerome Richards
# Trend Line
#
# Uses the least squres meathod to find a trend line
import numpy as np
import matplotlib.pyplot as plt
import Read_Data as RD
##########################################
# Please reference textbook: #
# #
# Numerical Analysis Second Edition #
# By: #
# Timothy Sauer #
# #
# to understand the logic and reasoning #
# behind each declared variable and #
# array. #
# #
##########################################
# Description: Uses least squares method to fit y = mx + b to the data and generate a trend line
# Parameters: data on the horizontal axis, x_data, and on the vertical axis, y_data
# Returns: two arrays of size two - x values with x[0] as the start point and x[1] as the end point on horizontal axis,
# and y values wity y[0] as the start point and y[1] as the end point on vertical axis
def TrendLineFunction(x_data, y_data):
if len(x_data) != len(y_data):
print('Both arrays must be the same length');
return 0, 0;
def F(x):
return (m*x + b);
N = len(x_data);
A = np.ones((N, 2));
for i in range(0, N):
A[i, 0] = x_data[i];
A_t = A.transpose();
a_h = np.matmul(A_t, A);
c = np.matmul(A_t, y_data);
a_inv = np.linalg.inv(a_h);
S = np.matmul(a_inv, c);
m = S[0]; # slop of trend line
b = S[1];
x = [];
y = [];
x.append(x_data[0]);
y.append(F(x_data[0]));
x.append(x_data[len(x_data) - 1]);
y.append(F(x_data[len(x_data) - 1]));
return x, y;