Metrics

A module for PyKEEN ranking and classification metrics.

Classes

Metric()

A base class for metrics.

ValueRange([lower, lower_inclusive, upper, ...])

A value range description.

RankBasedMetric()

A base class for rank-based metrics.

ClassificationMetric()

A base class for classification metrics.

Class Inheritance Diagram

Inheritance diagram of pykeen.metrics.utils.Metric, pykeen.metrics.utils.ValueRange, pykeen.metrics.ranking.RankBasedMetric, pykeen.metrics.classification.ClassificationMetric

Ranking metrics.

This module comprises various rank-based metrics, which get an array of individual ranks as input, as summarize them into a single-figure metric measuring different aspects of ranking performance.

We can generally distinguish:

Base Metrics

These metrics directly operate on the ranks:

The following metrics measures summarize the central tendency of ranks

The Hits at K metric is closely related to information retrieval and measures the fraction of times when the correct result is in the top-\(k\) ranked entries, i.e., the rank is at most \(k\)

The next metrics summarize the dispersion of ranks

and finally there is a simple metric to store the number of ranks which where aggregated

Inverse Metrics

The inverse metrics are reciprocals of the central tendency measures. They offer the advantage of having a fixed value range of \((0, 1]\), with a known optimal value of \(1\):

Adjusted Metrics

Adjusted metrics build upon base metrics, but adjust them for chance, cf. [berrendorf2020] and [hoyt2022]. All adjusted metrics derive from pykeen.metrics.ranking.DerivedRankBasedMetric and, for a given evaluation set, are affine transformations of the base metric with dataset-dependent, but fixed transformation constants. Thus, they can also be computed when the model predictions are not available anymore, but the evaluation set is known.

Expectation-Normalized Metrics

These metrics divide the metric by its expected value under random ordering. Thus, their expected value is always 1 irrespective of the evaluation set. They derive from pykeen.metrics.ranking.ExpectationNormalizedMetric, and there is currently only a single implementation:

Re-indexed Metrics

Re-indexed metrics subtract the expected value, and then normalize the optimal value to be 1. Thus, their expected value under random ordering is 0, their optimal value is 1, and larger values indicate better results. The classes derive from pykeen.metrics.ranking.ReindexedMetric, and the following implementations are available:

z-Adjusted Metrics

The final type of adjusted metrics uses the expected value as well as the variance of the metric under random ordering to normalize the metrics similar to z-score normalization. The z-score normalized metrics have an expected value of 0, and a variance of 1, and positive values indicate better results. While their value range is unbound, it can be interpreted through the lens of the inverse cumulative density function of the standard Gaussian distribution to retrieve a p-value. The classes derive from pykeen.metrics.ranking.ZMetric, and the following implementations are available:

Functions

generate_ranks(num_candidates[, ...])

Generate random ranks from a given array of the number of candidates for each ranking task.

generate_num_candidates_and_ranks(num_ranks, ...)

Generate random number of candidates, and coherent ranks.

generalized_harmonic_numbers(n[, p])

Calculate the generalized harmonic numbers from 1 to n (both inclusive).

harmonic_variances(n)

Pre-calculate variances of inverse rank distributions.

Classes

RankBasedMetric()

A base class for rank-based metrics.

DerivedRankBasedMetric([base_cls])

A derived rank-based metric.

ExpectationNormalizedMetric([base_cls])

An adjustment to create an expectation-normalized metric.

ReindexedMetric([base_cls])

A mixin to create an expectation normalized metric with max of 1 and expectation of 0.

ZMetric([base_cls])

A z-score adjusted metrics.

ArithmeticMeanRank()

The (arithmetic) mean rank.

AdjustedArithmeticMeanRank([base_cls])

The adjusted arithmetic mean rank (AMR).

AdjustedArithmeticMeanRankIndex([base_cls])

The adjusted arithmetic mean rank index (AMRI).

ZArithmeticMeanRank([base_cls])

The z-scored arithmetic mean rank.

InverseArithmeticMeanRank()

The inverse arithmetic mean rank.

GeometricMeanRank()

The (weighted) geometric mean rank.

AdjustedGeometricMeanRankIndex([base_cls])

The adjusted geometric mean rank index (AGMRI).

ZGeometricMeanRank([base_cls])

The z geometric mean rank (zGMR).

InverseGeometricMeanRank()

The inverse geometric mean rank.

HarmonicMeanRank()

The harmonic mean rank.

InverseHarmonicMeanRank()

The inverse harmonic mean rank.

AdjustedInverseHarmonicMeanRank([base_cls])

The adjusted MRR index.

ZInverseHarmonicMeanRank([base_cls])

The z-inverse harmonic mean rank (ZIHMR).

MedianRank()

The median rank.

InverseMedianRank()

The inverse median rank.

HitsAtK([k])

The Hits @ k.

AdjustedHitsAtK([base_cls])

The adjusted Hits at K (\(AH_k\)).

ZHitsAtK([base_cls])

The z-scored hits at k (\(ZAH_k\)).

StandardDeviation()

The ranks' standard deviation.

Variance()

The ranks' variance.

Count()

The ranks' count.

NoClosedFormError

The metric does not provide a closed-form implementation for the requested operation.

AffineTransformationParameters([scale, offset])

The parameters of an affine transformation.

Class Inheritance Diagram

Inheritance diagram of pykeen.metrics.ranking.RankBasedMetric, pykeen.metrics.ranking.DerivedRankBasedMetric, pykeen.metrics.ranking.ExpectationNormalizedMetric, pykeen.metrics.ranking.ReindexedMetric, pykeen.metrics.ranking.ZMetric, pykeen.metrics.ranking.ArithmeticMeanRank, pykeen.metrics.ranking.AdjustedArithmeticMeanRank, pykeen.metrics.ranking.AdjustedArithmeticMeanRankIndex, pykeen.metrics.ranking.ZArithmeticMeanRank, pykeen.metrics.ranking.InverseArithmeticMeanRank, pykeen.metrics.ranking.GeometricMeanRank, pykeen.metrics.ranking.AdjustedGeometricMeanRankIndex, pykeen.metrics.ranking.ZGeometricMeanRank, pykeen.metrics.ranking.InverseGeometricMeanRank, pykeen.metrics.ranking.HarmonicMeanRank, pykeen.metrics.ranking.InverseHarmonicMeanRank, pykeen.metrics.ranking.AdjustedInverseHarmonicMeanRank, pykeen.metrics.ranking.ZInverseHarmonicMeanRank, pykeen.metrics.ranking.MedianRank, pykeen.metrics.ranking.InverseMedianRank, pykeen.metrics.ranking.HitsAtK, pykeen.metrics.ranking.AdjustedHitsAtK, pykeen.metrics.ranking.ZHitsAtK, pykeen.metrics.ranking.StandardDeviation, pykeen.metrics.ranking.Variance, pykeen.metrics.ranking.Count, pykeen.metrics.ranking.NoClosedFormError, pykeen.metrics.ranking.AffineTransformationParameters