nussl/ml/confidence.py
def jensen_shannon_divergence(gmm_p, gmm_q, n_samples=10**5):
    """
    Compute Jensen-Shannon (JS) divergence between two Gaussian Mixture Models via 
    sampling. JS divergence is also known as symmetric Kullback-Leibler divergence.
    JS divergence has no closed form in general for GMMs, thus we use sampling to 
    compute it.

    Args:
        gmm_p (GaussianMixture): A GaussianMixture class fit to some data.
        gmm_q (GaussianMixture): Another GaussianMixture class fit to some data.
        n_samples (int): Number of samples to use to estimate JS divergence. 

    Returns:
        JS divergence between gmm_p and gmm_q
    """
    X = gmm_p.sample(n_samples)[0]
    log_p_X = gmm_p.score_samples(X)
    log_q_X = gmm_q.score_samples(X)
    log_mix_X = np.logaddexp(log_p_X, log_q_X)

    Y = gmm_q.sample(n_samples)[0]
    log_p_Y = gmm_p.score_samples(Y)
    log_q_Y = gmm_q.score_samples(Y)
    log_mix_Y = np.logaddexp(log_p_Y, log_q_Y)

    return (log_p_X.mean() - (log_mix_X.mean() - np.log(2))
            + log_q_Y.mean() - (log_mix_Y.mean() - np.log(2))) / 2

    Compute Jensen-Shannon (JS) divergence between two Gaussian Mixture Models via 
    sampling. JS divergence is also known as symmetric Kullback-Leibler divergence.
    JS divergence has no closed form in general for GMMs, thus we use sampling to 
    compute it.

    Args:
        gmm_p (GaussianMixture): A GaussianMixture class fit to some data.
        gmm_q (GaussianMixture): Another GaussianMixture class fit to some data.
        n_samples (int): Number of samples to use to estimate JS divergence. 

    Returns:
        JS divergence between gmm_p and gmm_q
    
Compute Jensen-Shannon (JS) divergence between two Gaussian Mixture Models via
sampling. JS divergence is also known as symmetric Kullback-Leibler divergence.
JS divergence has no closed form in general for GMMs, thus we use sampling to
compute it.
Compute Jensen - Shannon ( JS ) divergence between two Gaussian Mixture Models via sampling . JS divergence is also known as symmetric Kullback - Leibler divergence . JS divergence has no closed form in general for GMMs thus we use sampling to compute it .
def jensen_shannon_divergence(gmm_p, gmm_q, n_samples=10**5):
    X = gmm_p.sample(n_samples)[0]
    log_p_X = gmm_p.score_samples(X)
    log_q_X = gmm_q.score_samples(X)
    log_mix_X = np.logaddexp(log_p_X, log_q_X)
    Y = gmm_q.sample(n_samples)[0]
    log_p_Y = gmm_p.score_samples(Y)
    log_q_Y = gmm_q.score_samples(Y)
    log_mix_Y = np.logaddexp(log_p_Y, log_q_Y)
    return (log_p_X.mean() - (log_mix_X.mean() - np.log(2))
            + log_q_Y.mean() - (log_mix_Y.mean() - np.log(2))) / 2
def jensen_shannon_divergence ( gmm_p , gmm_q , n_samples = 10 ** 5 ) : X = gmm_p . sample ( n_samples ) [ 0 ] log_p_X = gmm_p . score_samples ( X ) log_q_X = gmm_q . score_samples ( X ) log_mix_X = np . logaddexp ( log_p_X , log_q_X ) Y = gmm_q . sample ( n_samples ) [ 0 ] log_p_Y = gmm_p . score_samples ( Y ) log_q_Y = gmm_q . score_samples ( Y ) log_mix_Y = np . logaddexp ( log_p_Y , log_q_Y ) return ( log_p_X . mean ( ) - ( log_mix_X . mean ( ) - np . log ( 2 ) ) + log_q_Y . mean ( ) - ( log_mix_Y . mean ( ) - np . log ( 2 ) ) ) / 2
Compute Jensen-Shannon (JS) divergence between two Gaussian Mixture Models via
sampling.
Compute Jensen - Shannon ( JS ) divergence between two Gaussian Mixture Models via sampling .
"""
    Compute Jensen-Shannon (JS) divergence between two Gaussian Mixture Models via 
    sampling. JS divergence is also known as symmetric Kullback-Leibler divergence.
    JS divergence has no closed form in general for GMMs, thus we use sampling to 
    compute it.

    Args:
        gmm_p (GaussianMixture): A GaussianMixture class fit to some data.
        gmm_q (GaussianMixture): Another GaussianMixture class fit to some data.
        n_samples (int): Number of samples to use to estimate JS divergence. 

    Returns:
        JS divergence between gmm_p and gmm_q
    """
JS divergence between gmm_p and gmm_q
JS divergence between gmm_p and gmm_q
gmm_p
A GaussianMixture class fit to some data.
A GaussianMixture class fit to some data .
gmm_q
Another GaussianMixture class fit to some data.
Another GaussianMixture class fit to some data .
n_samples
Number of samples to use to estimate JS divergence.
Number of samples to use to estimate JS divergence .