Esempio n. 1
0
def p_b_wins(strategy, scorea = 0):
	# Equal to P(b<a).
	Ts = strategy.Ts
	targets = strategy.targets
	distr = None
	for T, target, i in zip(Ts, targets, itertools.count()):
		distrT = scipy.array([[p_x_tries(x, T, target) for x in xs]])
		if distr is not None:
			table = scipy.fliplr(scipy.dot(distr.T, distrT))
			distr = scipy.zeros((1, 2*MAX_X))
			for offseti, offset in enumerate(xrange(-(MAX_X-1), MAX_X)):
				distr[0, -offseti-1] = scipy.trace(table, offset)
			distr = distr[:,:MAX_X]
		else:
			distr = distrT
		assert distr[0,:i].sum() == 0

	E = (xs * distr[0,:]).sum()
	#pylab.bar(xs, distr[0,:]); pylab.show()
	distra = scipy.array([[p_x_tries(x, 1, 100 - scorea) for x in xs]])
	m = scipy.dot(distra.T, distr)
	p = 0.
	for offseti, offset in enumerate(xrange(-(MAX_X-1), 0)):
		p += scipy.trace(m, offset)

	return p
Esempio n. 2
0
def p_b_wins():
	# Equal to P(b<a).
	T=6; distrb3 = scipy.array([[p_x_tries(x, T, payoff(T)) for x in xs]])
	T=7; distrb6 = scipy.array([[p_x_tries(x, T, payoff(T)) for x in xs]])
	T=3; distrb7 = scipy.array([[p_x_tries(x, T, payoff(T)) for x in xs]])

	table36 = scipy.fliplr(scipy.dot(distrb3.T, distrb6))
	distr36 = scipy.zeros((1, 2*MAX_X)) # First element should be 0.
	for offseti, offset in enumerate(xrange(-(MAX_X-1), MAX_X)):
		distr36[0,-offseti-1] = scipy.trace(table36, offset)
	del table36
	distr36 = distr36[:,:MAX_X]
	assert distr36[0,0] == 0

	table367 = scipy.fliplr(scipy.dot(distr36.T, distrb7))
	del distr36
	distr367 = scipy.zeros((1, 2*MAX_X)) # First two elements should be 0.
	for offseti, offset in enumerate(xrange(-(MAX_X-1), MAX_X)):
		distr367[0,-offseti-1] = scipy.trace(table367, offset)
	del table367
	distr367 = distr367[:,:MAX_X]
	assert distr367[0,0] == 0 and distr367[0,1] == 0

	#pylab.bar(xs, distr367[0,:]); pylab.show()
	m = scipy.dot(distra.T, distr367)
	p = 0.
	for offseti, offset in enumerate(xrange(-(MAX_X-1), 0)):
		p += scipy.trace(m, offset)

	return p
Esempio n. 3
0
 def _getFisher(self):
     """
     Return the fisher information matrix over the parameters
     TO CHECK: IT DOES NOT WORK PROPERLY
     """
     Ctot = self.vd.getGP().getCovar()
     Ki = sp.linalg.inv(Ctot.K())
     n_scales = self.vd.getNumberScales()
     out = sp.zeros((n_scales,n_scales))
     for m in range(n_scales):
         out[m,m] = 0.5 * sp.trace(sp.dot(Ki,sp.dot(Ctot.Kgrad_param(m),sp.dot(Ki,Ctot.Kgrad_param(m)))))
         for n in range(m):
             out[m,n]=0.5 * sp.trace(sp.dot(Ki,sp.dot(Ctot.Kgrad_param(m),sp.dot(Ki,Ctot.Kgrad_param(n)))))
             out[n,m]=out[m,n]
     return out
Esempio n. 4
0
def jitEigh(A,maxTries=10,warning=True):
    """
    Do a Eigenvalue Decompsition with Jitter,

    works as jitChol
    """
    warning = True
    jitter = 0
    i = 0

    while(True):
        if jitter == 0:
            jitter = abs(SP.trace(A))/A.shape[0]*1e-6
            S,U = linalg.eigh(A)

        else:
            if warning:
                # pdb.set_trace()
		# plt.figure()
		# plt.imshow(A, interpolation="nearest")
		# plt.colorbar()
		# plt.show()
                logging.error("Adding jitter of %f in jitEigh()." % jitter)
            S,U = linalg.eigh(A+jitter*SP.eye(A.shape[0]))

        if S.min()>1E-10:
            return S,U
            
        if i<maxTries:
            jitter = jitter*10
        i += 1
            
    raise linalg.LinAlgError, "Matrix non positive definite, jitter of " +  str(jitter) + " added but failed after " + str(i) + " trials."
Esempio n. 5
0
def jitEigh(A,maxTries=10,warning=True):
    """
    Do a Eigenvalue Decompsition with Jitter,

    works as jitChol
    """
    warning = True
    jitter = 0
    i = 0

    while(True):
        try:
            if jitter == 0:
                jitter = abs(SP.trace(A))/A.shape[0]*1e-6
                S,U = linalg.eigh(A)

            else:
                logging.error("Adding jitter of %f in jitEigh()." % jitter)
                S,U = linalg.eigh(A+jitter*SP.eye(A.shape[0]))

            if S.min()>1E-10:
                return S,U
        
        except:
            pass
            
        if i<maxTries:
            jitter = jitter*10
        i += 1
            
    raise linalg.LinAlgError, "Matrix non positive definite, jitter of " +  str(jitter) + " added but failed after " + str(i) + " trials."
Esempio n. 6
0
 def check_RCF(self):
     """Tests for right canonical form.
     
     Uses the criteria listed in sub-section 3.1, theorem 1 of arXiv:quant-ph/0608197v2.
     
     This is a consistency check mainly intended for debugging purposes.
     
     FIXME: The tolerances appear to be too tight!
     
     Returns
     -------
     (rnsOK, ls_trOK, ls_pos, ls_diag, normOK) : tuple of bool
         rnsOK: Right orthonormalization is fullfilled (self.r[n] = eye)
         ls_trOK: all self.l[n] have trace 1
         ls_pos: all self.l[n] are positive-definite
         ls_diag: all self.l[n] are diagonal
         normOK: the state it normalized
     """
     rnsOK = True
     ls_trOK = True
     ls_herm = True
     ls_pos = True
     ls_diag = True
     
     for n in xrange(1, self.N + 1):
         rnsOK = rnsOK and sp.allclose(self.r[n], sp.eye(self.r[n].shape[0]), atol=self.eps*2, rtol=0)
         ls_herm = ls_herm and sp.allclose(self.l[n] - m.H(self.l[n]), 0, atol=self.eps*2)
         ls_trOK = ls_trOK and sp.allclose(sp.trace(self.l[n]), 1, atol=self.eps*1000, rtol=0)
         ls_pos = ls_pos and all(la.eigvalsh(self.l[n]) > 0)
         ls_diag = ls_diag and sp.allclose(self.l[n], sp.diag(self.l[n].diagonal()))
     
     normOK = sp.allclose(self.l[self.N], 1., atol=self.eps*1000, rtol=0)
     
     return (rnsOK, ls_trOK, ls_pos, ls_diag, normOK)
 def grad_f0(self, a):
     "Compute the gradient of the objective function: -log(det(A(a)))."
     A = self.get_A(a)
     Ainv = la.inv(A)
     E = sp.dot(Ainv, self.D).transpose(1,0,2)
     out = -sp.trace(E, axis1=1, axis2=2)
     return out
Esempio n. 8
0
    def _learnStep(self):
        """ Main part of the algorithm. """
        I = eye(self.numParameters)
        self._produceSamples()
        utilities = self.shapingFunction(self._currentEvaluations)
        utilities /= sum(utilities)  # make the utilities sum to 1
        if self.uniformBaseline:
            utilities -= 1./self.batchSize
        samples = array(map(self._base2sample, self._population))

        dCenter = dot(samples.T, utilities)
        covGradient = dot(array([outer(s,s) - I for s in samples]).T, utilities)
        covTrace = trace(covGradient)
        covGradient -= covTrace/self.numParameters * I
        dA = 0.5 * (self.scaleLearningRate * covTrace/self.numParameters * I
                    +self.covLearningRate * covGradient)

        self._lastLogDetA = self._logDetA
        self._lastInvA = self._invA

        self._center += self.centerLearningRate * dot(self._A, dCenter)
        self._A = dot(self._A, expm2(dA))
        self._invA = dot(expm2(-dA), self._invA)
        self._logDetA += 0.5 * self.scaleLearningRate * covTrace
        if self.storeAllDistributions:
            self._allDistributions.append((self._center.copy(), self._A.copy()))
def _van_rossum_multiunit_dist_for_trial_pair(a, b, weighting, tau, kernel):
    if kernel is None:
        spike_counts = sp.atleast_2d([st.size for st in a + b])
        k_dist = spike_counts.T * (spike_counts - spike_counts.T)
    else:
        k_dist = kernel.summed_dist_matrix(a + b)

    non_diagonal = sp.logical_not(sp.eye(len(a)))
    summed_population = (
        sp.trace(k_dist) - sp.trace(k_dist, len(a)) - sp.trace(k_dist, -len(a)))
    labeled_line = (
        sp.sum(k_dist[:len(a), :len(a)][non_diagonal]) +
        sp.sum(k_dist[len(a):, len(a):][non_diagonal]) -
        sp.sum(k_dist[:len(a), len(a):][non_diagonal]) -
        sp.sum(k_dist[len(a):, :len(a)][non_diagonal]))
    return sp.sqrt(summed_population + weighting * labeled_line)
Esempio n. 10
0
    def check_RCF(self):
        """Tests for right canonical form.
        Uses the criteria listed in sub-section 3.1, theorem 1 of arXiv:quant-ph/0608197v2.
        """
        rnsOK = True
        ls_trOK = True
        ls_herm = True
        ls_pos = True
        ls_diag = True

        for n in xrange(1, self.N + 1):
            rnsOK = rnsOK and sp.allclose(
                self.r[n],
                sp.eye(self.r[n].shape[0]),
                atol=self.eps * 2,
                rtol=0)
            ls_herm = ls_herm and sp.allclose(
                self.l[n] - m.H(self.l[n]), 0, atol=self.eps * 2)
            ls_trOK = ls_trOK and sp.allclose(
                sp.trace(self.l[n]), 1, atol=self.eps * 2, rtol=0)
            ls_pos = ls_pos and all(la.eigvalsh(self.l[n]) > 0)
            ls_diag = ls_diag and sp.allclose(self.l[n],
                                              sp.diag(self.l[n].diagonal()))

        normOK = sp.allclose(self.l[self.N], 1., atol=self.eps, rtol=0)

        return (rnsOK, ls_trOK, ls_pos, ls_diag, normOK)
Esempio n. 11
0
def jitChol(A, maxTries=10, warning=True):

    """Do a Cholesky decomposition with jitter.
    
    Description:
    
    
    U, jitter = jitChol(A, maxTries, warning) attempts a Cholesky
     decomposition on the given matrix, if matrix isn't positive
     definite the function adds 'jitter' and tries again. Thereafter
     the amount of jitter is multiplied by 10 each time it is added
     again. This is continued for a maximum of 10 times.  The amount of
     jitter added is returned.
     Returns:
      U - the Cholesky decomposition for the matrix.
      jitter - the amount of jitter that was added to the matrix.
     Arguments:
      A - the matrix for which the Cholesky decomposition is required.
      maxTries - the maximum number of times that jitter is added before
       giving up (default 10).
      warning - whether to give a warning for adding jitter (default is True)

    See also
    CHOL, PDINV, LOGDET


    Copyright (c) 2005, 2006 Neil D. Lawrence
    
    """
    warning = True
    jitter = 0
    i = 0

    while(True):
        try:
            # Try --- need to check A is positive definite
            if jitter == 0:
                jitter = abs(SP.trace(A))/A.shape[0]*1e-6
                LC = linalg.cholesky(A, lower=True)
                return LC.T, 0.0
            else:
                if warning:
                    # pdb.set_trace()
		    # plt.figure()
		    # plt.imshow(A, interpolation="nearest")
		    # plt.colorbar()
		    # plt.show()
                    logging.error("Adding jitter of %f in jitChol()." % jitter)
                LC = linalg.cholesky(A+jitter*SP.eye(A.shape[0]), lower=True)

                return LC.T, jitter
        except linalg.LinAlgError:
            # Seems to have been non-positive definite.
            if i<maxTries:
                jitter = jitter*10
            else:
                raise linalg.LinAlgError, "Matrix non positive definite, jitter of " +  str(jitter) + " added but failed after " + str(i) + " trials."
        i += 1
    return LC
Esempio n. 12
0
def grassmann_logmap(A,p, tol=1e-13, skip_orthog_check=False):
    '''
    Computes the manifold log-map of (nxk) orthogonal matrix A,
    centered at the point p (i.e. the "pole"), which is also an
    (nxk) orthogonal matrix.
    The log-map takes a point on the manifold and maps it to the
    tangent space which is centered at a given pole.
    The dimension of the tangent space is k(n-k), 
    and points A,p are on Gr(n,k).
    @param A: The orthogonal matrix A, representing a point on
    the grassmann manifold.
    @param p: An orthogonal matrix p, representing a point on
    the grassmann manifold where the tangent space will be formed.
    Also called the "pole".
    @param tol: Numerical tolerance used to set singular values
    to exactly zero when within this tolerance of zero.
    @param skip_orthog_check: Set to True if you can guarantee
    that the inputs are already orthogonal matrices. Otherwise,
    this function will check, and if A and/or p are not orthogonal,
    the closest orthogonal matrix to A (or p) will be used.
    @return: A tuple (log_p(A), ||log_p(A)|| ), representing
    the tangent-space mapping of A, and the distance from the
    mapping of A to the pole in the tangent space.
    '''
    
    #check that A and p are orthogonal, if
    # not, then compute orthogonal representations and
    # send back a warning message.
    if not skip_orthog_check:
        if not isOrthogonal(A):
            print "WARNING: You are calling grassmann_logmap function on non-orthogonal input matrix A"
            print "(This function will compute an orthogonal representation for A using an SVD.)"
            A = closestOrthogonal(A)
        if not isOrthogonal(p):
            print "WARNING: You are calling grassmann_logmap function on non-orthogonal pole p."
            print "(This function will compute an orthogonal representation for p using an SVD.)"
            p = closestOrthogonal(p)
    
    #p_perp is the orthogonal complement to p, = null(p.T)
    p_perp = nullspace(p.T)
    
    #compute p_perp * p_perp.T * A * inv(p.T * A)
    T = sp.dot(p.T,A)
    try:
        Tinv = LA.inv(T)
    except(LA.LinAlgError):
        Tinv = LA.pinv(T)
        
    X = sp.dot( sp.dot( sp.dot(p_perp,p_perp.T), A), Tinv )
    
    u, s, vh = LA.svd(X, full_matrices=False)
    s[ s < tol ]= 0   #set extremely small values to zero
    theta = sp.diag( sp.arctan(s) )
    
    logA = sp.dot(u, sp.dot( theta,vh))    
    normA = sp.trace( sp.dot(logA.T, logA) )
    
    return logA, normA
Esempio n. 13
0
def covar_rescaling_factor(C):
    """
    Returns the rescaling factor for the Gower normalizion on covariance matrix C
    the rescaled covariance matrix has sample variance of 1
    """
    n = C.shape[0]
    P = sp.eye(n) - sp.ones((n,n))/float(n)
    trPCP = sp.trace(sp.dot(P,sp.dot(C,P)))
    r = (n-1) / trPCP
    return r
Esempio n. 14
0
def fullrho(qu, squ):
    print("#Starting full density matrix simulation!")
    
    #Build state from pure states received from the MPS processes
    rho = sp.zeros((2**N, 2**N), dtype=sp.complex128)
    psis = [None] * N_samp
    for n in range(N_samp):
        pnum, psi = squ.get()
        psis[pnum] = psi
        rho += sp.outer(psi, psi.conj())
        squ.task_done()
        
    rho /= sp.trace(rho)

    Hfull = get_full_op(get_ham(N, lam))

    linds = get_linds(N, eps)
    linds = [(n, L.reshape(tuple([sp.prod(L.shape[:sp.ndim(L)/2])]*2))) for (n, L) in linds]
    linds_full = [sp.kron(sp.eye(2**(n-1)), sp.kron(L, sp.eye(2**(N - n + 1) / L.shape[0]))) for (n, L) in linds]
    for L in linds_full:
        assert L.shape == Hfull.shape

    Qfull = -1.j * Hfull - 0.5 * sp.sum([L.conj().T.dot(L) for L in linds_full], axis=0)

    szs = [None] + [sp.kron(sp.kron(sp.eye(2**(n - 1)), Sz), sp.eye(2**(N - n))) for n in range(1, N + 1)]
    
    for i in range(N_steps + 1):
        rho /= sp.trace(rho)
        esz = []
        for n in range(1, N + 1):
            esz.append(sp.trace(szs[n].dot(rho)).real)

        if i % res_every == 0:
            if qu is None:
                print(esz)
            else:
                qu.put([-1, i, esz])
                qu.put([-2, i, [sp.NaN] * N]) #this slot is reserved for a second "exact" result
        
        #Do Euler steps, approximately integrating the Lindblad master equation
        rho += dt * (Qfull.dot(rho) + rho.dot(Qfull.conj().T) +
                       sum([L.dot(rho).dot(L.conj().T) for L in linds_full]))
Esempio n. 15
0
 def _fit(self, type, vc=False):
     #2. init
     if type=='null':
         self.gp[type].covar.Cg.setCovariance(0.5*self.covY)
         self.gp[type].covar.Cn.setCovariance(0.5*self.covY)
     elif type=='full':
         Cg0_K = self.gp['null'].covar.Cg.K()
         Cn0_K = self.gp['null'].covar.Cn.K()
         self.gp[type].covar.Cr.setCovariance((Cn0_K+Cg0_K)/3.)
         self.gp[type].covar.Cg.setCovariance(2.*Cg0_K/3.)
         self.gp[type].covar.Cn.setCovariance(2.*Cn0_K/3.)
     elif type=='block':
         Crf_K = self.gp['full'].covar.Cr.K()
         Cnf_K = self.gp['full'].covar.Cn.K()
         self.gp[type].covar.Cr.scale = sp.mean(Crf_K)
         self.gp[type].covar.Cn.setCovariance(Cnf_K)
     elif type=='rank1':
         Crf_K = self.gp['full'].covar.Cr.K()
         Cnf_K = self.gp['full'].covar.Cn.K()
         self.gp[type].covar.Cr.setCovariance(Crf_K)
         self.gp[type].covar.Cn.setCovariance(Cnf_K)
     else:
         print('poppo')
     self.gp[type].optimize(factr=self.factr, verbose=False)
     RV = {'Cg': self.gp[type].covar.Cg.K(),
             'Cn': self.gp[type].covar.Cn.K(),
             'LML': sp.array([self.gp[type].LML()]),
             'LMLgrad': sp.array([sp.mean((self.gp[type].LML_grad()['covar'])**2)])}
     if type=='null':        RV['Cr'] = sp.zeros(RV['Cg'].shape) 
     else:                   RV['Cr'] = self.gp[type].covar.Cr.K() 
     if vc:
         # tr(P CoR) = tr(C)tr(R) - tr(Ones C) tr(Ones R) / float(NP)
         #           = tr(C)tr(R) - C.sum() * R.sum() / float(NP)
         trRr = (self.Xr**2).sum()
         var_r = sp.trace(RV['Cr'])*trRr / float(self.Y.size-1)
         var_g = sp.trace(RV['Cg'])*self.trRg / float(self.Y.size-1)
         var_n = sp.trace(RV['Cn'])*self.Y.shape[0] 
         var_n-= RV['Cn'].sum() / float(RV['Cn'].shape[0])
         var_n/= float(self.Y.size-1) 
         RV['var'] = sp.array([var_r, var_g, var_n])
         if 0 and self.Y.size<5000:
             pdb.set_trace()
             Kr = sp.kron(RV['Cr'], sp.dot(self.Xr, self.Xr.T))
             Kn = sp.kron(RV['Cn'], sp.eye(self.Y.shape[0]))
             _var_r = sp.trace(Kr-Kr.mean(0)) / float(self.Y.size-1)
             _var_n = sp.trace(Kn-Kn.mean(0)) / float(self.Y.size-1)
             _var = sp.array([_var_r, var_g, _var_n])
             print(((_var-RV['var'])**2).mean())
         if type=='full':
             # calculate within region vcs 
             Cr_block = sp.mean(RV['Cr']) * sp.ones(RV['Cr'].shape)
             Cr_rank1 = lowrank_approx(RV['Cr'], rank=1)
             var_block = sp.trace(Cr_block)*trRr / float(self.Y.size-1)
             var_rank1 = sp.trace(Cr_rank1)*trRr / float(self.Y.size-1)
             RV['var_r'] = sp.array([var_block, var_rank1-var_block, var_r-var_rank1])
     return RV
