Explained Variance of the Value Head

~12 mincode completion

Implement explained_variance(y_true, y_pred) returning the scalar.

Examples

Perfect predictions explain everything

Input
explained_variance([1, 2, 3], [1, 2, 3])
Output
1

Predicting the mean explains nothing

Input
explained_variance([1, 2, 3], [2, 2, 2])
Output
0

Predicting backwards is much worse than the mean

Input
explained_variance([1, 2, 3], [3, 2, 1])
Output
-3

Hints

Hint 1

Convert the input with before doing elementwise work.

Hint 2

Watch for this: used the mean squared error without dividing by the variance of the targets.

Requirements

  • y_true: (n,) value targets

  • y_pred: (n,) value predictions

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Python
import numpy as np


def explained_variance(y_true, y_pred):
    """
    Fraction of the target variance the predictions account for.

    Args:
        y_true: (n,) value targets
        y_pred: (n,) value predictions

    Returns:
        scalar, 1 for perfect, 0 for no better than the mean, negative
        for worse. Returns 0.0 when the targets have no variance.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.