Exemple #1
0
    def K(self, logtheta, *args):
        '''K(params,x1) - params = [factor,length-scale(s)]'''
        # additional index: one index after dimension: derivative index!
        x1 = args[0][:, 0:self.dimension]
        #noise_diag comes from the last input dimension:
        noise_diag = args[0][:, -1]
        if (len(args) == 1):
            x2 = x1
            noise = diag(noise_diag) * (exp(2 * logtheta[-1]) + 1E-6)
        else:
            x2 = args[1][:, 0:self.dimension]
            noise = 0
        # 2. exponentiate params:
        V0 = exp(2 * logtheta[0])
        L = exp(logtheta[1:1 + self.dimension])

        d1 = x1[:, -1::]
        d2 = x2[:, -1::]
        x1 = x1[:, 0:self.dimension] / L
        x2 = x2[:, 0:self.dimension] / L

        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1, x2)
        # sq. distance is neede anyway:
        sqd = dd * dd
        sqd = sqd.sum(axis=2)

        #3. calcualte withotu derivatives, need this anyway:
        rv = V0 * exp(-0.5 * sqd) + noise

        return rv
Exemple #2
0
    def K(self, logtheta, *args):
        '''K(params,x1) - params = [factor,length-scale(s),noise(per class)]'''
        # additional index: one index after dimension: derivative index!
        x1 = args[0][:, 0:self.dimension]
        #cluster index is in the next index, convert to int to allow indexing
        cluster = array(args[0][:, self.dimension], dtype='int')
        if (len(args) == 1):
            x2 = x1
            #noise depends on the cluster memebership:
            noise = (exp(2 * logtheta[-self.clusters + cluster]) + 1E-2)
        else:
            x2 = args[1][:, 0:self.dimension]
            noise = [0]
        # 2. exponentiate params:
        V0 = exp(2 * logtheta[0])
        L = exp(logtheta[1:1 + self.dimension])

        d1 = x1[:, -1::]
        d2 = x2[:, -1::]
        x1 = x1[:, 0:self.dimension] / L
        x2 = x2[:, 0:self.dimension] / L

        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1, x2)
        # sq. distance is neede anyway:
        sqd = dd * dd
        sqd = sqd.sum(axis=2)

        #3. calcualte withotu derivatives, need this anyway:
        rv = V0 * exp(-0.5 * sqd) + diag(noise)
        return rv
Exemple #3
0
 def K(self, logtheta, *args):
     '''K(params,x1) - params = [factor,length-scale(s),noise(per class)]'''
     # additional index: one index after dimension: derivative index!
     x1      = args[0][:,0:self.dimension]
     #cluster index is in the next index, convert to int to allow indexing
     cluster = array(args[0][:,self.dimension],dtype='int')
     if(len(args)==1):
         x2 = x1
         #noise depends on the cluster memebership:
         noise=(exp(2*logtheta[-self.clusters+cluster])+1E-2)
     else:
         x2 = args[1][:,0:self.dimension]
         noise = [0]
     # 2. exponentiate params:
     V0 = exp(2*logtheta[0])
     L  = exp(logtheta[1:1+self.dimension])
     
     d1 = x1[:,-1::]
     d2 = x2[:,-1::]
     x1 = x1[:,0:self.dimension]/L
     x2 = x2[:,0:self.dimension]/L
     
     # calculate the distance betwen x1,x2 for each dimension separately.
     dd = sqdist.dist(x1,x2)
     # sq. distance is neede anyway:
     sqd = dd*dd
     sqd = sqd.sum(axis=2)
    
     #3. calcualte withotu derivatives, need this anyway:
     rv = V0*exp(-0.5*sqd) + diag(noise)
     return rv
