Beispiel #1
0
def energies(H, hermitian=False):
    """Given a hamiltonian matrix, calculates energies and sorts them."""
    if hermitian:
        eigvals, eigvecs = linalg.eigh(H)
    else:
        eigvals, eigvecs = linalg.eig(H)
        indexes = sp.real_if_close(eigvals).argsort()
        eigvals = sp.real_if_close(eigvals[indexes])
        eigvecs = eigvecs[:, indexes]
    return eigvals, eigvecs
def MarkovMutualInfo(transitionMatrix):
    p0 = MarkovSteadyState(transitionMatrix)
    #M = scipy.transpose(transitionMatrix)
    M = transitionMatrix  #***8testing
    sum, dot, log2 = scipy.sum, scipy.dot, lambda x: scipy.nan_to_num(
        scipy.log2(x))
    return scipy.real_if_close(sum(dot(p0, M * log2(M))) - sum(p0 * log2(p0)))
Beispiel #3
0
def eigensolve(H, hermitian=False):
    """
    Given a hamiltonian matrix, calculates energies and 
    eigenvectors (≈ wavefunctions) and sorts them by 
    the real part of the energy in ascending order.
    """
    if hermitian:
        energies, eigenvectors = linalg.eigh(H)
    else:
        energies, eigenvectors = linalg.eig(H)
        indexes = energies.argsort()
        energies = sp.real_if_close(energies[indexes])
        eigenvectors = eigenvectors[:, indexes]
    return energies, eigenvectors
Beispiel #4
0
def eigensolve(H, hermitian=False):
    """
    Given a hamiltonian matrix, calculates energies and 
    eigenvectors (≈ wavefunctions) and sorts them by 
    the real part of the energy in ascending order.
    """
    if hermitian:
        energies, eigenvectors = linalg.eigh(H)
    else:
        energies, eigenvectors = linalg.eig(H)
        indexes = energies.argsort()
        energies = sp.real_if_close(energies[indexes])
        eigenvectors = eigenvectors[:, indexes]
    return energies, eigenvectors
def MarkovSteadyState(transitionMatrix, tol=1.e-10):
    """
    transitionMatrix            : NxN matrix, where N = 2^i for 
                                  some integer i.  Rows should
                                  sum to 1.
    """
    # find the steady-state probabilities p0 by finding the
    # eigenvector with eigenvalue 1.
    M = scipy.transpose(transitionMatrix)
    valsM, vecsM = scipy.linalg.eig(M)
    valsM = scipy.real_if_close(valsM)
    indices = pylab.find(abs(valsM - 1.) < tol)
    if len(indices) != 1:
        raise Exception("MarkovPCA: No unique steady-state solution.  " +  \
            "Check form of transition matrix.")
    p0 = vecsM[:, indices[0]].T
    p0 = p0 / sum(p0)

    return p0
    def _evaluate(self, params, T=1):
        """
        Evaluate the cost for the model, returning the intermediate residuals,
        and chi-squared.

        (Summing up the residuals is a negligible amount of work. This 
         arrangment makes notification of observers much simpler.)
        """
        self.params.update(params)
        self.check_parameter_bounds(params)
        self.CalculateForAllDataPoints(params)
        self.ComputeInternalVariables(T)

        resvals = [
            res.GetValue(self.calcVals, self.internalVars, self.params)
            for res in self.residuals.values()
        ]

        # Occasionally it's useful to use residuals with a sqrt(-1) in them,
        #  to get negative squares. Then, however, we might get small imaginary
        #  parts in our results, which this shaves off.
        chisq = scipy.real_if_close(scipy.sum(scipy.asarray(resvals)**2),
                                    tol=self.imag_cutoff)
        if scipy.isnan(chisq):
            logger.warn('Chi^2 is NaN, converting to Infinity.')
            chisq = scipy.inf
        cost = 0.5 * chisq

        entropy = 0
        for expt, sf_ents in self.internalVars['scaleFactor_entropies'].items(
        ):
            for group, ent in sf_ents.items():
                entropy += ent

        self._notify(event='evaluation',
                     resvals=resvals,
                     chisq=chisq,
                     cost=cost,
                     free_energy=cost - T * entropy,
                     entropy=entropy,
                     params=self.params)

        return resvals, chisq, cost, entropy
Beispiel #7
0
def sqrtm(M):
    """ Returns the symmetric semi-definite positive square root of a matrix.
    """
    r = real_if_close(expm2(0.5 * logm(M)), 1e-8)
    return (r + r.T) / 2
Beispiel #8
0
def sqrtm(M):
    """ Returns the symmetric semi-definite positive square root of a matrix. """
    r = real_if_close(expm(0.5 * logm(M)), 1e-8)
    return (r + r.T) / 2
