diff --git a/least_squares_regression.cpp b/least_squares_regression.cpp new file mode 100644 index 0000000..ce3c67c --- /dev/null +++ b/least_squares_regression.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + + +void regression(vector x, vector y) +{ + int n = x.size(); + float mx = accumulate(x.begin(), x.end(), 0)/n; + float my = accumulate(y.begin(), y.end(), 0)/n; + + float sx = accumulate(x.begin(), x.end(), 0); + float sy = accumulate(y.begin(), y.end(), 0); + float sx_sy = 0; + float sx_x = 0; + for(int i =0; i x = { 5, 7, 12, 16, 20 }; + vector y = { 40, 120, 180, 210, 240 }; + regression(x,y); + return 0; +} \ No newline at end of file diff --git a/polynomial_regression.py b/polynomial_regression.py new file mode 100644 index 0000000..fbfec46 --- /dev/null +++ b/polynomial_regression.py @@ -0,0 +1,39 @@ +import numpy as np +import matplotlib.pyplot as plt + +def calculate_matrix(x, n): + m = len(x) + matrix = np.zeros((n+1, n+1)) + for i in range(n+1): + for j in range(n+1): + matrix[i][j] = np.sum(x**(i+j)) + return matrix + +def calculate_vector(x, y, n): + m = len(x) + vector = np.zeros(n+1) + for i in range(n+1): + vector[i] = np.sum(y * x**i) + return vector + +def calculate_b(x, y, n): + k = calculate_matrix(x, n) + l = calculate_vector(x, y, n) + return np.linalg.solve(k, l) + +if __name__ == "__main__": + X = np.array([1, 2, 3, 4, 5, 6, 7]) + Y = np.array([45000, 50000, 60000, 80000, 110000, 150000, 200000]) + + n = 3 # Adjust the degree of polynomial as needed + b_coeffs = calculate_b(X, Y, n) + x_vals = np.linspace(1, 7, 100) + y_vals = sum(b_coeffs[i] * x_vals**i for i in range(n+1)) + plt.scatter(X, Y, color='blue') + plt.plot(x_vals, y_vals, label='Polynomial Regression') + plt.xlabel('x') + plt.ylabel('y') + plt.title('Plot of Polynomial Regression') + plt.legend() + plt.grid(True) + plt.show()