Exemple #4
0
 def K(self, logtheta, *args):
     '''K(params,x1) - params = [factor,length-scale(s)]'''
     # additional index: one index after dimension: derivative index!
     x1 = args[0][:,0:self.dimension]
     #noise_diag comes from the last input dimension:
     noise_diag = args[0][:,-1]
     if(len(args)==1):
         x2 = x1
         noise=diag(noise_diag)*(exp(2*logtheta[-1])+1E-6)
     else:
        x2 = args[1][:,0:self.dimension]
        noise =0
     # 2. exponentiate params:
     V0 = exp(2*logtheta[0])
     L  = exp(logtheta[1:1+self.dimension])
     
     d1 = x1[:,-1::]
     d2 = x2[:,-1::]
     x1 = x1[:,0:self.dimension]/L
     x2 = x2[:,0:self.dimension]/L
     
     # calculate the distance betwen x1,x2 for each dimension separately.
     dd = sqdist.dist(x1,x2)
     # sq. distance is neede anyway:
     sqd = dd*dd
     sqd = sqd.sum(axis=2)
    
     #3. calcualte withotu derivatives, need this anyway:
     rv = V0*exp(-0.5*sqd) + noise
                       
     
     return rv
Exemple #5
0
    def K(self, logtheta, *args):
        '''K(params,x1) - params = [factor,length-scale(s)]'''
        # additional index: one index after dimension: derivative index!
        x1 = args[0][:, 0:self.dimension + 1]
        if (len(args) == 1):
            x2 = x1
        else:
            x2 = args[1][:, 0:self.dimension + 1]
        # 2. exponentiate params:
        V0 = exp(2 * logtheta[0])
        L = exp(logtheta[1:1 + self.dimension])

        d1 = x1[:, -1::]
        d2 = x2[:, -1::]
        x1 = x1[:, 0:self.dimension] / L
        x2 = x2[:, 0:self.dimension] / L

        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1, x2)
        # sq. distance is neede anyway:
        sqd = dd * dd
        sqd = sqd.sum(axis=2)

        #3. calcualte withotu derivatives, need this anyway:
        rv = V0 * exp(-0.5 * sqd)

        #4. go through all other combinations and calc the corresponding terms:
        def k_(d1):
            rv = dd[:, :, d1] / L[d1]
            return rv

        def k__(d1, d2):
            f1 = dd[:, :, d1] / L[d1]
            f2 = dd[:, :, d2] / L[d2]
            return ((d1 == d2) / (L[d1]**2) - f1 * f2)

        for i in arange(-1, self.dimension):
            for j in arange(-1, self.dimension):
                if i == j == -1:
                    continue
                index = Bdist(d1 == i, d2 == j)
                if not any(index):
                    continue
                if ((i == -1) & (j != -1)):
                    rv[index] *= -1 * k_(j)[index]
                elif ((i != -1) & (j == -1)):
                    rv[index] *= k_(i)[index]
                else:
                    rv[index] *= k__(i, j)[index]

        if (len(args) == 1
            ):  # add noise if we do not have two independen input data-sets
            rv += eye(len(x1)) * exp(2 * logtheta[-1])
        return rv
Exemple #6
0
 def K(self, logtheta, *args):
     '''K(params,x1) - params = [factor,length-scale(s)]'''
     # additional index: one index after dimension: derivative index!
     x1 = args[0][:,0:self.dimension+1]
     if(len(args)==1):
         x2 = x1
     else:
        x2 = args[1][:,0:self.dimension+1]
     # 2. exponentiate params:
     V0 = exp(2*logtheta[0])
     L  = exp(logtheta[1:1+self.dimension])
     
     d1 = x1[:,-1::]
     d2 = x2[:,-1::]
     x1 = x1[:,0:self.dimension]/L
     x2 = x2[:,0:self.dimension]/L
     
     # calculate the distance betwen x1,x2 for each dimension separately.
     dd = sqdist.dist(x1,x2)
     # sq. distance is neede anyway:
     sqd = dd*dd
     sqd = sqd.sum(axis=2)
    
     #3. calcualte withotu derivatives, need this anyway:
     rv = V0*exp(-0.5*sqd)
                       
     #4. go through all other combinations and calc the corresponding terms:
     def k_(d1):
         rv = dd[:,:,d1]/L[d1]
         return rv
     def k__(d1,d2):
         f1 = dd[:,:,d1]/L[d1]
         f2 = dd[:,:,d2]/L[d2]
         return ((d1==d2)/(L[d1]**2) - f1*f2)
     for i in arange(-1,self.dimension):
         for j in arange(-1,self.dimension):
             if i==j==-1:
                 continue
             index = Bdist(d1==i,d2==j)
             if not any(index):
                 continue
             if((i==-1) & (j!=-1)):
                 rv[index] *= -1*k_(j)[index]
             elif((i!=-1) &(j==-1)):
                 rv[index] *= k_(i)[index]
             else:
                 rv[index] *= k__(i,j)[index]
     
     if(len(args)==1):                                     # add noise if we do not have two independen input data-sets
         rv+=eye(len(x1))*exp(2*logtheta[-1])
     return rv