Esempio n. 16
0
def _calc_single_metric(trains_by_rate, metric, tau):
    logger.info("Calculating metric %s for time_scale %s." % (metric, str(tau)))
    result = sp.empty((cfg['evaluation_points'], cfg['evaluation_points']))
    dist_mat = metrics[metric](list(itertools.chain(*trains_by_rate)), tau)
    for i in xrange(cfg['evaluation_points']):
        i_start = cfg['spike_trains_per_rate'] * i
        i_stop = i_start + cfg['spike_trains_per_rate']
        for j in xrange(i, cfg['evaluation_points']):
            j_start = cfg['spike_trains_per_rate'] * j
            j_stop = j_start + cfg['spike_trains_per_rate']
            section = dist_mat[i_start:i_stop, j_start:j_stop]
            if i == j:
                result[i, i] = (sp.sum(section) - sp.trace(section)) / (
                    cfg['spike_trains_per_rate'] ** 2 -
                    cfg['spike_trains_per_rate'])
            else:
                result[i, j] = result[j, i] = sp.mean(section)
    return result
Esempio n. 17
0
File: pca.py Progetto: Yevgnen/prml
    def _em(self, X):
        # Constants
        n_samples, n_features = X.shape
        n_components = self.n_components
        max_iter = self.max_iter
        # tol = self.tol

        mu = X.mean(axis=0)
        X_centered = X - sp.atleast_2d(mu)

        # Initialize parameters
        latent_mean = 0
        sigma2 = 1
        weight = sprd.randn(n_features, n_components)

        # Main loop of EM algorithm
        for i in range(max_iter):
            # E step
            M = sp.dot(weight.T, weight) + sigma2 * sp.eye(n_components)
            inv_M = spla.inv(M)
            latent_mean = sp.dot(inv_M, sp.dot(weight.T, X_centered.T)).T

            # M step
            expectation_zzT = n_samples * sigma2 * inv_M + sp.dot(latent_mean.T, latent_mean)

            # Re-estimate W
            weight = sp.dot(sp.dot(X_centered.T, latent_mean), spla.inv(expectation_zzT))
            weight2 = sp.dot(weight.T, weight)

            # Re-estimate \sigma^2
            sigma2 = ((spla.norm(X_centered)**2 -
                       2 * sp.dot(latent_mean.ravel(), sp.dot(X_centered, weight).ravel()) +
                       sp.trace(sp.dot(expectation_zzT, weight2))) /
                      (n_samples * n_features))

        self.predict_mean = mu
        self.predict_cov = sp.dot(weight, weight.T) + sigma2 * sp.eye(n_features)
        self.latent_mean = latent_mean
        self.latent_cov = sigma2 * inv_M
        self.sigma2 = sigma2
        self.weight = weight
        self.inv_M = inv_M

        return self.latent_mean
Esempio n. 18
0
def axis_angle_from_rotation_matrix(rm):
    """
    Converts 3x3 rotation matrix to axis and angle representation.
    
    :type rm: 3x3 :obj:`float` matrix
    :param rm: Rotation matrix.
    :rtype: :obj:`tuple`
    :return: :samp:`(axis, radian_angle)` pair (angle in radians).
    """
    eps = (16 * sp.finfo(rm.dtype).eps)
    aa = sp.array((0, 0, 1), dtype=rm.dtype)
    theta = aa[0]
    c = (sp.trace(rm) - 1) / 2
    if (c > 1):
        c = 1
    if (c < -1):
        c = -1
    if (math.fabs(math.fabs(c) - 1) >= eps):
        theta = math.acos(c)
        s = math.sqrt(1 - c * c)
        inv2s = 1 / (2 * s)
        aa[0] = inv2s * (rm[2, 1] - rm[1, 2])
        aa[1] = inv2s * (rm[0, 2] - rm[2, 0])
        aa[2] = inv2s * (rm[1, 0] - rm[0, 1])
    elif (c >= 0):
        theta = 0
    else:
        rmI = (rm + sp.eye(3, 3, dtype=rm.dtype))
        theta = np.pi
        for i in range(0, 3):
            n2 = np.linalg.norm(rmI[:, i])
            if (n2 > 0):
                aa = col(rmI, i)
                break

    return aa, theta
Esempio n. 19
0
def axis_angle_from_rotation_matrix(rm):
    """
    Converts 3x3 rotation matrix to axis and angle representation.
    
    :type rm: 3x3 :obj:`float` matrix
    :param rm: Rotation matrix.
    :rtype: :obj:`tuple`
    :return: :samp:`(axis, radian_angle)` pair (angle in radians).
    """
    eps = (16*sp.finfo(rm.dtype).eps)
    aa = sp.array((0,0,1), dtype=rm.dtype) 
    theta = aa[0];
    c = (sp.trace(rm) - 1)/2;
    if (c > 1):
        c = 1;
    if (c < -1):
        c = -1;
    if (math.fabs(math.fabs(c)-1) >= eps):
        theta = math.acos(c);
        s = math.sqrt(1-c*c);
        inv2s = 1/(2*s);
        aa[0] = inv2s*(rm[2,1] - rm[1,2]);
        aa[1] = inv2s*(rm[0,2] - rm[2,0]);
        aa[2] = inv2s*(rm[1,0] - rm[0,1]);
    elif (c >= 0):
        theta = 0;
    else:
        rmI = (rm + sp.eye(3,3,dtype=rm.dtype));
        theta = np.pi;
        for i in range(0,3):
            n2 = np.linalg.norm(rmI[:,i]);
            if (n2 > 0):
                aa = col(rmI, i);
                break;

    return aa, theta
    def step(self, niter):
        """ xNES """
        f = self.f
        mu, sigma, B_mat = self.mu, self.sigma, self.B_mat
        eta_mu, eta_sigma, eta_B_mat = self.eta_mu, self.eta_sigma, self.eta_B_mat
        npop = self.npop
        d = self.d
        sigma_old = self.sigma_old

        I_mat = eye(d)

        with joblib.Parallel(n_jobs=self.n_jobs) as parallel:
            for i in range(niter):
                if (i % 10 == 0):

                    print('Itter %s of %s' % (str(i), str(niter)))
                    print('best Train Accuracy ', self.fitness_best)
                if (not self.stop):
                    for k in range(len(mu)):
                        if (not self.stop):
                            # if values over ~1e140, get error as too large
                            if (mu[k] > 1e140 or abs(mu[k]) < (1e-140)):
                                print('Alert! Value too large/small, mu[k] = ',
                                      mu[k])
                                self.stop = True
                    #        input("Press Enter to continue...")

                    z = randn(npop, d)  # random [npop X d] array
                    x = mu + sigma * dot(
                        z, B_mat)  # sampling next generation   # broadcast

                    # evaluate solutions in parallel on function f
                    f_try = parallel(joblib.delayed(f)(z) for z in x)
                    f_try = asarray(f_try)

                    # save if best

                    fitness = mean(f_try)  # mean fitness of next generation
                    if (self.fitness_best is not None):
                        if ((fitness - 1e-8) > (self.fitness_best)):
                            self.fitness_best = fitness  # update best fitness
                            self.mu_best = mu.copy()  # update best solution
                            self.counter = 0
                        else:
                            self.counter += 1
                    else:  # best fitness is mean fitness
                        self.fitness_best = fitness
                        self.mu_best = mu.copy()
                        self.counter = 0

                    if (self.fitness_best is None):
                        self.fitness_best = mean(f_try)
                        self.mu_best = mu.copy()
                        self.counter = 0

                    for j in range(len(f_try)):
                        #    validation_fit = self.f_val(x[j])
                        #   if(f_try[j]> self.fitness_best and validation_fit>self.fitness_val):
                        if (f_try[j] > self.fitness_best):
                            self.fitness_best = f_try[j]  # update best fitness
                            self.mu_best = x[j].copy()  # update best solution
                            self.counter = 0
                    #      self.fitness_val = validation_fit

                    if self.counter > self.patience:
                        self.done = True
                        return
                    self.best_testFit = self.f_test(self.mu_best)
                    #                print('best Train Accuracy ',self.fitness_best )
                    #                print('best Test Accuracy ',self.best_testFit )
                    # sort next generation w.r.t their fitnesses
                    unsorted_f_try = f_try
                    isort = argsort(f_try)
                    f_try = f_try[isort]
                    z = z[isort]
                    x = x[isort]

                    # u = utility if shaping, otherwise just use ordered fitnesses
                    u = self.utilities if self.use_fshape else f_try

                    # adaptation sampling
                    if self.use_adasam and sigma_old is not None:  # sigma_old must be available
                        eta_sigma = self.adasam(eta_sigma, mu, sigma, B_mat,
                                                sigma_old, x)

                    # G_delta = G_delta = u.s (s is z in gecco paper)
                    G_delta = dot(u, z)
                    # G_M = G_M = SUM u [dot] (s s_transpose - I)
                    G_M = dot(z.T, z * u.reshape(npop, 1)) - sum(u) * I_mat
                    # G_sigma = G_sigma = trace of G_M * (1/d)
                    G_sigma = trace(G_M) * (1.0 / d)
                    # G_B = G_B = G_M - G_delta*I
                    G_B = G_M - G_sigma * I_mat
                    # update old sigma
                    sigma_old = sigma

                    # update mu (center) = mu + eta_mu*sigma DOT (B, G_delta)
                    mu += eta_mu * sigma * dot(B_mat, G_delta)
                    # update sigma = sigma * exp(eta_delta * 0.5 * G_sigma)
                    sigma *= exp(0.5 * eta_sigma * G_sigma)
                    # update B = DOT (B, exp(0.5 eta_B * G_B))
                    B_mat = dot(B_mat, expm(0.5 * eta_B_mat * G_B))

                    # logging
                    for j in range(len(unsorted_f_try)):
                        self.history['fitness'].append(unsorted_f_try[j])

                #   self.history['fitness'].append(fitness)
                    self.history['sigma'].append(sigma)
                    self.history['eta_sigma'].append(eta_sigma)

        # keep last results
        self.mu, self.sigma, self.B_mat = mu, sigma, B_mat
        self.eta_sigma = eta_sigma
        self.sigma_old = sigma_old
        print('best Test Accuracy ', self.best_testFit)
Esempio n. 21
0
def SparseVFC(X,
              Y,
              Grid,
              M=100,
              a=5,
              beta=0.1,
              ecr=1e-5,
              gamma=0.9,
              lambda_=3,
              minP=1e-5,
              MaxIter=500,
              theta=0.75,
              div_cur_free_kernels=False):
    """Apply sparseVFC (vector field consensus) algorithm to learn an analytical function of vector field on the entire space robustly.
    Reference: Regularized vector field learning with sparse approximation for mismatch removal, Ma, Jiayi, etc. al, Pattern Recognition

    Arguments
    ---------
        X: 'np.ndarray'
            Current state. This corresponds to, for example, the spliced transcriptomic state.
        Y: 'np.ndarray'
            Velocity estimates in delta t. This corresponds to, for example, the inferred spliced transcriptomic velocity estimated calculated by velocyto, scvelo or dynamo.
        Grid: 'np.ndarray'
            Current state on a grid which is often used to visualize the vector field. This corresponds to, for example, the spliced transcriptomic state.
        M: 'np.ndarray'
            The number of basis functions to approximate the vector field. By default, it is 100.
        a: 'float'
            Paramerter of the model of outliers. We assume the outliers obey uniform distribution, and the volume of outlier's variation space is a. Default Value is 10.
        beta: 'float'
            Paramerter of Gaussian Kernel, k(x, y) = exp(-beta*||x-y||^2), Default value is 0.1.
        ecr: 'float'
            The minimum limitation of energy change rate in the iteration process. Default value is 1e-5.
        gamma: 'float'
            Percentage of inliers in the samples. This is an inital value for EM iteration, and it is not important. Default value is 0.9.
        lambda_: 'float'
            Represents the trade-off between the goodness of data fit and regularization. Default value is 3.
        minP: 'float'
            The posterior probability Matrix P may be singular for matrix inversion. We set the minimum value of P as minP. Default value is 1e-5.
        MaxIter: 'int'
            Maximum iterition times. Defualt value is 500.
        theta: 'float'
            Define how could be an inlier. If the posterior probability of a sample is an inlier is larger than theta, then it is regarded as an inlier. Default value is 0.75.

    Returns
    -------
    VecFld: 'dict'
    A dictionary which contains X, Y, beta, V, C, P, VFCIndex. Where V = f(X), P is the posterior probability and
    VFCIndex is the indexes of inliers which found by VFC. Note that V = con_K(Grid, ctrl_pts, beta).dot(C) gives the prediction of velocity on Grid (can be any point in the gene expressionstate space).
    """

    N, D = Y.shape

    # Construct kernel matrix K
    M = 500 if M is None else M
    tmp_X = np.unique(X, axis=0)  # return unique rows
    idx = np.random.RandomState(seed=0).permutation(
        tmp_X.shape[0])  # rand select some intial points
    idx = idx[range(min(M, tmp_X.shape[0]))]
    ctrl_pts = tmp_X[idx, :]
    # ctrl_pts = X[range(500), :]

    K = con_K(ctrl_pts, ctrl_pts,
              beta) if div_cur_free_kernels is False else con_K_div_cur_free(
                  ctrl_pts, ctrl_pts)[0]
    U = con_K(X, ctrl_pts,
              beta) if div_cur_free_kernels is False else con_K_div_cur_free(
                  X, ctrl_pts)[0]
    grid_U = con_K(
        Grid, ctrl_pts,
        beta) if div_cur_free_kernels is False else con_K_div_cur_free(
            Grid, ctrl_pts)[0]
    M = ctrl_pts.shape[0]

    # Initialization
    V = np.zeros((N, D))
    C = np.zeros((M, D))
    iter, tecr, E = 1, 1, 1
    sigma2 = sum(sum((Y - V)**2)) / (N * D)  ## test this

    while iter < MaxIter and tecr > ecr and sigma2 > 1e-8:
        # E_step
        E_old = E
        P, E = get_P(Y, V, sigma2, gamma, a)

        E = E + lambda_ / 2 * scipy.trace(C.T.dot(K).dot(C))
        tecr = abs((E - E_old) / E)

        # print('iterate: {}, gamma: {}, the energy change rate: {}, sigma2={}\n'.format(*[iter, gamma, tecr, sigma2]))

        # M-step. Solve linear system for C.
        P = scipy.maximum(P, minP)
        C = scipy.linalg.lstsq(((U.T * numpy.matlib.repmat(P.T, M, 1)).dot(U) + lambda_ * sigma2 * K), \
                               (U.T * numpy.matlib.repmat(P.T, M, 1)).dot(Y))[0]

        # Update V and sigma**2
        V = U.dot(C)
        Sp = sum(P)
        sigma2 = sum(P.T * np.sum((Y - V)**2, 1)) / np.dot(Sp, D)

        # Update gamma
        numcorr = len(np.where(P > theta)[0])
        gamma = numcorr / X.shape[0]

        if gamma > 0.95:
            gamma = 0.95
        elif gamma < 0.05:
            gamma = 0.05

        iter += 1

    grid_V = np.dot(grid_U, C)

    VecFld = {
        "X": ctrl_pts,
        "Y": Y,
        "beta": beta,
        "V": V,
        "C": C,
        "P": P,
        "VFCIndex": np.where(P > theta)[0],
        "sigma2": sigma2,
        "grid": Grid,
        "grid_V": grid_V
    }

    return VecFld
Esempio n. 22
0
def jitChol(A, maxTries=10, warning=True):
    """Do a Cholesky decomposition with jitter.
    
    Description:
    
    
    U, jitter = jitChol(A, maxTries, warning) attempts a Cholesky
     decomposition on the given matrix, if matrix isn't positive
     definite the function adds 'jitter' and tries again. Thereafter
     the amount of jitter is multiplied by 10 each time it is added
     again. This is continued for a maximum of 10 times.  The amount of
     jitter added is returned.
     Returns:
      U - the Cholesky decomposition for the matrix.
      jitter - the amount of jitter that was added to the matrix.
     Arguments:
      A - the matrix for which the Cholesky decomposition is required.
      maxTries - the maximum number of times that jitter is added before
       giving up (default 10).
      warning - whether to give a warning for adding jitter (default is True)

    See also
    CHOL, PDINV, LOGDET


    Copyright (c) 2005, 2006 Neil D. Lawrence
    
    """
    warning = True
    jitter = 0
    i = 0

    while (True):
        try:
            # Try --- need to check A is positive definite
            if jitter == 0:
                jitter = abs(SP.trace(A)) / A.shape[0] * 1e-6
                LC = linalg.cholesky(A, lower=True)
                return LC.T, 0.0
            else:
                if warning:
                    # pdb.set_trace()
                    # plt.figure()
                    # plt.imshow(A, interpolation="nearest")
                    # plt.colorbar()
                    # plt.show()
                    logging.error("Adding jitter of %f in jitChol()." % jitter)
                LC = linalg.cholesky(A + jitter * SP.eye(A.shape[0]),
                                     lower=True)

                return LC.T, jitter
        except linalg.LinAlgError:
            # Seems to have been non-positive definite.
            if i < maxTries:
                jitter = jitter * 10
            else:
                raise (linalg.LinAlgError,
                       "Matrix non positive definite, jitter of " +
                       str(jitter) + " added but failed after " + str(i) +
                       " trials.")
        i += 1
    return LC
Esempio n. 23
0
    def step(self, niter):
        """ xNES """
        f = self.f
        mu, sigma, bmat = self.mu, self.sigma, self.bmat
        eta_mu, eta_sigma, eta_bmat = self.eta_mu, self.eta_sigma, self.eta_bmat
        npop = self.npop
        dim = self.dim
        sigma_old = self.sigma_old

        eyemat = eye(dim)

        with joblib.Parallel(n_jobs=self.n_jobs) as parallel:

            for i in range(niter):
                s_try = randn(npop, dim)
                z_try = mu + sigma * dot(s_try, bmat)     # broadcast

                f_try = parallel(joblib.delayed(f)(z) for z in z_try)
                f_try = asarray(f_try)

                # save if best
                fitness = mean(f_try)
                if fitness - 1e-8 > self.fitness_best:
                    self.fitness_best = fitness
                    self.mu_best = mu.copy()
                    self.counter = 0
                else: self.counter += 1
                if self.counter > self.patience:
                    self.done = True
                    return

                isort = argsort(f_try)
                f_try = f_try[isort]
                s_try = s_try[isort]
                z_try = z_try[isort]

                u_try = self.utilities if self.use_fshape else f_try

                if self.use_adasam and sigma_old is not None:  # sigma_old must be available
                    eta_sigma = self.adasam(eta_sigma, mu, sigma, bmat, sigma_old, z_try)

                dj_delta = dot(u_try, s_try)
                dj_mmat = dot(s_try.T, s_try*u_try.reshape(npop,1)) - sum(u_try)*eyemat
                dj_sigma = trace(dj_mmat)*(1.0/dim)
                dj_bmat = dj_mmat - dj_sigma*eyemat

                sigma_old = sigma

                # update
                mu += eta_mu * sigma * dot(bmat, dj_delta)
                sigma *= exp(0.5 * eta_sigma * dj_sigma)
                bmat = dot(bmat, expm(0.5 * eta_bmat * dj_bmat))

                # logging
                self.history['fitness'].append(fitness)
                self.history['sigma'].append(sigma)
                self.history['eta_sigma'].append(eta_sigma)

        # keep last results
        self.mu, self.sigma, self.bmat = mu, sigma, bmat
        self.eta_sigma = eta_sigma
        self.sigma_old = sigma_old
