Skip to content

Alpha

evalica.alpha(data, distance='nominal', solver='pyo3')

Compute Krippendorff's alpha.

Parameters:

Name Type Description Default
data DataFrame

Ratings by observer (rows) and unit (columns).

required
distance DistanceFunc[T_distance_contra] | DistanceName

Distance metric (nominal, ordinal, interval, ratio) or a custom function.

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

The solver to use (naive or pyo3).

'pyo3'

Returns:

Type Description
AlphaResult

The alpha result.

Source code in evalica/__init__.py
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
def alpha(
    data: pd.DataFrame,
    distance: DistanceFunc[T_distance_contra] | DistanceName = "nominal",
    solver: Literal["naive", "pyo3"] = "pyo3",
) -> AlphaResult:
    """
    Compute Krippendorff's alpha.

    Args:
        data: Ratings by observer (rows) and unit (columns).
        distance: Distance metric (nominal, ordinal, interval, ratio) or a custom function.
        solver: The solver to use (naive or pyo3).

    Returns:
        The alpha result.

    """
    matrix = _as_unit_matrix(data)
    codes, unique_values = _factorize_values(matrix)

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

        numeric_values = np.asarray(unique_values, dtype=np.float64)

        if callable(distance):
            distance_matrix = _custom_distance(distance, unique_values)
            _alpha, observed, expected = _brzo.alpha(codes, numeric_values, distance_matrix)
        else:
            _alpha, observed, expected = _brzo.alpha(codes, numeric_values, distance)

        if expected == 0.0:
            _alpha = 1.0 if observed == 0.0 else 0.0
    else:
        _alpha, observed, expected = _alpha_naive(codes, unique_values, distance)

    return AlphaResult(
        alpha=_alpha,
        observed=observed,
        expected=expected,
        solver=solver,
    )

evalica.AlphaResult dataclass

The result of Krippendorff's alpha.

Attributes:

Name Type Description
alpha float

The alpha value.

observed float

The observed disagreement.

expected float

The expected disagreement.

solver str

The solver used.

Source code in evalica/__init__.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@dataclass(frozen=True)
class AlphaResult:
    """
    The result of Krippendorff's alpha.

    Attributes:
        alpha: The alpha value.
        observed: The observed disagreement.
        expected: The expected disagreement.
        solver: The solver used.

    """

    alpha: float
    observed: float
    expected: float
    solver: str

evalica.DistanceFunc

Bases: Protocol[T_distance_contra]

The distance function protocol.

Source code in evalica/__init__.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class DistanceFunc(Protocol[T_distance_contra]):
    """The distance function protocol."""

    def __call__(self, left: T_distance_contra, right: T_distance_contra, /) -> float:
        """
        Compute the distance between the values.

        Args:
            left: The left-hand side value.
            right: The right-hand side value.

        Returns:
            The non-negative distance between the values.

        """
        ...

__call__(left, right)

Compute the distance between the values.

Parameters:

Name Type Description Default
left T_distance_contra

The left-hand side value.

required
right T_distance_contra

The right-hand side value.

required

Returns:

Type Description
float

The non-negative distance between the values.

Source code in evalica/__init__.py
131
132
133
134
135
136
137
138
139
140
141
142
143
def __call__(self, left: T_distance_contra, right: T_distance_contra, /) -> float:
    """
    Compute the distance between the values.

    Args:
        left: The left-hand side value.
        right: The right-hand side value.

    Returns:
        The non-negative distance between the values.

    """
    ...