Exemple #7
0
    def Kd(self, logtheta, *args):
        '''Kd(params,x1) - params = [factor,length-scale(s)]'''
                # additional index: one index after dimension: derivative index!
        x1 = args[0][:,0:self.dimension]
        cluster = array(args[0][:,self.dimension],dtype='int')
        if(len(args)==1):
            x2 = x1
            noise=ones(len(x1))*exp(2*logtheta[-self.clusters+cluster]+1E-2)
        else:
           x2 = args[1][:,0:self.dimension]
           noise =zeros(len(x1))

        # 2. exponentiate params:
        V0 = exp(2*logtheta[0])
        L  = exp(logtheta[1:1+self.dimension])
        
        d1 = x1[:,-1::]
        d2 = x2[:,-1::]
        x1 = x1[:,0:self.dimension]/L
        x2 = x2[:,0:self.dimension]/L
        
        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1,x2)
        # sq. distance is neede anyway:
        sqd = dd*dd
        sqdd = sqd.transpose(2,0,1)
        sqd = sqd.sum(axis=2)
       
        #3. calcualte withotu derivatives, need this anyway:
        rv0 = V0*exp(-0.5*sqd)
        
       
        
        
        
        rv = zeros((self.n_params,len(x1),len(x2)))
        
        #3. calcualte without derivatives, need this anyway:
        rv[:] = V0*exp(-0.5*sqd)
        #amplitude:
        rv[0] = rv[0]*2
        #lengthscales:
        rv[1:1+self.dimension] *= sqdd
        #noise:
        for ic in range(self.clusters):
            Ic = (cluster==ic)
            diagonal = zeros(x1.shape[0])
            diagonal[Ic] = 2*noise[Ic]
            rv[-self.clusters+ic][:,:] = diag(diagonal)
        return rv
Exemple #8
0
    def Kd(self, logtheta, *args):
        '''Kd(params,x1) - params = [factor,length-scale(s)]'''
        # additional index: one index after dimension: derivative index!
        x1 = args[0][:, 0:self.dimension]
        cluster = array(args[0][:, self.dimension], dtype='int')
        if (len(args) == 1):
            x2 = x1
            noise = ones(
                len(x1)) * exp(2 * logtheta[-self.clusters + cluster] + 1E-2)
        else:
            x2 = args[1][:, 0:self.dimension]
            noise = zeros(len(x1))

        # 2. exponentiate params:
        V0 = exp(2 * logtheta[0])
        L = exp(logtheta[1:1 + self.dimension])

        d1 = x1[:, -1::]
        d2 = x2[:, -1::]
        x1 = x1[:, 0:self.dimension] / L
        x2 = x2[:, 0:self.dimension] / L

        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1, x2)
        # sq. distance is neede anyway:
        sqd = dd * dd
        sqdd = sqd.transpose(2, 0, 1)
        sqd = sqd.sum(axis=2)

        #3. calcualte withotu derivatives, need this anyway:
        rv0 = V0 * exp(-0.5 * sqd)

        rv = zeros((self.n_params, len(x1), len(x2)))

        #3. calcualte without derivatives, need this anyway:
        rv[:] = V0 * exp(-0.5 * sqd)
        #amplitude:
        rv[0] = rv[0] * 2
        #lengthscales:
        rv[1:1 + self.dimension] *= sqdd
        #noise:
        for ic in range(self.clusters):
            Ic = (cluster == ic)
            diagonal = zeros(x1.shape[0])
            diagonal[Ic] = 2 * noise[Ic]
            rv[-self.clusters + ic][:, :] = diag(diagonal)
        return rv
