class SimpleOLS:
"""A simple OLS estimator for the linear model y = beta0 + beta1*x + u.
Attributes:
beta0_hat (float): Estimated intercept. NaN until fit is called.
beta1_hat (float): Estimated slope. NaN until fit is called.
"""
def __init__(self) -> None:
"""Initializes the OLS estimator with no estimates."""
self.beta0_hat: float = np.nan
self.beta1_hat: float = np.nan
def fit(self, x: np.ndarray, y: np.ndarray) -> None:
"""Fit OLS to the provided data.
Args:
x (np.ndarray): Independent variable (1D array).
y (np.ndarray): Dependent variable (1D array).
"""
# Add constant to x
X = np.column_stack([np.ones(len(x)), x])
# OLS estimation
beta_hat = np.linalg.inv(X.T @ X) @ X.T @ y
self.beta0_hat, self.beta1_hat = beta_hat[0], beta_hat[1]