Loading content...
The Coefficient of Determination, commonly denoted as R², is a cornerstone statistical metric in regression analysis that quantifies how well a predictive model captures the underlying patterns in observed data. It represents the proportion of variance in the dependent variable that is explained by the independent variable(s) in the model.
The R² value is computed using the following formulation:
$$R^2 = 1 - \frac{SS_{res}}{SS_{tot}}$$
Where:
SS_res (Residual Sum of Squares): Measures the total squared deviation between predicted values and actual values: $$SS_{res} = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2$$
SS_tot (Total Sum of Squares): Measures the total squared deviation of actual values from their mean: $$SS_{tot} = \sum_{i=1}^{n} (y_i - \bar{y})^2$$
Here, yᵢ represents each observed value, ŷᵢ represents the corresponding predicted value, and ȳ is the mean of all observed values.
| R² Value Range | Interpretation |
|---|---|
| R² = 1.0 | Perfect fit — predictions exactly match observations |
| 0.7 ≤ R² < 1.0 | Strong explanatory power — model captures most variance |
| 0.4 ≤ R² < 0.7 | Moderate fit — reasonable but improvable |
| 0.0 ≤ R² < 0.4 | Weak fit — model explains little variance |
| R² < 0 | Poor fit — model performs worse than using the mean |
Important Note: An R² value less than zero is mathematically possible and indicates that the model's predictions are worse than simply predicting the mean value for all observations. This typically signals a fundamentally flawed model.
Implement a function that computes the coefficient of determination (R²) given arrays of observed values and their corresponding model predictions. Your result should be rounded to exactly 3 decimal places.
actual_values = [1.0, 2.0, 3.0, 4.0, 5.0]
predicted_values = [1.1, 2.1, 2.9, 4.2, 4.8]0.989Step 1: Calculate the mean of actual values ȳ = (1.0 + 2.0 + 3.0 + 4.0 + 5.0) / 5 = 3.0
Step 2: Calculate SS_tot (Total Sum of Squares) SS_tot = (1-3)² + (2-3)² + (3-3)² + (4-3)² + (5-3)² SS_tot = 4 + 1 + 0 + 1 + 4 = 10.0
Step 3: Calculate SS_res (Residual Sum of Squares) SS_res = (1.0-1.1)² + (2.0-2.1)² + (3.0-2.9)² + (4.0-4.2)² + (5.0-4.8)² SS_res = 0.01 + 0.01 + 0.01 + 0.04 + 0.04 = 0.11
Step 4: Calculate R² R² = 1 - (0.11 / 10.0) = 1 - 0.011 = 0.989
The model explains 98.9% of the variance in the data, indicating an excellent fit.
actual_values = [1.0, 2.0, 3.0, 4.0]
predicted_values = [1.0, 2.0, 3.0, 4.0]1.0When predictions exactly match actual values, SS_res = 0 (no residual error exists).
R² = 1 - (0 / SS_tot) = 1 - 0 = 1.0
A perfect R² of 1.0 means the model captures 100% of the variance — every prediction is spot-on. This is the theoretical maximum and rarely achievable in real-world data with noise.
actual_values = [10.0, 20.0, 30.0, 40.0, 50.0]
predicted_values = [12.0, 18.0, 33.0, 38.0, 52.0]0.975Step 1: Calculate the mean of actual values ȳ = (10 + 20 + 30 + 40 + 50) / 5 = 30.0
Step 2: Calculate SS_tot SS_tot = (10-30)² + (20-30)² + (30-30)² + (40-30)² + (50-30)² SS_tot = 400 + 100 + 0 + 100 + 400 = 1000.0
Step 3: Calculate SS_res SS_res = (10-12)² + (20-18)² + (30-33)² + (40-38)² + (50-52)² SS_res = 4 + 4 + 9 + 4 + 4 = 25.0
Step 4: Calculate R² R² = 1 - (25 / 1000) = 1 - 0.025 = 0.975
The model explains 97.5% of the observed variance, demonstrating strong predictive capability.
Constraints