import torch


def calculate_kl(mu_p, sig_p, mu_q, sig_q):
    """
    Calculates the Kullback-Leibler divergence between two univariate Gaussians (p and q)

    Args:
        mu_p: mean of the Gaussian p
        sig_p: standard deviation of the Gaussian p
        mu_q: mean of the Gaussian q
        sig_q: standard deviation of the Gaussian q
    """
    kl = 0.5 * (2 * torch.log(sig_p/ sig_q) - 1 + (sig_q / sig_p).pow(2) + ((mu_p - mu_q) / sig_p).pow(2)).sum()
    return kl