Linear Regression MVP

Here is a quick example of a math formula and a Python snippet working together.

Objective Function

We define our mean squared error cost function as:

$$J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2$$

Python Code

import numpy as np

def calculate_mse(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Computes Mean Squared Error."""
    return np.mean((y_true - y_pred) ** 2)

# Quick Test
y_true = np.array([1.0, 2.0, 3.0])
y_pred = np.array([1.1, 1.9, 3.2])
print(f"MSE: {calculate_mse(y_true, y_pred):.4f}")