def MarkovEntropy(transitionMatrix):
    p0 = MarkovSteadyState(transitionMatrix)
    sum, log2 = scipy.sum, lambda x: scipy.nan_to_num(scipy.log2(x))
    return -scipy.real_if_close(sum(p0 * log2(p0)))
Beispiel #10
0
def SmeanField(cluster,
               coocMat,
               meanFieldPriorLmbda=0.,
               numSamples=None,
               indTerm=True,
               alternateEnt=False,
               useRegularizedEq=True):
    """
    meanFieldPriorLmbda (0.): 3.23.2014
    indTerm (True)          : As of 2.19.2014, I'm not
                              sure whether this term should
                              be included, but I think so
    alternateEnt (False)    : Explicitly calculate entropy
                              using the full partition function
    useRegularizedEq (True) : Use regularized form of equation
                              even when meanFieldPriorLmbda = 0.
    """

    coocMatCluster = coocCluster(coocMat, cluster)
    # in case we're given an upper-triangular coocMat:
    coocMatCluster = symmetrizeUsingUpper(coocMatCluster)

    outer = scipy.outer
    N = len(cluster)

    freqs = scipy.diag(coocMatCluster)
    c = coocMatCluster - outer(freqs, freqs)

    Mdenom = scipy.sqrt(outer(freqs * (1. - freqs), freqs * (1 - freqs)))
    M = c / Mdenom

    if indTerm:
        Sinds = -freqs*scipy.log(freqs)             \
            -(1.-freqs)*scipy.log(1.-freqs)
        Sind = scipy.sum(Sinds)
    else:
        Sind = 0.

    # calculate off-diagonal (J) parameters
    if (meanFieldPriorLmbda != 0.) or useRegularizedEq:
        # 3.22.2014
        if meanFieldPriorLmbda != 0.:
            gamma = meanFieldPriorLmbda / numSamples
        else:
            gamma = 0.
        mq, vq = scipy.linalg.eig(M)
        mqhat = 0.5*( mq-gamma +                        \
                scipy.sqrt((mq-gamma)**2 + 4.*gamma) )
        jq = 1. / mqhat  #1. - 1./mqhat
        Jprime = scipy.real_if_close(                   \
                dot( vq , dot(scipy.diag(jq),vq.T) ) )
        JMF = zeroDiag(Jprime / Mdenom)

        ent = scipy.real_if_close(                      \
                Sind + 0.5*scipy.sum( scipy.log(mqhat)  \
                + 1. - mqhat ) )
    else:
        # use non-regularized equations
        Minv = scipy.linalg.inv(M)
        JMF = zeroDiag(Minv / Mdenom)

        logMvals = scipy.log(scipy.linalg.svdvals(M))
        ent = Sind + 0.5 * scipy.sum(logMvals)

    # calculate diagonal (h) parameters
    piFactor = scipy.repeat([(freqs - 0.5) / (freqs * (1. - freqs))],
                            N,
                            axis=0).T
    pjFactor = scipy.repeat([freqs], N, axis=0)
    factor2 = c * piFactor - pjFactor
    hMF = scipy.diag(scipy.dot(JMF, factor2.T)).copy()
    if indTerm:
        hMF -= scipy.log(freqs / (1. - freqs))

    J = replaceDiag(0.5 * JMF, hMF)

    if alternateEnt:
        ent = analyticEntropy(J)

    # make 'full' version of J (of size NfullxNfull)
    Nfull = len(coocMat)
    Jfull = JfullFromCluster(J, cluster, Nfull)

    return ent, Jfull