Esempio n. 24
0
    def train(self, x, y, sig=None, dc=None, threshold=None, fast=None, E_=None, Beta_=None):
        """
        The function trains the pgpda model using the training samples
        Inputs:
        x: the samples matrix of size n x d, for the precomputed case (self.precomputed==1), x is a KERNEL object.
        y: the vector with label of size n
        sig: the parameter of the kernel function
        dc: the number of dimension of the singanl subspace
        threshold: the value of the cummulative variance that should be reached
        fast = option used to perform a fast CV: only the parameter dc/threshold is learn
        
        Outputs:
        None - The model is included/updated in the object
        """

        # Initialization
        n = y.shape[0]
        C = int(y.max())
        eps = sp.finfo(sp.float64).eps
        list_model_dc = "NM1 NM3 NM4"

        if (sig is None) and (self.sig is None):
            self.sig = 0.5
        elif self.sig is None:
            self.sig = sig

        if (dc is None) and (self.dc is None):
            self.dc = 2
        elif self.dc is None:
            self.dc = dc

        if (threshold is None) and (self.threshold is None):
            self.threshold = 0.95
        elif self.threshold is None:
            self.threshold = threshold

        # Check of consistent dimension
        if list_model_dc.find(self.model) > -1:
            for i in range(C):
                ni = sp.size(sp.where(y == (i + 1))[0])
                if self.dc >= ni - 1:
                    self.dc = ni - 2

        # Estimate the parameters of each class
        for i in range(C):
            t = sp.where(y == (i + 1))[0]
            self.ni.append(sp.size(t))
            self.prop.append(float(self.ni[i]) / n)

            if fast is None:
                # Compute Mi
                Ki = KERNEL()
                if self.precomputed is None:
                    Ki.compute_kernel(x[t, :], kernel=self.kernel, sig=self.sig)
                else:
                    Ki.K = x.K[t, :][:, t].copy()
                    Ki.rank = Ki.K.shape[0]

                self.ri.append(Ki.rank - 1)
                Ki.center_kernel()
                Ki.scale_kernel(self.ni[i])
                TraceKi = sp.trace(Ki.K)

                # Eigenvalue decomposition
                E, Beta = linalg.eigh(Ki.K)
                idx = E.argsort()[::-1]
                E = E[idx]
                E[E < eps] = eps
                Beta = Beta[:, idx]
            else:
                E = E_[i]
                Beta = Beta_[i]
                self.ri.append(E.size - 1)
                TraceKi = sp.sum(E)

            # Parameter estimation
            if list_model_dc.find(self.model) == -1:
                di = estim_d(E[0 : self.ri[i] - 1], self.threshold)
            else:
                di = self.dc
            self.di.append(di)
            self.a.append(E[0:di])
            self.b.append((TraceKi - sp.sum(self.a[i])) / (self.ri[i] - di))

            if self.b[i] < eps:  # Sanity check for numerical precision
                self.b[i] = eps
                self.ib.append(1.0 / eps)
            else:
                self.ib.append(1 / self.b[i])
            self.Beta.append(Beta[:, 0:di])
            del Beta, E

        # Finish the estimation for the different models
        if self.model == "NM0" or self.model == "NM1":
            for i in range(C):
                # Compute the value of matrix A
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) / self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        elif self.model == "NM2" or self.model == "NM3":
            for i in range(C):
                # Update the value of a
                self.a[i][:] = sp.mean(self.a[i])
                # Compute the value of matrix A
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) / self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        elif self.model == "NM4":
            # Compute the value of a
            al = sp.zeros((self.dc))
            for i in range(self.dc):
                for j in range(C):
                    al[i] += self.prop[j] * self.a[j][i]
            for i in range(C):
                self.a[i] = al.copy()
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) / self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        self.A = sp.asarray(self.A)
Esempio n. 25
0
def m():
	"""
	Note4 2.3.1 10
	Create the following numpy array
	A = [[1,4,6],[0,-3,2],[-2,-2,-2]]
	B = [[2,-1,0], [2,-1,-0],[0,0,1]]
	Compute A-B, 4A + B, trace(A), tB the transpose of B, the determinant of A, AB and Bt A the
	matrix product of A and B.
	Create the numpy array v = [1, -1, 0]. Compute Av the matrix-vector multiplication.
	Find the eigenvalues and the eigenvectors of the matrix B. Hint. Use the linear algebra module
	in numpy.
	Solve the system Ax = v using the solve(A,v) function of linalg.
	perfom the QR factorization of A using the linalg function qr(A) (Check that Q is an orthogonal
	matrix, that is Q T Q = I).
	Perform the singular value decomposition of A using the linalg function svd(A)
	
	"""
	A = np.array([[1,4,6],[0,-3,2],[-2,-2,-2]])
	B = np.array([[2,2,2], [-1,-1,-3],[0,0,1]])
	
	print "A = \n", A 
	raw_input('press enter to get B, A-B,4A - B')
	print "B = \n", B
	print ''
	print ''
	print "The difference is \n", A-B
	print ''
	print ''
	print "4A - B =  \n", 4*A - B
	raw_input ('Press enter to get the trace of A,transpose of B,determinant of A ')
	print " Trace of A : ", sp.trace(A)
	print ''
	print ''
	print " Transpose of B : \n",sp.transpose(B)
	print ''
	print ''
	print "Determinant of A : ", np.linalg.det(A)
	raw_input('press enter to get AB, The matrix product of A and B,v')
	print "AB = \n",A*B
	print ''
	print ''
	print 'Matrix product of A and B : \n',sp.dot(A,B)
	print ''
	print ''
	v =np.array([1,-1,0])
	print 'v = ',v
	raw_input('press enter to get matrix-vector multiplication Av, eigen values,The eigen vectors')
	print 'Matrix-vector multiplication Av : \n', sp.dot(A,v)
	print ''
	print ''
	e_values, e_vectors = np.linalg.eig(B)
	print " Eigen values of B : ",e_values
	print " Eigen vectors of B : \n",e_vectors
	raw_input('press enter to get System solution, QR factorization,singular value decomposition')
	print "System solution Ax = v: \t", np.linalg.solve(A,v)
	print ''
	print ''
	print 'QR factorization: \n',np.linalg.qr(A)
	print ''
	print ''
	print 'Singular value decomposition of A: \n', np.linalg.svd(A)
Esempio n. 26
0
def transmission(self,rrgm_out):
     from scipy import matrix,trace
     last_element_index = len(self.block_sizes)-1
     G_pq = matrix(rrgm_out[last_element_index,0])
     T = trace(matrix(-2*self.contacts[1].SigmaRet.imag)*G_pq*matrix(-2*self.contacts[0].SigmaRet.imag)*G_pq.getH())
     return T
Esempio n. 27
0
    def score_2_dof(self, X, snp_dim="col", debug=False):
        """
        Parameters
        ----------
        X : (`N`, `1`) ndarray
            genotype vector (TODO: X should be small)

        Returns
        -------
        pvalue : float
            P value
        """
        import scipy as sp
        import scipy.linalg as la
        import scipy.stats as st

        # 1. calculate Qs and pvs
        Q_rho = sp.zeros(len(self.rho_list))
        Py = P(self.gp, self.y)
        for i in range(len(self.rho_list)):
            rho = self.rho_list[i]
            LT = sp.vstack((rho ** 0.5 * self.vec_ones, (1 - rho) ** 0.5 * self.Env.T))
            LTxoPy = sp.dot(LT, X * Py)
            Q_rho[i] = 0.5 * sp.dot(LTxoPy.T, LTxoPy)

        # Calculating pvs is split into 2 steps
        # If we only consider one value of rho i.e. equivalent to SKAT and used for interaction test
        if len(self.rho_list) == 1:
            rho = self.rho_list[0]
            L = sp.hstack((rho ** 0.5 * self.vec_ones.T, (1 - rho) ** 0.5 * self.Env))
            xoL = X * L
            PxoL = P(self.gp, xoL)
            LToxPxoL = 0.5 * sp.dot(xoL.T, PxoL)
            try:
                pval = davies_pvalue(Q_rho[0], LToxPxoL)
            except AssertionError:
                eighQ, UQ = la.eigh(LToxPxoL)
                pval = mod_liu_corrected(Q_rho[0], eighQ)
            # Script ends here for interaction test
            return pval
        # or if we consider multiple values of rho i.e. equivalent to SKAT-O and used for association test
        else:
            pliumod = sp.zeros((len(self.rho_list), 4))
            for i in range(len(self.rho_list)):
                rho = self.rho_list[i]
                L = sp.hstack(
                    (rho ** 0.5 * self.vec_ones.T, (1 - rho) ** 0.5 * self.Env)
                )
                xoL = X * L
                PxoL = P(self.gp, xoL)
                LToxPxoL = 0.5 * sp.dot(xoL.T, PxoL)
                eighQ, UQ = la.eigh(LToxPxoL)
                pliumod[i,] = mod_liu_corrected(Q_rho[i], eighQ)
            T = pliumod[:, 0].min()
            # if optimal_rho == 0.999:
            #    optimal_rho = 1

            # 2. Calculate qmin
            qmin = sp.zeros(len(self.rho_list))
            percentile = 1 - T
            for i in range(len(self.rho_list)):
                q = st.chi2.ppf(percentile, pliumod[i, 3])
                # Recalculate p-value for each Q rho of seeing values at least as extreme as q again using the modified matching moments method
                qmin[i] = (q - pliumod[i, 3]) / (2 * pliumod[i, 3]) ** 0.5 * pliumod[
                    i, 2
                ] + pliumod[i, 1]

            # 3. Calculate quantites that occur in null distribution
            Px1 = P(self.gp, X)
            m = 0.5 * sp.dot(X.T, Px1)
            xoE = X * self.Env
            PxoE = P(self.gp, xoE)
            ETxPxE = 0.5 * sp.dot(xoE.T, PxoE)
            ETxPx1 = sp.dot(xoE.T, Px1)
            ETxPx11xPxE = 0.25 / m * sp.dot(ETxPx1, ETxPx1.T)
            ZTIminusMZ = ETxPxE - ETxPx11xPxE
            eigh, vecs = la.eigh(ZTIminusMZ)

            eta = sp.dot(ETxPx11xPxE, ZTIminusMZ)
            vareta = 4 * sp.trace(eta)

            OneZTZE = 0.5 * sp.dot(X.T, PxoE)
            tau_top = sp.dot(OneZTZE, OneZTZE.T)
            tau_rho = sp.zeros(len(self.rho_list))
            for i in range(len(self.rho_list)):
                tau_rho[i] = self.rho_list[i] * m + (1 - self.rho_list[i]) / m * tau_top

            MuQ = sp.sum(eigh)
            VarQ = sp.sum(eigh ** 2) * 2 + vareta
            KerQ = sp.sum(eigh ** 4) / (sp.sum(eigh ** 2) ** 2) * 12
            Df = 12 / KerQ

            # 4. Integration
            # from time import time
            # start = time()
            pvalue = optimal_davies_pvalue(
                qmin, MuQ, VarQ, KerQ, eigh, vareta, Df, tau_rho, self.rho_list, T
            )
            # print("Elapsed: {} seconds".format(time() - start))

            # Final correction to make sure that the p-value returned is sensible
            multi = 3
            if len(self.rho_list) < 3:
                multi = 2
            idx = sp.where(pliumod[:, 0] > 0)[0]
            pval = pliumod[:, 0].min() * multi
            if pvalue <= 0 or len(idx) < len(self.rho_list):
                pvalue = pval
            if pvalue == 0:
                if len(idx) > 0:
                    pvalue = pliumod[:, 0][idx].min()

            if debug:
                info = {
                    "Qs": Q_rho,
                    "pvs_liu": pliumod,
                    "qmin": qmin,
                    "MuQ": MuQ,
                    "VarQ": VarQ,
                    "KerQ": KerQ,
                    "lambd": eigh,
                    "VarXi": vareta,
                    "Df": Df,
                    "tau": tau_rho,
                }
                return pvalue, info
            else:
                return pvalue
Esempio n. 28
0
File: mvSet.py Progetto: PMBio/limix
            T.append(sp.reshape(sp.kron(Cr_h, Xr), (N,P,rankCr*S), order='F'))
            KiT.append(gp.covar.solve_t(T[i]))
        Ht = sp.zeros((3,3))
        for i in range(3):
            Ht[i,i] = (sp.einsum('qpn,qpm->nm', T[i], KiT[i])**2).sum() 
            for j in range(0,i):
                Ht[i,j] = (sp.einsum('qpn,qpm->nm', T[i], KiT[j])**2).sum()
                Ht[j,i] = Ht[i,j] 
        if Y.shape[0]<=1000:
            Ki = la.inv(sp.kron(mvset.gp[type].covar.Cn.K(), sp.eye(Y.shape[0])))
            XrXr = sp.dot(Xr, Xr.T)
            KidK = [sp.dot(Ki, sp.kron(_Cr.K_grad_i(i), XrXr)) for i in range(3)]
            Ht0 = sp.zeros((3,3))
            for i in range(3):
                for j in range(3):
                    Ht0[i,j] = sp.trace(sp.dot(KidK[i], KidK[j]))
            pdb.set_trace()

    if 0:
        # 2. quadratic_term = y_Ki_dKp_Ki_dKq_Ki_y
        Z = Y-sp.dot(F, mvset.gp[type].mean.B[0]) 
        XrXrKiZ = sp.dot(Xr, sp.dot(Xr.T, gp.covar.solve_t(Z)))
        XrXrKiZC = [sp.dot(XrXrKiZ, _Cr.K_grad_i(i).T) for i in range(3)]
        KiXrXrKiZC = [gp.covar.solve_t(XrXrKiZC[i]) for i in range(3)]
        Hq = sp.zeros((3,3))
        for i in range(3):
            Hq[i,i] = (XrXrKiZC[i]*KiXrXrKiZC[i]).sum()
            for j in range(i):
                Hq[i,j] = (XrXrKiZC[i]*KiXrXrKiZC[j]).sum()
                Hq[j,i] = Hq[i,j]
        if Y.shape[0]<=1000:
Esempio n. 29
0
def bicgstab_iso(A, x, b, MVop, VVop, max_itr=500, atol=1E-14, rtol=1E-14):
    """Implements the Bi-CGSTAB method for isomorphic operations.
    
    The Bi-CGSTAB method is used to solve linear equations Ax = b.
    
    Should the vectors x, b be isomorphic to some other objects, say
    matrices x' and b', with corresponding opeator A'
    (for example, via the Choi-Jamiolkowski isomorphism), the method
    can similarly be carried out in the alternative form.
    
    With this function, the operations corresponding to matrix-vector
    and vector-vector multiplication are supplied by the caller to enable
    using the method in an isomorphic way.
    
    Parameters
    ----------
    A : ndarray
        The A matrix, or equivalent.        
    x : ndarray
        An initial value for the unknown vector, or equivalent.
    b : ndarray
        The b vector, or equivalent.
    MVop : function(ndarray, ndarray)
        The matrix-vector multiplication operation.
    VVop : function(ndarray, ndarray)
        The vector-vector multiplication operation.
    max_itr : int
        Maximum number of iterations.
    atol : float
        Absolute tolerance for solution.
    rtol : float
        Relative tolerance for solution.

    Returns
    -------
    x : ndarray
        The final value for the unknown vector x.
    convg : bool
        Whether the algorithm converged within max_itr iterations.
    """
    r_prv = b - MVop(A, x)
    
    r0 = r_prv.copy()
    
    rho_prv = 1
    alpha = 1
    omega_prv = 1
    
    v_prv = sp.zeros_like(x)
    p_prv = sp.zeros_like(x)
    
    for i in xrange(max_itr):
        rho = sp.trace(sp.dot(r0, r_prv))
        
        beta = (rho / rho_prv) * (alpha / omega_prv)
        
        p = r_prv + beta * (p_prv - omega_prv * v_prv)
        
        v = MVop(A, p)
        
        alpha = rho / VVop(r0, v)
        
        s = r_prv - alpha * v
        
        t = MVop(A, s)
            
        omega = VVop(t, s) / VVop(t, t)
        
        x += alpha * p + omega * s
        
        #Test x
        if sp.allclose(MVop(A, x), b, atol=atol, rtol=rtol):
            break
        
        r_prv = s - omega * t
        
        rho_prv = rho
        omega_prv = omega
        
        v_prv[:] = v
        p_prv[:] = p
        
    convg = i < max_itr - 1
    
    return x, convg
Esempio n. 30
0
    def update(self, population):
        """Update the current covariance matrix strategies from the
        *population*.

        :param population: A list of individuals from which to update the
                           parameters.
        """

        chosen, not_chosen, isPlenty = self._select(population)
        count1 = 0
        count2 = 0
        count3 = 0
        count4 = 0
        count5 = 0
        count6 = 0
        count7 = 0
        count8 = 0
        # Update the internal parameters for successful offspring

        # Only the offspring update the parameter set
        for ind in population:
            if ind.Rank < self.parents[
                    ind._ps[1]].Rank and ind.Rank <= self.mu:
                gm = numpy.outer(ind.theta, ind.theta) - numpy.identity(
                    self.dim)
                gsigma = 0
                if trace(gm) >= 0:
                    count1 += 1
                    gm = self.gradextention * gm
                    gsigma = numpy.trace(gm) / self.dim
                else:
                    count2 += 1
                    gsigma = numpy.trace(gm) / self.dim
                ga = gm - gsigma * numpy.identity(self.dim)
                proc = 0.5 * (self.etaA * ga)
                GGA = scipy.linalg.expm(proc)
                if self.dominates(ind, self.parents[ind._ps[1]]):
                    count7 += 1
                    if self.parents[ind._ps[1]].oddoreven == 1:
                        ind.domisigma = ind.domisigma * exp(
                            self.etasigma * gsigma / 2.0)
                        ind.dominateA = numpy.dot(ind.dominateA, GGA)
                else:
                    if self.parents[ind._ps[1]].oddoreven == 0:
                        ind.indsigma = ind.indsigma * exp(
                            self.etasigma * gsigma / 2.0)
                        ind.indicatorA = numpy.dot(ind.indicatorA, GGA)
                if numpy.sum(ind.valConstr[1:]) < numpy.sum(
                        self.parents[ind._ps[1]].valConstr[1:]):
                    count8 += 1

            elif ind.Rank > self.parents[
                    ind._ps[1]].Rank and ind.Rank <= self.mu:
                gm = numpy.outer(ind.theta, ind.theta) - numpy.identity(
                    self.dim)
                gsimga = numpy.trace(gm) / self.dim
                if gsimga > 0:
                    count3 += 1
                else:
                    count4 += 1
            elif ind.Rank > self.mu and self.parents[
                    ind._ps[1]].Rank <= self.mu:
                gm = self.infeasiblew * (numpy.outer(ind.theta, ind.theta) -
                                         numpy.identity(self.dim))
                gsigma = numpy.trace(gm) / self.dim
                if numpy.trace(
                        numpy.outer(ind.theta, ind.theta) -
                        numpy.identity(self.dim)) > 0:
                    count5 += 1
                else:
                    count6 += 1
                    gm = -gm
                    gsigma = -gsigma
                ga = gm - gsigma * numpy.identity(self.dim)
                proc = 0.5 * (self.etaA * ga)
                GGA = scipy.linalg.expm(proc)
                if self.parents[ind._ps[1]].isFeasible and not ind.isFeasible:
                    if self.parents[ind._ps[1]].oddoreven == 0:
                        self.parents[ind._ps[1]].indsigma = self.parents[
                            ind._ps[1]].indsigma * exp(
                                self.etasigma * gsigma / 2.0)
                        self.parents[ind._ps[1]].indicatorA = numpy.dot(
                            self.parents[ind._ps[1]].indicatorA, GGA)
                    elif self.parents[ind._ps[1]].oddoreven == 1:
                        self.parents[ind._ps[1]].domisigma = self.parents[
                            ind._ps[1]].domisigma * exp(
                                self.etasigma * gsigma / 2.0)
                        self.parents[ind._ps[1]].dominateA = numpy.dot(
                            self.parents[ind._ps[1]].dominateA, GGA)
                    if not self.parents[ind._ps[1]].madeinfeasible:
                        self.parents[ind._ps[1]].madeinfeasible = True
            if self.parents[ind._ps[1]].madeinfeasible:
                if self.parents[ind._ps[1]].oddoreven == 0:
                    self.parents[ind._ps[1]].oddoreven = 1
                    self.parents[ind._ps[1]].phase = 1
                else:
                    self.parents[ind._ps[1]].oddoreven = 0
                    self.parents[ind._ps[1]].phase = 2
            else:
                print(
                    str(ind.Rank) + " and parent achieved " +
                    str(self.parents[ind._ps[1]].Rank))

        self.dominating_Success = count7
        self.success_outer = count1
        self.success = count2
        self.missed_both_alive_out = count3
        self.missed_both_alive_out = count4
        self.parentonly_alive_out = count5
        self.parentonly_alive_in = count6
        self.less_constraint = count8
        self.parents = copy.deepcopy(chosen)
