Implement a NumPy function that computes the coefficient of determination from true values and model predictions. It tests whether you center the total sum of squares on the target mean, leave negative scores unclipped, return a plain float, and handle a constant target.
Implement `compute_r2(y_true, y_pred)` so that it returns the coefficient of determination
$$R^2 = 1 - \frac{\text{RSS}}{\text{TSS}}$$
where `RSS = sum((y_true - y_pred)**2)` is the residual sum of squares and `TSS = sum((y_true - y_true.mean())**2)` is the total sum of squares.
Requirements:
- Center TSS on the mean of `y_true`, never on `y_pred`.
- Do not clip the result into `[0, 1]`. A bad `y_pred` must produce a negative value.
- Return a scalar Python `float`, not a 0-d array.
- A manual NumPy implementation and `sklearn.metrics.r2_score` are both accepted.
Example:
```python
y_true = np.array([3.0, -0.5, 2.0, 7.0])
y_pred = np.array([2.5, 0.0, 2.0, 8.0])
compute_r2(y_true, y_pred) # ~0.9486
# a deliberately bad y_pred gives a negative result
compute_r2(np.array([1.0, 2.0, 3.0]), np.array([3.0, 2.0, 1.0])) # -3.0
```
```hint Check the bad example by hand
Compute RSS and TSS separately for the second example before you write any code, and compare their ratio with 1.
```
### Constraints & Assumptions
- `y_true` and `y_pred` are 1-D float arrays of equal length. The test inputs have `n = 8`.
- One test pairs `y_true` with predictions from an OLS fit. The other uses a deliberately bad `y_pred` whose correct answer is negative.
### What a Strong Answer Covers
- RSS measured against the predictions and TSS centered on the mean of `y_true`
- Why the value is left unclipped, and what a negative score says about the model
- Conversion of the inputs to float arrays and of the result to a plain `float`
- What happens when `y_true` is constant and TSS is 0
- The linear time cost
### Follow-up Questions
- Why can $R^2$ be negative, and what does a negative value mean?
- How should the function handle a constant `y_true`, where TSS is 0?
- When does $R^2$ equal the squared correlation between `y_true` and `y_pred`, and why does that shortcut fail on the bad example?
Overview: Implement a NumPy function that computes the coefficient of determination from true values and model predictions. It tests whether you center the total sum of squares on the target mean, leave negative scores unclipped, return a plain float, and handle a constant target.