Skip to content

Bradley-Terry

evalica.bradley_terry(xs, ys, ws, index=None, win_weight=1.0, tie_weight=0.5, solver='pyo3', tolerance=1e-06, limit=100)

Compute the Bradley-Terry scores for the given pairwise comparison.

Quote

Bradley, R.A., Terry, M.E.: Rank Analysis of Incomplete Block Designs: I. The Method of Paired Comparisons. Biometrika. 39, 324–345 (1952). https://doi.org/10.2307/2334029.

Parameters:

Name Type Description Default
xs Collection[T]

The left-hand side elements.

required
ys Collection[T]

The right-hand side elements.

required
ws Collection[Winner]

The winner elements.

required
index dict[T, int] | None

The index.

None
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

0.5
solver Literal['naive', 'pyo3']

The solver.

'pyo3'
tolerance float

The convergence tolerance.

1e-06
limit int

The maximum number of iterations.

100

Returns:

Type Description
BradleyTerryResult[T]

The Bradley-Terry result.

Source code in evalica/__init__.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def bradley_terry(
        xs: Collection[T],
        ys: Collection[T],
        ws: Collection[Winner],
        index: dict[T, int] | None = None,
        win_weight: float = 1.,
        tie_weight: float = .5,
        solver: Literal["naive", "pyo3"] = "pyo3",
        tolerance: float = 1e-6,
        limit: int = 100,
) -> BradleyTerryResult[T]:
    """
    Compute the Bradley-Terry scores for the given pairwise comparison.

    Quote:
        Bradley, R.A., Terry, M.E.: Rank Analysis of Incomplete Block Designs: I.
        The Method of Paired Comparisons. Biometrika. 39, 324–345 (1952).
        <https://doi.org/10.2307/2334029>.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        ws: The winner elements.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        tolerance: The convergence tolerance.
        limit: The maximum number of iterations.

    Returns:
        The Bradley-Terry result.

    """
    assert np.isfinite(win_weight), "win_weight must be finite"
    assert np.isfinite(tie_weight), "tie_weight must be finite"

    xs_indexed, ys_indexed, index = indexing(xs, ys, index)

    assert index is not None, "index is None"

    if solver == "pyo3":
        scores, iterations = bradley_terry_pyo3(
            xs_indexed,
            ys_indexed,
            ws,
            len(index),
            win_weight,
            tie_weight,
            tolerance,
            limit,
        )
    else:
        _matrices = matrices(xs_indexed, ys_indexed, ws, index)

        matrix = (win_weight * _matrices.win_matrix + tie_weight * _matrices.tie_matrix).astype(float)

        scores, iterations = bradley_terry_naive(matrix, tolerance, limit)

    return BradleyTerryResult(
        scores=pd.Series(scores, index=index, name=bradley_terry.__name__).sort_values(ascending=False, kind="stable"),
        index=index,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
        tolerance=tolerance,
        iterations=iterations,
        limit=limit,
    )

evalica.BradleyTerryResult dataclass

Bases: Generic[T]

The Bradley-Terry result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index dict[T, int]

The index.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

tolerance float

The convergence tolerance.

iterations int

The actual number of iterations.

limit int

The maximum number of iterations.

Source code in evalica/__init__.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
@dataclass(frozen=True)
class BradleyTerryResult(Generic[T]):
    """
    The Bradley-Terry result.

    Attributes:
        scores: The element scores.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        tolerance: The convergence tolerance.
        iterations: The actual number of iterations.
        limit: The maximum number of iterations.

    """

    scores: pd.Series[float]
    index: dict[T, int]
    win_weight: float
    tie_weight: float
    solver: str
    tolerance: float
    iterations: int
    limit: int

evalica.newman(xs, ys, ws, index=None, v_init=0.5, win_weight=1.0, tie_weight=1.0, solver='pyo3', tolerance=1e-06, limit=100)

Compute the scores for the given pairwise comparison using the Newman's algorithm.

Quote

Newman, M.E.J.: Efficient Computation of Rankings from Pairwise Comparisons. Journal of Machine Learning Research. 24, 1–25 (2023). https://www.jmlr.org/papers/v24/22-1086.html.

Parameters:

Name Type Description Default
xs Collection[T]

The left-hand side elements.

required
ys Collection[T]

The right-hand side elements.

required
ws Collection[Winner]

The winner elements.

required
index dict[T, int] | None

The index.

None
v_init float

The initial tie parameter.

0.5
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

1.0
solver Literal['naive', 'pyo3']

The solver.

'pyo3'
tolerance float

The convergence tolerance.

1e-06
limit int

The maximum number of iterations.

100

Returns:

Type Description
NewmanResult[T]

The Newman's result.

Source code in evalica/__init__.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
def newman(
        xs: Collection[T],
        ys: Collection[T],
        ws: Collection[Winner],
        index: dict[T, int] | None = None,
        v_init: float = .5,
        win_weight: float = 1.,
        tie_weight: float = 1.,
        solver: Literal["naive", "pyo3"] = "pyo3",
        tolerance: float = 1e-6,
        limit: int = 100,
) -> NewmanResult[T]:
    """
    Compute the scores for the given pairwise comparison using the Newman's algorithm.

    Quote:
        Newman, M.E.J.: Efficient Computation of Rankings from Pairwise Comparisons.
        Journal of Machine Learning Research. 24, 1&ndash;25 (2023).
        <https://www.jmlr.org/papers/v24/22-1086.html>.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        ws: The winner elements.
        index: The index.
        v_init: The initial tie parameter.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        tolerance: The convergence tolerance.
        limit: The maximum number of iterations.

    Returns:
        The Newman's result.

    """
    assert np.isfinite(win_weight), "win_weight must be finite"
    assert np.isfinite(tie_weight), "tie_weight must be finite"

    xs_indexed, ys_indexed, index = indexing(xs, ys, index)

    assert index is not None, "index is None"

    if solver == "pyo3":
        scores, v, iterations = newman_pyo3(
            xs_indexed,
            ys_indexed,
            ws,
            len(index),
            v_init,
            win_weight,
            tie_weight,
            tolerance,
            limit,
        )
    else:
        _matrices = matrices(xs_indexed, ys_indexed, ws, index)

        win_matrix = win_weight * _matrices.win_matrix.astype(float)
        tie_matrix = tie_weight * _matrices.tie_matrix.astype(float)

        scores, v, iterations = newman_naive(win_matrix, tie_matrix, v_init, tolerance, limit)

    return NewmanResult(
        scores=pd.Series(scores, index=index, name=newman.__name__).sort_values(ascending=False, kind="stable"),
        index=index,
        v=v,
        v_init=v_init,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
        tolerance=tolerance,
        iterations=iterations,
        limit=limit,
    )

evalica.NewmanResult dataclass

Bases: Generic[T]

The Newman's algorithm result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index dict[T, int]

The index.

v float

The tie parameter.

v_init float

The initial tie parameter.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

tolerance float

The convergence tolerance.

iterations int

The actual number of iterations.

limit int

The maximum number of iterations.

Source code in evalica/__init__.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
@dataclass(frozen=True)
class NewmanResult(Generic[T]):
    """
    The Newman's algorithm result.

    Attributes:
        scores: The element scores.
        index: The index.
        v: The tie parameter.
        v_init: The initial tie parameter.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        tolerance: The convergence tolerance.
        iterations: The actual number of iterations.
        limit: The maximum number of iterations.

    """

    scores: pd.Series[float]
    index: dict[T, int]
    v: float
    v_init: float
    win_weight: float
    tie_weight: float
    solver: str
    tolerance: float
    iterations: int
    limit: int