Skip to content

Venkatraman

Use Venkatraman to compare two models' receiver operating characteristic (ROC) curves on the same cases. It can find different trade-offs across cutoffs even when the models have the same AUROC.

Question Do the models make different trade-offs across cutoffs?
Inputs y_true known outcomes; predictions from candidate and reference for the same cases
Run Call (); optionally set permutation settings with .set_permutation(...)
statistic Non-negative sum of the gaps between the two curves
alternative two-sided
import numpy as np
import samesame as ss

y_true = np.array([0, 0, 0, 1, 1, 1])
candidate = np.array([0.1, 0.2, 0.3, 0.7, 0.8, 0.9])
reference = np.array([0.4, 0.5, 0.6, 0.5, 0.6, 0.7])

test = ss.Venkatraman(y_true, candidate, reference)
test = test.set_permutation(seed=np.random.default_rng(12345))
shape = test()
print(shape.pvalue, shape.statistic)
print(shape.extra["auroc"])

Expected output: a non-negative curve gap. A small p-value suggests that the curves differ; it does not show that one model has a higher AUROC.

Null and statistic

The test holds the outcomes fixed and swaps the two predictions within each case.

statistic is the non-negative sum of absolute gaps between the empirical ROC curves at rank thresholds; it is 0.0 for identical curves. The two-sided test asks whether the curves differ; it does not say which model is better. Use DeLong to compare AUROC values.

Edge cases

  • Tied predictions receive midranks (the average rank of the tied observations). The test does not add random jitter.
  • The curve gap cannot be negative. The result reports alternative="two-sided" because a difference in either direction matters.
  • extra["null"] is a read-only array of evaluated statistics. It can be shorter than the requested n_permutations when SciPy enumerates all possible arrangements.
  • extra["n_permutations"] holds the requested count; extra["auroc"] holds each model's AUROC.
  • The package does not implement Venkatraman(inference="asymptotic"). The call raises NotImplementedError.

Reference

samesame.Venkatraman

Bases: _PairedScorersBase

Check whether two models have different ROC curves.

Use this test to compare the models' trade-offs across cutoffs on the same cases with known outcomes. It does not say which model is better.

Parameters:

Name Type Description Default
y_true NDArray[int_]

Binary labels (0/1) with shape (n_samples,). Both classes must exist. The class copies and freezes the array.

required
candidate NDArray[float64]

Scores under test with shape (n_samples,). The class copies and freezes the array.

required
reference NDArray[float64]

Baseline scores with shape (n_samples,). The class copies and freezes the array.

required
inference Literal['permutation', 'asymptotic']

"permutation" is the default and the sole implemented mode. It swaps candidate and reference within each observation. "asymptotic" is not implemented. A call with it raises. Configure permutation randomness with :meth:set_permutation.

'permutation'
Notes

The test compares paired empirical ROC curves, not only their AUROC values. Equal AUROC values do not mean identical curves. The statistic is the summed absolute gap between the curves at rank thresholds. It is non-negative and symmetric, and equals 0.0 for identical scorers. The permutation null swaps candidate and reference within each observation. A small p-value is evidence against within-case exchangeability; it does not mean one model has higher AUROC. Compare AUROC values with DeLong.

See Also

DeLong : Paired comparison of AUROC values. Dsos : One-sided tail-weighted test in a defined harm direction.

Examples:

