def Asym_err(P_L, P_R): '''Returns the error in the asymmetry data''' try: P_L_err = sqrt(P_L) P_R_err = sqrt(P_R) A0_err = 2.*(sqrt(((sqr(P_L)*sqr(P_L_err))+(sqr(P_R)*sqr(P_R_err)))/pwr((P_L+P_R),4))) return A0_err except: print 'There was a problem finding the error in the asymmetry between the two channels. '\ 'Make sure the data file is correctly formatted, if it is, the asym module may need maintainance' sys.exit()
def _forward(self, x, train_flg): if self.running_mean is None: N, D = x.shape self.running_mean = np.zeros(D) self.running_var = np.zeros(D) if train_flg: mu = x.mean(axis=0) xc = x - mu var = np.mean(xc ** 2, axis=0) std = np.sqr(var + 1e-7) xn = xc / std self.batch_size = x.shape[0] self.xc = xc self.xn = xn self.std = std self.running_mean = self.momentum * self.running_mean + (1 - self.momentum) * mu self.running_var = self.momentum * self.running_var + (1 - self.momentum) * var else: xc = x - self.running_mean xn = xc / np.sqrt(self.running_var + 1e-7) return self.gamma * xn + self.beta
def nusselt_L_vert(self, Ra_L: float, Pr: float) -> float: """ Computes Nusselt number Nu = Nu( Ra_L, Pr ) for natural convection at the vertical plate Args: Ra_L: Rayleigh number [/] Ra = Gr_L * Pr Pr: Prandtl number [/] Returns: Nusselt number [/] Reference: [INCR96] Equ (9.26) if Ra_L <= 1e9 and Equ. (9.27) if Ra_L > 1e9 """ assert Pr > 1e-20 assert 0 < Ra_L and Ra_L < 1e30 if Ra_L <= 1e9: return 0.68 + 0.67 * pow(Ra_L, 0.25) \ / pow(1 + pow(0.492 / Pr, 9./16), 4./9) else: return np.sqr(0.825 + 0.387 * np.pow(Ra_L, 1. / 6) / pow(1 + pow(0.492 / Pr, 9. / 16), 8. / 27))
def proj(u, v): absu = np.sqr(np.dot(u, u)) if absu < 0.000000000000001: p = float(0.0) * np.array(u) else: p = (np.dot(u, v) / np.dot(u, u)) * np.array(u) return p
def do_transform_ip(self, buffer: Gst.Buffer) -> Gst.FlowReturn: if not self._model: Gst.warning( f"No model speficied for {self}. Plugin working in passthrough mode" ) return Gst.FlowReturn.OK # print(buffer.get_size(), buffer.duration / 10**6) # map Gst.Buffer to READ content is_ok, map_info = buffer.map(Gst.MapFlags.READ) if is_ok: # parsing audio info # https://lazka.github.io/pgi-docs/GstAudio-1.0/classes/AudioInfo.html audio_info = GstAudio.AudioInfo() audio_info.from_caps(self.sinkpad.get_current_caps()) # bpf = bytes per frame (for S16LE bpf = 2 bytes) # np.int16 -> due to audio format S16LE # https://lazka.github.io/pgi-docs/GstAudio-1.0/enums.html#GstAudio.AudioFormat.S16LE frame = np.ndarray(map_info.size // audio_info.bpf, buffer=map_info.data, dtype=np.int16) self._frames.append(frame) buffer.unmap(map_info) cut_on_silence = self._silence_threshold > 0 num_frames = len(self._frames) if num_frames >= self._max_num_frames_seq: self._do_speech_recognition() else: if cut_on_silence and num_frames >= self._min_num_frames_seq: square = np.sqr(frame) peaksquare = np.max(square) squaresum = np.sum(square) normalizer = 1 << 30 ncs = float(squaresum) / normalizer print(ncs) if ncs > self._silence_threshold: self._silent_buffers_num += 1 if self._silent_buffers_num >= self._silence_duration: self._do_speech_recognition() # self._amplitude_mean += np.mean(frame) # self._amplitude_mean /= 2 # rms = np.sqrt(self._amplitude_mean ** 2) # if num_frames >= self._min_num_frames_seq and rms < self._rms_threshold: # self._do_speech_recognition() return Gst.FlowReturn.OK
def checknorm(matrix): sumvec = [] for i in range(0, len(matrix)): sum = 0.0 for j in range(0, len(matrix)): norm = np.dot(matrix[i], matrix[j]) absnorm = np.sqr(norm * norm) sum += absnorm sumvec.append(sum) return sumvec
def chirp_uncertainty(scale_atom: float, frequency_sample_rate_hz: float, gamma: float, index_shift: float) -> Tuple[float, float, float]: """ Uncertainty of chirp :param scale_atom: from chirp_scale or chirp_scale_from_order :param frequency_sample_rate_hz: sample rate in hz :param gamma: from index_shift, M/(2Q) :param index_shift: index of shift :return: time std in seconds, frequency std in Hz, angular frequency std in Hz """ time_std_s = scale_atom / np.sqr(2) / frequency_sample_rate_hz angular_frequency_std = np.sqrt(1 + (index_shift * gamma)**2) / scale_atom / np.sqr(2) angular_frequency_std_hz = frequency_sample_rate_hz * angular_frequency_std frequency_std_hz = angular_frequency_std_hz / 2 / np.pi return time_std_s, frequency_std_hz, angular_frequency_std_hz
def make_angle(curr_index, shaped_coords, basedir, n_a): import imp rot = imp.load_source("operations", str(basedir + "/operations/expansion_check.py")) angleline = [] for j in range(0, curr_index - 2): angleline.append([0.0, 0.0, 0.0]) unit_v1 = rot.uvec( np.array(shaped_coords[curr_index]) - np.array(shaped_coords[curr_index - 1]) ) unit_v2 = rot.uvec( np.array(shaped_coords[curr_index - 2]) - np.array(shaped_coords[curr_index - 1]) ) phi = rot.angle3d(unit_v1, unit_v2) angleelement = (math.cos(phi) * unit_v1 - unit_v2) / math.sin(phi) angleline.append([angleelement[0], angleelement[1], angleelement[2]]) distv1 = np.array(shaped_coords[curr_index]) - np.array(shaped_coords[curr_index - 1]) distv2 = np.array(shaped_coords[curr_index - 2]) - np.array(shaped_coords[curr_index - 1]) sqrtvec = [ np.sqr(abs(distv1[0] * distv2[0])), np.sqr(abs(distv1[1] * distv2[1])), np.sqr(abs(distv1[2] * distv2[2])), ] if distv1[0] * distv2[0] < 0.0: sqrtvec[0] *= -1.0 if distv1[1] * distv2[1] < 0.0: sqrtvec[1] *= -1.0 if distv1[2] * distv2[2] < 0.0: sqrtvec[2] *= -1.0 angleelement = (distv1 - math.cos(phi) * distv2) * unit_v1 + unit_v2 * ( distv2 - distv1 * math.cos(phi) ) / (np.array(sqrtvec) * math.sin(phi)) angleline.append([angleelement[0], angleelement[1], angleelement[2]]) angleelement = (math.cos(phi) * unit_v2 - unit_v1) / math.sin(phi) angleline.append([angleelement[0], angleelement[1], angleelement[2]]) for j in range(curr_index + 1, n_a): angleline.append([0.0, 0.0, 0.0]) return angleline
def normalizaL2(self): # SO TUDO POSITIVO matQuad = np.sqr(self.matrix) sumQuad = np.sum(matQuad, axis=0) rMS = np.sqrt(sumQuad) orientacaoN = copy.deepcopy(self.orientacaoOrg) # MANTEM AS ORIENTACOES DOS CRITERIOS matrixNormL2 = np.zeros((self.nalt,self.ncrit)) for crit in range(self.ncrit): for a in range(self.nalt): matrixNormL2[a,crit] = self.matrix[a,crit]/rMS[crit] pNj = (self.pjOrg / rMS).tolist() qNj = (self.qjOrg / rMS).tolist() vNj = (self.vjOrg / rMS).tolist() self.errUlt = (0, -1, -1) return orientacaoN, matrixNormL2, qNj, pNj, vNj
def nd_rodogram(x, y, *args): """ Inner most calculation step of rodogram This function is used in the inner most loop of `neighbour_diff_squared`. Parameters ---------- x, y : np.array Returns ------- np.array """ res = np.sqr(np.abs(x - y)) return res
def ray_triangle(self, ray, triangle): u = triangle[0] - triangle[1] v = triangle[1] - triangle[2] n = numpy.cross(u,v) nnorm = n / numpy.sqrt(numpy.sum(numpy.sqr(n))) if iszeros(n): return (False, None, None) direction = ray[1] - ray[0] w0 = ray[0] - triangle[0] a = -numpy.dot(n, w0) b = numpy.dot(n, direction) if abs(b)<1e-6: if a==0: return (True, None, None) else: return (False, None, None)
def magnitudes(v, axis=-1): "same as magnitude for 1D vectors, but returns array of magnitudes for arrays of vectors" return sqrt(sqr(v).sum(axis))
ydata = [] ysum = 0 ynorm = float(sum(sedata)) for k in range(len(sedata)): ysum += sedata[-(k + 1)] ydata.append(ysum / ynorm) xscale = "relative rank" yscale = "relative cumulative abundance" else: ydata = np.log(sedata).tolist() xrdata = list(range(1, len(ydata) + 1)) if ptype == "log-log": xdata = np.log(xrdata).tolist() xscale = "log(rank)" elif xscale == "log-sqrlog": xdata = np.sqr(np.log(xrdata)).tolist() xscale = "sqr(log(rank))" else: xdata = xrdata xscake = "rank" (rx, ry) = ([], []) (r2x, r2y) = ([], []) if regression != "no": (rx, ry) = calc_regression(edata[gscnt]) rdata.append([rx, ry, r2x, r2y]) yscale = "log(count)" ldata.append([xdata[:], ydata[:], ckey, skey]) print("var ldata = %s;" % json.dumps(ldata)) print("var rdata = %s;" % json.dumps(rdata))
def nma_3Nminus6dof_asfunction(hessfile, basedir): extr = imp.load_source( "orcaextractors", str(basedir) + "/operations/hessian_from_hes.py" ) extrmass = imp.load_source( "orcaextractors", str(basedir) + "/operations/mass_from_hes.py" ) extrxyz = imp.load_source( "orcaextractors", str(basedir) + "/operations/geo_from_hes.py" ) hessian = extr.hessian_from_hes(hessfile) mass = extrmass.mass_from_hes(hessfile) coords = extrxyz.geo_from_hes(hessfile, "B") # Begin Center of mass com = [] for i in range(0, 3): curr_com = float(0.0) sum_mass = float(0.0) for j in range(0, len(mass)): curr_com += float(mass[j]) * float(coords[j * 3 + i]) sum_mass += float(mass[j]) com.append(float(curr_com) / float(sum_mass)) # End Center of mass # Begin shift coords shift_coords = coords for i in range(0, len(mass)): for j in range(0, 3): shift_coords[i * 3 + j] = float(shift_coords[i * 3 + j]) - float(com[j]) t_inert = [] t_row = [] ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 1] * shift_coords[i * 3 + 1] + shift_coords[i * 3 + 2] * shift_coords[i * 3 + 2] ) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 0] * shift_coords[i * 3 + 1]) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 0] * shift_coords[i * 3 + 2]) t_row.append(ele) t_inert.append(t_row) t_row = [] t_row.append(float(t_inert[0][1])) ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 0] * shift_coords[i * 3 + 0] + shift_coords[i * 3 + 2] * shift_coords[i * 3 + 2] ) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 1] * shift_coords[i * 3 + 2]) t_row.append(ele) t_inert.append(t_row) t_row = [] t_row.append(float(t_inert[0][2])) t_row.append(float(t_inert[1][2])) ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 0] * shift_coords[i * 3 + 0] + shift_coords[i * 3 + 1] * shift_coords[i * 3 + 1] ) t_row.append(ele) t_inert.append(t_row) # generate principal moments and their eigenvectors e_princ, princ = np.linalg.eig(np.array(t_inert)) # begin setup transrot transrotmat = [] for i in range(0, 3): transvec = [] for j in range(0, len(mass)): for k in range(0, i): transvec.append(float(0.0)) transvec.append(math.sqrt(float(mass[j]))) for k in range(i + 1, 3): transvec.append(float(0.0)) transrotmat.append(transvec) # begin rotvecs rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) py = np.dot(np.array(curr_coords).astype(float), np.array(princ[1]).astype(float)) pz = np.dot(np.array(curr_coords).astype(float), np.array(princ[2]).astype(float)) for j in range(0, 3): rotvec.append( (py * princ[2][j] - pz * princ[1][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) px = np.dot(np.array(curr_coords).astype(float), np.array(princ[0]).astype(float)) pz = np.dot(np.array(curr_coords).astype(float), np.array(princ[2]).astype(float)) for j in range(0, 3): rotvec.append( (pz * princ[0][j] - px * princ[2][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) px = np.dot(np.array(curr_coords).astype(float), np.array(princ[0]).astype(float)) py = np.dot(np.array(curr_coords).astype(float), np.array(princ[1]).astype(float)) for j in range(0, 3): rotvec.append( (px * princ[1][j] - py * princ[0][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) # Begin mass weight Hessianxyz for i in range(0, len(mass)): for j in range(0, 3): for k in range(0, len(mass)): for l in range(0, 3): hessian[i * 3 + j][k * 3 + l] /= math.sqrt( float(mass[i]) * float(mass[k]) ) if ( hessian[i * 3 + j][k * 3 + l] < 0.00000000000001 and hessian[i * 3 + j][k * 3 + l] > 0.0 ): hessian[i * 3 + j][k * 3 + l] = float(0.0) if ( hessian[i * 3 + j][k * 3 + l] > -0.00000000000001 and hessian[i * 3 + j][k * 3 + l] < 0.0 ): hessian[i * 3 + j][k * 3 + l] = float(0.0) # end mass weight Hessianxyz # begin normalize transrot norm_trm = [] for i in range(0, 6): scale = np.dot(transrotmat[i], transrotmat[i]) norm_vec = np.array(transrotmat[i]) / math.sqrt(scale) norm_trm.append(np.array(norm_vec).astype(float)) # end normalize transrot # Begin Ortho # Ortho for rotvecs for i in range(3, 6): ortho(6, norm_trm, 3) for i in range(6, len(mass) * 3): norm_trm = newvec(norm_trm, len(mass) * 3, i) ortho(i + 1, norm_trm, i) enter = 0 found = False for i in range(0, len(mass) * 3): currnorm = float(0.0) for j in range(0, len(mass) * 3): currnorm += np.dot(norm_trm[i], norm_trm[j]) if currnorm > 1.01: found = True while enter <= 500 and found: ortho(len(mass) * 3, norm_trm, 6) found = False for i in range(0, len(mass) * 3): currnorm = float(0.0) for j in range(0, len(mass) * 3): currnorm += np.dot(norm_trm[i], norm_trm[j]) if currnorm > 1.01: found = True enter += 1 if enter >= 500: print("had to orthogonalize more than 500 times! Check your results!") # ENd Ortho d_mat = [] for i in range(6, len(mass) * 3): d_mat.append(norm_trm[i]) dh = np.dot(d_mat, hessian) h_int = np.dot(dh, np.transpose(d_mat)) e_val, nm_matrix = np.linalg.eig(np.array(h_int)) mass_sqrtvec_matrix = [] for i in range(0, len(mass)): for p in range(0, 3): massline = [] for j in range(0, len(mass)): for k in range(0, 3): if i == j and p == k: massline.extend([float(1.0 / np.sqr(float(mass[i])))]) else: massline.extend([float(0.0)]) mass_sqrtvec_matrix.append(massline) mw_d_mat = np.dot(np.array(mass_sqrtvec_matrix), np.transpose(d_mat)) cart_nm = np.dot(mw_d_mat, nm_matrix) # begin sort eigenvalues/vectors done = False while not done: done = True for i in range(0, len(e_val) - 1): if float(e_val[i + 1]) < float(e_val[i]): done = False nm_matrix[:, [i, i + 1]] = nm_matrix[:, [i + 1, i]] cart_nm[:, [i, i + 1]] = cart_nm[:, [i + 1, i]] e_val[i + 1], e_val[i] = e_val[i], e_val[i + 1] return e_val, nm_matrix
def nma_3Nminus6dof(sysargs): basedir = sysargs[2] extr = imp.load_source( "orcaextractors", str(basedir) + "/operations/hessian_from_hes.py" ) extrmass = imp.load_source( "orcaextractors", str(basedir) + "/operations/mass_from_hes.py" ) extrxyz = imp.load_source( "orcaextractors", str(basedir) + "/operations/geo_from_hes.py" ) hessian = extr.hessian_from_hes(sysargs[1]) mass = extrmass.mass_from_hes(sysargs[1]) coords = extrxyz.geo_from_hes(sysargs[1], "B") # Begin Center of mass com = [] for i in range(0, 3): curr_com = float(0.0) sum_mass = float(0.0) for j in range(0, len(mass)): curr_com += float(mass[j]) * float(coords[j * 3 + i]) sum_mass += float(mass[j]) com.append(float(curr_com) / float(sum_mass)) # End Center of mass # Begin shift coords shift_coords = coords for i in range(0, len(mass)): for j in range(0, 3): shift_coords[i * 3 + j] = float(shift_coords[i * 3 + j]) - float(com[j]) # coords still match! good! # End shift coords # begin inertia tensor t_inert = [] t_row = [] ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 1] * shift_coords[i * 3 + 1] + shift_coords[i * 3 + 2] * shift_coords[i * 3 + 2] ) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 0] * shift_coords[i * 3 + 1]) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 0] * shift_coords[i * 3 + 2]) t_row.append(ele) t_inert.append(t_row) t_row = [] t_row.append(float(t_inert[0][1])) ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 0] * shift_coords[i * 3 + 0] + shift_coords[i * 3 + 2] * shift_coords[i * 3 + 2] ) t_row.append(ele) ele = float(0.0) for i in range(0, len(mass)): ele -= float(mass[i]) * (shift_coords[i * 3 + 1] * shift_coords[i * 3 + 2]) t_row.append(ele) t_inert.append(t_row) t_row = [] t_row.append(float(t_inert[0][2])) t_row.append(float(t_inert[1][2])) ele = float(0.0) for i in range(0, len(mass)): ele += float(mass[i]) * ( shift_coords[i * 3 + 0] * shift_coords[i * 3 + 0] + shift_coords[i * 3 + 1] * shift_coords[i * 3 + 1] ) t_row.append(ele) t_inert.append(t_row) # end inertia tensor # generate principal moments and their eigenvectors e_princ, princ = np.linalg.eig(np.array(t_inert)) # e_princ ok, princ ok! # begin setup transrot transrotmat = [] for i in range(0, 3): transvec = [] for j in range(0, len(mass)): for k in range(0, i): transvec.append(float(0.0)) transvec.append(math.sqrt(float(mass[j]))) for k in range(i + 1, 3): transvec.append(float(0.0)) transrotmat.append(transvec) # begin rotvecs rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) py = np.dot(np.array(curr_coords).astype(float), np.array(princ[1]).astype(float)) pz = np.dot(np.array(curr_coords).astype(float), np.array(princ[2]).astype(float)) for j in range(0, 3): rotvec.append( (py * princ[2][j] - pz * princ[1][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) # print curr_coords px = np.dot(np.array(curr_coords).astype(float), np.array(princ[0]).astype(float)) pz = np.dot(np.array(curr_coords).astype(float), np.array(princ[2]).astype(float)) for j in range(0, 3): rotvec.append( (pz * princ[0][j] - px * princ[2][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) rotvec = [] for i in range(0, len(mass)): curr_coords = [] for j in range(0, 3): curr_coords.append(shift_coords[3 * i + j]) # print curr_coords px = np.dot(np.array(curr_coords).astype(float), np.array(princ[0]).astype(float)) py = np.dot(np.array(curr_coords).astype(float), np.array(princ[1]).astype(float)) for j in range(0, 3): rotvec.append( (px * princ[1][j] - py * princ[0][j]) * math.sqrt(float(mass[i])) ) transrotmat.append(rotvec) # Begin mass weight Hessianxyz for i in range(0, len(mass)): for j in range(0, 3): for k in range(0, len(mass)): for l in range(0, 3): hessian[i * 3 + j][k * 3 + l] /= math.sqrt( float(mass[i]) * float(mass[k]) ) if ( hessian[i * 3 + j][k * 3 + l] < 0.00000000000001 and hessian[i * 3 + j][k * 3 + l] > 0.0 ): hessian[i * 3 + j][k * 3 + l] = float(0.0) if ( hessian[i * 3 + j][k * 3 + l] > -0.00000000000001 and hessian[i * 3 + j][k * 3 + l] < 0.0 ): hessian[i * 3 + j][k * 3 + l] = float(0.0) # end mass weight Hessianxyz # begin normalize transrot norm_trm = [] for i in range(0, 6): scale = np.dot(transrotmat[i], transrotmat[i]) norm_vec = np.array(transrotmat[i]) / math.sqrt(scale) norm_trm.append(np.array(norm_vec).astype(float)) # end normalize transrot # Begin Ortho # Ortho for rotvecs for i in range(3, 6): ortho(6, norm_trm, 3) for i in range(6, len(mass) * 3): norm_trm = newvec(norm_trm, len(mass) * 3, i) ortho(i + 1, norm_trm, i) enter = 0 found = False for i in range(0, len(mass) * 3): currnorm = float(0.0) for j in range(0, len(mass) * 3): currnorm += np.dot(norm_trm[i], norm_trm[j]) if currnorm > 1.01: found = True while (enter <= 500 and found) or enter < 10: ortho(len(mass) * 3, norm_trm, 6) found = False for i in range(0, len(mass) * 3): currnorm = float(0.0) for j in range(0, len(mass) * 3): currnorm += np.dot(norm_trm[i], norm_trm[j]) if 1.0 - (np.sqr(currnorm * currnorm)) > 0.01: found = True enter += 1 if enter >= 500: print("had to orthogonalize more than 500 times! Check your results!") # ENd Ortho d_mat = [] for i in range(6, len(mass) * 3): d_mat.append(norm_trm[i]) dh = np.dot(d_mat, hessian) h_int = np.dot(dh, np.transpose(d_mat)) e_val, nm_matrix = np.linalg.eigh(np.array(hessian)) b_mat = construct_bmat(shift_coords, basedir) # begin sort eigenvalues/vectors done = False while not done: done = True for i in range(0, len(e_val) - 1): if float(e_val[i + 1]) < float(e_val[i]): done = False nm_matrix[:, [i, i + 1]] = nm_matrix[:, [i + 1, i]] e_val[i + 1], e_val[i] = e_val[i], e_val[i + 1] for i in range(0, len(e_val)): if e_val[i] < 0.0: print(str(-1.0 * np.sqrt(abs(float(e_val[i])) * float(26424608)))) else: print(str(np.sqrt(float(e_val[i]) * float(26424608)))) exit(1) print(h_int) print(nm_matrix) print(np.array(nm_matrix))
def logp(self, data): """ Retrieve thetas """ theta7 = self.theta7 theta8 = self.theta8 k1 = self.k1 """ Parse data """ t = data[0] x0 = data[1] """ Long pause ll """ ## Full emptying time t_cs = 2. * tt.sqrt(x0) / k1 ## Stomach fullness x = 0.25 * tt.sqr(k1 * t) - k1 * t * tt.sqrt(x0) + x0 ## ll if time is less than full emptying a_part1 = k1 * t * theta8 * tt.pow( theta7, 2) * (0.5 * theta7 + theta8 * (0.25 * k1 * t * tt.sqrt(x0) - 0.5 * x0)) a_part2_1 = theta7 * (tt.pow(theta7, 2) + theta7 * theta8 * (x + x0) + x0 * tt.pow(theta8, 2) * x) a_part2_2 = tt.sqrt( theta7 * theta8) * (tt.arctan( (0.5 * k1 * t - tt.sqrt(x0)) * tt.sqrt(theta8 / theta7)) + tt.arctan(tt.sqrt(theta8 * x0 / theta7))) a_denom = k1 * tt.pow(theta7, 3) * theta8 * (theta7 + x0 * theta8) * ( theta7 + theta8 * x) a_psi = (a_part1 + a_part2_1 * a_part2_2) / a_denom a_phi = 1. / (tt.sqr(theta7 + theta8 * x)) ll_L_1 = tt.log(a_phi) - a_psi ## ll if time exceeds full emptying b_phi = 1. / tt.sqr(theta7) b_part1 = k1 * t_cs * theta8 * tt.pow( theta7, 2) * (0.5 * theta7 + theta8 * (0.25 * k1 * t_cs * tt.sqrt(x0) - 0.5 * x0)) b_part2_1 = theta7 * (tt.pow(theta7, 2) + theta7 * theta8 * (x0)) b_part2_2 = tt.sqrt( theta7 * theta8) * (tt.arctan( (0.5 * k1 * t_cs - tt.sqrt(x0)) * tt.sqrt(theta8 / theta7)) + tt.arctan(tt.sqrt(theta8 * x0 / theta7))) b_denom = k1 * tt.pow(theta7, 3) * theta8 * (theta7 + x0 * theta8) * (theta7) b_psi = (b_part1 + b_part2_1 * b_part2_2) / b_denom b_psi = b_psi + (t - t_cs) / tt.sqr(theta7) ll_L_2 = tt.log(b_phi) - b_psi ## Switch based on t_c ll_L = tt.switch(tt.lt(t, t_cs), ll_L_1, ll_L_2) return ll_L
def logp(self, data): """ Retrieve thetas """ theta5 = self.theta5 theta6 = self.theta6 theta7 = self.theta7 theta8 = self.theta8 theta9 = self.theta9 k1 = self.k1 #theta5_print = theano.printing.Print('theta5')(theta5) #theta6_print = theano.printing.Print('theta6')(theta6) #theta7_print = theano.printing.Print('theta7')(theta7) #theta8_print = theano.printing.Print('theta8')(theta8) #theta9_print = theano.printing.Print('theta9')(theta9) """ Parse data """ f_lengths = data[0] g_starts = data[1] rates = data[2] p_lengths = data[3] g_ends = data[4] p_lengths_print = theano.printing.Print('p_lengths')(p_lengths) g_ends_print = theano.printing.Print('g_ends')(g_ends) """ Transition kernel """ eps = 0.01 Q = eps + (1. - 2. * eps) / (1. + tt.exp(-0.1 * theta5 * (g_ends - 20. * theta6))) #Q_print = theano.printing.Print('Q')(Q) ll_Q = tt.log(Q) ll_notQ = tt.log(1 - tt.exp(ll_Q)) """ Short pause ll """ ll_S = bound( tt.log(theta7) - theta7 * p_lengths, p_lengths > 0, theta7 > 0) #ll_S = tt.log(theta7) - theta7*p_lengths # just exponential dist """ Long pause ll """ ## Full emptying time t_cs = 2. * tt.sqrt(g_ends) / k1 #t_cs_print = theano.printing.Print('t_cs')(t_cs) #p_lengths_print = theano.printing.Print('p_lengths')(p_lengths) ## ll if time is less than full emptying g_pausing_1 = 0.25 * tt.sqr( k1 * p_lengths) - tt.sqrt(g_ends) * k1 * p_lengths + g_ends phi_L_1 = 1. / (theta8 + theta9 * g_pausing_1) psi_L_1 = 2. * tt.arctan(0.5 * tt.sqrt(theta9 / theta8) * (k1 * p_lengths - 2. * tt.sqrt(g_ends))) psi_L_1 = psi_L_1 - 2. * tt.arctan(0.5 * tt.sqrt(theta9 / theta8) * (-2. * tt.sqrt(g_ends))) psi_L_1 = psi_L_1 / (k1 * tt.sqrt(theta8 * theta9)) ll_L_1 = tt.log(phi_L_1) - psi_L_1 ## ll if time exceeds full emptying phi_L_2 = 1. / theta8 psi_L_2 = 2. * tt.arctan(0.5 * tt.sqrt(theta9 / theta8) * (k1 * t_cs - 2. * tt.sqrt(g_ends))) psi_L_2 = psi_L_2 - 2. * tt.arctan(0.5 * tt.sqrt(theta9 / theta8) * (-2. * tt.sqrt(g_ends))) psi_L_2 = psi_L_2 / (k1 * tt.sqrt(theta8 * theta9)) psi_L_2 = psi_L_2 + (p_lengths - t_cs) / theta8 ll_L_2 = tt.log(phi_L_2) - psi_L_2 ## Switch based on t_c ll_L = tt.switch(tt.lt(p_lengths, t_cs), ll_L_1, ll_L_2) """ ll_1_print = theano.printing.Print('ll_1_print')(ll_L_1) ll_2_print = theano.printing.Print('ll_2_print')(ll_L_2) ll_L_print = theano.printing.Print('ll_L_print')(ll_L) """ ## Assemble 2 likelihood paths ll_Q_print = theano.printing.Print('ll_Q')(ll_Q) ll_notQ_print = theano.printing.Print('ll_notQ')(ll_notQ) ll_L_print = theano.printing.Print('ll_L')(ll_L) ll_S_print = theano.printing.Print('ll_S')(ll_S) ## Avoid numerical issues in logaddexp #ll_short = ll_notQ_print + ll_S_print #ll_long = ll_Q_print + ll_L_print ll_short = ll_notQ + ll_S ll_long = ll_Q + ll_L """ ll_pause = tt.switch(tt.lt(ll_short, ll_long), ll_short + tt.log1p(tt.exp(ll_long - ll_short)), ll_long + tt.log1p(tt.exp(ll_short - ll_long))) """ #ll_pause = ll_short + tt.log1p(tt.exp(ll_long - ll_short)) ll_pause = ll_long + tt.log1p(tt.exp(ll_short - ll_long)) ll_nans = tt.any(tt.isnan(ll_pause)) ll_nan_print = theano.printing.Print('ll_nans')(ll_nans) ll_pause_print = theano.printing.Print('ll_pause')(ll_pause) #ll = ll_pause # tt.log(tt.exp(ll_notQ + ll_S) + tt.exp(ll_Q + ll_L)) #ll_print = theano.printing.Print('ll')(ll) return ll_pause_print + ll_nan_print
def logp(self, data): """ Retrieve thetas """ theta4 = self.theta4 theta5 = self.theta5 theta6 = self.theta6 theta7 = self.theta7 theta8 = self.theta8 #theta9 = self.theta9 #self.theta10 = theta10 k1 = self.k1 """ Parse data """ f_lengths = data[0] g_starts = data[1] rates = data[2] p_lengths = data[3] g_ends = data[4] """ Transition kernel """ eps = 0.01 Q = eps + (1. - 2. * eps) / (1. + tt.exp(-0.1 * theta4 * (g_ends - 20. * theta5))) ll_Q = tt.log(Q) ll_notQ = tt.log(1 - tt.exp(ll_Q)) """ Short pause ll """ """ p = 0.5 ll_bout = p + bound(tt.log(theta6) - theta6 * p_lengths, p_lengths > 0, theta6 > 0) ll_drink = (1. - p) + bound(tt.log(theta9) - theta9 * p_lengths, p_lengths > 0, theta9 > 0) ll_S = ll_drink + tt.log1p(tt.exp(ll_bout - ll_drink)) """ ll_S = bound( tt.log(theta6) - theta6 * p_lengths, p_lengths > 0, theta6 > 0) """ Long pause ll """ ## Full emptying time t_cs = 2. * tt.sqrt(g_ends) / k1 ## ll if time is less than full emptying g_pausing_1 = 0.25 * tt.sqr( k1 * p_lengths) - tt.sqrt(g_ends) * k1 * p_lengths + g_ends phi_L_1 = 1. / (theta7 + theta8 * g_pausing_1) psi_L_1 = 2. * tt.arctan(0.5 * tt.sqrt(theta8 / theta7) * (k1 * p_lengths - 2. * tt.sqrt(g_ends))) psi_L_1 = psi_L_1 - 2. * tt.arctan(0.5 * tt.sqrt(theta8 / theta7) * (-2. * tt.sqrt(g_ends))) psi_L_1 = psi_L_1 / (k1 * tt.sqrt(theta7 * theta8)) ll_L_1 = tt.log(phi_L_1) - psi_L_1 ## ll if time exceeds full emptying phi_L_2 = 1. / theta7 psi_L_2 = 2. * tt.arctan(0.5 * tt.sqrt(theta8 / theta7) * (k1 * t_cs - 2. * tt.sqrt(g_ends))) psi_L_2 = psi_L_2 - 2. * tt.arctan(0.5 * tt.sqrt(theta8 / theta7) * (-2. * tt.sqrt(g_ends))) psi_L_2 = psi_L_2 / (k1 * tt.sqrt(theta7 * theta8)) psi_L_2 = psi_L_2 + (p_lengths - t_cs) / theta7 ll_L_2 = tt.log(phi_L_2) - psi_L_2 ## Switch based on t_c ll_L = tt.switch(tt.lt(p_lengths, t_cs), ll_L_1, ll_L_2) ## Avoid numerical issues in logaddexp ll_short = ll_notQ + ll_S ll_long = ll_Q + ll_L ll_pause = ll_short + tt.log1p(tt.exp(ll_long - ll_short)) return ll_pause
def compute_stats(data_chunk): chunkcount = data_chunk.size chunksum = np.sum(data_chunk) chunksqrsum = np.sum(np.sqr(data_chunk))
def sqr_magnitudes(v, axis=-1): "same as sqr_magnitude for 1D vectors, but returns array of magnitudes for arrays of vectors" return sqr(v).sum(axis)
customers.column y = customers['Yearly Amount Spent'] X = customers[['Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership']] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101) lm = LinearRegression() # Dealing with training data lm.fit(X_train, y_train) lm.coef_ prediction = lm.predict(X_test) plt.scatter(y_test, prediction) plt.xlabel('Y test(true value)') plt.ylabel('Predicted values') print('MAE', metrics.mean_absolute_error(y_test, prediction)) print('MSE', metrics.mean_squared_error(y_test, prediction)) print('RMSE', np.sqr(metrics.mean_squared_error(y_test, prediction))) metrics.explained_variance_score(y_test, prediction) sns.distplot((y_test - prediction), bins=50) # So which data feature have got the highest effect on the ecommerce cdf = pd.DataFrame(lm.coef_, X.columns, columns=['Coeff'])
def logp(self, data): """ Retrieve thetas """ theta4 = self.theta4 theta5 = self.theta5 theta6 = self.theta6 theta7 = self.theta7 theta8 = self.theta8 theta9 = self.theta9 p = self.p k1 = self.k1 """ Parse data """ f_lengths = data[0] g_starts = data[1] rates = data[2] p_lengths = data[3] g_ends = data[4] """ Transition kernel """ eps = 0.01 Q = eps + (1. - 2. * eps) / (1. + tt.exp(-0.1 * theta4 * (g_ends - 20. * theta5))) ll_Q = tt.log(Q) ll_notQ = tt.log(1 - tt.exp(ll_Q)) """ Short pause ll """ #ll_S = bound(tt.log(theta6) - theta6 * p_lengths, p_lengths > 0, theta6 > 0) #p = 0.8 ll_bout = p + bound( tt.log(theta6) - theta6 * p_lengths, p_lengths > 0, theta6 > 0) ll_drink = (1. - p) + bound( tt.log(theta9) - theta9 * p_lengths, p_lengths > 0, theta9 > 0) ll_S = ll_drink + tt.log1p(tt.exp(ll_bout - ll_drink)) """ Long pause ll """ x0 = g_ends t = p_lengths ## Full emptying time t_cs = 2. * tt.sqrt(x0) / k1 ## Stomach fullness x = 0.25 * tt.sqr(k1 * t) - k1 * t * tt.sqrt(x0) + x0 ## ll if time is less than full emptying a_part1 = k1 * t * theta8 * tt.pow( theta7, 2) * (0.5 * theta7 + theta8 * (0.25 * k1 * t * tt.sqrt(x0) - 0.5 * x0)) a_part2_1 = theta7 * (tt.pow(theta7, 2) + theta7 * theta8 * (x + x0) + x0 * tt.pow(theta8, 2) * x) a_part2_2 = tt.sqrt( theta7 * theta8) * (tt.arctan( (0.5 * k1 * t - tt.sqrt(x0)) * tt.sqrt(theta8 / theta7)) + tt.arctan(tt.sqrt(theta8 * x0 / theta7))) a_denom = k1 * tt.pow(theta7, 3) * theta8 * (theta7 + x0 * theta8) * ( theta7 + theta8 * x) a_psi = (a_part1 + a_part2_1 * a_part2_2) / a_denom a_phi = 1. / (tt.sqr(theta7 + theta8 * x)) ll_L_1 = tt.log(a_phi) - a_psi ## ll if time exceeds full emptying b_phi = 1. / tt.sqr(theta7) b_part1 = k1 * t_cs * theta8 * tt.pow( theta7, 2) * (0.5 * theta7 + theta8 * (0.25 * k1 * t_cs * tt.sqrt(x0) - 0.5 * x0)) b_part2_1 = theta7 * (tt.pow(theta7, 2) + theta7 * theta8 * (x0)) b_part2_2 = tt.sqrt( theta7 * theta8) * (tt.arctan( (0.5 * k1 * t_cs - tt.sqrt(x0)) * tt.sqrt(theta8 / theta7)) + tt.arctan(tt.sqrt(theta8 * x0 / theta7))) b_denom = k1 * tt.pow(theta7, 3) * theta8 * (theta7 + x0 * theta8) * (theta7) b_psi = (b_part1 + b_part2_1 * b_part2_2) / b_denom b_psi = b_psi + (t - t_cs) / tt.sqr(theta7) ll_L_2 = tt.log(b_phi) - b_psi ## Switch based on t_c ll_L = tt.switch(tt.lt(t, t_cs), ll_L_1, ll_L_2) ## Avoid numerical issues in logaddexp ll_short = ll_notQ + ll_S ll_long = ll_Q + ll_L #ll_pause = ll_short + tt.log1p(tt.exp(ll_long - ll_short)) ll_pause = ll_long + tt.log1p(tt.exp(ll_short - ll_long)) return ll_pause
def sqr_magnitudes(v): "same as sqr_magnitude for 1D vectors, but returns array of magnitudes for arrays of vectors" return sqr(v).sum(-1)
def f1(x): return (2 * np.sqr(x)) + (5 * x) - 2