← All notes

Statistical note

Explained variance

Explained variance: theory, interpretation, Python computation, and a scientific-imaging case study.

6 min read

A regression metric defines how residual magnitude, sign, scale, and outcome distribution contribute to model quality. The metric should match the scientific cost of errors.

Concept figure for Explained variance

Mathematical definition

The central quantity is

EV=1Var(yyhat)/Var(y).EV=1-Var(y-yhat)/Var(y).

This expression states what is being counted, averaged, ranked, or compared. Its scale and direction must be interpreted in the context of the data and sampling design.

Compute it in Python

import numpy as np
from sklearn import metrics

y_true = np.array([1.0, 2.5, 4.0, 7.5, 10.0, 14.0])
y_pred = np.array([1.2, 2.2, 4.8, 7.0, 9.1, 13.2])
print(metrics.explained_variance_score(y_true, y_pred))

The function is sklearn.metrics.explained_variance_score. Inspect its current signature and return object in the official documentation for the version installed in your environment.

Interpretation and cautions

Always examine residual plots and stratified errors. Scale-dependent metrics cannot be compared across outcomes without context; percentage metrics fail near zero; R-squared can be negative on unseen data.

A numerical value is not self-interpreting. Compare it with a baseline, uncertainty interval, operational threshold, or competing model, and retain the underlying observations or confusion counts.

Scientific-imaging case study

For residual variance relative to outcome variance in density-map or QPI prediction, report the score across independent acquisitions and check bias across the target range.

Split train, validation, and test data at the specimen or experimental level. Report variability across independent repeats so that the metric describes generalization rather than leakage.