>>> import numpy as np
>>> import samesame as ss
>>> y_true = np.array([0, 0, 0, 1, 1, 1])
>>> candidate = np.array([0.1, 0.2, 0.3, 0.7, 0.8, 0.9])
>>> reference = np.array([0.4, 0.5, 0.6, 0.5, 0.6, 0.7])
>>> test = ss.Venkatraman(
...     y_true, candidate, reference
... ).set_permutation(seed=42, n_permutations=32)
>>> result = test()
>>> result.test_name
'Venkatraman (permutation)'
>>> round(float(result.statistic), 3)
4.0
>>> round(float(result.pvalue), 3)
0.333
>>> round(float(result.extra["auroc"]["candidate"]), 3)
1.0
Source code in src/samesame/_paired.py
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
class Venkatraman(_PairedScorersBase):
    """Check whether two models have different ROC curves.

    Use this test to compare the models' trade-offs across cutoffs on the
    same cases with known outcomes. It does not say which model is better.

    Parameters
    ----------
    y_true :
        Binary labels (``0``/``1``) with shape ``(n_samples,)``. Both
        classes must exist. The class copies and freezes the array.
    candidate :
        Scores under test with shape ``(n_samples,)``. The class copies
        and freezes the array.
    reference :
        Baseline scores with shape ``(n_samples,)``. The class copies
        and freezes the array.
    inference :
        ``"permutation"`` is the default and the sole implemented mode.
        It swaps candidate and reference within each observation.
        ``"asymptotic"`` is not implemented. A call with it raises.
        Configure permutation randomness with :meth:`set_permutation`.

    Notes
    -----
    The test compares paired empirical ROC curves, not only their AUROC
    values. Equal AUROC values do not mean identical curves. The statistic
    is the summed absolute gap between the curves at rank thresholds. It
    is non-negative and symmetric, and equals ``0.0`` for identical
    scorers. The permutation null swaps candidate and reference within
    each observation. A small p-value is evidence against within-case
    exchangeability; it does not mean one model has higher AUROC. Compare
    AUROC values with `DeLong`.

    See Also
    --------
    DeLong : Paired comparison of AUROC values.
    Dsos : One-sided tail-weighted test in a defined harm direction.

    Examples
    --------
    >>> import numpy as np
    >>> import samesame as ss
    >>> y_true = np.array([0, 0, 0, 1, 1, 1])
    >>> candidate = np.array([0.1, 0.2, 0.3, 0.7, 0.8, 0.9])
    >>> reference = np.array([0.4, 0.5, 0.6, 0.5, 0.6, 0.7])
    >>> test = ss.Venkatraman(
    ...     y_true, candidate, reference
    ... ).set_permutation(seed=42, n_permutations=32)
    >>> result = test()
    >>> result.test_name
    'Venkatraman (permutation)'
    >>> round(float(result.statistic), 3)
    4.0
    >>> round(float(result.pvalue), 3)
    0.333
    >>> round(float(result.extra["auroc"]["candidate"]), 3)
    1.0
    """

    def __init__(
        self,
        y_true: NDArray[np.int_],
        candidate: NDArray[np.float64],
        reference: NDArray[np.float64],
        inference: Literal["permutation", "asymptotic"] = "permutation",
    ) -> None:
        super().__init__(y_true, candidate, reference, inference)

    def __call__(self) -> TestResult:
        """Run the test for the selected ``inference``.

        Returns
        -------
        TestResult
            It holds ``test_name="Venkatraman (permutation)"`` and
            ``alternative="two-sided"``. ``statistic`` equals the
            observed summed gap. ``extra`` carries read-only ``"null"``,
            ``"n_permutations"``, and per-scorer ``"auroc"``.

        Raises
        ------
        NotImplementedError
            If ``inference="asymptotic"``. This package implements only
            the permutation null for ``Venkatraman``.
        """
        if self._inference != "permutation":
            raise NotImplementedError(
                "inference='asymptotic' is not implemented for Venkatraman; "
                "use the permutation test instead"
            )
        _, _, auc_c, auc_r, pos = self._paired_aurocs()
        thresholds = np.arange(1, self._y_true.size + 1)

        def vec_stat(
            c: NDArray[np.float64], r: NDArray[np.float64], axis: int = -1
        ) -> NDArray[np.float64]:
            cc = np.asarray(c)
            rr = np.asarray(r)
            if cc.ndim == 1:
                return np.asarray(_roc_curve_gap(pos, cc, rr))
            r_ranks = rankdata(cc, axis=-1)
            s_ranks = rankdata(rr, axis=-1)
            r_cases = np.sort(r_ranks[:, pos], axis=1)
            r_controls = np.sort(r_ranks[:, ~pos], axis=1)
            s_cases = np.sort(s_ranks[:, pos], axis=1)
            s_controls = np.sort(s_ranks[:, ~pos], axis=1)
            out = np.empty(cc.shape[0], dtype=float)
            for b in range(cc.shape[0]):
                out[b] = _summed_gap(
                    r_cases[b], r_controls[b], s_cases[b], s_controls[b], thresholds
                )
            return out

        return self._run_swap_permutations(
            vec_stat,
            "greater",
            "Venkatraman (permutation)",
            "two-sided",
            auc_c,
            auc_r,
        )

__call__()

Run the test for the selected inference.

Returns:

Type Description
TestResult

It holds test_name="Venkatraman (permutation)" and alternative="two-sided". statistic equals the observed summed gap. extra carries read-only "null", "n_permutations", and per-scorer "auroc".

Raises:

Type Description
NotImplementedError

If inference="asymptotic". This package implements only the permutation null for Venkatraman.

Source code in src/samesame/_paired.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
def __call__(self) -> TestResult:
    """Run the test for the selected ``inference``.

    Returns
    -------
    TestResult
        It holds ``test_name="Venkatraman (permutation)"`` and
        ``alternative="two-sided"``. ``statistic`` equals the
        observed summed gap. ``extra`` carries read-only ``"null"``,
        ``"n_permutations"``, and per-scorer ``"auroc"``.

    Raises
    ------
    NotImplementedError
        If ``inference="asymptotic"``. This package implements only
        the permutation null for ``Venkatraman``.
    """
    if self._inference != "permutation":
        raise NotImplementedError(
            "inference='asymptotic' is not implemented for Venkatraman; "
            "use the permutation test instead"
        )
    _, _, auc_c, auc_r, pos = self._paired_aurocs()
    thresholds = np.arange(1, self._y_true.size + 1)

    def vec_stat(
        c: NDArray[np.float64], r: NDArray[np.float64], axis: int = -1
    ) -> NDArray[np.float64]:
        cc = np.asarray(c)
        rr = np.asarray(r)
        if cc.ndim == 1:
            return np.asarray(_roc_curve_gap(pos, cc, rr))
        r_ranks = rankdata(cc, axis=-1)
        s_ranks = rankdata(rr, axis=-1)
        r_cases = np.sort(r_ranks[:, pos], axis=1)
        r_controls = np.sort(r_ranks[:, ~pos], axis=1)
        s_cases = np.sort(s_ranks[:, pos], axis=1)
        s_controls = np.sort(s_ranks[:, ~pos], axis=1)
        out = np.empty(cc.shape[0], dtype=float)
        for b in range(cc.shape[0]):
            out[b] = _summed_gap(
                r_cases[b], r_controls[b], s_cases[b], s_controls[b], thresholds
            )
        return out

    return self._run_swap_permutations(
        vec_stat,
        "greater",
        "Venkatraman (permutation)",
        "two-sided",
        auc_c,
        auc_r,
    )