Esempio n. 31
0
    def take_step_split(self, dtau, ham_is_Herm=True):
        """Take a time-step dtau using the split-step integrator.
        
        This is the one-site version of a DMRG-like time integrator described
        at:
          http://arxiv.org/abs/1408.5056
        
        It has a fourth-order local error and is symmetric. It requires
        iteratively computing two matrix exponentials per site, and thus
        has less predictable CPU time requirements than the Euler or RK4 
        methods.
        
        NOTE:
        This requires the expokit extension, which is included in evoMPS but 
        must be compiled during, e.g. using setup.py to build all extensions.
        
        Parameters
        ----------
        dtau : complex
            The (imaginary or real) amount of imaginary time (tau) to step.
        ham_is_Herm : bool
            Whether the Hamiltonian is really Hermitian. If so, the lanczos
            method will be used for imaginary time evolution.
        """
        #TODO: Compute eta.
        self.eta_sq.fill(0)
        self.eta = 0
        
        assert self.canonical_form == 'right', 'take_step_split only implemented for right canonical form'
        assert self.ham_sites == 2, 'take_step_split only implemented for nearest neighbour Hamiltonians'
        dtau *= -1
        from expokit_expmv import zexpmv, zexpmvh

        if sp.iscomplex(dtau) or not ham_is_Herm:
            expmv = zexpmv
            fac = 1.j
            dtau = sp.imag(dtau)
        else:
            expmv = zexpmvh
            fac = 1
            
        norm_est = abs(self.H_expect.real)
    
        KL = [None] * (self.N + 1)
        KL[1] = sp.zeros((self.D[1], self.D[1]), dtype=self.typ)
        for n in xrange(1, self.N + 1):
            lop = Vari_Opt_Single_Site_Op(self, n, KL[n - 1], tau=fac)
            #print "Befor A", n, sp.inner(self.A[n].ravel().conj(), lop.matvec(self.A[n].ravel())).real
            An = expmv(lop, self.A[n].ravel(), dtau/2., norm_est=norm_est)            
            self.A[n] = An.reshape((self.q[n], self.D[n - 1], self.D[n]))
            self.l[n] = tm.eps_l_noop(self.l[n - 1], self.A[n], self.A[n])
            norm = m.adot(self.l[n], self.r[n])
            self.A[n] /= sp.sqrt(norm)
            #print "After A", n, sp.inner(self.A[n].ravel().conj(), lop.matvec(self.A[n].ravel())).real, norm.real
            
            #shift centre matrix right (RCF is like having a centre "matrix" at "1")
            G = tm.restore_LCF_l_seq(self.A[n - 1:n + 1], self.l[n - 1:n + 1],
                                     sanity_checks=self.sanity_checks) 
                                     
            if n > 1:
                self.AA[n - 1] = tm.calc_AA(self.A[n - 1], self.A[n])
                self.C[n - 1] = tm.calc_C_mat_op_AA(self.ham[n - 1], self.AA[n - 1])
                KL[n], ex = tm.calc_K_l(KL[n - 1], self.C[n - 1], self.l[n - 2], 
                                        self.r[n], self.A[n], self.AA[n - 1])
                
            if n < self.N:                    
                lop2 = Vari_Opt_SC_op(self, n, KL[n], tau=fac)
                #print "Befor G", n, sp.inner(G.ravel().conj(), lop2.matvec(G.ravel())).real
                G = expmv(lop2, G.ravel(), -dtau/2., norm_est=norm_est)
                G = G.reshape((self.D[n], self.D[n]))
                norm = sp.trace(self.l[n].dot(G).dot(self.r[n].dot(G.conj().T)))
                G /= sp.sqrt(norm)
                #print "After G", n, sp.inner(G.ravel().conj(), lop2.matvec(G.ravel())).real, norm.real
                
                for s in xrange(self.q[n + 1]):
                    self.A[n + 1][s] = G.dot(self.A[n + 1][s])                
                
                self.AA[n] = tm.calc_AA(self.A[n], self.A[n + 1])
                self.C[n] = tm.calc_C_mat_op_AA(self.ham[n], self.AA[n])           
   
        for n in xrange(self.N, 0, -1):
            lop = Vari_Opt_Single_Site_Op(self, n, KL[n - 1], tau=fac, sanity_checks=self.sanity_checks)
            #print "Before A", n, sp.inner(self.A[n].ravel().conj(), lop.matvec(self.A[n].ravel())).real
            An = expmv(lop, self.A[n].ravel(), dtau/2., norm_est=norm_est)
            self.A[n] = An.reshape((self.q[n], self.D[n - 1], self.D[n]))
            self.l[n] = tm.eps_l_noop(self.l[n - 1], self.A[n], self.A[n])
            norm = m.adot(self.l[n], self.r[n])
            self.A[n] /= sp.sqrt(norm)
            #print "After A", n, sp.inner(self.A[n].ravel().conj(), lop.matvec(self.A[n].ravel())).real, norm.real
            
            #shift centre matrix left (LCF is like having a centre "matrix" at "N")
            Gi = tm.restore_RCF_r_seq(self.A[n - 1:n + 1], self.r[n - 1:n + 1],
                                      sanity_checks=self.sanity_checks)
                                      
            if n < self.N:
                self.AA[n] = tm.calc_AA(self.A[n], self.A[n + 1])
                self.C[n] = tm.calc_C_mat_op_AA(self.ham[n], self.AA[n])                                          
                self.calc_K(n_low=n, n_high=n)
            
            if n > 1:
                lop2 = Vari_Opt_SC_op(self, n - 1, KL[n - 1], tau=fac, sanity_checks=self.sanity_checks)
                Gi = expmv(lop2, Gi.ravel(), -dtau/2., norm_est=norm_est)
                Gi = Gi.reshape((self.D[n - 1], self.D[n - 1]))
                norm = sp.trace(self.l[n - 1].dot(Gi).dot(self.r[n - 1].dot(Gi.conj().T)))
                G /= sp.sqrt(norm)
                #print "After G", n, sp.inner(Gi.ravel().conj(), lop2.matvec(Gi.ravel())).real, norm.real

                for s in xrange(self.q[n - 1]):
                    self.A[n - 1][s] = self.A[n - 1][s].dot(Gi)
            
                self.AA[n - 1] = tm.calc_AA(self.A[n - 1], self.A[n])
                self.C[n - 1] = tm.calc_C_mat_op_AA(self.ham[n - 1], self.AA[n - 1])
Esempio n. 32
0
def run_test_norm(T, ALPHAS, Ns, B, labeled, u, W_0, little_oh=False):
    TRC = np.zeros((len(ALPHAS), len(T)))
    TRCBC = np.zeros((len(ALPHAS), len(T)))
    normC = np.zeros((len(ALPHAS), len(T)))
    normC_tau_inv_u = np.zeros((len(ALPHAS), len(T)))
    normC_tau_inv_u_p1 = np.zeros((len(ALPHAS), len(T)))
    normC_tau_inv_u_p2 = np.zeros((len(ALPHAS), len(T)))
    BIAS = np.zeros((len(ALPHAS), len(T)))

    K = len(Ns)
    N = sum(Ns)
    u_n = u[:, 0] / sp.linalg.norm(u[:, 0])
    print(np.linalg.norm(u_n))
    for j, tau in enumerate(T):
        if little_oh:
            eps = tau**3.
        else:
            eps = tau**2.

        w, v = get_eig_Lnorm_withW0(eps, Ns, W_0)

        for i, alpha in enumerate(ALPHAS):
            gamma = tau**alpha
            d = (tau**(2 * alpha)) * np.power(
                w + tau**2.,
                -alpha)  # diagonalization of C_t,e - will be used later

            d_inv = 1. / d  # the eigenvalues of C_\tau,\eps^{-1}
            C_tau_inv_u = (v * d_inv.reshape(1, N)).dot(v.T.dot(u_n))
            normC_tau_inv_u[i, j] = sp.linalg.norm(C_tau_inv_u)

            # first term of the triangle inequality
            vK = v[:, :
                   K]  # using only the first K eigenvectors of C_\tau, \eps^{-1}
            dK = d_inv[:K]
            C_tau_inv_u_p1 = (vK * dK.reshape(1, K)).dot(vK.T.dot(u_n))
            normC_tau_inv_u_p1[i, j] = sp.linalg.norm(C_tau_inv_u_p1)

            # second term of the triangle inequality, using the K+1,...,Nth eigenpairs of C_\tau,\eps^{-1}
            v_K = v[:, K:]
            d_K = d_inv[K:]
            C_tau_inv_u_p2 = (v_K * d_K.reshape(1, N - K)).dot(v_K.T.dot(u_n))
            normC_tau_inv_u_p2[i, j] = sp.linalg.norm(C_tau_inv_u_p2)

            # prior_inv : C_{tau,eps}^{-1}, where
            # C_{tau, eps}^{-1} = tau^{-2alpha}(L + tau^2 I)^alpha
            prior_inv = v.dot(
                sp.sparse.diags([1. / thing for thing in d],
                                format='csr').dot(v.T))
            # B/gamma^2
            B_over_gamma2 = B / (gamma * gamma)
            # post_inv  : (B/gamma^2 + C_{tau,\eps}^{-1})^{-1}
            post_inv = prior_inv + B_over_gamma2
            # C^{-1}
            post = post_inv.I
            bias = sp.linalg.norm(post.dot(prior_inv.dot(u_n)))
            BIAS[i, j] = bias
            normC[i, j] = sp.linalg.norm(post)

            trC = sp.trace(post)
            TRC[i, j] = trC

            post2 = post.dot(post)
            trCBC = sp.trace(post2[np.ix_(labeled[0], labeled[0])])
            TRCBC[i, j] = trCBC / (gamma**2.)

    return TRC, TRCBC, normC, normC_tau_inv_u, normC_tau_inv_u_p1, normC_tau_inv_u_p2, BIAS
Esempio n. 33
0
def contract_uni(A, B):
    M = sp.dot(matmul.H(A[0][0]), B[0][0]) + sp.dot(matmul.H(A[0][1]), B[0][1])
    tr = sp.trace(matmul.H(A[0][0])) * sp.trace(B[0][0]) + sp.trace(
        matmul.H(A[0][1])) * sp.trace(B[0][1])
    return tr.real
Esempio n. 34
0
def bicgstab_iso(A, x, b, MVop, VVop, max_itr=500, atol=1E-14, rtol=1E-14):
    """Implements the Bi-CGSTAB method for isomorphic operations.
    
    The Bi-CGSTAB method is used to solve linear equations Ax = b.
    
    Should the vectors x, b be isomorphic to some other objects, say
    matrices x' and b', with corresponding opeator A'
    (for example, via the Choi-Jamiolkowski isomorphism), the method
    can similarly be carried out in the alternative form.
    
    With this function, the operations corresponding to matrix-vector
    and vector-vector multiplication are supplied by the caller to enable
    using the method in an isomorphic way.
    
    Parameters
    ----------
    A : ndarray
        The A matrix, or equivalent.        
    x : ndarray
        An initial value for the unknown vector, or equivalent.
    b : ndarray
        The b vector, or equivalent.
    MVop : function(ndarray, ndarray)
        The matrix-vector multiplication operation.
    VVop : function(ndarray, ndarray)
        The vector-vector multiplication operation.
    max_itr : int
        Maximum number of iterations.
    atol : float
        Absolute tolerance for solution.
    rtol : float
        Relative tolerance for solution.

    Returns
    -------
    x : ndarray
        The final value for the unknown vector x.
    convg : bool
        Whether the algorithm converged within max_itr iterations.
    """
    r_prv = b - MVop(A, x)
    
    r0 = r_prv.copy()
    
    rho_prv = 1
    alpha = 1
    omega_prv = 1
    
    v_prv = sp.zeros_like(x)
    p_prv = sp.zeros_like(x)
    
    for i in xrange(max_itr):
        rho = sp.trace(sp.dot(r0, r_prv))
        
        beta = (rho / rho_prv) * (alpha / omega_prv)
        
        p = r_prv + beta * (p_prv - omega_prv * v_prv)
        
        v = MVop(A, p)
        
        alpha = rho / VVop(r0, v)
        
        s = r_prv - alpha * v
        
        t = MVop(A, s)
            
        omega = VVop(t, s) / VVop(t, t)
        
        x += alpha * p + omega * s
        
        #Test x
        if sp.allclose(MVop(A, x), b, atol=atol, rtol=rtol):
            break
        
        r_prv = s - omega * t
        
        rho_prv = rho
        omega_prv = omega
        
        v_prv[:] = v
        p_prv[:] = p
        
    convg = i < max_itr - 1
    
    return x, convg
Esempio n. 35
0
def mkl_train(fname_l, reg, ltrain, no_mkl, flip):

    global cache

    d0 = io.loadmat(fname_l[0])

    if flip:
        keytrain = "kernel_traintest"
        keytest = "kernel_traintrain"        
    else:
        keytrain = "kernel_traintrain"
        keytest = "kernel_traintest"

    ltr = ltrain.astype(double)
    #l = ((l==thecat)-1*(l!=thecat)).astype(double)
    labels = Features.Labels(ltr)
    
    if True:
        tr1_l = []
        tr2_l = []
        ker_l = []
        feat_l = []
    
        combker = Kernel.CombinedKernel()
        combfeat = Features.CombinedFeatures()
        
        m,n = d0[keytrain].shape
        yy,xx = mgrid[:m,:n]

        #print "loading nfiles=", len(fname_l)
        for i,fname in enumerate(fname_l):
            if fname in cache_train:
                kermat, tr1, tr2 = cache_train[fname]
            else:
                #print "loading", fname
                d = io.loadmat(fname)
                kermat = (d[keytrain]).astype(double)
                #kermat += kermat.min()
                del d
                #kdiag = 
                #kermat /= kdiag
                tr1 = trace(kermat)*len(fname_l)#*diag(kermat)
                if tr1 == 0:
                    print "trace is 0, skipping"
                    continue
                print i+1, "trace", tr1, "min", kermat.min(), "max", kermat.max(), fname                
                kermat /= tr1
                #print kermat
                #kermat = exp(kermat*len(kermat))
                #tr2 = trace(kermat)
                #kermat /= tr2
                #print kermat
                tr2 = 0
                cache_train[fname] = kermat, tr1, tr2
                #print diag(kermat)

            ker = Kernel.CustomKernel()
            ker.set_full_kernel_matrix_from_full(kermat)
            feat = Features.DummyFeatures(n)
            combker.append_kernel(ker)
            combfeat.append_feature_obj(feat)
        
            ker_l += [ker]
            feat_l += [feat]
            tr1_l += [tr1]
            tr2_l += [tr2]

        #combker.init(combfeat, combfeat)
        cache = {'ker_l': ker_l, 'feat_l': feat_l, 'tr1_l':tr1_l, 'tr2_l':tr2_l,
                 'combker': combker, 'combfeat': combfeat}

        del xx, yy    

    if no_mkl:
        #w_l = array([.2, .8])
        #w_l /= w_l.sum()
        for i,ker in enumerate(ker_l):
            ker.set_combined_kernel_weight(1./(len(ker_l)))
            #ker.set_combined_kernel_weight(w_l[i])

    tstart = time.time()
    #svm1 = Classifier.SVMLight(reg, combker, labels)
    #svm1 = Classifier.LibSVM(reg, combker, labels)
    #svm1 = Classifier.GMNPSVM(reg, combker, labels)
    #svm1 = Classifier.SVMSGD(reg, combker, labels)
    #svm1 = Classifier.CPLEXSVM(reg, combker, labels)
    if len(fname_l) == 1:
        no_mkl = True
        #svm1 = Classifier.LibSVM(reg, combker, labels)
        svm1 = Classifier.SVMLight(reg, combker, labels)
    else:
        svm1 = Classifier.SVMLight(reg, combker, labels)
    svm1.set_mkl_enabled(not no_mkl)
    
    #svm1.set_C(reg/10., reg)
    #print svm1.get_C1(), svm1.get_C2()

    svm1.parallel.set_num_threads(4)
    #print ker_l[0]


    kkk = combker.get_kernel_matrix()
    assert(not isnan(kkk).any())
    assert(not isinf(kkk).any())# trace(kkk)

    #svm1.set_linadd_enabled(True)
    #svm1.set_shrinking_enabled(True)
    #svm1.set_bias_enabled(False)
    #svm1.set_epsilon(1e-1)
    #svm1.set_tube_epsilon(1e-4)
    #svm1.set_weight_epsilon(0)
    #svm1.set_C_mkl(1e3)
    #ipshell()
    svm1.set_mkl_norm(1)

    svm1.train()

    alphas = svm1.get_alphas()
    bias = svm1.get_bias()
    svs = svm1.get_support_vectors()
    
    #print ker_l[0]

    #svm1 = Classifier.SVMLight(reg, ker, labels)
    #svm1.train()
    #print svm1.get_alphas()

    weight_l = []
    for i,ker in enumerate(ker_l):
        weight = ker.get_combined_kernel_weight()
        weight_l += [weight]

    print weight_l
    print "-w " + " -w ".join([str(e) for e in weight_l])
    kweights = array(weight_l)
    kweights /= kweights.sum()

    traces1 = array(tr1_l)
    #traces2 = array(tr2_l)

    #raise
    
    #tend = time.time()
    #print "time:", tend-tstart
    
    #kernel_train = combker.get_kernel_matrix()
    
#     weight_l = []
#     for i,ker in enumerate(ker_l):
#         weight = ker.get_combined_kernel_weight()
#         weight_l += [weight]

    #combker.init(combfeat,combfeat)
    #print svm1.classify().get_labels()[:10]

    #kk = combker.get_kernel_matrix()
    #print "trace=", trace(kk)
    #trainperf = (sign(svm1.classify().get_labels())==ltr).mean() 
    #print "trainperf=", trainperf, weight_l

    #print svm1.get_alphas()
    #raise

#     mkl = {"svm": svm1, 
#            "ker_l": ker_l,
#            "feat_l": feat_l,
#            "tr_l": tr_l,
#            "combker": combker,
#            "combfeat": combfeat,
#            }

    #print bias

    mkl = {"alphas": alphas.astype('float32'),
           "bias": array(bias).astype('float32'),
           "svs": svs.astype('int32'),
           "kweights": kweights.astype('float32'),
           "traces1": traces1.astype('float32'),
           #"traces2": traces2.astype('float32'),
           }
    
    #print svm1.get_alphas()

    return mkl
