Skip to content

Friedman test and Nemenyi post-hoc comparison

Compare algorithm ranks across independent datasets, calculate Friedman and Nemenyi results, and distinguish approximate significance from economic value.

The Friedman test compares three or more methods using their ranks within the same independent dataset blocks. Nemenyi post-hoc comparison then tests all pairs of average ranks. A quant can use this combination to compare forecasting algorithms across separate prediction problems without treating their raw loss scales as interchangeable.
The omnibus hypothesis test asks whether the methods have the same expected rank under the null of exchangeable method labels within blocks. Rejection does not identify the differing pairs; that is the post-hoc question. Demšar (2006, §3.2.2) explains this design for algorithm comparisons.

Five datasets, three algorithms

This is a stipulated example, not measured financial performance. Algorithms A, B and C are evaluated on five independent datasets using one predeclared metric; rank 1 is best and there are no ties.
Dataset blockABC
1123
2123
3132
4213
5123
Average rank1.22.02.8

Let NN be the number of datasets, kk the number of algorithms, and RjR_j algorithm jj's average rank. For untied ranks, the Friedman statistic is

Q=12Nk(k+1)∑j=1kRj2−3N(k+1).Q=\frac{12N}{k(k+1)}\sum_{j=1}^{k}R_j^2-3N(k+1).

Here N=5N=5, k=3k=3, so

Q=5(1.22+22+2.82)−60=6.4.Q=5(1.2^2+2^2+2.8^2)-60=6.4.
Using the asymptotic chi-squared distribution with k−1=2k-1=2 degrees of freedom gives a p-value of 0.040760.04076. This crosses a predeclared 5% omnibus threshold under that approximation.
Five blocks are a small sample. This is not an exact finite-sample p-value: SciPy's Friedman documentation explicitly cautions about small samples. An exact or permutation reference requires a valid exchangeability design; the chi-squared calculation alone does not establish it.

Which pair clears the critical difference?

For the conventional Nemenyi all-pairs reference,

CD=qαk(k+1)6N,qα=q1−α;k,∞range2.\mathrm{CD}=q_{\alpha}\sqrt{\frac{k(k+1)}{6N}}, \qquad q_{\alpha}=\frac{q^{\mathrm{range}}_{1-\alpha;k,\infty}}{\sqrt{2}}.
The second expression specifies the convention: divide the studentized-range quantile by 2\sqrt{2}. At α=0.05\alpha=0.05 and k=3k=3, qα≈2.34370q_{\alpha}\approx2.34370, giving CD≈1.48229\mathrm{CD}\approx1.48229. The SciPy studentized-range distribution supplies the quantile; Demšar's Table 5 uses the divided convention.
PairAbsolute average-rank gapExceeds CD?
A–B0.8No
B–C0.8No
A–C1.6Yes

Only A–C is separated by this conventional approximate procedure. A–B and B–C non-rejection does not establish equivalence, or make A–C non-rejection follow by transitivity.

Stipulated five-independent-dataset example: average ranks A 1.2, B 2.0 and C 2.8. A–B and B–C gaps are 0.8; the A–C gap of 1.6 alone exceeds the approximate 5% Nemenyi critical difference of 1.4823.
Stipulated five-independent-dataset example: average ranks A 1.2, B 2.0 and C 2.8. A–B and B–C gaps are 0.8; the A–C gap of 1.6 alone exceeds the approximate 5% Nemenyi critical difference of 1.4823.
Original calculation from the stipulated ranks. The common CD adjusts for the all-pairs family; small-sample approximation remains a limitation.

Choose the comparison before looking at the ranks

Nemenyi targets all pairs and family-wise error. A predesignated control-versus-challengers question has a different comparison family. Do not select that control after seeing the winner. Demšar (§3.2.2) distinguishes these designs.
The dataset is the replication unit. Cross-validation folds from one dataset are not independent dataset blocks; correlated assets do not become independent simply because each has its own ticker. Demšar (§3 and §3.2.3) separates dataset replication from resampling. The membership lesson Finance-aware validation develops overlapping-label and time-dependence issues; its public page is a preview.

For ties, assign average ranks and use a tie-corrected Friedman calculation. SciPy documents that correction; the untied formula and CD illustration above should not be copied unchanged into a heavily tied experiment.

Ranks discard magnitude. A tiny loss improvement and a large one can generate the same ordering. Alongside the rank analysis, report raw out-of-sample metrics, uncertainty, trading costs and exposure constraints. Neither the lowest average rank nor a rejected pairwise null establishes a profitable strategy.

Sources

Last updated
  • model comparison
  • rank tests
  • friedman test
  • nemenyi test
  • multiple comparisons
  • independent datasets
← Back to the glossaryBack to top ↑