(K_XX,
                 K_XY,
                 K_YY,
                 unit_diagonal=False,
                 mmd_est='unbiased')
return mmd2
Based on https://github.com/dougalsutherland/opt-mmd/blob/master/two_sample/mmd.py
    but changed to not compute the full kernel matrix at once.
Based on https://github.com/dougalsutherland/opt-mmd/blob/master/two_sample/mmd.py
    but changed to not compute the full kernel matrix at once.
Based on https : // github . com / dougalsutherland / opt - mmd / blob / master / two_sample / mmd . py but changed to not compute the full kernel matrix at once .
def _compute_mmd2(K_XX,
                 K_XY,
                 K_YY,
                 unit_diagonal=False,
                 mmd_est='unbiased'):
    """
    Based on https://github.com/dougalsutherland/opt-mmd/blob/master/two_sample/mmd.py
    but changed to not compute the full kernel matrix at once.
    """
    if mmd_est not in ['unbiased', 'u-statistic']:
        raise ValueError(
            "mmd_est should be one of [unbiased', 'u-statistic] but got {}.".
            format(mmd_est))

    m = K_XX.shape[0]
    if K_XX.shape != (m, m):
        raise ValueError("K_XX shape should be {} but got {} instead.".format(
            (m, m), K_XX.shape))

    if K_XY.shape != (m, m):
        raise ValueError("K_XX shape should be {} but got {} instead.".format(
            (m, m), K_XY.shape))

    if K_YY.shape != (m, m):
        raise ValueError("K_XX shape should be {} but got {} instead.".format(
            (m, m), K_YY.shape))

    # Get the various sums of kernels that we'll use
    # Kts drop the diagonal, but we don't need to compute them explicitly
    if unit_diagonal:
        diag_X = diag_Y = 1
        sum_diag_X = sum_diag_Y = m
        sum_diag2_X = sum_diag2_Y = m
    else:
        diag_X = np.diagonal(K_XX)
        diag_Y = np.diagonal(K_YY)

        sum_diag_X = diag_X.sum()
        sum_diag_Y = diag_Y.sum()

        sum_diag2_X = _sqn(diag_X)
        sum_diag2_Y = _sqn(diag_Y)

    Kt_XX_sums = K_XX.sum(axis=1) - diag_X
    Kt_YY_sums = K_YY.sum(axis=1) - diag_Y
    K_XY_sums_0 = K_XY.sum(axis=0)
    K_XY_sums_1 = K_XY.sum(axis=1)

    Kt_XX_sum = Kt_XX_sums.sum()
    Kt_YY_sum = Kt_YY_sums.sum()
    K_XY_sum = K_XY_sums_0.sum()

    if mmd_est == 'biased':
        mmd2 = ((Kt_XX_sum + sum_diag_X) / (m * m) + (Kt_YY_sum + sum_diag_Y) /
                (m * m) - 2 * K_XY_sum / (m * m))
    else:
        mmd2 = (Kt_XX_sum + Kt_YY_sum) / (m * (m - 1))

        if mmd_est == 'unbiased':
            mmd2 -= 2 * K_XY_sum / (m * m)
        else:
            mmd2 -= 2 * (K_XY_sum - np.trace(K_XY)) / (m * (m - 1))

    return mmd2
def _compute_mmd2 ( K_XX , K_XY , K_YY , unit_diagonal = False , mmd_est = 'unbiased' ) : if mmd_est not in [ 'unbiased' , 'u-statistic' ] : raise ValueError ( "mmd_est should be one of [unbiased', 'u-statistic] but got {}." . format ( mmd_est ) ) m = K_XX . shape [ 0 ] if K_XX . shape != ( m , m ) : raise ValueError ( "K_XX shape should be {} but got {} instead." . format ( ( m , m ) , K_XX . shape ) ) if K_XY . shape != ( m , m ) : raise ValueError ( "K_XX shape should be {} but got {} instead." . format ( ( m , m ) , K_XY . shape ) ) if K_YY . shape != ( m , m ) : raise ValueError ( "K_XX shape should be {} but got {} instead." . format ( ( m , m ) , K_YY . shape ) ) # Get the various sums of kernels that we'll use # Kts drop the diagonal, but we don't need to compute them explicitly if unit_diagonal : diag_X = diag_Y = 1 sum_diag_X = sum_diag_Y = m sum_diag2_X = sum_diag2_Y = m else : diag_X = np . diagonal ( K_XX ) diag_Y = np . diagonal ( K_YY ) sum_diag_X = diag_X . sum ( ) sum_diag_Y = diag_Y . sum ( ) sum_diag2_X = _sqn ( diag_X ) sum_diag2_Y = _sqn ( diag_Y ) Kt_XX_sums = K_XX . sum ( axis = 1 ) - diag_X Kt_YY_sums = K_YY . sum ( axis = 1 ) - diag_Y K_XY_sums_0 = K_XY . sum ( axis = 0 ) K_XY_sums_1 = K_XY . sum ( axis = 1 ) Kt_XX_sum = Kt_XX_sums . sum ( ) Kt_YY_sum = Kt_YY_sums . sum ( ) K_XY_sum = K_XY_sums_0 . sum ( ) if mmd_est == 'biased' : mmd2 = ( ( Kt_XX_sum + sum_diag_X ) / ( m * m ) + ( Kt_YY_sum + sum_diag_Y ) / ( m * m ) - 2 * K_XY_sum / ( m * m ) ) else : mmd2 = ( Kt_XX_sum + Kt_YY_sum ) / ( m * ( m - 1 ) ) if mmd_est == 'unbiased' : mmd2 -= 2 * K_XY_sum / ( m * m ) else : mmd2 -= 2 * ( K_XY_sum - np . trace ( K_XY ) ) / ( m * ( m - 1 ) ) return mmd2