Esempio n. 36
0
    def _fit(self, type, vc=False):
        # 2. init
        if type == 'null':
            self.gp[type].covar.Cn.setCovariance(sp.eye(2))
        elif type == 'full':
            Cr0_K = 1e-4 * sp.ones((2, 2)) + 1e-4 * sp.eye(2)
            Cn0_K = 0.99 * self.gp['null'].covar.Cn.K()
            self.gp[type].covar.Cr.setCovariance(Cr0_K)
            self.gp[type].covar.Cn.setCovariance(Cn0_K)
        elif type == 'block':
            Crf_K = self.gp['full'].covar.Cr.K()
            Cnf_K = self.gp['full'].covar.Cn.K()
            self.gp[type].covar.Cr.scale = sp.mean(Crf_K)
            self.gp[type].covar.Cn.setCovariance(Cnf_K)
        elif type == 'rank1':
            Crf_K = self.gp['full'].covar.Cr.K()
            Cnf_K = self.gp['full'].covar.Cn.K()
            self.gp[type].covar.Cr.setCovariance(Crf_K)
            self.gp[type].covar.Cn.setCovariance(Cnf_K)
        else:
            print('poppo')
        conv = self.gp[type].optimize(factr=self.factr, verbose=False)[0]
        B = self.gp[type].mean.b.reshape((int(self.mean.W.shape[1] / 2), 2),
                                         order='F')
        RV = {
            'Cr': self.gp[type].covar.Cr.K(),
            'Cn': self.gp[type].covar.Cn.K(),
            'B': B,
            'conv': sp.array([conv]),
            'LML': sp.array([self.gp[type].LML()]),
            'LMLgrad':
            sp.array([sp.mean((self.gp[type].LML_grad()['covar'])**2)])
        }
        if vc:
            # tr(P WW) = tr(PWWP) = ((PW)**2).sum()
            # tr(P D) = (PD).sum() = D.sum() - 1/n * (Ones*D).sum()
            #                      = D.sum() - D.sum()
            def var_WW(W):
                PW = W - W.mean(0)
                rv = (PW**2).sum()
                rv /= float(W.shape[0] - 1)
                return rv

            var_r = var_WW(self.gp[type].covar.W())
            var_c = sp.var(sp.dot(self.mean.W, self.gp[type].mean.b))
            D = self.gp[type].covar.d_inv()**(-1)
            var_n = (1 - 1 / float(D.shape[0])) * \
                D.sum() / float(self.Y.size - 1)
            # var_n = sp.diagonal(sp.diag(D)-sp.diag(D).mean(0)).sum()/float(self.Y.size-1)
            RV['var'] = sp.array([var_r, var_c, var_n])
            if 0 and self.Y.size < 5000:
                pdb.set_trace()
                Kr = sp.kron(RV['Cr'], sp.dot(self.Xr, self.Xr.T))
                Kn = sp.kron(RV['Cn'], sp.eye(self.Y.shape[0]))
                _var_r = sp.trace(Kr - Kr.mean(0)) / float(self.Y.size - 1)
                _var_n = sp.trace(Kn - Kn.mean(0)) / float(self.Y.size - 1)
                _var = sp.array([_var_r, var_c, _var_n])
                print(((_var - RV['var'])**2).mean())
            if type == 'full':

                def build_W(C):
                    S, U = la.eigh(C)
                    I = S > 1e-9
                    Ch = U[:, I] * S[I]**0.5
                    RV = sp.zeros(
                        (self.Y.shape[0], Ch.shape[1] * self.Xr.shape[1]))
                    RV[self.Ie] = sp.kron(Ch[0], self.Xr[self.Ie])
                    RV[~self.Ie] = sp.kron(Ch[1], self.Xr[~self.Ie])
                    return RV

                # calculate within region vcs
                Cr_block = sp.mean(RV['Cr']) * sp.ones(RV['Cr'].shape)
                Cr_rank1 = lowrank_approx(RV['Cr'], rank=1)
                var_block = var_WW(build_W(Cr_block))
                var_rank1 = var_WW(build_W(Cr_rank1))
                # trRr = (self.Xr**2).sum()
                # var_block = sp.trace(Cr_block)*trRr / float(self.Y.size-1)
                # var_rank1 = sp.trace(Cr_rank1)*trRr / float(self.Y.size-1)
                RV['var_r'] = sp.array(
                    [var_block, var_rank1 - var_block, var_r - var_rank1])
        return RV
Esempio n. 37
0
def compute_divKL(direction, variables, model, idx):
    """
        Function that computes the  Kullback–Leibler divergence of the model_cv using the variables : idx +/- one of variables
        Inputs:
            variables: the variable to add to idx
            model_cv:  the model build with all the variables
            idx:       the pool of retained variables
        Output:
            divKL: the estimated Kullback–Leibler divergence

        Used in GMM.forward_selection() and GMM.backward_selection()
    """
    # Get machine precision
    eps = sp.finfo(sp.float64).eps

    # Initialization
    divKL  = sp.zeros(variables.size)
    invCov = sp.empty((model.C,len(idx),len(idx)))

    if len(idx)==0:
        for k,var in enumerate(variables):
            for i in xrange(model.C):
                alphaI = 1/float(model.cov[i,var,var])
                if alphaI < eps:
                    alphaI = eps
                for j in xrange(i+1,model.C):
                    alphaJ = 1/float(model.cov[j,var,var])
                    if alphaJ < eps:
                        alphaJ = eps

                    md  = (model.mean[i,var]-model.mean[j,var])
                    divKL[k] += 0.5*( alphaI*model.cov[j,var,var] + alphaJ*model.cov[i,var,var] + md*(alphaI + alphaJ)*md ) * model.prop[i]*model.prop[j]
    else:
        # Compute invcov de idx
        for c in xrange(model.C):
            vp,Q,_ = model.decomposition(model.cov[c,idx,:][:,idx])
            invCov[c,:,:] = sp.dot(Q,((1/vp)*Q).T)
        del vp,Q

        if direction=='forward':
            invCov_update = sp.empty((model.C,len(idx)+1,len(idx)+1))
        elif direction=='backward':
            invCov_update = sp.empty((model.C,len(idx)-1,len(idx)-1))

        for k,var in enumerate(variables):
            if direction=='forward':
                id_t = list(idx)
                id_t.append(var)
            elif direction=='backward':
                id_t    = list(idx)
                id_t.remove(var)
                mask    = sp.ones(len(idx), dtype=bool)
                mask[k] = False

            if direction=='forward':
                for c in xrange(model.C):
                    alpha = model.cov[c,var,var] - sp.dot(model.cov[c,var,:][idx], sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T) )
                    if alpha < eps:
                        alpha = eps

                    invCov_update[c,:-1,:][:,:-1] = invCov[c,:,:] + 1/alpha * sp.outer( sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T) , sp.dot(model.cov[c,var,:][idx],invCov[c,:,:]) )
                    invCov_update[c,:-1,-1]       = - 1/alpha * sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T)
                    invCov_update[c,-1,:-1]       = - 1/alpha * sp.dot(model.cov[c,var,:][idx],invCov[c,:,:])
                    invCov_update[c,-1,-1]        = 1/alpha

            elif direction=='backward':
                for c in xrange(model.C):
                    invCov_update[c,:,:] = invCov[c,mask,:][:,mask] - 1/invCov[c,k,k] * sp.outer(model.cov[c,var,:][id_t] , model.cov[c,var,:][id_t])

            for i in xrange(model.C):
                for j in xrange(i+1,model.C):
                    md  = (model.mean[i,id_t]-model.mean[j,id_t])
                    divKL[k] += 0.5*( sp.trace( sp.dot( invCov_update[j,:,:],model.cov[i,id_t,:][:,id_t] ) + sp.dot( invCov_update[i,:,:],model.cov[j,id_t,:][:,id_t] ) ) + sp.dot(md,sp.dot(invCov_update[j,:,:]+invCov_update[i,:,:],md.T)) ) * model.prop[i]*model.prop[j]

    return divKL
Esempio n. 38
0
def sample_path(tdvp_class,num):
    """
    Performs a complete dissipative evolution for the defined number
    of steps and probes global operators after each step.
    
    Parameters
    ----------
    tdvp_class : tdvp_gen_instance
        class object to work on. Must be derived from
        tdvp_gen_dissipative
    num : int
        (Unique) Job number in the distributed computation framework.
        Also used to seed RNG in case the results shall be deterministically
        reproducible.
    """
    
    # Re-seed the random generator with a number based on the iteration number
    rand.seed((num))
    
    print ">> Starting job #"+str(num)
    #print ">> [" + str(num) + "] seeded with " + str(2222+num)
        
    return_dict = {}
    
    
    t = 0.    
    
    q = sp.empty(N + 1, dtype=sp.int32)
    q.fill(qn)    
    
    """
    Now we are ready to create an instance of the generic evoMPS class extended
    by take_step_dissipative(). In fact, the class to be used comes from the grid,
    so it could in general be a non-generic instance.
    """
    
    s = tdvp_class(N, D, q, ham)
    #s.randomize()
    
    #start = sp.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) / 4.0; #Totally mixed start
    #start = sp.array([[.9997 + 0.j,0,0,1e-5],[.0,0,.01,0],[0, 0, 1e-5,0],[.0,1e-8,0,0.1]]) #zEROstart
    #start = sp.array([[.01,0,0,1e-5],[.0,0,.01,0],[0, .97 + 0.j, 1e-5,0],[.0,1e-8,0,0.1]]) #UPstart
    start = sp.array([[.01,0,0,1e-5],[0, .97 + 0.j, 1e-5,0],[.0,0,.01,0],[.0,1e-8,0,0.1]]) #DOWNstart
    #start = sp.array([[.01,0,0,1e-5],[0, .0001 + 0.j, 1e-5,0],[.0,0,.01,0],[.0,1e-8,0,0.999]]) #AFstart    
    #start = sp.array([[0.0001, 0.00948, 0.0005, 0.0001],[0.0005, 0.0474, 0.0025, 0.0005], [0.0001, 0.00948, 
  #0.0005, 0.0001],[0.00948, 0.898704, 0.0474, 
  #0.00948]]) # Half Doubly occupied starting state
    
    start = start / sp.trace(start)
    for i in xrange(1,N):
        s.apply_op_1s(start,i)
        #s.apply_op_1s(start_1,i)
        #s.apply_op_1s(start_2,i+1)
    
        
    #s.A[N] = sp.array([[1 + 0.j, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]);

    #print s.A[N-1]
    
    #quit()
    
    for i in xrange(total_steps):
        
        # Define necessary operators...

        #print "Starting step", i

        p1 = sp.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]);
        p2 = sp.array([[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]);
        p3 = sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]);
        p4 = sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]);
        
        EINS1 = sp.eye(2);
        EINS2 = sp.eye(4);
        EINS3 = sp.eye(8);
        EINS4 = sp.eye(16);
        
        PLUS = [[0, 0],[1, 0]];
        MINUS = [[0, 1],[0, 0]];
        PLUSMINUS = [[0, 0],[0, 1]];
        MINUSPLUS = [[1, 0],[0, 0]];
        
        Z = [[1, 0],[0, -1]];
        X = [[0, 1],[1, 0]];
        Y = [[0, -1.j],[1.j, 0]];
        
        C1UP = sp.kron(MINUS, EINS3);
        C1DAGUP = sp.kron(PLUS, EINS3);
        C1DOWN = sp.kron(Z, sp.kron(MINUS, EINS2));
        C1DAGDOWN = sp.kron(Z, sp.kron(PLUS, EINS2));
        C2UP = sp.kron(Z, sp.kron(Z, sp.kron(MINUS, EINS1)));
        C2DAGUP = sp.kron(Z, sp.kron(Z, sp.kron(PLUS, EINS1)));
        C2DOWN = sp.kron(Z, sp.kron(Z, sp.kron(Z, MINUS)));
        C2DAGDOWN = sp.kron(Z, sp.kron(Z, sp.kron(Z, PLUS)));
        
        N1UP = sp.dot(C1DAGUP,C1UP);
        N1DOWN = sp.dot(C1DAGDOWN,C1DOWN);
        N2UP = sp.dot(C2DAGUP,C2UP);
        N2DOWN = sp.dot(C2DAGDOWN,C2DOWN);
        
        P1UP = sp.dot(C1DAGUP,C1UP) - sp.dot(C1DAGUP,sp.dot(C1UP,sp.dot(C1DAGDOWN,C1DOWN)));
        P1DOWN = sp.dot(C1DAGDOWN,C1DOWN) - sp.dot(C1DAGDOWN,sp.dot(C1DOWN,sp.dot(C1DAGUP,C1UP)));
        P2UP = sp.dot(C2DAGUP,C2UP) - sp.dot(C2DAGUP,sp.dot(C2UP,sp.dot(C2DAGDOWN,C2DOWN)));
        P2DOWN = sp.dot(C2DAGDOWN,C2DOWN) - sp.dot(C2DAGDOWN,sp.dot(C2DOWN,sp.dot(C2DAGUP,C2UP)));
        
        JEINS12UP = sqrtgamma1 * sp.dot(C1DAGDOWN,sp.dot(C2UP,sp.dot(P1UP,P2UP)));
        JEINS12DOWN = sqrtgamma1 * sp.dot(C1DAGUP,sp.dot(C2DOWN,sp.dot(P1DOWN,P2DOWN)));
        JEINS21UP = sqrtgamma1 * sp.dot(C2DAGDOWN,sp.dot(C1UP,sp.dot(P2UP,P1UP)));
        JEINS21DOWN = sqrtgamma1 * sp.dot(C2DAGUP,sp.dot(C1DOWN,sp.dot(P2DOWN,P1DOWN)));
        JZWEI12UP = sqrtgamma1 * sp.conj(JEINS12UP).T;
        JZWEI12DOWN = sqrtgamma1 * sp.conj(JEINS12DOWN).T;        
        JZWEI21UP = sqrtgamma1 * sp.conj(JEINS21UP).T;
        JZWEI21DOWN = sqrtgamma1 * sp.conj(JEINS21DOWN).T;
        JDREI12 = sqrtgamma2 * sp.dot(C1DAGDOWN,sp.dot(C2DOWN,N2UP));
        JDREI21 = sqrtgamma2 * sp.dot(C2DAGDOWN,sp.dot(C1DOWN,N1UP));
            
        H = C1DAGUP.dot(C2UP) + C2DAGUP.dot(C1UP) + (C1DAGDOWN.dot(C2DOWN)) + (C2DAGDOWN.dot(C1DOWN)) + U * ((C1DAGUP.dot(C1UP.dot(C1DAGDOWN.dot(C1DOWN))))+(C2DAGUP.dot(C2UP.dot(C2DAGDOWN.dot(C2DOWN)))))
        
        LHL = (JEINS12UP.conj().T.dot(JEINS12UP) +
              JEINS12DOWN.conj().T.dot(JEINS12DOWN) +
              JEINS21UP.conj().T.dot(JEINS21UP) +
              JEINS21DOWN.conj().T.dot(JEINS21DOWN) +
              JZWEI12UP.conj().T.dot(JZWEI12UP) + 
              JZWEI12DOWN.conj().T.dot(JZWEI12DOWN) + 
              JZWEI21UP.conj().T.dot(JZWEI21UP) + 
              JZWEI21DOWN.conj().T.dot(JZWEI21DOWN) +
              JDREI12.conj().T.dot(JDREI12) + 
              JDREI21.conj().T.dot(JDREI21)
              )
        Q = H - 0.5 * LHL
        L1_up = JEINS12UP + JEINS21UP
        L1_down = JEINS12DOWN + JEINS21DOWN
        L2_up = JZWEI12UP + JZWEI21UP
        L2_down = JZWEI12DOWN + JZWEI21DOWN
        L3 = JDREI12 + JDREI21  
     
        # Integrate exactly.

        #s.update(auto_truncate=True, restore_CF=True)
        #s.take_step_RK4(step)
        
        # Or.... take a dissipative approach.
        s.take_step_dissipative(step, l_nns)
        
        # Real time evolution

        timestep_meas = sp.zeros((4), dtype=sp.complex128);
        NsSum = sp.zeros((3), dtype=sp.complex128);
        DoubleSum = 0
        mafSum = 0
        mafAbsSum = 0
        AFBSum = 0
        entSum = 0
        afbop = sp.kron(p2+p3,p2+p3).reshape(4,4,4,4)
        afbop = afbop/sp.trace(afbop)
        for site in xrange(1,N+1):
            timestep_meas[0]+= np.real(s.expect_1s(p1,site)) / N;
            timestep_meas[1]+= np.real(s.expect_1s(p2,site)) / N;
            timestep_meas[2]+= np.real(s.expect_1s(p3,site)) / N;
            timestep_meas[3]+= np.real(s.expect_1s(p4,site)) / N;
            DoubleSum+= 2 * np.real(s.expect_1s(sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]),site)) / (N);
            rho1 = s.density_1s(site)
            entSum += - sp.trace(rho1.dot(np.log(rho1))) / N
            if site < N:
                AFBSum+= np.real(s.expect_2s(afbop,site)) / (2*(N-1))
                mafSum+=(np.real(s.expect_2s((N1UP + N2UP).reshape(4,4,4,4),site)) - np.real(s.expect_2s((N1DOWN + N2DOWN).reshape(4,4,4,4),site))) / (2*(N-1)); # Fraction of sites with antiferromagnetic neighbors
                mafAbsSum= np.abs((np.real(s.expect_2s((N1UP+N2UP).reshape(4,4,4,4),site)) + np.real(s.expect_2s((N1DOWN + N2DOWN).reshape(4,4,4,4),site)))) / (2*(N-1));
                nupval = np.real(s.expect_2s((N1UP + N2UP).reshape(4,4,4,4),site)) / (2*(N-1));
                ndownval = np.real(s.expect_2s((N1DOWN + N2DOWN).reshape(4,4,4,4),site)) / (2*(N-1));
                nsumval = np.real(s.expect_2s((N1UP + N2UP + N1DOWN + N2DOWN).reshape(4,4,4,4),site)) / (2*(N-1));                
                NsSum[0]+= nsumval
                NsSum[1]+= nupval
                NsSum[2]+= ndownval
                
        MP[i] = timestep_meas
        DO[i] = DoubleSum
        Ns[i] = NsSum
        maf[i] = mafSum
        mafabs[i] = mafAbsSum
        AFB[i] = AFBSum
        ENT[i] = entSum
        
        #print "Step", i, "completed."        
        
        t += step
        
        if(i == 5* total_steps / 10):
            print "job ",num,": 50% Complete."
    
    return_dict["MP"] = MP
    return_dict["Ns"] = Ns
    return_dict["DO"] = DO
    return_dict["maf"] = maf
    return_dict["mafabs"] = mafabs
    return_dict["AFB"] = AFB
    return_dict["ENT"] = ENT
    
    print "<< Job #"+str(num)+" done."
    return return_dict
