import torch


def cbcc(detections: torch.Tensor, reasonings: torch.Tensor):
    """Classification-By-Components Competition.

    Returns probability distributions over the classes.

    `detections` must be of shape [batch_size, num_components].
    `reasonings` must be of shape [num_components, num_classes, 2].

    """
    A, B = reasonings.permute(2, 1, 0).clamp(0, 1)
    pk = A
    nk = (1 - A) * B
    numerator = (detections @ (pk - nk).T) + nk.sum(1)
    probs = numerator / (pk + nk).sum(1)
    return probs