import torch


def cov(x: torch.Tensor) -> torch.Tensor:
    """ Sample covariance.

    Args:
        x (torch.Tensor): shape (B, L)
    
    Returns:
        torch.Tensor: shape (L, L)
    """
    
    # E[x x^T]
    exxt = torch.mean(x[:,None,:] * x[:,:,None], dim=0)

    # E[x] E[x]^T
    ex = torch.mean(x, dim=0)
    exext = ex[:,None] * ex[None,:]

    return exxt - exext