Exemplo n.º 1
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:
    num_of_samples = 1000
    dim = 2
    eta_v = power(10.0, range(-8, 0))  # 10^{-8}, 10^{-7}, ..., 10^{-1}

    # define a kernel:
    k = Kernel({'name': 'RBF', 'sigma': 1})
    # k = Kernel({'name': 'exponential', 'sigma': 1})
    # k = Kernel({'name': 'Cauchy', 'sigma': 1})
    # k = Kernel({'name': 'student', 'd': 1})
    # k = Kernel({'name': 'Matern3p2', 'l': 1})
    # k = Kernel({'name': 'Matern5p2', 'l': 1})
    # k = Kernel({'name': 'polynomial', 'exponent': 2, 'c': 1})
    # k = Kernel({'name': 'ratquadr', 'c': 1})
    # k = Kernel({'name': 'invmquadr', 'c': 1})

    # print(k)  # print the picked kernel

    # initialization:
    length = len(eta_v)
    error_v = zeros(length)  # vector of estimated entropy values

    # define a dataset:
    y = randn(num_of_samples, dim)

    # true Gram matrix:
    gram_matrix = k.gram_matrix1(y)

    for (tk, eta) in enumerate(eta_v):
        tol = eta * num_of_samples
        r = k.ichol(y, tol)
        gram_matrix_hat = dot(r, r.T)
        dim_red = r.shape[1]
        error_v[tk] = norm(gram_matrix - gram_matrix_hat) / \
                      norm(gram_matrix)
        print("tk={0}/{1}, log10(eta):{2}, dimension (reduced/original): "
              "{3}/{4}".format(tk + 1, length, log10(eta), dim_red,
                               num_of_samples))

    # plot:
    plt.plot(eta_v, error_v)
    plt.xlabel('Tolerance (eta)')
    plt.ylabel('Relative error in the incomplete Cholesky decomposition')
    plt.xscale('log')
    plt.show()
Exemplo n.º 3
0
    def __init__(self, mult=True, kernel=Kernel()):
        """ Initialize the estimator.

        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the
               estimation. 'False': estimation up to 'proportionality'.
               (default is True)
        kernel : Kernel, optional
                 For examples, see 'ite.cost.x_kernel.Kernel'


        """

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

        # other attributes:
        self.kernel = kernel
Exemplo n.º 4
0
    def __init__(self, mult=True, kernel=Kernel(), eta=1e-2):
        """ Initialize the estimator.

        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the
               estimation. 'False': estimation up to 'proportionality'.
               (default is True)
        kernel : Kernel, optional
                 For examples, see 'ite.cost.x_kernel.Kernel'
        eta : float, >0, optional
              It is used to control the quality of the incomplete Cholesky
              decomposition based Gram matrix approximation. Smaller 'eta'
              means larger-sized Gram factor and better approximation.
              (default is 1e-2)
        """

        # initialize with 'InitKernel':
        super().__init__(mult=mult, kernel=kernel)

        # other attributes:
        self.eta = eta
Exemplo n.º 5
0
    def __init__(self, mult=True, kernel=Kernel(), eta=1e-2, kappa=0.01):
        """ Initialize the estimator.

        Parameters
        ----------
        mult : bool, optional
               'True': multiplicative constant relevant (needed) in the
               estimation. 'False': estimation up to 'proportionality'.
               (default is True)
        kernel : Kernel, optional
                 For examples, see 'ite.cost.x_kernel.Kernel'
        eta : float, >0, optional
              It is used to control the quality of the incomplete Cholesky
              decomposition based Gram matrix approximation. Smaller 'eta'
              means larger sized Gram factor and better approximation.
              (default is 1e-2)
        kappa: float, >0
               Regularization parameter.

        Examples
        --------
        >>> import ite
        >>> from ite.cost.x_kernel import Kernel
        >>> co1 = ite.cost.BIKCCA()
        >>> co2 = ite.cost.BIKCCA(eta=1e-4)
        >>> co3 = ite.cost.BIKCCA(eta=1e-4, kappa=0.02)
        >>> k =  Kernel({'name': 'RBF', 'sigma': 0.3})
        >>> co4 = ite.cost.BIKCCA(eta=1e-4, kernel=k)

        """

        # initialize with 'InitEtaKernel':
        super().__init__(mult=mult, kernel=kernel, eta=eta)

        # other attributes:
        self.kappa = kappa
 def com_kernel(self):
     k2 = Kernel({'name': 'RBF','sigma': self.theta})
     self.co = ite.cost.BKExpected(kernel=k2)     #此处参考ite的用法
     return self.co