Beispiel #11
0
            sum += (w[i] * integrand( ((end-start)/2.0)*xx[i] + (start+end)/2.0, x, y, step));
        float integral=(end-start)*sum/2.0;
        float diagonal=((x+0.0)*(x+0.0)*step*step)/(2*mass)*(fabs(x-y)<0.001);
        return diagonal+2*((y+0.0)*(y+0.0)*step*step)*step*integral/PI;
    }
    """)
    
# Chose which kernel to execute.
#krnl_identity_matrix(gpu_matrix_x,gpu_matrix_y,gpu_matrix_res)
krnl_gaussian_matrix(gpu_matrix_x,gpu_matrix_y,start_val,end_val,(end_val-start_val)/side_length,gpu_matrix_res)
t2=time.time()
print "Time generating matrix:", (t2-t1)

# Reshape to matrix.
shape=(side_length,side_length)
# Calculate eigenvalues of the matrix.

#print gpu_matrix_res.get()

[eigs,_]=la.eig(gpu_matrix_res.get().reshape(shape))
indexes = eigs.argsort()
eigs = sp.real_if_close(eigs[indexes])
t3=time.time()
print "Time calculating eigenvalues:", (t3-t2)
print "Total time:", (t3-t1)
print "Energy eigenvalue:", eigs[0] # Print eigenvalues.
#gpu_matrix_res.get()

# Timing, again.
# t2=time.time()
# print "Time: ", (t2-t1)
Beispiel #12
0
def dyn_var_fixed_point(net, dv0=None, with_logs=True, xtol=1e-6, time=0,
                        stability=False, fsolve_factor=100, 
                        maxfev=10000):
    """
    Return the dynamic variables values at the closest fixed point of the net.

    dv0  Initial guess for the fixed point. If not given, the current state
         of the net is used.
    with_logs   If True, the calculation is done in terms of logs of variables,
                so that they cannot be negative.
    xtol Tolerance to aim for.
    time Time to plug into equations.
    stability   If True, return the stability for the fixed point. -1 indicates
                stable node, +1 indicates unstable node, 0 indicates saddle
    fsolve_factor   'factor' argument for fsolve. For more information, see 
                    help(scipy.optimize.fsolve). Should be in range 0.1 to 100.
    maxfev      'maxfev' argument for fsolve. For more information, see 
                    help(scipy.optimize.fsolve). Should be an integer > 1.
    """
    net.compile()

    if dv0 is None:
        dv0 = scipy.array(net.getDynamicVarValues())
    else:
        dv0 = scipy.asarray(dv0)

    consts = net.constantVarValues

    zeros = scipy.zeros(len(dv0), scipy.float_)
    if with_logs:
        # We take the absolute value of dv0 to avoid problems from small 
        #  numerical noise negative values.
        if scipy.any(dv0 <= 0):
            logger.warning('Non-positive values in initial guess for fixed '
                           'point and with_logs = True. Rounding them up to '
                           'double_tiny. The most negative value was %g.' 
                           % min(dv0))
            dv0 = scipy.maximum(dv0, _double_tiny_)

        # XXX: Would like to replace these with C'd versions, if it's holding
        #      any of our users up.
        def func(logy):
            return net.res_function_logdv(time, logy, zeros, consts)
        def fprime(logy):
            y = scipy.exp(logy)
            y = scipy.maximum(y, _double_tiny_)
            return net.dres_dc_function(time, y, zeros, consts)

        x0 = scipy.log(dv0)
        # To transform sigma_x to sigma_log_x, we divide by x. We can set
        #  our sigma_log_x use to be the mean of what our xtol would yield
        #  for each chemical.
        xtol = scipy.mean(xtol/dv0)
    else:
        def func(y):
            return net.res_function(time, y, zeros, consts)
        def fprime(y):
            return net.dres_dc_function(time, y, zeros, consts)
        x0 = dv0

    try:
        dvFixed, infodict, ier, mesg =\
                scipy.optimize.fsolve(func, x0=x0.copy(), full_output=True,
                                      fprime=fprime,
                                      xtol=xtol, maxfev=maxfev,
                                      factor=fsolve_factor)
    except (scipy.optimize.minpack.error, ArithmeticError) as X:
        raise FixedPointException(('Failure in fsolve.', X))

    tiny = _double_epsilon_

    if with_logs:
        dvFixed = scipy.exp(dvFixed)

    if ier != 1:
        if scipy.all(abs(dvFixed) < tiny) and not scipy.all(abs(x0) < 1e6*tiny):
            # This is the case where the answer is zero, and our initial guess
            # was reasonably large. In this case, the solver fails because
            # it's looking at a relative tolerance, but it's not really a
            # failure.
            pass
        else:
            raise FixedPointException(mesg, infodict)

    if not stability:
        return dvFixed
    else:
        jac = net.dres_dc_function(time, dvFixed, zeros, consts)
        u = scipy.linalg.eigvals(jac)
        u = scipy.real_if_close(u)
        if scipy.all(u < 0):
            stable = -1
        elif scipy.all(u > 0):
            stable = 1
        else:
            stable = 0
        return (dvFixed, stable)
Beispiel #13
0
import Common

for model_ii, (model, temp, temp) in enumerate(Common.model_list):
    # Load the hessian
    h = scipy.io.read_array(os.path.join(model, 'hessian.dat'))

    # Load the list of keys
    f = file(os.path.join(model, 'hessian_keys.dat'), 'r')
    keys = f.readlines()
    f.close()
    # Strip off extraneous characters
    keys = [k.strip() for k in keys]

    e, v = Utility.eig(h)
    v = scipy.real_if_close(v)

    Plotting.figure(figsize=(6, 4.5))
    N = 4
    # Plot the eigenvectors
    for ii in range(N):
        Plotting.subplot(N, 1, ii + 1)
        Plotting.plot_eigvect(v[:, ii], labels=keys)
        for p in Plotting.gca().patches:
            p.set_fc([0.5] * 3)
        Plotting.gca().set_ylim(-1, 1)
        Plotting.gca().set_yticks([-1, 0, 1])
        if ii == 0:
            Plotting.title('(%s) %s' %
                           (string.ascii_lowercase[model_ii], model),
                           fontsize='x-large')
Beispiel #14
0
    if ier != 1:
        if scipy.all(abs(dvFixed) < tiny) and not scipy.all(abs(x0) < 1e6*tiny):
            # This is the case where the answer is zero, and our initial guess
            # was reasonably large. In this case, the solver fails because
            # it's looking at a relative tolerance, but it's not really a
            # failure.
            pass
        else:
            raise FixedPointException(mesg, infodict)

    if not stability:
        return dvFixed
    else:
        jac = net.dres_dc_function(time, dvFixed, zeros, consts)
        u = scipy.linalg.eigvals(jac)
        u = scipy.real_if_close(u)
        if scipy.all(u < 0):
            stable = -1
        elif scipy.all(u > 0):
            stable = 1
        else:
            stable = 0
        return (dvFixed, stable)

def find_ypic_sens(y, yp, time, var_types, rtol, atol_for_sens, constants, 
                   net, opt_var, redirect_msgs=False):
    # On some systems, the f2py'd functions don't like len(constants)=0.
    if len(constants) == 0:
        constants = [0]
    var_types = scipy.asarray(var_types)
    y = scipy.asarray(y, scipy.float_)
Beispiel #15
0
    def _evaluate(self, params, periodic_distance=False, 
                  oscillate_distance=False, T=1):
        """
        Evaluate the cost for the model, returning the intermediate residuals,
        and chi-squared.

        (Summing up the residuals is a negligible amount of work. This 
         arrangment makes notification of observers much simpler.)
        """
        self.params.update(params)
        self.check_parameter_bounds(params)
             
        
        if (periodic_distance  == True or
            oscillate_distance == True):          
            def get_nested_dict(dic, nest_level):
                for level in range(nest_level):
                    dic = dic.values()[0]    
                return dic

            def get_expt_trajectory(expt_dict): 
                return [item[1][0] for item in sorted(expt_dict.iteritems())]

            def get_expt_times(expt_dict): 
                return [item[0] for item in sorted(expt_dict.iteritems())]

            # Get calculations corresponding to experiment times
            def get_corresponding_calcs(traj_dict, expt_time):
                return [traj_dict[time] for time in expt_time]

            exptData            = self.exptColl.GetData()  
            expts               = get_nested_dict(exptData, 2).values()
            expt_times          = map(get_expt_times, expts)
            expt_trajectories   = map(get_expt_trajectory, expts)


            calcData            = self.CalculateForAllDataPoints(params) 
            calcs               = get_nested_dict(calcData, 1).values()

            # Get calculations corresponding to experimental sampling times
            calc_trajectories   = map(get_corresponding_calcs, calcs, expt_times)
 
        
            def p_dist(a,b): return(oc.periodic_distance(a, b))
            def o_dist(a,b): return(oc.oscillate_distance(a, b))
            #WARNING exp must come first, it does not change
            if periodic_distance:
                cost  = sum(map(p_dist, expt_trajectories, calc_trajectories))                
            else:
                cost = sum(map(o_dist, expt_trajectories, calc_trajectories))
            if scipy.isnan(cost):
                logger.warn('cost is NaN, converting to Infinity.')
                print 'cost is inf'
                return(scipy.inf)
            else:
                return(cost)

        self.CalculateForAllDataPoints(params)
        self.ComputeInternalVariables(T)
        resvals = [res.GetValue(self.calcVals, self.internalVars, self.params, no_sf=True)
                   for res in self.residuals.values()]

        # Occasionally it's useful to use residuals with a sqrt(-1) in them,
        #  to get negative squares. Then, however, we might get small imaginary
        #  parts in our results, which this shaves off.
        chisq = scipy.real_if_close(scipy.sum(scipy.asarray(resvals)**2), 
                                    tol=self.imag_cutoff)
        if scipy.isnan(chisq):
            logger.warn('Chi^2 is NaN, converting to Infinity.')
            chisq = scipy.inf
        cost = 0.5 * chisq

        entropy = 0
        for expt, sf_ents in self.internalVars['scaleFactor_entropies'].items():
            for group, ent in sf_ents.items():
                entropy += ent

        self._notify(event = 'evaluation', 
                     resvals = resvals,
                     chisq = chisq,
                     cost = cost, 
                     free_energy = cost-T*entropy,
                     entropy = entropy,
                     params = self.params)

        return resvals, chisq, cost, entropy