#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Apr 22 08:06:00 2025 @author: amriksen """ import numpy as np import matplotlib.pyplot as plt # Synthetic data: hours studied per week (x) and marks obtained (y) np.random.seed(42) x = np.linspace(0, 10, 30) # hours per week from 0 to 10 # assume marks = base + b*x + c*x^2 + noise a_true, b_true, c_true = 50, 10, -0.5 # underlying parameters y = a_true + b_true * x + c_true * x**2 + np.random.randn(len(x)) * 5.0 # Construct design matrix X with a column of ones (intercept) and x X = np.vstack((np.ones_like(x), x)).T # Compute OLS coefficients: beta = (X^T X)^{-1} X^T y beta = np.linalg.inv(X.T @ X) @ X.T @ y intercept, slope = beta print(f"Estimated intercept: {intercept:.3f}") print(f"Estimated slope: {slope:.3f}") # Plot the data and the fitted regression line plt.scatter(x, y, label='Data points') plt.plot(x, X @ beta, label='OLS fit', linewidth=2) plt.xlabel('x') plt.ylabel('y') plt.title('Ordinary Least Squares Regression (Multivariate Form)') plt.legend() plt.show()