Skip to content

Counting

evalica.counting(xs, ys, winners, index=None, weights=None, win_weight=1.0, tie_weight=0.5, solver=SOLVER, **kwargs)

Count individual elements.

Parameters:

Name Type Description Default
xs Collection[T_contra]

The left-hand side elements.

required
ys Collection[T_contra]

The right-hand side elements.

required
winners Collection[Winner]

The winner elements.

required
index Index | None

The index.

None
weights Collection[float] | None

The example weights.

None
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

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

The solver.

SOLVER
**kwargs Any

The additional arguments.

{}

Returns:

Type Description
CountingResult

The counting result.

Source code in evalica/__init__.py
363
364
365
366
367
368
369
370
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
400
401
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
def counting(
    xs: Collection[T_contra],
    ys: Collection[T_contra],
    winners: Collection[Winner],
    index: pd.Index | None = None,
    weights: Collection[float] | None = None,
    win_weight: float = 1.0,
    tie_weight: float = 0.5,
    solver: Literal["naive", "pyo3"] = SOLVER,
    **kwargs: Any,  # noqa: ANN401, ARG001
) -> CountingResult:
    """
    Count individual elements.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        winners: The winner elements.
        index: The index.
        weights: The example weights.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        **kwargs: The additional arguments.

    Returns:
        The counting 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"

    weights = _wrap_weights(weights, len(xs_indexed))

    if solver == "pyo3":
        if not PYO3_AVAILABLE:
            raise SolverError(solver)

        scores = _brzo.counting(
            xs=xs_indexed,
            ys=ys_indexed,
            winners=winners,
            weights=weights,
            total=len(index),
            win_weight=win_weight,
            tie_weight=tie_weight,
        )
    else:
        scores = counting_naive(
            xs=xs_indexed,
            ys=ys_indexed,
            winners=winners,
            weights=weights,
            total=len(index),
            win_weight=win_weight,
            tie_weight=tie_weight,
        )

    return CountingResult(
        scores=pd.Series(scores, index=index, name=counting.__name__).sort_values(ascending=False, kind="stable"),
        index=index,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
    )

evalica.CountingResult dataclass

The counting result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index Index

The index.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

Source code in evalica/__init__.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
@dataclass(frozen=True)
class CountingResult:
    """
    The counting result.

    Attributes:
        scores: The element scores.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    """

    scores: pd.Series[float]
    index: pd.Index
    win_weight: float
    tie_weight: float
    solver: str

evalica.average_win_rate(xs, ys, winners, index=None, weights=None, win_weight=1.0, tie_weight=0.5, solver=SOLVER, **kwargs)

Count pairwise win rates between the elements and average per element.

Parameters:

Name Type Description Default
xs Collection[T_contra]

The left-hand side elements.

required
ys Collection[T_contra]

The right-hand side elements.

required
winners Collection[Winner]

The winner elements.

required
index Index | None

The index.

None
weights Collection[float] | None

The example weights.

None
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

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

The solver.

SOLVER
**kwargs Any

The additional arguments.

{}

Returns:

Type Description
AverageWinRateResult

The average win rate result.

Source code in evalica/__init__.py
455
456
457
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
def average_win_rate(
    xs: Collection[T_contra],
    ys: Collection[T_contra],
    winners: Collection[Winner],
    index: pd.Index | None = None,
    weights: Collection[float] | None = None,
    win_weight: float = 1.0,
    tie_weight: float = 0.5,
    solver: Literal["naive", "pyo3"] = SOLVER,
    **kwargs: Any,  # noqa: ANN401, ARG001
) -> AverageWinRateResult:
    """
    Count pairwise win rates between the elements and average per element.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        winners: The winner elements.
        index: The index.
        weights: The example weights.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.
        **kwargs: The additional arguments.

    Returns:
        The average win rate 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"

    weights = _wrap_weights(weights, len(xs_indexed))

    if solver == "pyo3":
        if not PYO3_AVAILABLE:
            raise SolverError(solver)

        scores = _brzo.average_win_rate(
            xs=xs_indexed,
            ys=ys_indexed,
            winners=winners,
            weights=weights,
            total=len(index),
            win_weight=win_weight,
            tie_weight=tie_weight,
        )

    else:
        _matrices = matrices(
            xs_indexed=xs_indexed,
            ys_indexed=ys_indexed,
            winners=winners,
            index=index,
            weights=weights,
            solver="naive",
        )

        matrix = _make_matrix(_matrices.win_matrix, _matrices.tie_matrix, win_weight, tie_weight)

        with np.errstate(all="ignore"):
            denominator = np.nan_to_num(matrix + matrix.T)

            matrix /= denominator

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "Mean of empty slice")

            scores = np.nan_to_num(np.nanmean(matrix, axis=1), copy=False)

    return AverageWinRateResult(
        scores=pd.Series(
            scores,
            index=index,
            name=average_win_rate.__name__,
        ).sort_values(ascending=False, kind="stable"),
        index=index,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
    )

evalica.AverageWinRateResult dataclass

The average win rate result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index Index

The index.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

Source code in evalica/__init__.py
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
@dataclass(frozen=True)
class AverageWinRateResult:
    """
    The average win rate result.

    Attributes:
        scores: The element scores.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    """

    scores: pd.Series[float]
    index: pd.Index
    win_weight: float
    tie_weight: float
    solver: str