Esempio n. 39
0
    def score_2_dof(self, X, snp_dim='col', debug=False):
        """
        Parameters
        ----------
        X : (`N`, `1`) ndarray
            genotype vector (TODO: X should be small)

        Returns
        -------
        pvalue : float
            P value
        """
        #1. calculate Qs and pvs
        Q_rho = sp.zeros(len(self.rho_list))
        Py = P(self.gp, self.y)
        xoPy = X * Py
        for i in xrange(len(self.rho_list)):
            rho = self.rho_list[i]
            LT = sp.vstack((rho**0.5 * self.vec_ones,
                            (1 - rho)**0.5 * self.Env.T))
            LTxoPy = sp.dot(LT, X * Py)
            Q_rho[i] = 0.5 * sp.dot(LTxoPy.T, LTxoPy)

        # Calculating pvs is split into 2 steps
        # If we only consider one value of rho i.e. equivalent to SKAT and used for interaction test
        if len(self.rho_list) == 1:
            rho = self.rho_list[0]
            L = sp.hstack((rho**0.5 * self.vec_ones.T,
                           (1 - rho)**0.5 * self.Env))
            xoL = X * L
            PxoL = P(self.gp, xoL)
            LToxPxoL = 0.5 * sp.dot(xoL.T, PxoL)
            pval = self.qwedaviesskat.getPv(Q_rho[0], LToxPxoL)
            # Script ends here for interaction test
            return pval
        #or if we consider multiple values of rho i.e. equivalent to SKAT-O and used for association test
        else:
            pliumod = sp.zeros((len(self.rho_list), 4))
            for i in xrange(len(self.rho_list)):
                rho = self.rho_list[i]
                L = sp.hstack((rho**0.5 * self.vec_ones.T,
                               (1 - rho)**0.5 * self.Env))
                xoL = X * L
                PxoL = P(self.gp, xoL)
                LToxPxoL = 0.5 * sp.dot(xoL.T, PxoL)
                eighQ, UQ = la.eigh(LToxPxoL)
                pliumod[i, ] = self.qweliumod.getPv(Q_rho[i], eighQ)
            T = pliumod[:, 0].min()
            rho_opt = pliumod[:, 0].argmin()
            optimal_rho = self.rho_list[rho_opt]
            # if optimal_rho == 0.999:
            #    optimal_rho = 1

            # 2. Calculate qmin
            qmin = sp.zeros(len(self.rho_list))
            percentile = 1 - T
            for i in xrange(len(self.rho_list)):
                q = st.chi2.ppf(percentile, pliumod[i, 3])
                # Recalculate p-value for each Q rho of seeing values at least as extreme as q again using the modified matching moments method
                qmin[i] = (q - pliumod[i, 3]) / (
                    2 * pliumod[i, 3])**0.5 * pliumod[i, 2] + pliumod[i, 1]

            # 3. Calculate quantites that occur in null distribution
            Px1 = P(self.gp, X)
            m = 0.5 * sp.dot(X.T, Px1)
            xoE = X * self.Env
            PxoE = P(self.gp, xoE)
            ETxPxE = 0.5 * sp.dot(xoE.T, PxoE)
            ETxPx1 = sp.dot(xoE.T, Px1)
            ETxPx11xPxE = 0.25 / m * sp.dot(ETxPx1, ETxPx1.T)
            ZTIminusMZ = ETxPxE - ETxPx11xPxE
            eigh, vecs = la.eigh(ZTIminusMZ)

            eta = sp.dot(ETxPx11xPxE, ZTIminusMZ)
            vareta = 4 * sp.trace(eta)

            OneZTZE = 0.5 * sp.dot(X.T, PxoE)
            tau_top = sp.dot(OneZTZE, OneZTZE.T)
            tau_rho = sp.zeros(len(self.rho_list))
            for i in xrange(len(self.rho_list)):
                tau_rho[i] = self.rho_list[i] * m + (
                    1 - self.rho_list[i]) / m * tau_top

            MuQ = sp.sum(eigh)
            VarQ = sp.sum(eigh**2) * 2 + vareta
            KerQ = sp.sum(eigh**4) / (sp.sum(eigh**2)**2) * 12
            Df = 12 / KerQ

            #4. Integration
            pvalue = self.qwedavies.getPv(qmin, MuQ, VarQ, KerQ, eigh, vareta,
                                          Df, tau_rho, self.rho_list, T)

            # Final correction to make sure that the p-value returned is sensible
            multi = 3
            if len(self.rho_list) < 3:
                multi = 2
            idx = sp.where(pliumod[:, 0] > 0)[0]
            pval = pliumod[:, 0].min() * multi
            if pvalue <= 0 or len(idx) < len(self.rho_list):
                pvalue = pval
            if pvalue == 0:
                if len(idx) > 0:
                    pvalue = pliumod[:, 0][idx].min()

            if debug:
                info = {
                    'Qs': Q_rho,
                    'pvs_liu': pliumod,
                    'qmin': qmin,
                    'MuQ': MuQ,
                    'VarQ': VarQ,
                    'KerQ': KerQ,
                    'lambd': eigh,
                    'VarXi': vareta,
                    'Df': Df,
                    'tau': tau_rho
                }
                return pvalue, info
            else:
                return pvalue
Esempio n. 40
0
def sample_path(tdvp_class, num):
    """
    Performs a complete dissipative evolution for the defined number
    of steps and probes global operators after each step.
    
    Parameters
    ----------
    tdvp_class : tdvp_gen_instance
        class object to work on. Must be derived from
        tdvp_gen_dissipative
    num : int
        (Unique) Job number in the distributed computation framework.
        Also used to seed RNG in case the results shall be deterministically
        reproducible.
    """

    # Re-seed the random generator with a number based on the iteration number
    rand.seed((num))

    print ">> Starting job #" + str(num)
    #print ">> [" + str(num) + "] seeded with " + str(2222+num)

    return_dict = {}

    t = 0.

    q = sp.empty(N + 1, dtype=sp.int32)
    q.fill(qn)
    """
    Now we are ready to create an instance of the generic evoMPS class extended
    by take_step_dissipative(). In fact, the class to be used comes from the grid,
    so it could in general be a non-generic instance.
    """

    s = tdvp_class(N, D, q, ham, ham_sites=2)
    #s.randomize()

    #start = sp.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) / 4.0; #Totally mixed start
    #start = sp.array([[.9997 + 0.j,0,0,1e-5],[.0,0,.01,0],[0, 0, 1e-5,0],[.0,1e-8,0,0.1]]) #zEROstart
    start = sp.array([[.01, 0, 0, 1e-5], [.0, 0, .01, 0],
                      [0, .97 + 0.j, 1e-5, 0], [.0, 1e-8, 0, 0.1]])  #UPstart
    #start = sp.array([[.01,0,0,1e-5],[0, .97 + 0.j, 1e-5,0],[.0,0,.01,0],[.0,1e-8,0,0.1]]) #DOWNstart
    #start = sp.array([[.01,0,0,1e-5],[0, .0001 + 0.j, 1e-5,0],[.0,0,.01,0],[.0,1e-8,0,0.999]]) #AFstart
    #start = sp.array([[0.0001, 0.00948, 0.0005, 0.0001],[0.0005, 0.0474, 0.0025, 0.0005], [0.0001, 0.00948,
    #0.0005, 0.0001],[0.00948, 0.898704, 0.0474,
    #0.00948]]) # Half Doubly occupied starting state

    start = start / sp.trace(start)
    for i in xrange(1, N):
        s.apply_op_1s(start, i)
        #s.apply_op_1s(start_1,i)
        #s.apply_op_1s(start_2,i+1)

    #s.A[N] = sp.array([[1 + 0.j, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]);

    #print s.A[N-1]

    #quit()

    s.update()

    for i in xrange(total_steps):

        # Define necessary operators...

        #print "Starting step", i

        p1 = sp.array([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
        p2 = sp.array([[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
        p3 = sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]])
        p4 = sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])

        EINS1 = sp.eye(2)
        EINS2 = sp.eye(4)
        EINS3 = sp.eye(8)
        EINS4 = sp.eye(16)

        PLUS = [[0, 0], [1, 0]]
        MINUS = [[0, 1], [0, 0]]
        PLUSMINUS = [[0, 0], [0, 1]]
        MINUSPLUS = [[1, 0], [0, 0]]

        Z = [[1, 0], [0, -1]]
        X = [[0, 1], [1, 0]]
        Y = [[0, -1.j], [1.j, 0]]

        C1UP = sp.kron(MINUS, EINS3)
        C1DAGUP = sp.kron(PLUS, EINS3)
        C1DOWN = sp.kron(Z, sp.kron(MINUS, EINS2))
        C1DAGDOWN = sp.kron(Z, sp.kron(PLUS, EINS2))
        C2UP = sp.kron(Z, sp.kron(Z, sp.kron(MINUS, EINS1)))
        C2DAGUP = sp.kron(Z, sp.kron(Z, sp.kron(PLUS, EINS1)))
        C2DOWN = sp.kron(Z, sp.kron(Z, sp.kron(Z, MINUS)))
        C2DAGDOWN = sp.kron(Z, sp.kron(Z, sp.kron(Z, PLUS)))

        N1UP = sp.dot(C1DAGUP, C1UP)
        N1DOWN = sp.dot(C1DAGDOWN, C1DOWN)
        N2UP = sp.dot(C2DAGUP, C2UP)
        N2DOWN = sp.dot(C2DAGDOWN, C2DOWN)

        P1UP = sp.dot(C1DAGUP, C1UP) - sp.dot(
            C1DAGUP, sp.dot(C1UP, sp.dot(C1DAGDOWN, C1DOWN)))
        P1DOWN = sp.dot(C1DAGDOWN, C1DOWN) - sp.dot(
            C1DAGDOWN, sp.dot(C1DOWN, sp.dot(C1DAGUP, C1UP)))
        P2UP = sp.dot(C2DAGUP, C2UP) - sp.dot(
            C2DAGUP, sp.dot(C2UP, sp.dot(C2DAGDOWN, C2DOWN)))
        P2DOWN = sp.dot(C2DAGDOWN, C2DOWN) - sp.dot(
            C2DAGDOWN, sp.dot(C2DOWN, sp.dot(C2DAGUP, C2UP)))

        JEINS12UP = sqrtgamma1 * sp.dot(C1DAGDOWN,
                                        sp.dot(C2UP, sp.dot(P1UP, P2UP)))
        JEINS12DOWN = sqrtgamma1 * sp.dot(
            C1DAGUP, sp.dot(C2DOWN, sp.dot(P1DOWN, P2DOWN)))
        JEINS21UP = sqrtgamma1 * sp.dot(C2DAGDOWN,
                                        sp.dot(C1UP, sp.dot(P2UP, P1UP)))
        JEINS21DOWN = sqrtgamma1 * sp.dot(
            C2DAGUP, sp.dot(C1DOWN, sp.dot(P2DOWN, P1DOWN)))
        JZWEI12UP = sqrtgamma1 * sp.conj(JEINS12UP).T
        JZWEI12DOWN = sqrtgamma1 * sp.conj(JEINS12DOWN).T
        JZWEI21UP = sqrtgamma1 * sp.conj(JEINS21UP).T
        JZWEI21DOWN = sqrtgamma1 * sp.conj(JEINS21DOWN).T
        JDREI12 = sqrtgamma2 * sp.dot(C1DAGDOWN, sp.dot(C2DOWN, N2UP))
        JDREI21 = sqrtgamma2 * sp.dot(C2DAGDOWN, sp.dot(C1DOWN, N1UP))

        H = C1DAGUP.dot(C2UP) + C2DAGUP.dot(C1UP) + (C1DAGDOWN.dot(C2DOWN)) + (
            C2DAGDOWN.dot(C1DOWN)) + U * (
                (C1DAGUP.dot(C1UP.dot(C1DAGDOWN.dot(C1DOWN)))) +
                (C2DAGUP.dot(C2UP.dot(C2DAGDOWN.dot(C2DOWN)))))

        LHL = (JEINS12UP.conj().T.dot(JEINS12UP) +
               JEINS12DOWN.conj().T.dot(JEINS12DOWN) +
               JEINS21UP.conj().T.dot(JEINS21UP) +
               JEINS21DOWN.conj().T.dot(JEINS21DOWN) +
               JZWEI12UP.conj().T.dot(JZWEI12UP) +
               JZWEI12DOWN.conj().T.dot(JZWEI12DOWN) +
               JZWEI21UP.conj().T.dot(JZWEI21UP) +
               JZWEI21DOWN.conj().T.dot(JZWEI21DOWN) +
               JDREI12.conj().T.dot(JDREI12) + JDREI21.conj().T.dot(JDREI21))
        Q = H - 0.5 * LHL
        L1_up = JEINS12UP + JEINS21UP
        L1_down = JEINS12DOWN + JEINS21DOWN
        L2_up = JZWEI12UP + JZWEI21UP
        L2_down = JZWEI12DOWN + JZWEI21DOWN
        L3 = JDREI12 + JDREI21

        # Integrate exactly.

        #s.update(auto_truncate=True, restore_CF=True)
        #s.take_step_RK4(step)

        # Or.... take a dissipative approach.

        s.take_step_dissipative(step, l_nns)

        # Real time evolution

        timestep_meas = sp.zeros((4), dtype=sp.complex128)
        NsSum = sp.zeros((3), dtype=sp.complex128)
        DoubleSum = 0
        mafSum = 0
        mafAbsSum = 0
        AFBSum = 0
        entSum = 0
        afbop = sp.kron(p2 + p3, p2 + p3).reshape(4, 4, 4, 4)
        afbop = afbop / sp.trace(afbop)
        for site in xrange(1, N + 1):
            timestep_meas[0] += np.real(s.expect_1s(p1, site)) / N
            timestep_meas[1] += np.real(s.expect_1s(p2, site)) / N
            timestep_meas[2] += np.real(s.expect_1s(p3, site)) / N
            timestep_meas[3] += np.real(s.expect_1s(p4, site)) / N
            DoubleSum += 2 * np.real(
                s.expect_1s(
                    sp.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
                              [0, 0, 0, 1]]), site)) / (N)
            rho1 = s.density_1s(site)
            entSum += -sp.trace(rho1.dot(np.log(rho1))) / N
            if site < N:
                AFBSum += np.real(s.expect_2s(afbop, site)) / (2 * (N - 1))
                mafSum += (np.real(
                    s.expect_2s(
                        (N1UP + N2UP).reshape(4, 4, 4, 4), site)) - np.real(
                            s.expect_2s((N1DOWN + N2DOWN).reshape(4, 4, 4, 4),
                                        site))) / (2 * (N - 1))
                # Fraction of sites with antiferromagnetic neighbors
                mafAbsSum = np.abs((np.real(
                    s.expect_2s(
                        (N1UP + N2UP).reshape(4, 4, 4, 4), site)) + np.real(
                            s.expect_2s((N1DOWN + N2DOWN).reshape(4, 4, 4, 4),
                                        site)))) / (2 * (N - 1))
                #nupval = np.real(s.expect_2s((N1UP + N2UP).reshape(4,4,4,4),site)) / (2*(N-1));
                nupval = np.real(
                    s.expect_2s(
                        (N1UP + N2UP).reshape(4, 4, 4, 4), site)) / (2 *
                                                                     (N - 1))
                #ndownval = np.real(s.expect_2s((N1DOWN + N2DOWN).reshape(4,4,4,4),site)) / (2*(N-1));
                ndownval = np.real(
                    s.expect_2s((N1DOWN + N2DOWN).reshape(4, 4, 4, 4),
                                site)) / (2 * (N - 1))
                nsumval = np.real(
                    s.expect_2s((N1UP + N2UP + N1DOWN + N2DOWN).reshape(
                        4, 4, 4, 4), site)) / (2 * (N - 1))
                NsSum[0] += nsumval
                NsSum[1] += nupval
                NsSum[2] += ndownval
                rho2 = s.density_2s(site, site + 1)
                spin_structure_op_up = sp.kron(
                    Z, sp.kron(EINS1, sp.kron(Z, EINS1)))
                spin_structure_op_down = sp.kron(EINS1,
                                                 sp.kron(Z, sp.kron(EINS1, Z)))

                norm_factor = 4.0 / (N**2)
                spin_structure_up_value = np.real(
                    norm_factor *
                    (sp.exp(1.j * sp.pi) *
                     sp.trace(sp.dot(rho2, spin_structure_op_up))))
                spin_structure_down_value = np.real(
                    norm_factor *
                    (sp.exp(-1.j * sp.pi) *
                     sp.trace(sp.dot(rho2, spin_structure_op_down))))

                SSTRUCT[i, 0] += spin_structure_up_value / N
                SSTRUCT[i, 1] += spin_structure_down_value / N

        MP[i] = timestep_meas
        DO[i] = DoubleSum
        Ns[i] = NsSum
        maf[i] = mafSum
        mafabs[i] = mafAbsSum
        AFB[i] = AFBSum
        ENT[i] = entSum

        #print "Step", i, "completed."

        t += step

        if (i == 1 * total_steps / 10):
            print "job ", num, ": 10% Complete."
        if (i == 2 * total_steps / 10):
            print "job ", num, ": 20% Complete."
        if (i == 3 * total_steps / 10):
            print "job ", num, ": 30% Complete."
        if (i == 4 * total_steps / 10):
            print "job ", num, ": 40% Complete."
        if (i == 5 * total_steps / 10):
            print "job ", num, ": 50% Complete."
        if (i == 6 * total_steps / 10):
            print "job ", num, ": 60% Complete."
        if (i == 7 * total_steps / 10):
            print "job ", num, ": 70% Complete."
        if (i == 8 * total_steps / 10):
            print "job ", num, ": 80% Complete."
        if (i == 9 * total_steps / 10):
            print "job ", num, ": 90% Complete."

    return_dict["MP"] = MP
    return_dict["Ns"] = Ns
    return_dict["DO"] = DO
    return_dict["maf"] = maf
    return_dict["mafabs"] = mafabs
    return_dict["AFB"] = AFB
    return_dict["ENT"] = ENT
    return_dict["SSTRUCT"] = SSTRUCT

    print "<< Job #" + str(num) + " done."
    return return_dict
Esempio n. 41
0
 def _fit(self, type, vc=False):
     # 2. init
     if type == 'null':
         self.gp[type].covar.Cg.setCovariance(0.5 * self.covY)
         self.gp[type].covar.Cn.setCovariance(0.5 * self.covY)
     elif type == 'full':
         Cg0_K = self.gp['null'].covar.Cg.K()
         Cn0_K = self.gp['null'].covar.Cn.K()
         self.gp[type].covar.Cr.setCovariance((Cn0_K + Cg0_K) / 3.)
         self.gp[type].covar.Cg.setCovariance(2. * Cg0_K / 3.)
         self.gp[type].covar.Cn.setCovariance(2. * Cn0_K / 3.)
     elif type == 'block':
         Crf_K = self.gp['full'].covar.Cr.K()
         Cnf_K = self.gp['full'].covar.Cn.K()
         self.gp[type].covar.Cr.scale = sp.mean(Crf_K)
         self.gp[type].covar.Cn.setCovariance(Cnf_K)
     elif type == 'rank1':
         Crf_K = self.gp['full'].covar.Cr.K()
         Cnf_K = self.gp['full'].covar.Cn.K()
         self.gp[type].covar.Cr.setCovariance(Crf_K)
         self.gp[type].covar.Cn.setCovariance(Cnf_K)
     else:
         print('poppo')
     self.gp[type].optimize(factr=self.factr, verbose=False)
     RV = {
         'Cg': self.gp[type].covar.Cg.K(),
         'Cn': self.gp[type].covar.Cn.K(),
         'LML': sp.array([self.gp[type].LML()]),
         'LMLgrad':
         sp.array([sp.mean((self.gp[type].LML_grad()['covar'])**2)])
     }
     if type == 'null':
         RV['Cr'] = sp.zeros(RV['Cg'].shape)
     else:
         RV['Cr'] = self.gp[type].covar.Cr.K()
     if vc:
         # tr(P CoR) = tr(C)tr(R) - tr(Ones C) tr(Ones R) / float(NP)
         #           = tr(C)tr(R) - C.sum() * R.sum() / float(NP)
         trRr = (self.Xr**2).sum()
         var_r = sp.trace(RV['Cr']) * trRr / float(self.Y.size - 1)
         var_g = sp.trace(RV['Cg']) * self.trRg / float(self.Y.size - 1)
         var_n = sp.trace(RV['Cn']) * self.Y.shape[0]
         var_n -= RV['Cn'].sum() / float(RV['Cn'].shape[0])
         var_n /= float(self.Y.size - 1)
         RV['var'] = sp.array([var_r, var_g, var_n])
         if 0 and self.Y.size < 5000:
             pdb.set_trace()
             Kr = sp.kron(RV['Cr'], sp.dot(self.Xr, self.Xr.T))
             Kn = sp.kron(RV['Cn'], sp.eye(self.Y.shape[0]))
             _var_r = sp.trace(Kr - Kr.mean(0)) / float(self.Y.size - 1)
             _var_n = sp.trace(Kn - Kn.mean(0)) / float(self.Y.size - 1)
             _var = sp.array([_var_r, var_g, _var_n])
             print(((_var - RV['var'])**2).mean())
         if type == 'full':
             # calculate within region vcs
             Cr_block = sp.mean(RV['Cr']) * sp.ones(RV['Cr'].shape)
             Cr_rank1 = lowrank_approx(RV['Cr'], rank=1)
             var_block = sp.trace(Cr_block) * trRr / float(self.Y.size - 1)
             var_rank1 = sp.trace(Cr_rank1) * trRr / float(self.Y.size - 1)
             RV['var_r'] = sp.array(
                 [var_block, var_rank1 - var_block, var_r - var_rank1])
     return RV
