def allele_frequency(X):
    """
    Compute allele frequency from its expectation.

    ``X`` is a matrix whose last dimension correspond to the different set of
    chromosomes. The first dimension represent different variants and the second
    dimension represent the different samples.

    Parameters
    ----------
    X : array_like
        Allele expectations encoded as a variants-by-samples-by-alleles matrix.

    Returns
    -------
    frequenct : ndarray
        Allele frequencies encoded as a variants-by-alleles matrix.
    """
    ploidy = X.shape[-1]
    if X.ndim < 3:
        n = 1
    else:
        n = X.shape[1]
    return X.sum(-2) / (ploidy * n)