def add_localized_dGf_constraints(self, cid2dG0_f, cid2bounds, c_range, T=300): self.T = T for (rid, sparse) in self.reactions: dG0_r = 0 for (cid, coeff) in sparse.iteritems(): if cid in cid2dG0_f: dG0_r += coeff * cid2dG0_f[cid] else: dG0_r = None break (curr_c_min, curr_c_max) = cid2bounds.get(cid, (None, None)) if curr_c_min == None: curr_c_min = c_range[0] if curr_c_max == None: curr_c_max = c_range[1] if coeff < 0: dG0_r += coeff * common.R * T * pylab.log(curr_c_max) else: dG0_r += coeff * common.R * T * pylab.log(curr_c_min) if dG0_r != None and dG0_r > 0: # this reaction is a localized bottleneck, add a constraint that its flux = 0 constraint_name = rid + "_irreversible" self.cpl.linear_constraints.add(names=[constraint_name], senses="E", rhs=[0]) self.cpl.linear_constraints.set_coefficients(constraint_name, rid, 1)
def plot(self, criteria="AICc"): pylab.plot(self.x, [self.all_results[x]["BIC"] for x in self.x], "o-", label="BIC") pylab.plot(self.x, [self.all_results[x]["AIC"] for x in self.x], "o-", label="AIC") pylab.plot(self.x, [self.all_results[x]["AICc"] for x in self.x], "o-", label="AICc") m = np.array([self.all_results[x][criteria] for x in self.x]).min() # [exp((m-this[criteria])/2) for this in amf.all_results] pylab.axhline(m - pylab.log(0.9) * 2, color="k", label="90% equi-probability", alpha=0.9) pylab.axhline(m - pylab.log(0.5) * 2, color="k", label="50% equi-probability", alpha=0.5) pylab.axhline(m - pylab.log(0.3) * 2, color="k", label="30% equi-probability", alpha=0.3) pylab.legend()
def projectCl(lvec,P,D,dNdz,z,growthFac=None): """ project C_l's given a power spectrum class P (Camb or BBKS) and Distance class D together arguments: lvec: vector of l values P: p.pk p.k contains the power spectrum, e.g. pt.Camb instance D: frw.Distance instance dNdz,z, growthFac: vectors suitable for trapezoid z-integration presently it crashes if z=0.0 is included, start from a small z value """ lvec = M.asarray(lvec) dNdz2 = M.asarray(dNdz)**2 z = M.asarray(z) da1 = 1./D.rtc(z)/D.h #comoving Da in h^1Mpc dNdz2vc = dNdz2/D.vc(z)/D.h**3 # comovin volume in (h^-1Mpc)^3 #`use growth factor if given if growthFac: dNdz2vc = dNdz2vc*(growthFac**2) lk = M.log(P.k) pk = P.pk ## return M.asarray([utils.trapz(utils.splineResample(pk,lk, ## M.log(l*da1))*dNdz2vc,z) for l in lvec]) return M.asarray([utils.trapz(utils.interpolateLin(pk,lk, M.log(l*da1))*dNdz2vc,z) for l in lvec])
def _make_log_freq_map(self): """ :: For the given ncoef (bands-per-octave) and nfft, calculate the center frequencies and bandwidths of linear and log-scaled frequency axes for a constant-Q transform. """ fp = self.feature_params bpo = float(self.nbpo) # Bands per octave self._fftN = float(self.nfft) hi_edge = float( self.hi ) lo_edge = float( self.lo ) f_ratio = 2.0**( 1.0 / bpo ) # Constant-Q bandwidth self._cqtN = float( P.floor(P.log(hi_edge/lo_edge)/P.log(f_ratio)) ) self._dctN = self._cqtN self._outN = float(self.nfft/2+1) if self._cqtN<1: print "warning: cqtN not positive definite" mxnorm = P.empty(self._cqtN) # Normalization coefficients fftfrqs = self._fftfrqs #P.array([i * self.sample_rate / float(self._fftN) for i in P.arange(self._outN)]) logfrqs=P.array([lo_edge * P.exp(P.log(2.0)*i/bpo) for i in P.arange(self._cqtN)]) logfbws=P.array([max(logfrqs[i] * (f_ratio - 1.0), self.sample_rate / float(self._fftN)) for i in P.arange(self._cqtN)]) #self._fftfrqs = fftfrqs self._logfrqs = logfrqs self._logfbws = logfbws self._make_cqt()
def add_thermodynamic_constraints(cpl, dG0_f, c_range=(1e-6, 1e-2), T=default_T, bounds=None): """ For any compound that does not have an explicit bound set by the 'bounds' argument, create a bound using the 'margin' variables (the last to columns of A). """ Nc = dG0_f.shape[0] if bounds != None and len(bounds) != Nc: raise Exception("The concentration bounds list must be the same length as the number of compounds") if bounds == None: bounds = [(None, None)] * Nc for c in xrange(Nc): if pylab.isnan(dG0_f[c, 0]): continue # unknown dG0_f - cannot bound this compound's concentration at all b_low = bounds[c][0] or c_range[0] b_high = bounds[c][1] or c_range[1] # lower bound: dG0_f + R*T*ln(Cmin) <= x_i cpl.variables.set_lower_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(b_low)) # upper bound: x_i <= dG0_f + R*T*ln(Cmax) cpl.variables.set_upper_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(b_high))
def likelihood(self, x0, X, Y, U): """returns the log likelihood of the states `X` and observations `Y` under the current model p(X,Y|M) Parameters ---------- x0 : matrix initial state X : list of matrix state sequence Y : list of matrix observation sequence U : list of matrix input sequence Notes ---------- This calculates p(X,Y|M) = p(x0)\prod_{t=1}^Tp(y_t|x_t)\prod_{t=1}^Tp(x_t|x_{t-1}) using the model currently defined in self. """ l1 = pb.sum([pb.log(self.observation_dist(x,y)) for (x,y) in zip(X,Y)]) l2 = pb.sum([ pb.log(self.transition_dist(x,u,xdash)) for (x,u,xdash) in zip(X[:-1],U[:-1],X[1:])]) l3 = self.init_dist(x0) l = l1 + l2 + l3 assert not pb.isinf(l).any(), (l1,l2,l3) return l
def functions_one(): x = pylab.linspace(-20, 20, 1001) y1 = pylab.log(1 / (pylab.cos(x)**2)) y2 = pylab.log(1 / (pylab.sin(x)**2)) pylab.plot(x, y1) pylab.plot(x, y2) pylab.show()
def add_thermodynamic_constraints(cpl, dG0_f, c_range=(1e-6, 1e-2), T=default_T, bounds=None): """ For any compound that does not have an explicit bound set by the 'bounds' argument, create a bound using the 'margin' variables (the last to columns of A). """ Nc = dG0_f.shape[0] if bounds != None and len(bounds) != Nc: raise Exception( "The concentration bounds list must be the same length as the number of compounds" ) if bounds == None: bounds = [(None, None)] * Nc for c in xrange(Nc): if pylab.isnan(dG0_f[c, 0]): continue # unknown dG0_f - cannot bound this compound's concentration at all b_low = bounds[c][0] or c_range[0] b_high = bounds[c][1] or c_range[1] # lower bound: dG0_f + R*T*ln(Cmin) <= x_i cpl.variables.set_lower_bounds('c%d' % c, dG0_f[c, 0] + R * T * pylab.log(b_low)) # upper bound: x_i <= dG0_f + R*T*ln(Cmax) cpl.variables.set_upper_bounds('c%d' % c, dG0_f[c, 0] + R * T * pylab.log(b_high))
def calcAUC(data, y0, lag, mgr, asym, time): """ Calculate the area under the curve of the logistic function using its integrated formula [ A( [A-y0] log[ exp( [4m(l-t)/A]+2 )+1 ]) / 4m ] + At """ # First check that max growth rate is not zero # If so, calculate using the data instead of the equation if mgr == 0: auc = calcAUCData(data, time) else: timeS = time[0] timeE = time[-1] t1 = asym - y0 #try: t2_s = py.log(py.exp((4 * mgr * (lag - timeS) / asym) + 2) + 1) t2_e = py.log(py.exp((4 * mgr * (lag - timeE) / asym) + 2) + 1) #except RuntimeWarning as rw: # Exponent is too large, setting to 10^3 # newexp = 1000 # t2_s = py.log(newexp + 1) # t2_e = py.log(newexp + 1) t3 = 4 * mgr t4_s = asym * timeS t4_e = asym * timeE start = (asym * (t1 * t2_s) / t3) + t4_s end = (asym * (t1 * t2_e) / t3) + t4_e auc = end - start if py.absolute(auc) == float('Inf'): x = py.diff(time) auc = py.sum(x * data[1:]) return auc
def duxbury_icdf(X,L,s): """ Returns the inverse duxbury cdf evaluated at X. The duxbury CDF is 1 - exp( -(L^2)*exp( - (s/x)^2 ) ) """ return (-s*s/pylab.log( -pylab.log(1-X)/(L*L)) )**(0.5)
def weibull_lsq(data): """ Returns the weibull parameters estimated by using the least square method for the given data. The weibull CDF is 1 - exp(-(x/l)^k). One should be aware of the fact that this approach weighs the extreme (small or large) observations more than the bulk. """ # Evaluate the emperical CDF at the observations # and rescale to convert into emperical probability n = len(data) print type(data) print type(empCDF(data,data)) ecdf = empCDF(data,data)*n/(1.0 + n) # Make the array of "infered" variables and independent variables y = pylab.log(-pylab.log(1-ecdf)) x = pylab.log(data) # estimate regression coefficients of y = a*x + b a, b = lsqReg(x,y) # Extract the weibull parameters k = a l = pylab.exp(-b/k) return k, l
def query(q,DBs,Ms,n,l=1000,minVal=0.0,maxVal=1.0): #parameters P1=.01779 P2=.0000156 rho = log(P1)/log(P2) sims = len(Ms)#number of multi-runs for whp k = len(Ms[0])#number of random matrix projections per run candidates = set() #first iterate over the runs for s in xrange(sims): #next iterate over the n^rho nearby points hashVal = decodeGt24(q,Ms[s],minVal,maxVal) if DBs[s].has_key(hashVal): for c in DBs[s][hashVal]: candidates.add(c) for r in xrange(int(n**rho+.5)): hashVal = decodeGt24(q,Ms[s],minVal,maxVal,True) if DBs[s].has_key(hashVal): for c in DBs[s][hashVal]: candidates.add(c) if len(candidates)>2*l:return candidates return candidates
def _make_log_freq_map(self): """ :: For the given ncoef (bands-per-octave) and nfft, calculate the center frequencies and bandwidths of linear and log-scaled frequency axes for a constant-Q transform. """ fp = self.feature_params bpo = float(self.nbpo) # Bands per octave self._fftN = float(self.nfft) hi_edge = float(self.hi) lo_edge = float(self.lo) f_ratio = 2.0**(1.0 / bpo) # Constant-Q bandwidth self._cqtN = float(P.floor(P.log(hi_edge / lo_edge) / P.log(f_ratio))) self._dctN = self._cqtN self._outN = float(self.nfft / 2 + 1) if self._cqtN < 1: print("warning: cqtN not positive definite") mxnorm = P.empty(int(self._cqtN)) # Normalization coefficients # P.array([i * self.sample_rate / float(self._fftN) for i in P.arange(self._outN)]) fftfrqs = self._fftfrqs logfrqs = P.array([ lo_edge * P.exp(P.log(2.0) * i / bpo) for i in P.arange(self._cqtN) ]) logfbws = P.array([ max(logfrqs[i] * (f_ratio - 1.0), self.sample_rate / float(self._fftN)) for i in P.arange(int(self._cqtN)) ]) #self._fftfrqs = fftfrqs self._logfrqs = logfrqs self._logfbws = logfbws self._make_cqt()
def obs(pi=pi, phi=phi): logp = pl.log(1 - phi) * num_nonzeros + mc.binomial_like( r[nonzeros] * n[nonzeros], n[nonzeros], pi[nonzeros]) for n_i in n[~nonzeros]: logp += pl.log(phi + (1 - phi) * pl.exp(pl.log(1 - pi[~nonzeros]) * n[~nonzeros])).sum() return logp
def plot(self, criteria='AICc'): pylab.plot(self.x, [self.all_results[x]['BIC'] for x in self.x], 'o-', label='BIC') pylab.plot(self.x, [self.all_results[x]['AIC'] for x in self.x], 'o-', label='AIC') pylab.plot(self.x, [self.all_results[x]['AICc'] for x in self.x], 'o-', label='AICc') m = np.array([self.all_results[x][criteria] for x in self.x]).min() # [exp((m-this[criteria])/2) for this in amf.all_results] pylab.axhline(m - pylab.log(0.9) * 2, color='k', label='90% equi-probability', alpha=0.9) pylab.axhline(m - pylab.log(0.5) * 2, color='k', label='50% equi-probability', alpha=0.5) pylab.axhline(m - pylab.log(0.3) * 2, color='k', label='30% equi-probability', alpha=0.3) pylab.legend()
def dispersion_covariate_model(name, input_data, delta_lb, delta_ub): lower = pl.log(delta_lb) upper = pl.log(delta_ub) eta = mc.Uniform('eta_%s' % name, lower=lower, upper=upper, value=.5 * (lower + upper)) Z = input_data.select(lambda col: col.startswith('z_'), axis=1) Z = Z.select(lambda col: Z[col].std() > 0, 1) # drop blank cols if len(Z.columns) > 0: zeta = mc.Normal('zeta_%s' % name, 0, .25**-2, value=pl.zeros(len(Z.columns))) @mc.deterministic(name='delta_%s' % name) def delta(eta=eta, zeta=zeta, Z=Z.__array__()): return pl.exp(eta + pl.dot(Z, zeta)) return dict(eta=eta, Z=Z, zeta=zeta, delta=delta) else: @mc.deterministic(name='delta_%s' % name) def delta(eta=eta): return pl.exp(eta) * pl.ones_like(input_data.index) return dict(eta=eta, delta=delta)
def covariate_constraint( mu=vars['mu_age'], alpha=vars['alpha'], beta=vars['beta'], U_all=U_all, X_sex_max=X_sex_max, X_sex_min=X_sex_min, lower=pl.log(model.parameters[name]['level_bounds']['lower']), upper=pl.log(model.parameters[name]['level_bounds']['upper'])): log_mu_max = pl.log(mu.max()) log_mu_min = pl.log(mu.min()) alpha = pl.array([float(x) for x in alpha]) if len(alpha) > 0: for U_i in U_all: log_mu_max += max(0, alpha[U_i].max()) log_mu_min += min(0, alpha[U_i].min()) # this estimate is too crude, and is causing problems #if len(beta) > 0: # log_mu_max += pl.sum(pl.maximum(X_max*beta, X_min*beta)) # log_mu_min += pl.sum(pl.minimum(X_max*beta, X_min*beta)) # but leaving out the sex effect results in strange problems, too log_mu_max += X_sex_max * float(beta[sex_index]) log_mu_min += X_sex_min * float(beta[sex_index]) lower_violation = min(0., log_mu_min - lower) upper_violation = max(0., log_mu_max - upper) return mc.normal_like([lower_violation, upper_violation], 0., 1.e-6**-2)
def add_localized_dGf_constraints(self, cid2dG0_f, cid2bounds, c_range, T=300): self.T = T for (rid, sparse) in self.reactions: dG0_r = 0 for (cid, coeff) in sparse.iteritems(): if (cid in cid2dG0_f): dG0_r += coeff * cid2dG0_f[cid] else: dG0_r = None break (curr_c_min, curr_c_max) = cid2bounds.get(cid, (None, None)) if (curr_c_min == None): curr_c_min = c_range[0] if (curr_c_max == None): curr_c_max = c_range[1] if (coeff < 0): dG0_r += coeff * common.R * T * pylab.log(curr_c_max) else: dG0_r += coeff * common.R * T * pylab.log(curr_c_min) if (dG0_r != None and dG0_r > 0): # this reaction is a localized bottleneck, add a constraint that its flux = 0 constraint_name = rid + "_irreversible" self.cpl.linear_constraints.add(names=[constraint_name], senses='E', rhs=[0]) self.cpl.linear_constraints.set_coefficients( constraint_name, rid, 1)
def plot_data(firstch=0, lastch=0, flt=1): extract_data(firstch, lastch, flt) # executes C command print("done extracting") dat = numpy.loadtxt(outfname) # loads text data (numpy) print("data shape", dat.shape) s = dat.shape points = s[0] #how many points in teh dataset tm = dat[:, 0] tmd = numpy.diff(tm) tstep = sum(tmd) / len(tmd) print("min time", min(tm), "max time", max(tm)) print(tm[0:10]) tmp = int(points / pwelch_ratio) tmp2 = P.log(tmp) / P.log(2) # get in powers of to tmp3 = int(tmp2) np = pow(2, tmp3) print("nperseg = ", np) print("tstep = ", tstep) fx, pden = signal.welch(dat[:, 1], 1.0 / tstep, nperseg=np) for j in range(0, lastch + 1 - firstch): print("plotting", j) plt.plot(tm, dat[:, j + 1]) plt.show() plt.plot(fx, pden, 'r-') plt.xscale('log') plt.yscale('log') plt.show()
def add_margin_dGf_constraints(self, c_mid=1e-4): """ Sets the thermodynamic constraints on each of the reactions. Note that when using pCr optimization, there is no incentive to minimize the number of reactions or the flux, and this can cause the emergence of futile cycles in the solutions. should always follow add_dGr_constraints() """ if (self.unbounded_compounds_with_dG0_f == None): raise Exception( "Cannot add concentration constraints before calling add_dGr_constraints()" ) self.pCr = True self.cpl.variables.add(names=["pCr"], lb=[0], ub=[1e6]) for cid in sorted(self.unbounded_compounds_with_dG0_f): self.cpl.linear_constraints.add(names=[cid + "_conc_minimum"], senses='G', rhs=[pylab.log(c_mid)]) self.cpl.linear_constraints.set_coefficients( cid + "_conc_minimum", cid + "_concentration", 1) self.cpl.linear_constraints.set_coefficients( cid + "_conc_minimum", "pCr", 1) self.cpl.linear_constraints.add(names=[cid + "_conc_maximum"], senses='L', rhs=[pylab.log(c_mid)]) self.cpl.linear_constraints.set_coefficients( cid + "_conc_maximum", cid + "_concentration", 1) self.cpl.linear_constraints.set_coefficients( cid + "_conc_maximum", "pCr", -1)
def add_specific_dGf_constraints(self, cid2bounds): """ add constraints on dG_f for compounds that have a dG0_f and have a specific bound on the concentrations should always follow add_dGr_constraints() """ if (self.unbounded_compounds_with_dG0_f == None): raise Exception( "Cannot add concentration constraints before calling add_dGr_constraints()" ) bounded_compounds = set() for cid in sorted( self.unbounded_compounds_with_dG0_f.intersection( cid2bounds.keys())): (curr_c_min, curr_c_max) = cid2bounds[cid] if (curr_c_min != None): self.cpl.variables.set_lower_bounds(cid + "_concentration", pylab.log(curr_c_min)) bounded_compounds.add(cid) if (curr_c_max != None): self.cpl.variables.set_upper_bounds(cid + "_concentration", pylab.log(curr_c_max)) bounded_compounds.add(cid) self.unbounded_compounds_with_dG0_f = self.unbounded_compounds_with_dG0_f.difference( bounded_compounds)
def query(q, DBs, Ms, n, l=1000, minVal=0.0, maxVal=1.0): #parameters P1 = .01779 P2 = .0000156 rho = log(P1) / log(P2) sims = len(Ms) #number of multi-runs for whp k = len(Ms[0]) #number of random matrix projections per run candidates = set() #first iterate over the runs for s in xrange(sims): #next iterate over the n^rho nearby points hashVal = decodeGt24(q, Ms[s], minVal, maxVal) if DBs[s].has_key(hashVal): for c in DBs[s][hashVal]: candidates.add(c) for r in xrange(int(n**rho + .5)): hashVal = decodeGt24(q, Ms[s], minVal, maxVal, True) if DBs[s].has_key(hashVal): for c in DBs[s][hashVal]: candidates.add(c) if len(candidates) > 2 * l: return candidates return candidates
def setup(dm, key, data_list, rate_stoch): """ Generate the PyMC variables for a log-normal model of a function of age Parameters ---------- dm : dismod3.DiseaseModel the object containing all the data, priors, and additional information (like input and output age-mesh) key : str the name of the key for everything about this model (priors, initial values, estimations) data_list : list of data dicts the observed data to use in the beta-binomial liklihood function rate_stoch : pymc.Stochastic a PyMC stochastic (or deterministic) object, with len(rate_stoch.value) == len(dm.get_estimation_age_mesh()). Results ------- vars : dict Return a dictionary of all the relevant PyMC objects for the log-normal model. vars['rate_stoch'] is of particular relevance, for details see the beta_binomial_model """ vars = {} est_mesh = dm.get_estimate_age_mesh() vars['rate_stoch'] = rate_stoch # set up priors and observed data prior_str = dm.get_priors(key) dismod3.utils.generate_prior_potentials(vars, prior_str, est_mesh) vars['observed_rates'] = [] for d in data_list: age_indices = dismod3.utils.indices_for_range(est_mesh, d['age_start'], d['age_end']) age_weights = d.get('age_weights', pl.ones(len(age_indices)) / len(age_indices)) lb, ub = dm.bounds_per_1(d) se = (pl.log(ub) - pl.log(lb)) / (2. * 1.96) if pl.isnan(se) or se <= 0.: se = 1. print 'data %d: log(value) = %f, se = %f' % (d['id'], pl.log(dm.value_per_1(d)), se) @mc.observed @mc.stochastic(name='obs_%d' % d['id']) def obs(f=vars['rate_stoch'], age_indices=age_indices, age_weights=age_weights, value=pl.log(dm.value_per_1(d)), tau=se**-2, data=d): f_i = dismod3.utils.rate_for_range(f, age_indices, age_weights) return mc.normal_like(value, pl.log(f_i), tau) vars['observed_rates'].append(obs) return vars
def covariate_constraint(mu=vars['mu_age'], alpha=vars['alpha'], beta=vars['beta'], U_all=U_all, X_sex_max=X_sex_max, X_sex_min=X_sex_min, lower=pl.log(model.parameters[name]['level_bounds']['lower']), upper=pl.log(model.parameters[name]['level_bounds']['upper'])): log_mu_max = pl.log(mu.max()) log_mu_min = pl.log(mu.min()) alpha = pl.array([float(x) for x in alpha]) if len(alpha) > 0: for U_i in U_all: log_mu_max += max(0, alpha[U_i].max()) log_mu_min += min(0, alpha[U_i].min()) # this estimate is too crude, and is causing problems #if len(beta) > 0: # log_mu_max += pl.sum(pl.maximum(X_max*beta, X_min*beta)) # log_mu_min += pl.sum(pl.minimum(X_max*beta, X_min*beta)) # but leaving out the sex effect results in strange problems, too log_mu_max += X_sex_max*float(beta[sex_index]) log_mu_min += X_sex_min*float(beta[sex_index]) lower_violation = min(0., log_mu_min - lower) upper_violation = max(0., log_mu_max - upper) return mc.normal_like([lower_violation, upper_violation], 0., 1.e-6**-2)
def obs(f=vars['rate_stoch'], age_indices=age_indices, age_weights=age_weights, value=pl.log(dm.value_per_1(d)), tau=se**-2, data=d): f_i = dismod3.utils.rate_for_range(f, age_indices, age_weights) return mc.normal_like(value, pl.log(f_i), tau)
def obs(pi=pi): return ( pop_A_prev * pop_A_N * pl.log(pi) + (1 - pop_A_prev) * pop_A_N * pl.log(1 - pi) + pop_B_prev * pop_B_N * pl.log(pi) + (1 - pop_B_prev) * pop_B_N * pl.log(1 - pi) )
def make_pCr_problem(S, dG0_f, c_mid=1e-3, ratio=3.0, T=default_T, bounds=None, log_stream=None): """Creates a Cplex problem for finding the pCr. Simply sets up all the constraints. Does not set the objective. Args: S: stoichiometric matrix. dG0_f: deltaG0'-formation values for all compounds (in kJ/mol) (1 x compounds) c_mid: the default concentration to center the pCr on. ratio: the ratio between the distance of the upper bound from c_mid and the lower bound from c_mid (in logarithmic scale) bounds: the concentration bounds for metabolites. log_stream: where to write Cplex logs to. Returns: A cplex.Cplex object for the problem. """ Nc = S.shape[1] if Nc != dG0_f.shape[0]: raise Exception("The S matrix has %d columns, while the dG0_f vector has %d" % (Nc, dG0_f.shape[0])) if bounds and len(bounds) != Nc: raise Exception("The concentration bounds list must be the same length as the number of compounds") cpl = create_cplex(S, dG0_f, log_stream) # Add pC variable. cpl.variables.add(names=['pC'], lb=[0], ub=[1e6]) # Add variables for concentration bounds for each metabolite. for c in xrange(Nc): if pylab.isnan(dG0_f[c, 0]): continue # unknown dG0_f - cannot bound this compound's concentration at all # dG at the center concentration. dG_f_mid = dG0_f[c, 0] + R*T*pylab.log(c_mid) if bounds == None or bounds[c][0] == None: # lower bound: x_i + r/(1+r) * R*T*ln(10)*pC >= dG0_f + R*T*ln(Cmid) cpl.linear_constraints.add(senses='G', names=['c%d_lower' % c], rhs=[dG_f_mid]) cpl.linear_constraints.set_coefficients('c%d_lower' % c, 'c%d' % c, 1) cpl.linear_constraints.set_coefficients('c%d_lower' % c, 'pC', R*T*pylab.log(10) * ratio / (ratio + 1.0)) else: # this compound has a specific lower bound on its activity cpl.variables.set_lower_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(bounds[c][0])) if bounds == None or bounds[c][1] == None: # upper bound: x_i - 1/(1+r) * R*T*ln(10)*pC <= dG0_f + R*T*ln(Cmid) cpl.linear_constraints.add(senses='L', names=['c%d_upper' % c], rhs=[dG_f_mid]) cpl.linear_constraints.set_coefficients('c%d_upper' % c, 'c%d' % c, 1) cpl.linear_constraints.set_coefficients('c%d_upper' % c, 'pC', -R*T*pylab.log(10) / (ratio + 1.0)) else: # this compound has a specific upper bound on its activity cpl.variables.set_upper_bounds('c%d' % c, dG0_f[c, 0] + R*T*pylab.log(bounds[c][1])) return cpl
def tune_alpha(self, drug_name, alphas=None, N=80, l1_ratio=0.5, n_folds=10, show=True, shuffle=False, alpha_range=[-2.8,0.1]): """Interactive tuning of the model (alpha). This is much faster than :meth:`plot_cindex` but much slower than ElasticNetCV .. plot:: :include-source: from gdsctools import * ic = IC50(gdsctools_data("IC50_v5.csv.gz")) gf = GenomicFeatures(gdsctools_data("genomic_features_v5.csv.gz")) en = GDSCElasticNet(ic, gf) en.tune_alpha(1047, N=40, l1_ratio=0.1) """ if alphas is None: # logspace returns a vector in natural space that guarantees a # uniform spacing in a log space (log10 or ln) # -2.8 to 0.5 means alpha from 1.58e-3 to 3.16 # This is equivalent to log(1.58e-3)=-6.45 to log(3.16)=1.15 in ln # scale a1, a2 = alpha_range alphas = pylab.logspace(a1, a2, N) # Let us now do a CV across difference alphas all_scores = [] for alpha in alphas: scores = self.fit(drug_name, alpha, l1_ratio=l1_ratio, n_folds=n_folds, shuffle=shuffle) all_scores.append(scores) # We can now plot the results that is the mean scores + error enveloppe df = pd.DataFrame(all_scores) # we also identify the max correlation and corresponding alpha maximum = df.mean(axis=1).max() alpha_best = alphas[df.mean(axis=1).argmax()] if show is True: mu = df.mean(axis=1) sigma = df.var(axis=1) pylab.clf() pylab.errorbar(pylab.log(alphas), mu, yerr=sigma, color="gray") pylab.plot(pylab.log(alphas), mu, 'or') pylab.axvline(pylab.log(alpha_best), lw=4, alpha=0.5, color='g') pylab.title("Mean scores (pearson) across alphas for Kfold=%s" % n_folds) pylab.xlabel("ln(alpha)") pylab.ylabel("mean score (pearson)") pylab.grid() results = {"alpha_best":alpha_best, "ln_alpha":pylab.log(alpha_best), "maximum_Rp":maximum} return results
def plot_risetimes(a, b, **kwargs): # plt.ion() # if kwargs is not None: # for key, value in kwargs.iteritems(): # if key == 'file_list': # file_list = value # if key == 'scan_line': # scan_line = value # varray = plt.array(get_value_from_cfg(file_list, scan_line)) n_files = a.shape[-1] cmap = plt.get_cmap('jet') c = [cmap(i) for i in plt.linspace(0, 1, n_files)] fig1, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10)) [ax.set_color_cycle(c) for ax in (ax1, ax2)] r = [] for i in xrange(n_files): x, y = a[:,i], b[:,i] # xo, yo = x, y #, get_envelope(x, y) xo, yo = get_envelope(x, y) p = plt.polyfit(xo, np.log(yo), 1) # Right way to fit... a la Nicolas - the fit expert! l = ax1.plot(x, plt.log(plt.absolute(y))) lcolor = l[-1].get_color() ax1.plot(xo, plt.log(yo), color=lcolor, marker='o', mec=None) ax1.plot(x, p[1] + x * p[0], color=lcolor, ls='--', lw=3) l = ax2.plot(x, y) lcolor = l[-1].get_color() ax2.plot(xo, yo, 'o', color=lcolor) xi = plt.linspace(plt.amin(x), plt.amax(x)) yi = plt.exp(p[1] + p[0] * xi) ax2.plot(xi, yi, color=lcolor, ls='--', lw=3) print p[1], p[0], 1 / p[0] # plt.draw() # ax1.cla() # ax2.cla() r.append(1/p[0]) ax2.set_ylim(0, 1000) plt.figure(2) plt.plot(r, lw=3, c='purple') # plt.gca().set_ylim(0, 10000) # ax3 = plt.subplot(111) # ax3.semilogy(x, y) # ax3.semilogy(xo, yo) return r
def log_mat(X): res = zeros(X.shape) * complex(0,0) for i in range(X.shape[0]): for j in range(X.shape[1]): if (X[i,j] > 0): res[i,j] = complex(log(X[i,j]), 0) elif (X[i,j] < 0): res[i,j] = complex(log(-X[i,j]), pi) else: res[i,j] = nan return res
def log_mat(X): res = zeros(X.shape) * complex(0, 0) for i in range(X.shape[0]): for j in range(X.shape[1]): if (X[i, j] > 0): res[i, j] = complex(log(X[i, j]), 0) elif (X[i, j] < 0): res[i, j] = complex(log(-X[i, j]), pi) else: res[i, j] = nan return res
def mStar(m,nu): """ Returns M* based on an array of nu(M)'s. M* is defined to be the mass at which nu(M) = 1. Used for concentration distribution. """ closest = N.where(nu < 1.)[0][-1] #nu increases with M logmstar = M.log(m[closest]) + M.log(m[closest+1]/m[closest])/M.log(nu[closest+1]/nu[closest])*\ M.fabs(M.log(nu[closest])) return M.exp(logmstar)
def runEisensteinHu(self, sig8): """ use (much faster, but somewhat less accurate) Eisenstein & Hu code. Not tested recently. """ #Output EHu file f = file('ehu.in','w') #f.write((str(self.cp.omega_baryon + self.cp.omega_cdm))+', '+str(self.cp.omega_lambda)+', '+\ # str(self.cp.omega_neutrino)+', '+str(self.cp.omega_baryon)+'\n') h = self.cp.hubble/100. om0 = (self.cp.ombh2 + self.cp.omch2)/h**2 f.write(str(om0)+', '+str(1.-om0)+', '+ str(self.cp.omega_neutrino)+', '+str(self.cp.ombh2/h**2)+'\n') f.write(str(h)+', '+str(self.cp.temp_cmb)+', '+str(self.cp.massless_neutrinos)+'\n') f.write(str(self.cp.transfer_redshift[0])+'\n') f.write(str(self.cp.transfer_kmax)+', '+str(self.cp.transfer_k_per_logint)+'\n') f.write('1\n') tilt = self.cp.scalar_spectral_index[0] f.write(str(tilt)+'\n') f.write('0\n') f.close() # run EHu code os.system('../ehu/power < ehu.in > ehu.crap') # read into c.k, c.pk eh = N.loadtxt('trans.dat') self.k = eh[:,0]*1. #print self.k self.logk = M.log(self.k) self.trans = eh[:,1]*1. if tilt == 1.: delH = 1.94e-5*(self.cp.omega_cdm + self.cp.omega_baryon)**(-0.785) delta = delH**2*(3000.0*self.k/(self.cp.hubble/100.))**4*self.trans**2 else: delH = 1.94e-5*self.cp.omega_cdm**(-0.785 - 0.05*M.log(tilt))\ * M.exp(-0.95*(tilt - 1.) - 0.169*(tilt - 1)**2) delta = delH**2*(3000.0*self.k/(self.cp.hubble/100.))**(3 + tilt)*self.trans**2 # Just an approximate normalization; really need sig8. self.pk = (2.*M.pi**2 * delta/self.k**3)*(self.cp.hubble/100.)**3 if self.cp.transfer_redshift[0] > 0.: ps = PowerSpectrum(self.cp) sig8use = sig8*ps.d1(self.cp.transfer_redshift[0])/ps.d1(0.) else: sig8use = sig8 normalizePk(self,sig8use) # sets c.logpk, too return
def run(self): """ create an inifile from the parameters and run camb on it and store the results in k,pk """ self.printIniFile() os.system(self.cambPath + "/camb " + self.iniName) self.k, self.pk = utils.readColumns(self.cp.output_root + "_matterpower.dat") self.logk, self.logpk = M.log(self.k), M.log(self.pk) self.pkSplineCoeff = SS.cspline1d(self.logpk)
def DFA(data, npoints=None, degree=1, use_median=False): """ computes the detrended fluctuation analysis returns the fluctuation F and the corresponding window length L :args: data (n-by-1 array): the data from which to compute the DFA npoints (int): the number of points to evaluate; if omitted the log(n) will be used degree (int): degree of the polynomial to use for detrending use_median (bool): use median instead of mean fluctuation :returns: F, L: the fluctuation F as function of the window length L """ # max window length: n/4 #0th: compute integral integral = cumsum(data - mean(data)) #1st: compute different window lengths n_samples = npoints if npoints is not None else int(log(len(data))) lengths = sort(array(list(set( logspace(2,log(len(data)/4.),n_samples,base=exp(1)).astype(int) )))) #print lengths all_flucs = [] used_lengths = [] for wlen in lengths: # compute the fluctuation of residuals from a linear fit # according to Kantz&Schreiber, ddof must be the degree of polynomial, # i.e. 1 (or 2, if mean also counts? -> see in book) curr_fluc = [] # rrt = 0 for startIdx in arange(0,len(integral),wlen): pt = integral[startIdx:startIdx+wlen] if len(pt) > 3*(degree+1): resids = pt - polyval(polyfit(arange(len(pt)),pt,degree), arange(len(pt))) # if abs(wlen - lengths[0]) < -1: # print resids[:20] # elif rrt == 0: # print "wlen", wlen, "l0", lengths[0] # rrt += 1 curr_fluc.append(std(resids, ddof=degree+1)) if len(curr_fluc) > 0: if use_median: all_flucs.append(median(curr_fluc)) else: all_flucs.append(mean(curr_fluc)) used_lengths.append(wlen) return array(all_flucs), array(used_lengths)
def get_transportE(self, column, start, stop): try: b = self.data[column][start:stop] a = self.data["t"][start:stop] m, c = pylab.polyfit(pylab.log(a), b, 1) E_t1 = m * pylab.log(a[100]) + c E_t = m * pylab.log(a) + c return E_t1, E_t except KeyError: return 0, 0 pass
def add_global_dGf_constraints(self, global_c_range=(1e-6, 1e-2)): """ add global constraints on dG_f for compounds that have a dG0_f (and don't have a specific bound already) should always follow add_dGr_constraints() """ if self.unbounded_compounds_with_dG0_f == None: raise Exception("Cannot add concentration constraints before calling add_dGr_constraints()") # add constraints on dG_f for compounds that have a dG0_f for cid in sorted(self.unbounded_compounds_with_dG0_f): self.cpl.variables.set_lower_bounds(cid + "_concentration", pylab.log(global_c_range[0])) self.cpl.variables.set_upper_bounds(cid + "_concentration", pylab.log(global_c_range[1]))
def update_G_until(G, order): if order <= 3: gam = 0.577215664901532 return [0, -gam-pl.log(2.0), 2.0 - gam - pl.log(2.0), 2.0 - gam - pl.log(2.0)] if len(G) > order: return G last_ord = len(G) - 1 while last_ord < order: g = G[last_ord]+2.0/last_ord G += [g, g] last_ord += 2 return G
def runEHuShiftBAO(self, sig8, shiftfact=1.): #Output EHu file f = file('ehu.in','w') f.write((str(self.cp.omega_baryon + self.cp.omega_cdm))+', '+str(self.cp.omega_lambda)+', '+\ str(self.cp.omega_neutrino)+', '+str(self.cp.omega_baryon)+'\n') f.write(str(self.cp.hubble/100.)+', '+str(self.cp.temp_cmb)+', '+str(self.cp.massless_neutrinos)+'\n') f.write(str(self.cp.transfer_redshift[0])+'\n') f.write(str(self.cp.transfer_kmax)+', '+str(self.cp.transfer_k_per_logint)+'\n') f.write('1\n') tilt = self.cp.scalar_spectral_index[0] f.write(str(tilt)+'\n') f.write('0\n') f.close() # run EHu code os.system('~/ehu/power < ehu.in') # read into c.k, c.pk eh = N.loadtxt('trans.dat') self.k = eh[:,0]*1. self.logk = M.log(self.k) if (getnowig): self.trans = eh[:,3]*1. else: self.trans = eh[:,1]*1. #M.loglog(self.k,self.trans) #M.show() if tilt == 1.: delH = 1.94e-5*(self.cp.omega_cdm + self.cp.omega_baryon)**(-0.785) delta = delH**2*(3000.0*self.k/(self.cp.hubble/100.))**4*self.trans**2 else: delH = 1.94e-5*self.cp.omega_cdm**(-0.785 - 0.05*M.log(tilt))\ * M.exp(-0.95*(tilt - 1.) - 0.169*(tilt - 1)**2) delta = delH**2*(3000.0*self.k/(self.cp.hubble/100.))**(3 + tilt)*self.trans**2 # Just an approximate normalization; really need sig8. self.pk = (2.*M.pi**2 * delta/self.k**3)*(self.cp.hubble/100.)**3 if self.cp.transfer_redshift[0] > 0.: ps = PowerSpectrum(self.cp) sig8use = sig8*ps.d1(self.cp.transfer_redshift[0])/ps.d1(0.) else: sig8use = sig8 normalizePk(self,sig8use) # sets c.logpk, too self.pkSplineCoeff = SS.cspline1d(self.logpk) return
def calculate_uncertainty(reaction, min_C, max_C, T): N_subs = 0 N_prod = 0 for cid, coeff in reaction.sparse.iteritems(): if cid == 1 or cid == 80: # uncertainty in H2O and H+ are ignored continue if coeff > 0: N_prod += coeff else: N_subs -= coeff ddG_min = R * T * (N_prod * pylab.log(min_C) - N_subs * pylab.log(max_C)) ddG_max = R * T * (N_prod * pylab.log(max_C) - N_subs * pylab.log(min_C)) return (ddG_min, ddG_max)
def interpolateLinLog(y,x,xNew): """ linear interpolation in LOG space of y[x] onto y[xNew] Linearly extrapolates if outside range """ logx = M.log(x) logy = M.log(y) logxNew = M.log(xNew) logxInd = M.clip(M.searchsorted(logx,logxNew)-1,0,len(logx)-2) logxFract = (logxNew-logx[logxInd])/(logx[logxInd+1]-logx[logxInd]) return M.exp(logy[logxInd]+logxFract*(logy[logxInd+1]-logy[logxInd]))
def tune_alpha(self, drug_name, alphas=None, N=100, l1_ratio=0.5, n_folds=10, plot=True): """ .. plot:: an.tune_alpha("1047", N=100, l1_ratio=0.1) an.tune_alpha("1047", N=100, l1_ratio=0.01) an.tune_alpha("1047", N=100, l1_ratio=0.001) an.tune_alpha("1047", N=100, l1_ratio=0.0001) 29, 34, 52, 1014, 1015, 1024, 1036, 1047, 1061 """ # alphas = 10**-linspace(6,1,100) if alphas is None: alphas = pylab.logspace(-5, 0, N) all_scores = [] median_scores = [] for alpha in alphas: scores = self.elastic_net(drug_name, alpha, l1_ratio=l1_ratio, n_folds=n_folds) median_scores.append(np.mean(scores)) all_scores.append(scores) #pylab.plot(pylab.log(alphas), median_scores, '-o') df = pd.DataFrame(all_scores) maximum = df.mean(axis=1).max() alpha_best = alphas[df.mean(axis=1).argmax()] if plot is True: mu = df.mean(axis=1) sigma = df.std(axis=1) pylab.clf() pylab.errorbar(pylab.log(alphas), mu, yerr=sigma) pylab.plot(pylab.log(alphas), mu, 'or') pylab.axvline(pylab.log(alpha_best), lw=4, alpha=0.5, color='g') pylab.title("Mean scores across alphas") pylab.xlabel("alpha") pylab.ylabel("mean score") return alphas, all_scores, maximum, alpha_best
def weibull_mle_cfun(p,data): """ Returns the weibull mle log cost function (normalized) The weibull CDF is 1 - exp(-(x/l)^k). """ k = p[0] l = p[1] n = len(data) cost = 0 cost = cost + pylab.log(k/l) cost = cost + (k-1)*pylab.sum(pylab.log(data/l))/n cost = cost - pylab.sum( (data/l)**k)/n return -cost
def plot_cindex(self, drug_name, alphas, l1_ratio=0.5, n_folds=10, hold=False): # This is longish (300 seconds with 10 folds and 80 alphas # for GDSC v5 data sets. from dreamtools.core.cindex import cindex CI_train = {} CI_test = {} for c in range(n_folds): CI_train[c] = [] CI_test[c] = [] from easydev import Progress pb = Progress(len(alphas)) for i, alpha in enumerate(alphas): self.elastic_net(drug_name, alpha=alpha, l1_ratio=l1_ratio, n_folds=n_folds) # Look at the first fold only for kf in range(n_folds): x_train = self.kfold_data['x_train'][kf].values y_train = self.kfold_data['y_train'][kf].values x_test = self.kfold_data['x_test'][kf].values y_test = self.kfold_data['y_test'][kf].values x_train_pred = self.en.predict(x_train) x_test_pred = self.en.predict(x_test) CI_test[kf].append(1-cindex(x_test_pred, y_test, [True]*len(y_test))) CI_train[kf].append(1-cindex(x_train_pred, y_train, [True] * len(y_train))) pb.animate(i) mu_train = pd.DataFrame(CI_train).transpose().mean() sigma_train = pd.DataFrame(CI_train).transpose().std() mu_test = pd.DataFrame(CI_test).transpose().mean() sigma_test = pd.DataFrame(CI_test).transpose().std() best_alpha = alphas[pd.DataFrame(CI_test).mean(axis=1).argmax()] pylab.clf() pylab.errorbar(pylab.log(alphas), mu_train, yerr=sigma_train, label="train") pylab.errorbar(pylab.log(alphas)+.1, mu_test, yerr=sigma_test, label="test") pylab.plot(pylab.log(alphas), mu_train, 'ob') pylab.plot(pylab.log(alphas)+.1, mu_train, 'or') pylab.legend() pylab.axvline(pylab.log(best_alpha), lw=2, color="purple") return best_alpha
def duxbury_mle_cfun(p,L,data): """ Returns the duxbury mle log cost function (normalized) The duxbury CDF is 1 - exp( -(L^2)*exp( - (s/x)^2 ) ) """ s = p[0] n = len(data) cost = 0 cost = cost + 2*pylab.log(s) cost = cost - 3*pylab.sum( pylab.log(data) )/n cost = cost - L*L*pylab.sum( pylab.exp(-((s/data)**2)))/n cost = cost - pylab.sum( (s/data)**2)/n return -cost
def plot(self, criteria='AICc'): pylab.plot(self.x, [self.all_results[x]['BIC'] for x in self.x], 'o-', label='BIC') pylab.plot(self.x, [self.all_results[x]['AIC'] for x in self.x], 'o-', label='AIC') pylab.plot(self.x, [self.all_results[x]['AICc'] for x in self.x], 'o-', label='AICc') m = np.array([self.all_results[x][criteria] for x in self.x]).min() # [exp((m-this[criteria])/2) for this in amf.all_results] pylab.axhline(m - pylab.log(0.9)*2, color='k', label='90% equi-probability', alpha=0.9) pylab.axhline(m - pylab.log(0.5)*2, color='k', label='50% equi-probability', alpha=0.5) pylab.axhline(m - pylab.log(0.3)*2, color='k', label='30% equi-probability', alpha=0.3) pylab.legend()
def plot_one(filename, name, axes, xbegin1, xwidth): print "opening " + filename # open output and create ray pf = load(filename) ray = pf.h.ortho_ray(0, [0.5, 0.5]) # Get exact result (xexact, exact) = make_exact(pf.current_time) # rough computation of level for each point in ray (probably better way) level = -pylab.log(ray['dx'] / ray['dx'][0]) / pylab.log(2.0) #get axis ax = pylab.axes(axes) #plot grey for refined region x1 = pylab.array([0.25, 0.75]) y1 = pylab.array([0.0, 0.0]) ax.fill_between(x1, y1, y1 + 3, facecolor='grey', alpha=0.2) # plot pylab.axhline(0, color='k', linestyle='dotted') pylab.scatter(ray['x'] + 0.5 * ray['dx'], ray['Density'], c=level, s=40, linewidths=(0, )) pylab.plot(xexact, exact) #set boundaries pylab.axis([xbegin1, xbegin1 + xwidth, 0.7, 3.0]) ax.xaxis.set_major_locator(pylab.matplotlib.ticker.MultipleLocator(0.1)) # Set axis (clunky) if axes[1] < 0.3: pylab.xlabel('Position') else: ax.set_xticklabels([]) if axes[0] < 0.12: pylab.ylabel('Density') else: ax.set_yticklabels([]) pylab.text(xbegin1 + 0.05 * xwidth, 1.2, name, va='top') pylab.text(xbegin1 + 0.05 * xwidth, 1.0, "t=%3.2f" % pf.current_time, va='top')
def plot_one(filename, name, axes): field = 'Density' print "opening " + filename # open output and create ray pf = load(filename) ray = pf.h.ortho_ray(0, [0.5, 0.5]) # first interpolate the exact solution onto the ray ray_exact = { 'x': ray['x'], 'Density': pylab.stineman_interp(ray['x'], exact['x'], exact['Density']), 'x-velocity': pylab.stineman_interp(ray['x'], exact['x'], exact['x-velocity']), 'Pressure': pylab.stineman_interp(ray['x'], exact['x'], exact['Pressure']), 'InternalEnergy': pylab.stineman_interp(ray['x'], exact['x'], exact['InternalEnergy']) } # compute first order and max error norms delta = ray['dx'] * abs(ray[field] - ray_exact[field]) norm = delta.sum() maxnorm = abs(ray[field] - ray_exact[field]).max() # rough computation of level for each point in ray (probably better way) level = -pylab.log(ray['dx'] / ray['dx'][0]) / pylab.log(2.0) # plot ax = pylab.axes(axes) pylab.axhline(0, color='k', linestyle='dotted') pylab.plot(exact['x'], exact[field], c='black', linewidth=(1, )) pylab.scatter(ray['x'], ray[field], c=level, s=8, linewidths=(0, )) # Set axis (clunky) pylab.axis([0, 0.999, 0, 1.1]) # for density if axes[1] < 0.5: pylab.xlabel('Position') else: ax.set_xticklabels([]) if axes[0] < 0.1: pylab.ylabel(field) else: ax.set_yticklabels([]) error_label(name, norm, maxnorm, 0.41, 1.0)
def likelihood(self, verbose=None): ''' Compute the log-likelihood of the current simulation based on the number of new diagnoses. ''' if verbose is None: verbose = self['verbose'] loglike = 0 if self.data is not None and len( self.data ): # Only perform likelihood calculation if data are available for d, datum in enumerate(self.data['new_positives']): if not pl.isnan( datum): # Skip days when no tests were performed estimate = self.results['diagnoses'][d] p = cvu.poisson_test(datum, estimate) logp = pl.log(p) loglike += logp sc.printv( f' {self.data["date"][d]}, data={datum:3.0f}, model={estimate:3.0f}, log(p)={logp:10.4f}, loglike={loglike:10.4f}', 2, verbose) self.results['likelihood'] = loglike sc.printv(f'Likelihood: {loglike}', 1, verbose) return loglike
def likelihood(self, verbose=None): ''' Compute the log-likelihood of the current simulation based on the number of new diagnoses. ''' if verbose is None: verbose = self['verbose'] if not self.results['ready']: self.run(calc_likelihood=False, verbose=verbose) # To avoid an infinite loop loglike = 0 for d, datum in enumerate(self.data['new_positives']): if not pl.isnan(datum): # Skip days when no tests were performed estimate = self.results['diagnoses'][d] p = cv.poisson_test(datum, estimate) logp = pl.log(p) loglike += logp if verbose >= 2: print( f' {self.data["date"][d]}, data={datum:3.0f}, model={estimate:3.0f}, log(p)={logp:10.4f}, loglike={loglike:10.4f}' ) self.results['likelihood'] = loglike if verbose >= 1: print(f'Likelihood: {loglike}') return loglike
def psp_parameter_estimate_fixmem(time, value): smoothing_kernel = 10 smoothed_value = p.convolve( value, p.ones(smoothing_kernel) / float(smoothing_kernel), "same") mean_est_part = int(len(value) * .1) mean_estimate = p.mean(smoothed_value[-mean_est_part:]) noise_estimate = p.std(value[-mean_est_part:]) integral = p.sum(smoothed_value - mean_estimate) * (time[1] - time[0]) f = 1. A_estimate = (max(smoothed_value) - mean_estimate) / (1. / 4.) min_A = noise_estimate if A_estimate < min_A: A_estimate = min_A t1_est = integral / A_estimate * f t2_est = 2 * t1_est tmax_est = time[p.argmax(smoothed_value)] + p.log(t2_est / t1_est) * (t1_est * t2_est) / (t1_est - t2_est) return p.array([ tmax_est, A_estimate, t1_est, mean_estimate])
def logNormalise(values, maxValue): if not maxValue: for e in values: if not maxValue or maxValue < values[e]: maxValue = values[e] emin = None emax = None for e in values: if values[e] != 0: values[e] = log(values[e]) / log(maxValue) if not emin or emin > values[e]: emin = values[e] if not emax or emax < values[e]: emax = values[e] for e in values: values[e] = (values[e] - emin) / (emax - emin)
def calcul_coefficients(self): """calculate coefficients for the chirplets Returns : apodization coeeficients """ num_coeffs = linspace(0, self._duration, int(self._samplerate * self._duration)) if self._polynome_degree: temp = (self._max_frequency - self._min_frequency) temp /= ( (self._polynome_degree + 1) * self._duration**self._polynome_degree ) * num_coeffs**self._polynome_degree + self._min_frequency wave = cos(2 * pi * num_coeffs * temp) else: temp = (self._min_frequency * (self._max_frequency / self._min_frequency)** (num_coeffs / self._duration) - self._min_frequency) temp *= self._duration / log( self._max_frequency / self._min_frequency) wave = cos(2 * pi * temp) coeffs = wave * hanning(len(num_coeffs))**2 return coeffs
def likelihood(self, weights=None, verbose=None): ''' Compute the log-likelihood of the current simulation based on the number of new diagnoses. ''' if verbose is None: verbose = self['verbose'] if weights is None: weights = {} loglike = 0 if self.data is not None: # Only perform likelihood calculation if data are available for key in self.reskeys: if key in self.data: if key in weights: weight = weights[key] else: weight = 1 for d, datum in enumerate(self.data[key]): if not pl.isnan(datum) and d < len( self.results[key].values): estimate = self.results[key][d] if datum and estimate: p = cvu.poisson_test(datum, estimate) logp = pl.log(p) loglike += weight * logp sc.printv( f' {self.data["date"][d]}, data={datum:3.0f}, model={estimate:3.0f}, log(p)={logp:10.4f}, loglike={loglike:10.4f}', 2, verbose) self.results['likelihood'] = loglike sc.printv(f'Likelihood: {loglike}', 1, verbose) return loglike
def spec_env(frame, coefs): mags = abs(frame) N = len(mags) ceps = pl.rfft(pl.log(mags[:N - 1])) ceps[coefs:] = 0 mags[:N - 1] = pl.exp(pl.irfft(ceps)) return mags
def transform(species, pH, I): dG_tag = [] for (nH, z, dG) in species: dG_tag.append(dG + R * T * nH * log(10) * pH - (z**2 - nH) * debye_huckel(I)) dG_tag_total = -(R * T) * log_sum_exp(array(dG_tag) / (-R * T)) return dG_tag_total
def equil_cont(T): pK1 = 17.788 - .073104 * T - .0051087 * 35 + 1.1463 * 10**-4 * T**2 pK2 = 20.919 - .064209 * T - .011887 * 35 + 8.7313 * 10**-5 * T**2 H_CO2 = pylab.exp( 9345.17 / T - 167.8108 + 23.3585 * pylab.log(T) + (.023517 - 2.3656 * 10**-4 * T + 4.7036 * 10**-7 * T**2) * 35) return [pK1, pK2, H_CO2]