Esempio n. 42
0
    def train(self,
              x,
              y,
              sig=None,
              dc=None,
              threshold=None,
              fast=None,
              E_=None,
              Beta_=None):
        '''
        The function trains the pgpda model using the training samples
        Inputs:
        x: the samples matrix of size n x d, for the precomputed case (self.precomputed==1), x is a KERNEL object.
        y: the vector with label of size n
        sig: the parameter of the kernel function
        dc: the number of dimension of the singanl subspace
        threshold: the value of the cummulative variance that should be reached
        fast = option used to perform a fast CV: only the parameter dc/threshold is learn
        
        Outputs:
        None - The model is included/updated in the object
        '''

        # Initialization
        n = y.shape[0]
        C = int(y.max())
        eps = sp.finfo(sp.float64).eps
        list_model_dc = 'NM1 NM3 NM4'

        if (sig is None) and (self.sig is None):
            self.sig = 0.5
        elif self.sig is None:
            self.sig = sig

        if (dc is None) and (self.dc is None):
            self.dc = 2
        elif self.dc is None:
            self.dc = dc

        if (threshold is None) and (self.threshold is None):
            self.threshold = 0.95
        elif self.threshold is None:
            self.threshold = threshold

        # Check of consistent dimension
        if (list_model_dc.find(self.model) > -1):
            for i in range(C):
                ni = sp.size(sp.where(y == (i + 1))[0])
                if self.dc >= ni - 1:
                    self.dc = ni - 2

        # Estimate the parameters of each class
        for i in range(C):
            t = sp.where(y == (i + 1))[0]
            self.ni.append(sp.size(t))
            self.prop.append(float(self.ni[i]) / n)

            if fast is None:
                # Compute Mi
                Ki = KERNEL()
                if self.precomputed is None:
                    Ki.compute_kernel(x[t, :],
                                      kernel=self.kernel,
                                      sig=self.sig)
                else:
                    Ki.K = x.K[t, :][:, t].copy()
                    Ki.rank = Ki.K.shape[0]

                self.ri.append(Ki.rank - 1)
                Ki.center_kernel()
                Ki.scale_kernel(self.ni[i])
                TraceKi = sp.trace(Ki.K)

                # Eigenvalue decomposition
                E, Beta = linalg.eigh(Ki.K)
                idx = E.argsort()[::-1]
                E = E[idx]
                E[E < eps] = eps
                Beta = Beta[:, idx]
            else:
                E = E_[i]
                Beta = Beta_[i]
                self.ri.append(E.size - 1)
                TraceKi = sp.sum(E)

            # Parameter estimation
            if list_model_dc.find(self.model) == -1:
                di = estim_d(E[0:self.ri[i] - 1], self.threshold)
            else:
                di = self.dc
            self.di.append(di)
            self.a.append(E[0:di])
            self.b.append((TraceKi - sp.sum(self.a[i])) / (self.ri[i] - di))

            if self.b[i] < eps:  # Sanity check for numerical precision
                self.b[i] = eps
                self.ib.append(1.0 / eps)
            else:
                self.ib.append(1 / self.b[i])
            self.Beta.append(Beta[:, 0:di])
            del Beta, E

        # Finish the estimation for the different models
        if self.model == 'NM0' or self.model == 'NM1':
            for i in range(C):
                # Compute the value of matrix A
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) /
                                       self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        elif self.model == 'NM2' or self.model == 'NM3':
            for i in range(C):
                # Update the value of a
                self.a[i][:] = sp.mean(self.a[i])
                # Compute the value of matrix A
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) /
                                       self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        elif self.model == 'NM4':
            # Compute the value of a
            al = sp.zeros((self.dc))
            for i in range(self.dc):
                for j in range(C):
                    al[i] += self.prop[j] * self.a[j][i]
            for i in range(C):
                self.a[i] = al.copy()
                temp = self.Beta[i] * ((1 / self.a[i] - self.ib[i]) /
                                       self.a[i]).reshape(self.di[i])
                self.A.append(sp.dot(temp, self.Beta[i].T) / self.ni[i])

        self.A = sp.asarray(self.A)
Esempio n. 43
0
    def test_gpbase(self):

        covar = linear.LinearCF(n_dimensions=self.n_dimensions)
        n_train = self.X['train'].shape[0]

        theta = 1E-1
        prior_cov = priors.GaussianPrior(key='covar',theta=SP.array([1.]))
        prior_lik = priors.GaussianPrior(key='lik',theta=SP.array([1.]))
        prior = priors.PriorList([prior_cov,prior_lik])
        
        lik = likelihood_base.GaussIsoLik()
        gp = gp_base.GP(covar_r=covar,likelihood=lik,prior=prior)
        gp.setData(Y=self.Yuni['train'],X=self.X['train'])

        # log likelihood and gradient derivation
        hyperparams = {'covar':SP.array([0.5]), 'lik':SP.array([0.5])}
        LML = gp.LML(hyperparams)
        LMLgrad = gp.LMLgrad(hyperparams)
        
        K = covar.K(hyperparams['covar']) + lik.K(hyperparams['lik'],n_train)
        Kgrad_covar = covar.Kgrad_theta(hyperparams['covar'],0)
        Kgrad_lik = lik.Kgrad_theta(hyperparams['lik'],n_train,0)

        KinvY = LA.solve(K,self.Yuni['train'])
        _LML = self.n_train/2*SP.log(2*SP.pi) + 0.5*SP.log(LA.det(K)) + 0.5*(self.Yuni['train']*KinvY).sum() + prior.LML(hyperparams)
        LMLgrad_covar = 0.5 * SP.trace(LA.solve(K, Kgrad_covar)) - 0.5*SP.dot(KinvY.T,SP.dot(Kgrad_covar,KinvY))
        LMLgrad_covar+= prior_cov.LMLgrad(hyperparams)['covar']
        LMLgrad_lik = 0.5 * SP.trace(LA.solve(K, Kgrad_lik)) - 0.5*SP.dot(KinvY.T,SP.dot(Kgrad_lik,KinvY))
        LMLgrad_lik+= prior_lik.LMLgrad(hyperparams)['lik']

        
        assert SP.allclose(LML,_LML), 'ouch, marginal log likelihood does not match'
        assert SP.allclose(LMLgrad['covar'], LMLgrad_covar), 'ouch, gradient with respect to theta does not match'
        assert SP.allclose(LMLgrad['lik'], LMLgrad_lik), 'ouch, gradient with respect to theta does not match'

        # predict
        Ystar = gp.predict(hyperparams,self.X['test'])
        Kstar = covar.Kcross(hyperparams['covar'])
        _Ystar = SP.dot(Kstar.T,LA.solve(K,self.Yuni['train'])).flatten()
        assert SP.allclose(Ystar,_Ystar), 'ouch, predictions, do not match'
        
        # optimize
        opts = {'gradcheck':True,'messages':False}
        hyperparams_opt,lml_opt = optimize_base.opt_hyper(gp,hyperparams,opts=opts)
        
        # log likelihood and gradient derivation
        LML = gp.LML(hyperparams_opt)
        LMLgrad = gp.LMLgrad(hyperparams_opt)

        K = covar.K(hyperparams_opt['covar']) + lik.K(hyperparams_opt['lik'],n_train)
        Kgrad_covar = covar.Kgrad_theta(hyperparams_opt['covar'],0)
        Kgrad_lik = lik.Kgrad_theta(hyperparams_opt['lik'],n_train,0)
        
        KinvY = LA.solve(K,self.Yuni['train'])
        _LML = self.n_train/2*SP.log(2*SP.pi) + 0.5*SP.log(LA.det(K)) + 0.5*(self.Yuni['train']*KinvY).sum() + prior.LML(hyperparams_opt)
        LMLgrad_covar = 0.5 * SP.trace(LA.solve(K, Kgrad_covar)) - 0.5*SP.dot(KinvY.T,SP.dot(Kgrad_covar,KinvY))
        LMLgrad_covar+= prior_cov.LMLgrad(hyperparams_opt)['covar']
        LMLgrad_lik = 0.5 * SP.trace(LA.solve(K, Kgrad_lik)) - 0.5*SP.dot(KinvY.T,SP.dot(Kgrad_lik,KinvY))
        LMLgrad_lik+= prior_lik.LMLgrad(hyperparams_opt)['lik']

        assert SP.allclose(LML,_LML), 'ouch, marginal log likelihood does not match'
        assert SP.allclose(LMLgrad['covar'], LMLgrad_covar), 'ouch, gradient with respect to theta does not match'
        assert SP.allclose(LMLgrad['lik'], LMLgrad_lik), 'ouch, gradient with respect to theta does not match'
   
        # predict
        Ystar = gp.predict(hyperparams_opt,self.X['test'])
        Kstar = covar.Kcross(hyperparams_opt['covar'])
        _Ystar = SP.dot(Kstar.T,LA.solve(K,self.Yuni['train'])).flatten()
        assert SP.allclose(Ystar,_Ystar), 'ouch, predictions, do not match'
Esempio n. 44
0
    def test_gpbase(self):

        covar = linear.LinearCF(n_dimensions=self.n_dimensions)
        n_train = self.X['train'].shape[0]

        theta = 1E-1
        prior_cov = priors.GaussianPrior(key='covar', theta=SP.array([1.]))
        prior_lik = priors.GaussianPrior(key='lik', theta=SP.array([1.]))
        prior = priors.PriorList([prior_cov, prior_lik])

        lik = likelihood_base.GaussIsoLik()
        gp = gp_base.GP(covar_r=covar, likelihood=lik, prior=prior)
        gp.setData(Y=self.Yuni['train'], X=self.X['train'])

        # log likelihood and gradient derivation
        hyperparams = {'covar': SP.array([0.5]), 'lik': SP.array([0.5])}
        LML = gp.LML(hyperparams)
        LMLgrad = gp.LMLgrad(hyperparams)

        K = covar.K(hyperparams['covar']) + lik.K(hyperparams['lik'], n_train)
        Kgrad_covar = covar.Kgrad_theta(hyperparams['covar'], 0)
        Kgrad_lik = lik.Kgrad_theta(hyperparams['lik'], n_train, 0)

        KinvY = LA.solve(K, self.Yuni['train'])
        _LML = self.n_train / 2 * SP.log(2 * SP.pi) + 0.5 * SP.log(
            LA.det(K)) + 0.5 * (self.Yuni['train'] *
                                KinvY).sum() + prior.LML(hyperparams)
        LMLgrad_covar = 0.5 * SP.trace(LA.solve(
            K, Kgrad_covar)) - 0.5 * SP.dot(KinvY.T, SP.dot(
                Kgrad_covar, KinvY))
        LMLgrad_covar += prior_cov.LMLgrad(hyperparams)['covar']
        LMLgrad_lik = 0.5 * SP.trace(LA.solve(K, Kgrad_lik)) - 0.5 * SP.dot(
            KinvY.T, SP.dot(Kgrad_lik, KinvY))
        LMLgrad_lik += prior_lik.LMLgrad(hyperparams)['lik']

        assert SP.allclose(
            LML, _LML), 'ouch, marginal log likelihood does not match'
        assert SP.allclose(
            LMLgrad['covar'], LMLgrad_covar
        ), 'ouch, gradient with respect to theta does not match'
        assert SP.allclose(
            LMLgrad['lik'],
            LMLgrad_lik), 'ouch, gradient with respect to theta does not match'

        # predict
        Ystar = gp.predict(hyperparams, self.X['test'])
        Kstar = covar.Kcross(hyperparams['covar'])
        _Ystar = SP.dot(Kstar.T, LA.solve(K, self.Yuni['train'])).flatten()
        assert SP.allclose(Ystar, _Ystar), 'ouch, predictions, do not match'

        # optimize
        opts = {'gradcheck': True, 'messages': False}
        hyperparams_opt, lml_opt = optimize_base.opt_hyper(gp,
                                                           hyperparams,
                                                           opts=opts)

        # log likelihood and gradient derivation
        LML = gp.LML(hyperparams_opt)
        LMLgrad = gp.LMLgrad(hyperparams_opt)

        K = covar.K(hyperparams_opt['covar']) + lik.K(hyperparams_opt['lik'],
                                                      n_train)
        Kgrad_covar = covar.Kgrad_theta(hyperparams_opt['covar'], 0)
        Kgrad_lik = lik.Kgrad_theta(hyperparams_opt['lik'], n_train, 0)

        KinvY = LA.solve(K, self.Yuni['train'])
        _LML = self.n_train / 2 * SP.log(2 * SP.pi) + 0.5 * SP.log(
            LA.det(K)) + 0.5 * (self.Yuni['train'] *
                                KinvY).sum() + prior.LML(hyperparams_opt)
        LMLgrad_covar = 0.5 * SP.trace(LA.solve(
            K, Kgrad_covar)) - 0.5 * SP.dot(KinvY.T, SP.dot(
                Kgrad_covar, KinvY))
        LMLgrad_covar += prior_cov.LMLgrad(hyperparams_opt)['covar']
        LMLgrad_lik = 0.5 * SP.trace(LA.solve(K, Kgrad_lik)) - 0.5 * SP.dot(
            KinvY.T, SP.dot(Kgrad_lik, KinvY))
        LMLgrad_lik += prior_lik.LMLgrad(hyperparams_opt)['lik']

        assert SP.allclose(
            LML, _LML), 'ouch, marginal log likelihood does not match'
        assert SP.allclose(
            LMLgrad['covar'], LMLgrad_covar
        ), 'ouch, gradient with respect to theta does not match'
        assert SP.allclose(
            LMLgrad['lik'],
            LMLgrad_lik), 'ouch, gradient with respect to theta does not match'

        # predict
        Ystar = gp.predict(hyperparams_opt, self.X['test'])
        Kstar = covar.Kcross(hyperparams_opt['covar'])
        _Ystar = SP.dot(Kstar.T, LA.solve(K, self.Yuni['train'])).flatten()
        assert SP.allclose(Ystar, _Ystar), 'ouch, predictions, do not match'
Esempio n. 45
0
    def take_step_implicit(self, dtau, midpoint=True):
        """A backward (implicit) integration step.
        
        Based on p. 8-10 of arXiv:1103.0936v2 [cond-mat.str-el].
        
        NOTE: Not currently working as well as expected. Iterative solution of 
              implicit equation stops converging at some point...
        
        This is made trickier by the gauge freedom. We solve the implicit equation iteratively, aligning the tangent
        vectors along the gauge orbits for each step to obtain the physically relevant difference dA. The gauge-alignment is done
        by solving a matrix equation.
        
        The iteration seems to be difficult. At the moment, iteration over the whole chain is combined with iteration over a single
        site (a single A[n]). Iteration over the chain is needed because the evolution of a single site depends on the state of the
        whole chain. The algorithm runs through the chain from N to 1, iterating a few times over each site (depending on how many
        chain iterations we have done, since iterating over a single site becomes more fruitful as the rest of the chain stabilizes).
        
        In running_update mode, which is likely the most sensible choice, the current midpoint guess is updated after visiting each
        site. I.e. The trial backwards step at site n is based on the updates that were made to site n + 1, n + 2 during the current chain
        iteration.
        
        Parameters
        ----------
        dtau : complex
            The (imaginary or real) amount of imaginary time (tau) to step.
        midpoint : bool
            Whether to use approximately time-symmetric midpoint integration,
            or just a backward-Euler step.
        """        
        #---------------------------
        #Hard-coded params:
        debug = True
        dbg_bstep = False
        safe_mode = True
        
        tol = sp.finfo(sp.complex128).eps * 3
        max_iter = 10
        itr_switch_mode = 10
        #---------------------------
        
        if midpoint:
            dtau = dtau / 2
        
        self.restore_RCF()

        #Take a copy of the current state
        A0 = sp.empty_like(self.A)
        for n in xrange(1, self.N + 1):
            A0[n] = self.A[n].copy()
        
        #Take initial forward-Euler step
        self.take_step(dtau)     

        itr = 0
        delta = 1
        delta_prev = 0                
        final_check = False

        while delta > tol * (self.N - 1) and itr < max_iter or final_check:
            print "OUTER"
            running_update = itr < itr_switch_mode
            
            A_np1 = A0[self.N]            
            
            #Prepare for next calculation of B from the new A
            self.restore_RCF() #updates l and r
            
            if running_update:
                self.calc_C() #we really do need all of these, since B directly uses C[n-1]
                self.calc_K()            
            
            g0_n = sp.eye(self.D[self.N - 1], dtype=self.typ)       #g0_n is the gauge transform matrix needed to solve the implicit equation
            
            #Loop through the chain, optimizing the individual A's
            delta = 0
            for n in reversed(xrange(1, self.N)): #We start at N - 1, since the right vector can't be altered here.
                print "SWEEP"
                if not running_update: #save new A[n + 1] and replace with old version for building B
                    A_np1_new = self.A[n + 1].copy()
                    self.A[n + 1] = A_np1  
                    A_np1 = self.A[n].copy()
                    max_itr_n = 1 #wait until the next run-through of the chain to change A[n] again
                else:
                    max_itr_n = itr + 1 #do more iterations here as the outer loop progresses
                
                delta_n = 1
                itr_n = 0
                while True:
                    print "INNER"
                    #Find transformation to gauge-align A0 with the backwards-obtained A.. is this enough?
                    M = m.mmul(A0[n][0], g0_n, self.r[n], m.H(self.A[n][0]))
                    for s in xrange(1, self.q[n]):
                        M += m.mmul(A0[n][s], g0_n, self.r[n], m.H(self.A[n][s]))
                    
                    g0_nm1 = la.solve(self.r[n - 1], M, sym_pos=True, overwrite_b=True)
                    
                    if not (delta_n > tol and itr_n < max_itr_n):
                        break
                    
                    B = self.calc_B(n)
                    
                    if B is None:
                        delta_n = 0
                        fnorm = 0
                        break
                    
                    g0_nm1_inv = la.inv(g0_nm1) #sadly, we need the inverse too...    
                    r_dA = sp.zeros_like(self.r[n - 1])
                    dA = sp.empty_like(self.A[n])
                    sqsum = 0
                    for s in xrange(self.q[n]):
                        dA[s] = m.mmul(g0_nm1_inv, A0[n][s], g0_n)
                        dA[s] -= self.A[n][s] 
                        dA[s] -= dtau * B[s]
                        if not final_check:
                            self.A[n][s] += dA[s]
    
                    for s in xrange(self.q[n]):
                        r_dA += m.mmul(dA[s], self.r[n], m.H(dA[s]))
                        sqsum += sum(dA[s]**2)
                    
                    fnorm = sp.sqrt(sqsum)
                    
                    delta_n = sp.sqrt(sp.trace(m.mmul(self.l[n - 1], r_dA)))
                    
                    if running_update: #Since we want to use the current A[n] and A[n + 1], we need this:
                        if safe_mode:
                            self.restore_RCF()
                            self.calc_C()
                            self.calc_K()
                        else:
                            self.restore_RCF(start=n) #will also renormalize
                            self.calc_C(n_low=n-1, n_high=n)
                            self.calc_K(n_low=n, n_high=n+1)
                                        
                    itr_n += 1
                    
                    if final_check:
                        break
                    
                if not running_update: #save new A[n + 1] and replace with old version for building B
                    self.A[n + 1] = A_np1_new
                
                if debug:
                    print "delta_%d: %g, (%d iterations)" % (n, delta_n.real, itr_n) + ", fnorm = " + str(fnorm)
                delta += delta_n
                
                if safe_mode:
                    self.calc_r()
                else:
                    self.calc_r(n - 2, n - 1) #We only need these for the next step.
                
                g0_n = g0_nm1                
                            
            itr += 1
            if debug:
                print "delta: %g  delta delta: %g (%d iterations)" % (delta.real, (delta - delta_prev).real, itr)
            delta_prev = delta
            
            if debug:
                if final_check:
                    break
                elif delta <= tol * (self.N - 1) or itr >= max_iter:
                    print "Final check to get final delta:"
                    final_check = True
        
        #Test backward step!        
        if dbg_bstep:
            Anew = sp.empty_like(self.A)
            for n in xrange(1, self.N + 1):
                Anew[n] = self.A[n].copy()
            
#            self.calc_l()
#            self.simple_renorm()
#            self.restore_RCF()
            self.calc_C()
            self.calc_K()        
            self.take_step(-dtau)
            self.restore_RCF()
            
            delta2 = 0            
            for n in reversed(xrange(1, self.N + 1)):
                #print n
                dA = A0[n] - self.A[n]
                #Surely this dA should also preserve the gauge choice, since both A's are in ON_R...                
                #print dA/A0[n]
                r_dA = sp.zeros_like(self.r[n - 1])
                sqsum = 0
                for s in xrange(self.q[n]):
                    r_dA += m.mmul(dA[s], self.r[n], m.H(dA[s]))
                    sqsum += sum(dA[s]**2)
                delta_n = sp.sqrt(sp.trace(m.mmul(self.l[n - 1], r_dA)))                
                delta2 += delta_n
                if debug:
                    print "A[%d] OK?: " % n + str(sp.allclose(dA, 0)) + ", delta = " + str(delta_n) + ", fnorm = " + str(sp.sqrt(sqsum))
                #print delta_n
            if debug:
                print "Total delta: " + str(delta2)
                
            for n in xrange(1, self.N + 1):
                self.A[n] = Anew[n]
        else:
            delta2 = 0
            
        if midpoint:
            #Take a final step from the midpoint
            #self.restore_RCF() #updates l and r            
            self.calc_l()
            self.simple_renorm()
            self.calc_C()
            self.calc_K()
            self.take_step(dtau)
            
        return itr, delta, delta2
