def __init__(self,
                 mult=True,
                 shannon_co_name='BHShannon_KnnK',
                 shannon_co_pars=None,
                 ce_co_name='BCCE_KnnK',
                 ce_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        shannon_co_name : str, optional 
                          You can change it to any Shannon entropy
                          (default is 'BHShannon_KnnK').
        shannon_co_pars : dictionary, optional
                     Parameters for the Shannon entropy estimator (default
                     is None (=> {}); in this case the default parameter
                     values of the Shannon entropy estimator are used).
        ce_co_name : str, optional 
                     You can change it to any cross-entropy estimator
                     (default is 'BCCE_KnnK').
        ce_co_pars : dictionary, optional
                     Parameters for the cross-entropy estimator (default
                     is None (=> {}); in this case the default parameter
                     values of the cross-entropy estimator are used).
                      
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDKL_HSCE()
        >>> co2 = ite.cost.MDKL_HSCE(shannon_co_name='BHShannon_KnnK')
        >>> co3 = ite.cost.MDKL_HSCE(shannon_co_name='BHShannon_KnnK',\
                                     shannon_co_pars={'k':6,'eps':0.2})
        >>> co4 = ite.cost.MDKL_HSCE(shannon_co_name='BHShannon_KnnK',\
                                     shannon_co_pars={'k':5,'eps':0.2},\
                                     ce_co_name='BCCE_KnnK',\
                                     ce_co_pars={'k':6,'eps':0.1})
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the Shannon entropy estimator:
        shannon_co_pars = shannon_co_pars or {}
        shannon_co_pars['mult'] = True  # guarantee this property
        self.shannon_co = co_factory(shannon_co_name, **shannon_co_pars)

        # initialize the cross-entropy estimator:
        ce_co_pars = ce_co_pars or {}
        ce_co_pars['mult'] = True  # guarantee this property
        self.ce_co = co_factory(ce_co_name, **ce_co_pars)
    def __init__(self, mult=True, l2_co_name='BDL2_KnnK', l2_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        l2_co_name : str, optional 
                     You can change it to any L2 divergence estimator
                     (default is 'BDL2_KnnK').
        l2_co_pars : dictionary, optional
                     Parameters for the L2 divergence estimator (default
                     is None (=> {}); in this case the default parameter
                     values of the L2 divergence estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MIL2_DL2()
        >>> co2 = ite.cost.MIL2_DL2(l2_co_name='BDL2_KnnK')
        >>> dict_ch = {'knn_method': 'cKDTree', 'k': 2, 'eps': 0.1}
        >>> co3 = ite.cost.MIL2_DL2(l2_co_name='BDL2_KnnK',\
                                    l2_co_pars=dict_ch)
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the L2 divergence estimator:
        l2_co_pars = l2_co_pars or {}
        l2_co_pars['mult'] = mult  # guarantee this property
        self.l2_co = co_factory(l2_co_name, **l2_co_pars)
Пример #3
0
    def __init__(self, mult=True, js_co_name='MDJS_HS', js_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        js_co_name : str, optional 
                     You can change it to any Jensen-Shannon divergence 
                     estimator (default is 'MDJS_HS').
        js_co_pars : dictionary, optional
                     Parameters for the Jensen-Shannnon divergence
                     estimator (default is None (=> {}); in this case the
                     default parameter values of the Jensen-Shannon
                     divergence estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MKJS_DJS()
        >>> co2 = ite.cost.MKJS_DJS(js_co_name='MDJS_HS')
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the Jensen-Shannon divergence estimator:
        js_co_pars = js_co_pars or {}
        js_co_pars['mult'] = True  # guarantee this property
        js_co_pars['w'] = array([1 / 2, 1 / 2])  # uniform weights
        self.js_co = co_factory(js_co_name, **js_co_pars)
Пример #4
0
    def __init__(self, mult=True, kl_co_name='BDKL_KnnK', kl_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        kl_co_name : str, optional 
                     You can change it to any Kullback-Leibler divergence 
                     estimator. (default is 'BDKL_KnnK')
        kl_co_pars : dictionary, optional
                     Parameters for the KL divergence estimator. (default
                     is None (=> {}); in this case the default parameter
                     values of the KL divergence estimator are used)

        --------
        >>> import ite
        >>> co1 = ite.cost.MHShannon_DKLN()
        >>> co2 = ite.cost.MHShannon_DKLN(kl_co_name='BDKL_KnnK')

        >>> dict_ch = {'knn_method': 'cKDTree', 'k': 4, 'eps': 0.2}
        >>> co3 = ite.cost.MHShannon_DKLN(kl_co_name='BDKL_KnnK', \
                                          kl_co_pars=dict_ch)
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)
        
        # initialize the KL divergence estimator:
        kl_co_pars = kl_co_pars or {}
        kl_co_pars['mult'] = True  # guarantee this property
        self.kl_co = co_factory(kl_co_name, **kl_co_pars) 
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 w=array([1 / 2, 1 / 2]),
                 renyi_co_name='BHRenyi_KnnK',
                 renyi_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        alpha : float, \ne 1, optional
                Parameter of the Jensen-Renyi divergence (default is 0.99).
        w : ndarray, w = [w1,w2], w_i > 0, w_2 > 0, w1 + w2 = 1, optional.
            Parameters of the Jensen-Renyi divergence (default is w = 
            array([1/2,1/2]) )
        renyi_co_name : str, optional 
                        You can change it to any Renyi entropy estimator 
                        (default is 'BHRenyi_KnnK').
        renyi_co_pars : dictionary, optional
                        Parameters for the Renyi entropy estimator
                        (default is None (=> {}); in this case the default
                        parameter values of the Renyi entropy estimator
                        are used).
                      
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDJR_HR()
        >>> co2 = ite.cost.MDJR_HR(renyi_co_name='BHRenyi_KnnK', alpha=0.8)
        >>> co3 = ite.cost.MDJR_HR(renyi_co_name='BHRenyi_KnnK',\
                                   renyi_co_pars={'k': 6}, alpha=0.5,\
                                   w=array([1/4,3/4]))
        
        """

        # initialize with 'InitAlpha':
        super().__init__(mult=mult, alpha=alpha)

        # initialize the Renyi entropy estimator:
        renyi_co_pars = renyi_co_pars or {}
        renyi_co_pars['mult'] = mult  # guarantee this property
        renyi_co_pars['alpha'] = alpha  # -||-
        self.renyi_co = co_factory(renyi_co_name, **renyi_co_pars)

        # other attributes (w):
        # verification:
        if sum(w) != 1:
            raise Exception('sum(w) has to be 1!')

        if not all(w > 0):
            raise Exception('The coordinates of w have to be positive!')

        if len(w) != 2:
            raise Exception('The length of w has to be 2!')

        self.w = w
Пример #6
0
def main():
    # parameters:
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(100, 5*1000+1, 100)  # number of samples
    cost_name = 'BKExpected'  # dim >= 1

    # initialization:
    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)

    # RBF kernel (sigma = std / bandwith parameter):
    kernel = Kernel({'name': 'RBF', 'sigma': 1})
    # polynomial kernel (quadratic / cubic; c = offset parameter = 1):
    # kernel = Kernel({'name': 'polynomial', 'exponent': 2, 'c': 1})
    # kernel = Kernel({'name': 'polynomial', 'exponent': 3, 'c': 1})

    co = co_factory(cost_name, mult=True, kernel = kernel) # cost object

    k_hat_v = zeros(length)  # vector of estimated kernel values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2), 
    # analytical value (k):
    if distr == 'normal':
        # mean (m1,m2):
        m1 = rand(dim)
        m2 = rand(dim)
        
        # (random) linear transformation applied to the data (l1,l2) -> 
        # covariance matrix (c1,c2):
        l2 = rand(dim, dim)
        l1 = rand(dim, dim)
        c1 = dot(l1, l1.T)
        c2 = dot(l2, l2.T)

        # generate samples (y1~N(m1,c1), y2~N(m2,c2)):
        y1 = multivariate_normal(m1, c1, num_of_samples_max)
        y2 = multivariate_normal(m2, c2, num_of_samples_max)

        par1 = {"mean": m1, "cov": c1}
        par2 = {"mean": m2, "cov": c2}
    else:
        raise Exception('Distribution=?')        
        
    k = analytical_value_k_expected(distr, distr, co.kernel, par1, par2)
    
    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        k_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk+1, length))
 
    # plot:    
    plt.plot(num_of_samples_v, k_hat_v, num_of_samples_v, ones(length)*k)
    plt.xlabel('Number of samples')
    plt.ylabel('Expected kernel')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
def main():
    # parameters:
    alpha = 0.7  # parameter of Sharma-Mittal divergence, >0, \ne 1
    beta = 0.5  # parameter of Sharma-Mittal divergence, \ne 1
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(1000, 30*1000+1, 2000)
    cost_name = 'BDSharmaMittal_KnnK'  # dim >= 1
    
    # initialization:
    distr = 'normal'  # fixed    
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    # cost object:
    co = co_factory(cost_name, mult=True, alpha=alpha, beta=beta)
    d_hat_v = zeros(length)  # vector of estimated divergence values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2), 
    # analytical value (d):
    if distr == 'normal':
        # mean (m1,m2):
        m2 = rand(dim)
        m1 = m2
        
        # (random) linear transformation applied to the data (l1,l2) ->
        # covariance matrix (c1,c2):
        l2 = rand(dim, dim)
        l1 = rand(1) * l2
        # Note: (m2,l2) => (m1,c1) choice guarantees y1<<y2 (in practise,
        # too).

        c1 = dot(l1, l1.T)
        c2 = dot(l2, l2.T)
        
        # generate samples (y1~N(m1,c1), y2~N(m2,c2)):
        y1 = multivariate_normal(m1, c1, num_of_samples_max)
        y2 = multivariate_normal(m2, c2, num_of_samples_max)
        
        par1 = {"mean": m1, "cov": c1}
        par2 = {"mean": m2, "cov": c2}
    else:
        raise Exception('Distribution=?')        
    
    d = analytical_value_d_sharma_mittal(distr, distr, alpha, beta, par1,
                                         par2)
    
    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        d_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk+1, length))
    
    # plot:    
    plt.plot(num_of_samples_v, d_hat_v, num_of_samples_v, ones(length)*d)
    plt.xlabel('Number of samples')
    plt.ylabel('Sharma-Mittal divergence')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
Пример #8
0
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 u=1,
                 tsallis_co_name='BHTsallis_KnnK',
                 tsallis_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        alpha: float, 0 < alpha <= 2, \ne 1, optional
               Parameter of the exponentiated Jensen-Tsallis kernel-1
               (default is 0.99).
        u: float, 0 < u, optional
           Parameter of the exponentiated Jensen-Tsallis kernel-1 (default
           is 1).
        tsallis_co_name : str, optional 
                          You can change it to any Tsallis entropy
                          estimator (default is 'BHTsallis_KnnK').
        tsallis_co_pars : dictionary, optional
                          Parameters for the Tsallis entropy estimator 
                          (default is None (=> {}); in this case the
                          default parameter values of the Tsallis entropy
                          estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MKExpJT1_HT()
        >>> co2 = ite.cost.MKExpJT1_HT(tsallis_co_name='BHTsallis_KnnK')
        >>> co3 = ite.cost.MKExpJT1_HT(alpha=0.7,u=1.2,\
                                       tsallis_co_name='BHTsallis_KnnK')
        >>> dict_ch = {'knn_method': 'cKDTree', 'k': 4, 'eps': 0.1}
        >>> co4 = ite.cost.MKExpJT1_HT(tsallis_co_name='BHTsallis_KnnK',\
                                       tsallis_co_pars=dict_ch)
        
        """

        # verification (alpha == 1 is checked via 'InitUAlpha'):
        # if alpha <= 0 or alpha > 2:
        #    raise Exception('0 < alpha <= 2 has to hold!')

        # initialize with 'InitUAlpha':
        super().__init__(mult=mult, u=u, alpha=alpha)

        # initialize the Tsallis entropy estimator:
        tsallis_co_pars = tsallis_co_pars or {}
        tsallis_co_pars['mult'] = True  # guarantee this property
        tsallis_co_pars['alpha'] = alpha  # -||-
        self.tsallis_co = co_factory(tsallis_co_name, **tsallis_co_pars)

        # other attributes (u):
        self.u = u
Пример #9
0
def main():
    # parameters:
    distr = 'normal'  # distribution: 'uniform', 'normal'
    dim = 2  # dimension of the distribution
    num_of_samples_v = arange(1000, 50 * 1000 + 1, 1000)

    cost_name = 'BHShannon_KnnK'  # dim >= 1
    # cost_name = 'BHShannon_SpacingV'  # dim = 1
    # cost_name = 'BHShannon_MaxEnt1'  # dim = 1; approximation around N
    # cost_name = 'BHShannon_MaxEnt2'  # dim = 1; approximation around N
    # cost_name = 'MHShannon_DKLN'   # dim >= 1
    # cost_name = 'MHShannon_DKLU'  # dim >= 1

    # initialization:
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    h_hat_v = zeros(length)  # vector of estimated entropy values

    # distr, dim -> samples (y), distribution parameters (par), analytical
    # value (h):
    if distr == 'uniform':
        # U[a,b], (random) linear transformation applied to the data (l):
        a = -rand(1, dim)
        b = rand(1, dim)  # guaranteed that a<=b (coordinate-wise)
        l = rand(dim, dim)
        y = dot(rand(num_of_samples_max, dim) * (b - a) + a, l.T)  # lxU[a,b]

        par = {"a": a, "b": b, "l": l}
    elif distr == 'normal':
        # mean (m), covariance matrix (c):
        m = rand(dim)
        l = rand(dim, dim)
        c = dot(l, l.T)

        # generate samples (y~N(m,c)):
        y = multivariate_normal(m, c, num_of_samples_max)

        par = {"cov": c}
    else:
        raise Exception('Distribution=?')

    h = analytical_value_h_shannon(distr, par)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        h_hat_v[tk] = co.estimation(y[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, h_hat_v, num_of_samples_v, ones(length) * h)
    plt.xlabel('Number of samples')
    plt.ylabel('Shannon entropy')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
    def __init__(self,
                 mult=True,
                 w=array([1 / 2, 1 / 2]),
                 shannon_co_name='BHShannon_KnnK',
                 shannon_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        w : ndarray, w = [w1,w2], w_i > 0, w_2 > 0, w1 + w2 = 1, optional.
                Parameters of the Jensen-Shannon divergence (default is
                w = array([1/2,1/2]) )
        shannon_co_name : str, optional 
                          You can change it to any Shannon entropy
                          estimator (default is 'BHShannon_KnnK').
        shannon_co_pars : dictionary, optional
                          Parameters for the Shannon entropy estimator 
                          (default is None (=> {}); in this case the
                          default parameter values of the Shannon entropy
                          estimator are used).
                      
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDJS_HS()
        >>> co2 = ite.cost.MDJS_HS(shannon_co_name='BHShannon_KnnK')
        >>> co3 = ite.cost.MDJS_HS(shannon_co_name='BHShannon_KnnK',\
                                   shannon_co_pars={'k':6,'eps':0.2},\
                                   w=array([1/4,3/4]))
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the Shannon entropy estimator:
        shannon_co_pars = shannon_co_pars or {}
        shannon_co_pars['mult'] = mult  # guarantee this property
        self.shannon_co = co_factory(shannon_co_name, **shannon_co_pars)

        # other attributes (w):
        # verification:
        if sum(w) != 1:
            raise Exception('sum(w) has to be 1!')

        if not all(w > 0):
            raise Exception('The coordinates of w have to be positive!')

        if len(w) != 2:
            raise Exception('The length of w has to be 2!')

        self.w = w
Пример #11
0
def main():
    # parameters:
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(1000, 50 * 1000 + 1, 1000)
    rho = 0.9  # parameter of the probability product kernel, >0
    cost_name = 'BKProbProd_KnnK'  # dim >= 1

    # initialization:
    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True, rho=rho)  # cost object
    k_hat_v = zeros(length)  # vector of estimated kernel values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2),
    # analytical value (k):
    if distr == 'normal':
        # mean (m1,m2):
        m2 = rand(dim)
        m1 = m2

        # (random) linear transformation applied to the data (l1,l2) ->
        # covariance matrix (c1,c2):
        l2 = rand(dim, dim)
        l1 = rand(1) * l2
        # Note: (m2,l2) => (m1,l1) choice guarantees y1<<y2 (in practise,
        # too).

        c1 = dot(l1, l1.T)
        c2 = dot(l2, l2.T)

        # generate samples (y1~N(m1,c1), y2~N(m2,c2)):
        y1 = multivariate_normal(m1, c1, num_of_samples_max)
        y2 = multivariate_normal(m2, c2, num_of_samples_max)

        par1 = {"mean": m1, "cov": c1}
        par2 = {"mean": m2, "cov": c2}
    else:
        raise Exception('Distribution=?')

    k = analytical_value_k_prob_product(distr, distr, rho, par1, par2)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        k_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, k_hat_v, num_of_samples_v, ones(length) * k)
    plt.xlabel('Number of samples')
    plt.ylabel('Probability product kernel')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
Пример #12
0
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 u=1,
                 jr_co_name='MDJR_HR',
                 jr_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        alpha: float, 0 < alpha < 1, optional
               Parameter of the exponentiated Jensen-Renyi kernel-2
               (default is 0.99).
        u: float, 0 < u, optional
           Parameter of the exponentiated Jensen-Renyi kernel-2 (default
           is 1).
        jr_co_name : str, optional 
                     You can change it to any Jensen-Renyi divergence 
                     estimator (default is 'MDJR_HR').
        jr_co_pars : dictionary, optional
                     Parameters for the Jensen-Renyi divergence estimator 
                     (default is None (=> {}); in this case the default
                     parameter values of the Jensen-Renyi divergence
                     estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MKExpJR2_DJR()
        >>> co2 = ite.cost.MKExpJR2_DJR(jr_co_name='MDJR_HR')
        >>> co3 = ite.cost.MKExpJR2_DJR(alpha=0.7,u=1.2,\
                                        jr_co_name='MDJR_HR')

        """

        # verification (alpha == 1 is checked via 'InitUAlpha'):
        # if alpha <= 0 or alpha > 1:
        #    raise Exception('0 < alpha < 1 has to hold!')

        # initialize with 'InitUAlpha':
        super().__init__(mult=mult, u=u, alpha=alpha)

        # initialize the Jensen-Renyi divergence estimator:
        jr_co_pars = jr_co_pars or {}
        jr_co_pars['mult'] = True  # guarantee this property
        jr_co_pars['alpha'] = alpha  # -||-
        jr_co_pars['w'] = array([1 / 2, 1 / 2])  # uniform weights
        self.jr_co = co_factory(jr_co_name, **jr_co_pars)

        # other attributes (u):
        self.u = u
Пример #13
0
def main():
    # parameters:
    distr = 'normalI'  # 'uniform', 'normalI' (isotropic normal, Id cov.) 
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(1000, 50*1000+1, 1000)
    cost_name = 'BDChi2_KnnK'  # dim >= 1
    
    # initialization:
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    d_hat_v = zeros(length)  # vector of estimated divergence values

    # distr, dim -> samples (y1<<y2), distribution parameters (par1,par2), 
    # analytical value (d):
    if distr == 'uniform':
        b = 3 * rand(dim)
        a = b * rand(dim)
            
        y1 = rand(num_of_samples_max, dim) * a  # U[0,a]
        y2 = rand(num_of_samples_max, dim) * b
        # U[0,b], a<=b (coordinate-wise) => y1<<y2
        
        par1 = {"a": a}
        par2 = {"a": b}        
    elif distr == 'normalI':
        # mean (m1,m2):
        m1 = 2 * rand(dim)
        m2 = 2 * rand(dim)
        
        # generate samples (y1~N(m1,I), y2~N(m2,I)):
        y1 = multivariate_normal(m1, eye(dim), num_of_samples_max)
        y2 = multivariate_normal(m2, eye(dim), num_of_samples_max)
        
        par1 = {"mean": m1}
        par2 = {"mean": m2}
    else:
        raise Exception('Distribution=?')        
    
    d = analytical_value_d_chi_square(distr, distr, par1, par2)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        d_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk+1, length))
    
    # plot:    
    plt.plot(num_of_samples_v, d_hat_v, num_of_samples_v, ones(length)*d)
    plt.xlabel('Number of samples')
    plt.ylabel('Chi square divergence')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
def main():
    # parameters:
    distr = 'uniform'  # possibilities: 'uniform', 'normal'
    dim = 2  # dimension of the distribution
    num_of_distributions = 5
    # each distribution is represented by num_of_samples samples:
    num_of_samples = 500

    # kernel used to evaluate the distributions:
    cost_name = 'BKExpected'
    # cost_name = 'BKProbProd_KnnK'
    # cost_name = 'MKExpJR1_HR'
    # cost_name = 'MKExpJR2_DJR'
    # cost_name = 'MKExpJS_DJS'
    # cost_name = 'MKExpJT1_HT'
    # cost_name = 'MKExpJT2_DJT'
    # cost_name = 'MKJS_DJS'
    # cost_name = 'MKJT_HT'

    # initialization:
    co = co_factory(cost_name, mult=True)
    ys = list()

    # generate samples from the distributions (ys):
    for n in range(num_of_distributions):
        # generate samples from the n^th distribution (y):
        if distr == 'uniform':
            a, b = -rand(dim), rand(dim)  # a,b
            # (random) linear transformation applied to the data (r x
            # U[a,b]):
            r = randn(dim, dim)
            y = dot(rand(num_of_samples, dim) * (b - a).T + a.T, r)
        elif distr == 'normal':
            m = rand(dim)  # mean
            # cov:
            l = rand(dim, dim)
            c = dot(l, l.T)  # cov

            # generate samples (yy~N(m,c)):
            y = multivariate_normal(m, c, num_of_samples)

        ys.append(y)

    # Gram matrix and its minimal eigenvalue:
    g = co.gram_matrix(ys)
    eigenvalues = eigh(g)[0]  # eigenvalues: reals, in increasing order
    min_eigenvalue = eigenvalues[0]

    # plot:
    plt.plot(range(1, num_of_distributions + 1), eigenvalues)
    plt.xlabel('Index of the sorted eigenvalues: i')
    plt.ylabel('Eigenvalues of the (estimated) Gram matrix')
    plt.title("Minimal eigenvalue: " + str(round(min_eigenvalue, 2)))
    plt.show()
    def __init__(self,
                 hess=2,
                 mult=True,
                 chi_square_co_name='BDChi2_KnnK',
                 chi_square_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        hess : float, optional 
               =f^{(2)}(1), the second derivative of f at 1 (default is 2).
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        chi_square_co_name : str, optional 
                             You can change it to any Pearson chi square 
                             divergence estimator (default is
                             'BDChi2_KnnK').
        chi_square_co_pars : dictionary, optional
                             Parameters for the Pearson chi-square
                             divergence estimator (default is None
                             (=> {}); in this case the default parameter
                             values of the Pearson chi square divergence
                             estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDf_DChi2(hess=2)
        >>> co2 = ite.cost.MDf_DChi2(hess=1,\
                                     chi_square_co_name='BDChi2_KnnK')
        >>> dict_ch = {'k': 6}
        >>> co3 = ite.cost.MDf_DChi2(hess=2,\
                                     chi_square_co_name='BDChi2_KnnK',\
                                     chi_square_co_pars=dict_ch)
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the chi^2 divergence estimator:
        chi_square_co_pars = chi_square_co_pars or {}
        chi_square_co_pars['mult'] = mult  # guarantee this property
        self.chi_square_co = co_factory(chi_square_co_name,
                                        **chi_square_co_pars)
        # other attributes (hess):
        self.hess = hess
Пример #16
0
def main():
    # parameters:
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(100, 12 * 1000 + 1, 500)
    u = 1  # >0, parameter of the Jensen-Renyi kernel
    cost_name = 'MKExpJR2_DJR'  # dim >= 1

    # initialization:
    alpha = 2
    # fixed; parameter of the Jensen-Renyi kernel; for alpha = 2 we have
    # explicit formula for the Jensen-Renyi divergence, and hence for the
    # Jensen-Renyi kernel(-2).

    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True, alpha=alpha, u=u)  # cost object

    k_hat_v = zeros(length)  # vector of estimated kernel values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2),
    # analytical value (k):
    if distr == 'normal':
        # generate samples (y1,y2); y1~N(m1,s1^2xI), y2~N(m2,s2^2xI):
        m1, s1 = randn(dim), rand(1)
        m2, s2 = randn(dim), rand(1)
        y1 = multivariate_normal(m1, s1**2 * eye(dim), num_of_samples_max)
        y2 = multivariate_normal(m2, s2**2 * eye(dim), num_of_samples_max)

        par1 = {"mean": m1, "std": s1}
        par2 = {"mean": m2, "std": s2}
    else:
        raise Exception('Distribution=?')

    k = analytical_value_k_ejr2(distr, distr, u, par1, par2)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        k_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, k_hat_v, num_of_samples_v, ones(length) * k)
    plt.xlabel('Number of samples')
    plt.ylabel('Exponentiated Jensen-Renyi kernel-2')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 tsallis_co_name='BDTsallis_KnnK',
                 tsallis_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        alpha : float, optional
                Parameter of the Renyi mutual information (default is
                0.99).
        tsallis_co_name : str, optional 
                          You can change it to any Tsallis divergence 
                          estimator (default is 'BDTsallis_KnnK').
        tsallis_co_pars : dictionary, optional
                          Parameters for the Tsallis divergence estimator 
                          (default is None (=> {}); in this case the
                          default parameter values of the Tsallis
                          divergence estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MITsallis_DT()
        >>> co2 = ite.cost.MITsallis_DT(tsallis_co_name='BDTsallis_KnnK')
        >>> co3 = ite.cost.MITsallis_DT(tsallis_co_name='BDTsallis_KnnK',\
                                        alpha=0.4)
        >>> dict_ch = {'knn_method': 'cKDTree', 'k': 2, 'eps': 0.1}
        >>> co4 = ite.cost.MITsallis_DT(mult=True,alpha=0.9,\
                                        tsallis_co_name='BDTsallis_KnnK',\
                                        tsallis_co_pars=dict_ch)
        
        """

        # initialize with 'InitAlpha':
        super().__init__(mult=mult, alpha=alpha)

        # initialize the Tsallis divergence estimator:
        tsallis_co_pars = tsallis_co_pars or {}
        tsallis_co_pars['mult'] = mult  # guarantee this property
        tsallis_co_pars['alpha'] = alpha  # -||-
        self.tsallis_co = co_factory(tsallis_co_name, **tsallis_co_pars)
def main():
    # parameters:
    ds = array([4, 1])  # subspace dimensions: ds[0], ..., ds[M-1]
    num_of_samples_v = arange(1000, 20 * 1000 + 1, 1000)

    cost_name = 'MIShannon_DKL'  # d_m >= 1, M >= 2
    # cost_name = 'MIShannon_HS'  # d_m >= 1, M >= 2

    # initialization:
    distr = 'normal'  # distribution; fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    # vector of estimated mutual information values:
    i_hat_v = zeros(length)

    # distr, ds -> samples (y), distribution parameters (par), analytical
    # value (i):
    if distr == 'normal':
        dim = sum(ds)  # dimension of the joint distribution

        # mean (m), covariance matrix (c):
        m = rand(dim)
        l = rand(dim, dim)
        c = dot(l, l.T)

        # generate samples (y~N(m,c)):
        y = multivariate_normal(m, c, num_of_samples_max)

        par = {"ds": ds, "cov": c}
    else:
        raise Exception('Distribution=?')

    i = analytical_value_i_shannon(distr, par)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        i_hat_v[tk] = co.estimation(y[0:num_of_samples], ds)  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, i_hat_v, num_of_samples_v, ones(length) * i)
    plt.xlabel('Number of samples')
    plt.ylabel('Shannon mutual information')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
Пример #19
0
def main():
    # parameters:
    alpha = 0.7  # parameter of Renyi mutual information, \ne 1
    dim = 2  # >=2; dimension of the distribution
    num_of_samples_v = arange(100, 10*1000+1, 500)

    cost_name = 'MIRenyi_DR'
    # cost_name = 'MIRenyi_HR'
    
    # initialization:
    distr = 'normal'  # distribution; fixed    
    ds = ones(dim, dtype='int')  # dimensions of the 'subspaces'
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True, alpha=alpha)  # cost object
    # vector of estimated mutual information values:
    i_hat_v = zeros(length)

    # distr, dim -> samples (y), distribution parameters (par), analytical 
    # value (i):
    if distr == 'normal':
        # mean (m), covariance matrix (c):
        m = rand(dim) 
        l = rand(dim, dim)
        c = dot(l, l.T)
        
        # generate samples (y~N(m,c)): 
        y = multivariate_normal(m, c, num_of_samples_max)
           
        par = {"cov": c} 
    else:
        raise Exception('Distribution=?')
    
    i = analytical_value_i_renyi(distr, alpha, par)
        
    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        i_hat_v[tk] = co.estimation(y[0:num_of_samples], ds)  # broadcast
        print("tk={0}/{1}".format(tk+1, length))
        
    # plot:    
    plt.plot(num_of_samples_v, i_hat_v, num_of_samples_v, ones(length)*i)
    plt.xlabel('Number of samples')
    plt.ylabel('Renyi mutual information')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
Пример #20
0
def main():
    # parameters:
    dim = 2  # dimension of the distribution
    num_of_samples_v = arange(1000, 50 * 1000 + 1, 2000)
    cost_name = 'BDHellinger_KnnK'  # dim >= 1

    # initialization:
    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    d_hat_v = zeros(length)  # vector of estimated divergence values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2),
    # analytical value (d):
    if distr == 'normal':
        # mean (m1,m2):
        m1, m2 = rand(dim), rand(dim)

        # (random) linear transformation applied to the data (l1,l2) ->
        # covariance matrix (c1,c2):
        l1, l2 = rand(dim, dim), rand(dim, dim)
        c1, c2 = dot(l1, l1.T), dot(l2, l2.T)

        # generate samples (y1~N(m1,c1), y2~N(m2,c2)):
        y1 = multivariate_normal(m1, c1, num_of_samples_max)
        y2 = multivariate_normal(m2, c2, num_of_samples_max)

        par1, par2 = {"mean": m1, "cov": c1}, {"mean": m2, "cov": c2}
    else:
        raise Exception('Distribution=?')

    d = analytical_value_d_hellinger(distr, distr, par1, par2)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        # with broadcasting:
        d_hat_v[tk] = co.estimation(y1[0:num_of_samples], y2[0:num_of_samples])
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, d_hat_v, num_of_samples_v, ones(length) * d)
    plt.xlabel('Number of samples')
    plt.ylabel('Hellinger distance')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
def main():
    # parameters:
    dim1 = 1  # dimension of y1
    dim2 = 2  # dimension of y2
    num_of_samples_v = arange(1000, 30 * 1000 + 1, 1000)
    cost_name = 'BcondHShannon_HShannon'  # dim1 >= 1, dim2 >= 1

    # initialization:
    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    # vector of estimated conditional entropy values:
    cond_h_hat_v = zeros(length)
    dim = dim1 + dim2

    # distr, dim1 -> samples (y), distribution parameters (par),
    # analytical value (cond_h):
    if distr == 'normal':
        # mean (m), covariance matrix (c):
        m, l = rand(dim), rand(dim, dim)
        c = dot(l, l.T)

        # generate samples (y~N(m,c)):
        y = multivariate_normal(m, c, num_of_samples_max)

        par = {"dim1": dim1, "cov": c}
    else:
        raise Exception('Distribution=?')

    cond_h = analytical_value_cond_h_shannon(distr, par)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        # broadcasting:
        cond_h_hat_v[tk] = co.estimation(y[:num_of_samples], dim1)
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, cond_h_hat_v,
             num_of_samples_v, ones(length) * cond_h)
    plt.xlabel('Number of samples')
    plt.ylabel('Conditional Shannon entropy')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
def main():
    # parameters:
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(1000, 30 * 1000 + 1, 1000)
    alpha = 0.8  # parameter of the Sharma-Mittal entropy; alpha \ne 1
    beta = 0.6  # parameter of the Sharma-Mittal entropy; beta \ne 1
    cost_name = 'BHSharmaMittal_KnnK'  # dim >= 1

    # initialization:
    distr = 'normal'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    # cost object:
    co = co_factory(cost_name, mult=True, alpha=alpha, beta=beta)
    h_hat_v = zeros(length)  # vector of estimated entropy values

    # distr, dim -> samples (y), distribution parameters (par), analytical
    # value (h):
    if distr == 'normal':
        # mean (m), covariance matrix (c):
        m = rand(dim)
        l = rand(dim, dim)
        c = dot(l, l.T)

        # generate samples (y~N(m,c)):
        y = multivariate_normal(m, c, num_of_samples_max)

        par = {"cov": c}
    else:
        raise Exception('Distribution=?')

    h = analytical_value_h_sharma_mittal(distr, alpha, beta, par)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        h_hat_v[tk] = co.estimation(y[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, h_hat_v, num_of_samples_v, ones(length) * h)
    plt.xlabel('Number of samples')
    plt.ylabel('Sharma-Mittal entropy')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 bregman_co_name='BDBregman_KnnK',
                 bregman_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        alpha : float, \ne 1, optional
                Parameter of the symmetric Bregman distance (default is
                0.99).
        bregman_co_name : str, optional 
                          You can change it to any nonsymmetric Bregman 
                          distance estimator (default is 'BDBregman_KnnK').
        bregman_co_pars : dictionary, optional
                          Parameters for the nonsymmetric Bregman distance 
                          estimator (default is None (=> {}); in this case
                          the default parameter values of the nonsymmetric
                          Bregman distance estimator are used).
                      
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDSymBregman_DB()
        >>> co2 =\
                ite.cost.MDSymBregman_DB(bregman_co_name='BDBregman_KnnK')
        >>> co3 =\
                ite.cost.MDSymBregman_DB(bregman_co_name='BDBregman_KnnK',\
                                         bregman_co_pars={'k':6,'eps':0.2})
        
        """

        # initialize with 'InitAlpha':
        super().__init__(mult=mult, alpha=alpha)

        # initialize the nonsymmetric Bregman distance estimator:
        bregman_co_pars = bregman_co_pars or {}
        bregman_co_pars['mult'] = mult  # guarantee this property
        bregman_co_pars['alpha'] = alpha  # guarantee this property
        self.bregman_co = co_factory(bregman_co_name, **bregman_co_pars)
def main():
    # parameters:
    dim = 2  # dimension of the distribution
    w = array([1/3, 2/3])  # weight in the Jensen-Renyi divergence
    num_of_samples_v = arange(100, 12*1000+1, 500)
    cost_name = 'MDJR_HR'  # dim >= 1
    
    # initialization:
    alpha = 2  # parameter of the Jensen-Renyi divergence, \ne 1; fixed    
    distr = 'normal'  # fixed    
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True, alpha=alpha, w=w)  # cost object
    d_hat_v = zeros(length)  # vector of estimated divergence values

    # distr, dim -> samples (y1,y2), distribution parameters (par1,par2), 
    # analytical value (d):
    if distr == 'normal':
        # generate samples (y1,y2); y1~N(m1,s1^2xI), y2~N(m2,s2^2xI):
        m1, s1 = randn(dim), rand(1)
        m2, s2 = randn(dim), rand(1)
        y1 = multivariate_normal(m1, s1**2 * eye(dim), num_of_samples_max)
        y2 = multivariate_normal(m2, s2**2 * eye(dim), num_of_samples_max)
      
        par1 = {"mean": m1, "std": s1}
        par2 = {"mean": m2, "std": s2}
    else:
        raise Exception('Distribution=?')        
    
    d = analytical_value_d_jensen_renyi(distr, distr, w, par1, par2)
    
    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        d_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk+1, length))
    
    # plot:    
    plt.plot(num_of_samples_v, d_hat_v, num_of_samples_v, ones(length)*d)
    plt.xlabel('Number of samples')
    plt.ylabel('Jensen-Renyi divergence')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
Пример #25
0
def main():
    # parameters:
    distr = 'uniform'  # possibilities: 'uniform', 'normal'
    m = 2  # number of components
    num_of_samples_v = arange(1000, 30 * 1000 + 1, 1000)
    cost_name = 'BASpearman1'  # m >= 2
    # cost_name = 'BASpearman2'  # m >= 2
    # cost_name = 'BASpearman3'  # m >= 2
    # cost_name = 'BASpearman4'  # m >= 2
    # cost_name = 'BASpearmanCondLT'  # m >= 2
    # cost_name = 'BASpearmanCondUT'  # m >= 2
    # cost_name = 'BABlomqvist'  # m >= 2
    # cost_name = 'MASpearmanUT'  # m >= 2
    # cost_name = 'MASpearmanLT'  # m >= 2

    # initialization:
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True)  # cost object
    a_hat_v = zeros(length)  # vector to store the association values
    ds = ones(m, dtype='int')

    # distr -> samples (y); analytical value (a):
    if distr == 'uniform':
        y = rand(num_of_samples_max, m)
    elif distr == 'normal':
        y = randn(num_of_samples_max, m)
    else:
        raise Exception('Distribution=?')

    a = 0

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        a_hat_v[tk] = co.estimation(y[0:num_of_samples], ds)  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, a_hat_v, num_of_samples_v, ones(length) * a)
    plt.xlabel('Number of samples')
    plt.ylabel('Association')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
    def __init__(self,
                 mult=True,
                 alpha=0.99,
                 tsallis_co_name='BHTsallis_KnnK',
                 tsallis_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        alpha : float, \ne 1, optional
                Parameter of the Jensen-Tsallis divergence (default is
                0.99).
        tsallis_co_name : str, optional 
                          You can change it to any Tsallis entropy
                          estimator (default is 'BHTsallis_KnnK').
        tsallis_co_pars : dictionary, optional
                          Parameters for the Tsallis entropy estimator
                          (default is None (=> {}); in this case the
                          default parameter values of  the Tsallis entropy
                          estimator are used).
                      
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MDJT_HT()
        >>> co2 = ite.cost.MDJT_HT(tsallis_co_name='BHTsallis_KnnK',\
                                   alpha=0.8)
        >>> co3 = ite.cost.MDJT_HT(tsallis_co_name='BHTsallis_KnnK',\
                                   tsallis_co_pars={'k':6}, alpha=0.5)
        
        """

        # initialize with 'InitAlpha':
        super().__init__(mult=mult, alpha=alpha)

        # initialize the Tsallis entropy estimator:
        tsallis_co_pars = tsallis_co_pars or {}
        tsallis_co_pars['mult'] = mult  # guarantee this property
        tsallis_co_pars['alpha'] = alpha  # -||-
        self.tsallis_co = co_factory(tsallis_co_name, **tsallis_co_pars)
Пример #27
0
    def __init__(self, mult=True, u=1, js_co_name='MDJS_HS', js_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        u: float, 0 < u, optional
           Parameter of the exponentiated Jensen-Shannon kernel (default
           is 1).
        js_co_name : str, optional 
                     You can change it to any Jensen-Shannon divergence 
                     estimator (default is 'MDJS_HS').
        js_co_pars : dictionary, optional
                     Parameters for the Jensen-Shannnon divergence
                     estimator (default is None (=> {}); in this case the
                     default parameter values of the Jensen-Shannon
                     divergence estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MKExpJS_DJS()
        >>> co2 = ite.cost.MKExpJS_DJS(u=1.2, js_co_name='MDJS_HS')
        
        """

        if u <= 0:
            raise Exception('u has to be positive!')

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the Jensen-Shannon divergence estimator:
        js_co_pars = js_co_pars or {}
        js_co_pars['mult'] = True  # guarantee this property
        js_co_pars['w'] = array([1 / 2, 1 / 2])  # uniform weights
        self.js_co = co_factory(js_co_name, **js_co_pars)

        # other attributes (u):
        self.u = u
Пример #28
0
    def __init__(self, mult=True, alpha=0.99, renyi_co_name='BHRenyi_KnnK',
                 renyi_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
               estimation. 'False': estimation up to 'proportionality'.        
               (default is True)
        alpha : float, alpha \ne 1, optional
                alpha in the Tsallis entropy. (default is 0.99)
        renyi_co_name : str, optional 
                     You can change it to any Renyi entropy estimator.
                     (default is 'BHRenyi_KnnK')
        renyi_co_pars : dictionary, optional
                     Parameters for the Renyi entropy estimator. (default
                     is None (=> {}); in this case the default parameter
                     values of the Renyi entropy estimator are used)

        Examples
        --------
        >>> import ite
        >>> co1 = ite.cost.MHTsallis_HR()
        >>> co2 = ite.cost.MHTsallis_HR(renyi_co_name='BHRenyi_KnnK')
        >>> co3 = ite.cost.MHTsallis_HR(alpha=0.9, \
                                       renyi_co_name='BHRenyi_KnnK')

        >>> dict_ch = {'knn_method': 'cKDTree', 'k': 5, 'eps': 0.1}
        >>> co4 = ite.cost.MHTsallis_HR(alpha=0.9, \
                                       renyi_co_name='BHRenyi_KnnK', \
                                       renyi_co_pars=dict_ch)

        """

        # initialize with 'InitX':
        super().__init__(mult=mult, alpha=alpha)
        
        # initialize the Renyi entropy estimator:
        renyi_co_pars = renyi_co_pars or {}
        renyi_co_pars['mult'] = mult    # guarantee this property
        renyi_co_pars['alpha'] = alpha  # -||-
        self.renyi_co = co_factory(renyi_co_name, **renyi_co_pars)
Пример #29
0
def main():
    # parameters:
    alpha = 0.7  # parameter of Bregman divergence, \ne 1
    dim = 1  # dimension of the distribution
    num_of_samples_v = arange(1000, 10 * 1000 + 1, 1000)
    cost_name = 'BDBregman_KnnK'  # dim >= 1

    # initialization:
    distr = 'uniform'  # fixed
    num_of_samples_max = num_of_samples_v[-1]
    length = len(num_of_samples_v)
    co = co_factory(cost_name, mult=True, alpha=alpha)  # cost object
    d_hat_v = zeros(length)  # vector of estimated divergence values

    # distr, dim -> samples (y1<<y2), distribution parameters (par1,par2),
    # analytical value (d):
    if distr == 'uniform':
        b = 3 * rand(dim)
        a = b * rand(dim)
        y1 = rand(num_of_samples_max, dim) * a  # U[0,a]
        y2 = rand(num_of_samples_max, dim) * b
        # Note: y2 ~ U[0,b], a<=b (coordinate-wise) => y1<<y2

        par1 = {"a": a}
        par2 = {"a": b}
    else:
        raise Exception('Distribution=?')

    d = analytical_value_d_bregman(distr, distr, alpha, par1, par2)

    # estimation:
    for (tk, num_of_samples) in enumerate(num_of_samples_v):
        d_hat_v[tk] = co.estimation(y1[0:num_of_samples],
                                    y2[0:num_of_samples])  # broadcast
        print("tk={0}/{1}".format(tk + 1, length))

    # plot:
    plt.plot(num_of_samples_v, d_hat_v, num_of_samples_v, ones(length) * d)
    plt.xlabel('Number of samples')
    plt.ylabel('Bregman divergence')
    plt.legend(('estimation', 'analytical value'), loc='best')
    plt.title("Estimator: " + cost_name)
    plt.show()
    def __init__(self,
                 mult=True,
                 mmd_co_name='BDMMD_UStat_IChol',
                 mmd_co_pars=None):
        """ Initialize the estimator. 
        
        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the 
                estimation. 'False': estimation up to 'proportionality'.        
                (default is True)
        mmd_co_name : str, optional 
                      You can change it to any MMD estimator (default is 
                      'BDMMD_UStat_IChol').
        mmd_co_pars : dictionary, optional
                      Parameters for the MMD estimator (default is None
                      (=> {}); in this case the default parameter values
                      of the MMD estimator are used).
                     
        Examples
        --------
        >>> import ite
        >>> from ite.cost.x_kernel import Kernel
        >>> co1 = ite.cost.MDEnergyDist_DMMD()
        >>> co2 =\
                ite.cost.MDEnergyDist_DMMD(mmd_co_name='BDMMD_UStat_IChol')
        >>> dict_ch = {'kernel': \
                       Kernel({'name': 'RBF','sigma': 0.1}), 'eta': 1e-2}
        >>> co3 =\
               ite.cost.MDEnergyDist_DMMD(mmd_co_name='BDMMD_UStat_IChol',\
                                          mmd_co_pars=dict_ch)
        
        """

        # initialize with 'InitX':
        super().__init__(mult=mult)

        # initialize the MMD estimator:
        mmd_co_pars = mmd_co_pars or {}
        mmd_co_pars['mult'] = mult  # guarantee this property
        self.mmd_co = co_factory(mmd_co_name, **mmd_co_pars)