Exemple #9
0
 def Kd(self, logtheta, *args):
     '''Kd(params,x1) - params = [factor,length-scale(s)]'''
             # additional index: one index after dimension: derivative index!
     x1 = args[0][:,0:self.dimension]
     noise_diag = args[0][:,-1]
     if(len(args)==1):
         x2 = x1
         noise=diag(noise_diag)*exp(2*logtheta[-1])
     else:
        x2 = args[1][:,0:self.dimension]
        noise =eye(len(x1))*0
     # 2. exponentiate params:
     V0 = exp(2*logtheta[0])
     L  = exp(logtheta[1:1+self.dimension])
     
     d1 = x1[:,-1::]
     d2 = x2[:,-1::]
     x1 = x1[:,0:self.dimension]/L
     x2 = x2[:,0:self.dimension]/L
     
     # calculate the distance betwen x1,x2 for each dimension separately.
     dd = sqdist.dist(x1,x2)
     # sq. distance is neede anyway:
     sqd = dd*dd
     sqdd = sqd.transpose(2,0,1)
     sqd = sqd.sum(axis=2)
    
     #3. calcualte withotu derivatives, need this anyway:
     rv0 = V0*exp(-0.5*sqd)
     
    
     
     
     
     rv = zeros((self.n_params,len(x1),len(x2)))
     
     #3. calcualte without derivatives, need this anyway:
     rv[:] = V0*exp(-0.5*sqd)
     #amplitude:
     rv[0] = rv[0]*2
     #lengthscales:
     rv[1:1+self.dimension] *= sqdd
     #noise:
     rv[-1] = 2*noise
     
     return rv
Exemple #10
0
    def Kd(self, logtheta, *args):
        '''Kd(params,x1) - params = [factor,length-scale(s)]'''
        # additional index: one index after dimension: derivative index!
        x1 = args[0][:, 0:self.dimension]
        noise_diag = args[0][:, -1]
        if (len(args) == 1):
            x2 = x1
            noise = diag(noise_diag) * exp(2 * logtheta[-1])
        else:
            x2 = args[1][:, 0:self.dimension]
            noise = eye(len(x1)) * 0
        # 2. exponentiate params:
        V0 = exp(2 * logtheta[0])
        L = exp(logtheta[1:1 + self.dimension])

        d1 = x1[:, -1::]
        d2 = x2[:, -1::]
        x1 = x1[:, 0:self.dimension] / L
        x2 = x2[:, 0:self.dimension] / L

        # calculate the distance betwen x1,x2 for each dimension separately.
        dd = sqdist.dist(x1, x2)
        # sq. distance is neede anyway:
        sqd = dd * dd
        sqdd = sqd.transpose(2, 0, 1)
        sqd = sqd.sum(axis=2)

        #3. calcualte withotu derivatives, need this anyway:
        rv0 = V0 * exp(-0.5 * sqd)

        rv = zeros((self.n_params, len(x1), len(x2)))

        #3. calcualte without derivatives, need this anyway:
        rv[:] = V0 * exp(-0.5 * sqd)
        #amplitude:
        rv[0] = rv[0] * 2
        #lengthscales:
        rv[1:1 + self.dimension] *= sqdd
        #noise:
        rv[-1] = 2 * noise

        return rv
Exemple #11
0
 def dist(self, x1, x2, L):
     """Distance function"""
     x1 = array(x1, dtype='float64') / L
     x2 = array(x2, dtype='float64') / L
     return sqdist.dist(x1, x2)
Exemple #12
0
 def dist(self,x1,x2,L):
     """Distance function"""
     x1 = array(x1,dtype='float64')/L
     x2 = array(x2,dtype='float64')/L
     return sqdist.dist(x1,x2)