Esempio n. 46
0
    def restore_RCF(self, dbg=False):
        if dbg:
            self.calc_l()
            self.calc_r()
            print "BEFORE..."
            h_before, h_left_before, h_right_before = self.restore_RCF_dbg()

            print (h_left_before, h_before, h_right_before)

        self.restore_RCF_r()

        if dbg:
            self.calc_l()
            print "MIDDLE..."
            h_mid, h_left_mid, h_right_mid = self.restore_RCF_dbg()

            print (h_left_mid, h_mid, h_right_mid)

        fac = 1 / self.l[0].trace().real
        if dbg:
            print "Scale l[0]: %g" % fac
        self.l[0] *= fac
        self.u_gnd_l.r *= 1/fac

        self.restore_RCF_l()

        if dbg:
            print "Uni left:"
        self.u_gnd_l.A = self.A[0]
        self.u_gnd_l.l = self.l[0]
        self.u_gnd_l.calc_lr() #Ensures largest ev of E=1
        self.l[0] = self.u_gnd_l.l #No longer diagonal!
        self.A[0] = self.u_gnd_l.A
        if self.sanity_checks:
            if not sp.allclose(self.l[0], sp.diag(sp.diag(self.l[0])), atol=1E-12, rtol=1E-12):
                print "Sanity Fail in restore_RCF!: True l[0] not diagonal!"
        self.l[0] = mm.simple_diag_matrix(sp.diag(self.l[0]))

        fac = 1 / sp.trace(self.l[0]).real
        if dbg:
            print "Scale l[0]: %g" % fac
        self.l[0] *= fac
        self.u_gnd_l.r *= 1/fac

        self.u_gnd_l.l = self.l[0]

        if dbg:
            print "Uni right:"
        self.u_gnd_r.A = self.A[self.N + 1]
        self.u_gnd_r.r = self.r[self.N]
        self.u_gnd_r.calc_lr() #Ensures largest ev of E=1
        self.r[self.N] = self.u_gnd_r.r
        self.A[self.N + 1] = self.u_gnd_r.A
        if self.sanity_checks:
            if not sp.allclose(self.r[self.N], sp.eye(self.D[self.N]), atol=1E-12, rtol=1E-12):
                print "Sanity Fail in restore_RCF!: True r[N] not eye!"
        self.r[self.N] = mm.eyemat(self.D[self.N], dtype=self.typ)
        self.u_gnd_r.r = self.r[self.N]
        self.r[self.N + 1] = self.r[self.N]

        self.l[self.N + 1][:] = self.eps_l(self.N + 1, self.l[self.N])

        if self.sanity_checks:
            l_n = self.l[0]
            for n in xrange(0, self.N + 1):
                l_n = self.eps_l(n, l_n)
                if not sp.allclose(l_n, self.l[n], atol=1E-12, rtol=1E-12):
                    print "Sanity Fail in restore_RCF!: l_%u is bad" % n

            r_nm1 = self.r[self.N + 1]
            for n in reversed(xrange(1, self.N + 2)):
                r_nm1 = self.eps_r(n, r_nm1)
                if not sp.allclose(r_nm1, self.r[n - 1], atol=1E-12, rtol=1E-12):
                    print "Sanity Fail in restore_RCF!: r_%u is bad" % (n - 1)
        if dbg:
            print "AFTER..."
            h_after, h_left_after, h_right_after = self.restore_RCF_dbg()

            print (h_left_after, h_after, h_right_after)

            print h_after - h_before

            print (h_after.sum() - h_before.sum() + h_left_after - h_left_before
                   + h_right_after - h_right_before)
Esempio n. 47
0
    def evolute(self, ngen, x0=None, verbose=True):
        """
        This function evolutes the XNES algorithm for number of generations.
        
        :param ngen: (int) number of generations to evolute
        :param x0: (list) initial guess for the search (must be of same size as ``len(bounds)``)
        :param verbose: (bool) print statistics to screen
        
        :return: (dict) dictionary containing major XNES search results
        """
        f = self.f
        self.verbose = verbose
        if x0 is not None:
            assert len(
                x0
            ) == self.npop, 'the length of x0 ({}) MUST equal to npop ({})'.format(
                len(x0), self.npop)
            self.mu = np.array(x0)
        else:
            self.mu = self.init_sample(self.bounds)

        mu, sigma, bmat = self.mu, self.sigma, self.bmat
        eta_mu, eta_sigma, eta_bmat = self.eta_mu, self.eta_sigma, self.eta_bmat
        npop = self.npop
        dim = self.dim
        sigma_old = self.sigma_old

        eyemat = eye(dim)

        with joblib.Parallel(n_jobs=self.ncores) as parallel:

            for i in range(ngen):
                s_try = randn(npop, dim)
                z_try = mu + sigma * dot(s_try, bmat)  # broadcast

                for k in range(len(z_try)):
                    z_try[k] = self.ensure_bounds(vec=z_try[k],
                                                  bounds=self.bounds)

                f_try = parallel(joblib.delayed(f)(z) for z in z_try)
                f_try = asarray(f_try)

                # save if best
                fitness = mean(f_try)

                isort = argsort(f_try)
                f_try = f_try[isort]
                s_try = s_try[isort]
                z_try = z_try[isort]

                for m in range(len(f_try)):
                    if f_try[m] > self.fitness_best:
                        self.fitness_best = f_try[m]
                        self.x_best = copy.deepcopy(z_try[m])

                if fitness - 1e-8 > self.fitness_best:
                    self.mu_best = mu.copy()
                    self.counter = 0
                else:
                    self.counter += 1

                u_try = self.utilities if self.use_fshape else f_try

                if self.use_adasam and sigma_old is not None:  # sigma_old must be available
                    eta_sigma = self.adasam(eta_sigma, mu, sigma, bmat,
                                            sigma_old, z_try)

                dj_delta = dot(u_try, s_try)
                dj_mmat = dot(s_try.T, s_try *
                              u_try.reshape(npop, 1)) - sum(u_try) * eyemat
                dj_sigma = trace(dj_mmat) * (1.0 / dim)
                dj_bmat = dj_mmat - dj_sigma * eyemat

                sigma_old = sigma

                # update
                mu += eta_mu * sigma * dot(bmat, dj_delta)
                sigma *= exp(0.5 * eta_sigma * dj_sigma)
                bmat = dot(bmat, expm(0.5 * eta_bmat * dj_bmat))

                # logging
                self.history['fitness'].append(self.fitness_best)
                self.history['sigma'].append(sigma)
                self.history['eta_sigma'].append(eta_sigma)

                # Print data
                if self.verbose and i % self.npop:
                    print(
                        '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
                    )
                    print(
                        'XNES step {}/{}, NPOP={}, ETA_MU={}, ETA_SIGMA={}, ETA_BMAT={}, Ncores={}'
                        .format((i + 1) * self.npop, ngen * self.npop,
                                self.npop, np.round(self.eta_mu, 2),
                                np.round(self.eta_sigma, 2),
                                np.round(self.eta_bmat, 2), self.ncores))
                    print(
                        '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
                    )
                    print('Best Swarm Fitness:',
                          np.round(self.fitness_best, 6))
                    print('Best Swarm Position:', np.round(self.x_best, 6))
                    print('MU:', np.round(mu, 3))
                    print('Sigma:', np.round(sigma, 3))
                    print('BMAT:', np.round(bmat, 3))
                    print(
                        '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
                    )

        # keep last results
        self.mu, self.sigma, self.bmat = mu, sigma, bmat
        self.eta_sigma = eta_sigma
        self.sigma_old = sigma_old

        if self.verbose:
            print(
                '------------------------ NES Summary --------------------------'
            )
            print('Best fitness (y) found:', self.fitness_best)
            print('Best individual (x) found:', self.x_best)
            print(
                '--------------------------------------------------------------'
            )

        #-------------------------------------
        #return population ranked for PESA2
        #-------------------------------------
        pesa_pop = defaultdict(list)
        for i in range(len(z_try)):
            pesa_pop[i].append(list(z_try[i]))
            pesa_pop[i].append(f_try[i])

        return self.x_best, self.fitness_best, pesa_pop
Esempio n. 48
0
def groupBrainSync(S):
    # Group BrainSync algorithm developed by Haleh Akrami

    numT = S.shape[0]
    numV = S.shape[1]
    SubNum = S.shape[2]

    # init random matrix for Os
    Os = sp.zeros((numT, numT, SubNum))
    for i in range(SubNum):  #initializeing O
        #        R = 2 * rnd.random(size=(numT, numT)) - 1; #define a random matrix with unity distributian from -1 to 1
        Os[:, :, i] = special_ortho_group.rvs(
            numT)  #(sp.dot(R , R.T)^(-1/2) , R;  #orthogonal rows of matrix

    Error = 1
    PreError = 1
    relcost = 1

    alpha = 1e-6
    var = 0
    Costdif = sp.zeros(10000)

    print('init done')

    # Initialize PreError from gloal average
    X = sp.zeros((numT, numV))
    for j in range(SubNum):  #calculate X
        X = sp.dot(Os[:, :, j], S[:, :, j]) + X

    X = X / SubNum
    InError = 0

    for j in range(SubNum):
        etemp = sp.dot(Os[:, :, j], S[:, :, j]) - X
        InError = InError + sp.trace(sp.dot(etemp,
                                            etemp.T))  #calculating error

    # Find best Orthogognal map, by minimizing error (distance) from average
    while relcost > alpha:
        var = var + 1

        print('subject iteration')
        for i in tqdm(range(SubNum)):
            X = sp.zeros((numT, numV))
            for j in range(SubNum):  #calculate X average excluded subject i
                if j != i:
                    X = sp.dot(Os[:, :, j], S[:, :, j]) + X
            # Y is i excluded average
            Y = X / (SubNum - 1)

            # Update Orthogonal matrix with BrainSync projection technique
            U, _, V = sp.linalg.svd(sp.dot(Y, S[:, :, i].T))
            Os[:, :, i] = sp.dot(U, V)

    # print('calculate error')
        Error = 0
        # New Average with all subject updated orthogonal matrix
        # update last subject outside loop
        X2 = (X + sp.dot(Os[:, :, i], S[:, :, i])) / SubNum

        # Calculate error of all subjects from average map
        for j in range(SubNum):
            etemp = sp.dot(Os[:, :, j], S[:, :, j]) - X2
            Error = Error + sp.trace(sp.dot(etemp,
                                            etemp.T))  #calculating error

        relcost = np.abs(Error - PreError) / np.abs(InError)
        Costdif[var] = PreError - Error
        PreError = Error

        print('Error = %g, var = %g, relcost = %g\n' % (Error, var, relcost))

    Costdif = Costdif[:var]  # = []
    Costdif = Costdif[1:]
    TotalError = Error

    return X2, Os, Costdif, TotalError
Esempio n. 49
0
def mkl_train(
    fname_l,
    reg,
    ltrain,
    no_mkl,
    flip,
    mkl_norm=DEFAULT_MKL_NORM,
    bias_enabled=DEFAULT_BIAS_ENABLED,
    epsilon=DEFAULT_EPSILON,
    C_mkl=DEFAULT_C_MKL,
    mkl_epsilon=DEFAULT_MKL_EPSILON,
    tube_epsilon=DEFAULT_TUBE_EPSILON,
):

    global cache

    print "-" * 80
    print "reg =", reg
    print "mkl_norm =", mkl_norm
    print "bias_enabled =", bias_enabled
    print "epsilon =", epsilon
    print "C_mkl =", C_mkl
    print "mkl_epsilon =", mkl_epsilon
    print "tube_epsilon =", tube_epsilon
    print "-" * 80

    d0 = io.loadmat(fname_l[0])

    if flip:
        keytrain = "kernel_traintest"
        keytest = "kernel_traintrain"
    else:
        keytrain = "kernel_traintrain"
        keytest = "kernel_traintest"

    ltr = ltrain.astype(double)
    # l = ((l==thecat)-1*(l!=thecat)).astype(double)
    labels = Features.Labels(ltr)

    if True:
        tr1_l = []
        tr2_l = []
        ker_l = []
        # feat_l = []

        combker = Kernel.CombinedKernel()
        # combfeat = Features.CombinedFeatures()

        m, n = d0[keytrain].shape
        yy, xx = mgrid[:m, :n]

        print "loading nfiles=", len(fname_l)
        for i, fname in enumerate(fname_l):
            # if fname in cache_train:
            #    kermat, tr1, tr2 = cache_train[fname]
            # else:
            if True:
                # print "loading", fname
                d = io.loadmat(fname)
                # kermat = (d[keytrain]).astype(double)
                kermat = d[keytrain]
                # kermat += kermat.min()
                del d
                # kdiag =
                # kermat /= kdiag
                tr1 = trace(kermat) * len(fname_l)  # *diag(kermat)
                if tr1 == 0:
                    print "trace is 0, skipping"
                    continue
                print i + 1, "trace", tr1, "min", kermat.min(), "max", kermat.max(), fname
                kermat /= tr1
                # print kermat
                # kermat = exp(kermat*len(kermat))
                # tr2 = trace(kermat)
                # kermat /= tr2
                # print kermat
                tr2 = 0
                # cache_train[fname] = kermat, tr1, tr2
                # print diag(kermat)

            yy, xx = mgrid[: kermat.shape[0], : kermat.shape[1]]
            kermat_triangle = kermat[yy <= xx]
            ker = Kernel.CustomKernel()
            ker.set_triangle_kernel_matrix_from_triangle(kermat_triangle.astype(double))
            combker.append_kernel(ker)
            del kermat

            ker_l += [ker]
            tr1_l += [tr1]
            tr2_l += [tr2]

        cache = {
            "ker_l": ker_l,
            #'feat_l': feat_l,
            "tr1_l": tr1_l,
            "tr2_l": tr2_l,
            "combker": combker,
            #'combfeat': combfeat,
        }

        del xx, yy

    if no_mkl:
        # w_l = array([.2, .8])
        # w_l /= w_l.sum()
        for i, ker in enumerate(ker_l):
            ker.set_combined_kernel_weight(1.0 / (len(ker_l)))
            # ker.set_combined_kernel_weight(w_l[i])

    tstart = time.time()
    # svm1 = Classifier.SVMLight(reg, combker, labels)
    # svm1 = Classifier.LibSVM(reg, combker, labels)
    # svm1 = Classifier.GMNPSVM(reg, combker, labels)
    # svm1 = Classifier.SVMSGD(reg, combker, labels)
    # svm1 = Classifier.CPLEXSVM(reg, combker, labels)
    if len(fname_l) == 1:
        no_mkl = True
        # svm1 = Classifier.LibSVM(reg, combker, labels)
        svm1 = Classifier.SVMLight(reg, combker, labels)
    else:
        # svm1 = Classifier.SVMLight(reg, combker, labels)
        svm1 = Classifier.MKLClassification()
        svm1.set_C(reg, reg)
        svm1.set_kernel(combker)
        svm1.set_labels(labels)
        print combker.get_subkernel_weights()
        # raise
    # svm1.set_mkl_enabled(not no_mkl)

    # svm1.set_C(reg/10., reg)
    # print svm1.get_C1(), svm1.get_C2()

    svm1.parallel.set_num_threads(4)
    # print ker_l[0]

    kkk = combker.get_kernel_matrix()
    assert not isnan(kkk).any()
    assert not isinf(kkk).any()

    # ipshell()

    # svm1.set_linadd_enabled(True)
    # svm1.set_shrinking_enabled(True)
    svm1.set_bias_enabled(bias_enabled)
    svm1.set_epsilon(epsilon)
    svm1.set_tube_epsilon(tube_epsilon)
    svm1.set_mkl_epsilon(mkl_epsilon)
    svm1.set_C_mkl(C_mkl)
    svm1.set_mkl_norm(mkl_norm)

    assert svm1.train()
    www = combker.get_subkernel_weights()
    # print "sum before", www.sum()
    print "-w " + " -w ".join([str(e) for e in www])
    print "rectify"
    www[www < MKL_WEIGHTS_CUTOFF] = 0
    www = sp.array(www, dtype="float64")
    # print "sum after", www.sum()

    www /= www.sum()
    print "-w " + " -w ".join([str(e) for e in www])

    alphas = svm1.get_alphas()
    bias = svm1.get_bias()
    svs = svm1.get_support_vectors()

    #     weight_l = []
    #     for i,ker in enumerate(ker_l):
    #         weight = ker.get_combined_kernel_weight()
    #         weight_l += [weight]

    weight_l = www
    #     print weight_l
    #     print "-w " + " -w ".join([str(e) for e in weight_l])
    kweights = array(weight_l)
    kweights /= kweights.sum()

    traces1 = array(tr1_l)
    # traces2 = array(tr2_l)

    # raise

    # tend = time.time()
    # print "time:", tend-tstart

    # kernel_train = combker.get_kernel_matrix()

    #     weight_l = []
    #     for i,ker in enumerate(ker_l):
    #         weight = ker.get_combined_kernel_weight()
    #         weight_l += [weight]

    # combker.init(combfeat,combfeat)
    # print svm1.classify().get_labels()[:10]

    # kk = combker.get_kernel_matrix()
    # print "trace=", trace(kk)
    # trainperf = (sign(svm1.classify().get_labels())==ltr).mean()
    # print "trainperf=", trainperf, weight_l

    # print svm1.get_alphas()
    # raise

    #     mkl = {"svm": svm1,
    #            "ker_l": ker_l,
    #            "feat_l": feat_l,
    #            "tr_l": tr_l,
    #            "combker": combker,
    #            "combfeat": combfeat,
    #            }

    # print bias

    mkl = {
        "alphas": alphas.astype("float32"),
        "bias": array(bias).astype("float32"),
        "svs": svs.astype("int32"),
        "kweights": kweights.astype("float32"),
        "traces1": traces1.astype("float32"),
        # "traces2": traces2.astype('float32'),
    }

    # print svm1.get_alphas()

    return mkl
Esempio n. 50
0
        for i in range(3):
            Ht[i, i] = (sp.einsum('qpn,qpm->nm', T[i], KiT[i])**2).sum()
            for j in range(0, i):
                Ht[i, j] = (sp.einsum('qpn,qpm->nm', T[i], KiT[j])**2).sum()
                Ht[j, i] = Ht[i, j]
        if Y.shape[0] <= 1000:
            Ki = la.inv(
                sp.kron(mvset.gp[type].covar.Cn.K(), sp.eye(Y.shape[0])))
            XrXr = sp.dot(Xr, Xr.T)
            KidK = [
                sp.dot(Ki, sp.kron(_Cr.K_grad_i(i), XrXr)) for i in range(3)
            ]
            Ht0 = sp.zeros((3, 3))
            for i in range(3):
                for j in range(3):
                    Ht0[i, j] = sp.trace(sp.dot(KidK[i], KidK[j]))
            pdb.set_trace()

    if 0:
        # 2. quadratic_term = y_Ki_dKp_Ki_dKq_Ki_y
        Z = Y - sp.dot(F, mvset.gp[type].mean.B[0])
        XrXrKiZ = sp.dot(Xr, sp.dot(Xr.T, gp.covar.solve_t(Z)))
        XrXrKiZC = [sp.dot(XrXrKiZ, _Cr.K_grad_i(i).T) for i in range(3)]
        KiXrXrKiZC = [gp.covar.solve_t(XrXrKiZC[i]) for i in range(3)]
        Hq = sp.zeros((3, 3))
        for i in range(3):
            Hq[i, i] = (XrXrKiZC[i] * KiXrXrKiZC[i]).sum()
            for j in range(i):
                Hq[i, j] = (XrXrKiZC[i] * KiXrXrKiZC[j]).sum()
                Hq[j, i] = Hq[i, j]
        if Y.shape[0] <= 1000: