def orthoproject(a,b) : # proyeksi orthogonal thd a (j,k) = b.shape b_ = np.empty( [j,k], ) for i in range( 0,j ) : x = float( dot( b[i],a ))/dot( a,a ) b_[i] = b[i] - x*a return b_
def lifting(a,b) : # proyeksi thd v in L' x = float( dot(a, b) )/dot(a, a) while True : b = b - a y = float( dot(a, b) )/dot(a, a) if abs(x) < abs(y) : break x = y # undo subtraction of b return b + a
def forward(self, ngrami): x = self.C[ngrami[0, :], :][0] x = x.reshape((x.shape[0] * x.shape[1], 1)) o = np.dot(self.H, x) + self.D a = self.sigmoid(o) e = np.dot(self.U, a) + self.B z = np.dot(self.W, x) y = np.add(e, z) return y, a, x
def train(self, w, z, eta=0.005, epoca=1000, error=0.01): count = 0 training_samples_pos = sample(range(w.shape[0]), min(w.shape[0], epoca)) while count < epoca: for i in training_samples_pos: traini = w[i, :] #print(traini) y, a, x = self.forward(traini) self.probs = self.softmax(y) dl_da = zeros(self.hiddenLayerSize) dl_dx = zeros((self.grams-1)*self.m) for j in range(0, self.probs.shape[0]): dl_dyj = self.cost(z[i], self.probs, j) self.B[j, 0] += eta * dl_dyj dl_dx += dl_dyj * self.W[j, :] self.W[j, :] = self.W[j, :] + eta * (dl_dyj * x[:, 0]) dl_da += dl_dyj * self.U[j, :] self.U[j, :] = self.U[j, :] + eta * dl_dyj * a.T dl_do = zeros((self.hiddenLayerSize, 1)) da_do = self.sigmoidPrime(a) for k in range(0, self.hiddenLayerSize): dl_do[k] = da_do[k, 0] * dl_da[k] dl_dx += np.dot(self.H.T, dl_do)[:, 0] self.D += eta * dl_do self.H += eta * np.dot(dl_do, x.T) #updating word features for the input words traini for k in range(0, self.grams-1): startk, endk = k * self.m, (k+1) * self.m wordk_id = traini[0, k] self.C[wordk_id, :] = self.C[wordk_id, :] + eta * dl_dx[startk:endk] count += 1
def distmat(X, Y): n = len(X) m = len(Y) xx = ml.sum(X*X, axis=1) yy = ml.sum(Y*Y, axis=1) xy = ml.dot(X, Y.T) return npy.tile(xx, (m, 1)).T+npy.tile(yy, (n, 1)) - 2*xy
def zl_distvec(X, Y): n = len(X) m = len(Y) xx = ml.sum(X * X, axis=1) yy = ml.sum(Y * Y, axis=1) xy = ml.dot(X, Y.T) return tile(xx, (m, 1)).T + tile(yy, (n, 1)) - 2 * xy
def distance(X, Y): n = len(X) m = len(Y) xx = matlib.sum(X*X, axis=1) yy = matlib.sum(Y*Y, axis=1) xy = matlib.dot(X, Y.T) return tile(xx, (m, 1)).T+tile(yy, (n, 1)) - 2*xy
def zl_distvec(X, Y): n = len(X) m = len(Y) xx = ml.sum(X*X, axis=1) yy = ml.sum(Y*Y, axis=1) xy = ml.dot(X, Y.T) return tile(xx, (m, 1)).T+tile(yy, (n, 1)) - 2*xy
def distMat(X, Y): n = len(X) m = len(Y) xx = ml.sum(X * X, axis=1) yy = ml.sum(Y * Y, axis=1) xy = ml.dot(X, Y.T) return np.tile(xx, (m, 1)).T + np.tile(yy, (n, 1)) - 2 * xy
def forward_kinematics_for_inverse(self, effector_name, thetas): # calculate forward_kinematics for just one chain T = identity(4) for i, joint in enumerate(self.chains[effector_name]): angle = thetas[i] Tl = self.local_trans(joint, angle) T = dot(T, Tl) return T
def Intersec(s,o,v): cent=np.array(s[0:3]) rad=s[3] vt=o-cent a=np.dot(v,v) b=2*np.dot(v,vt) c=np.dot(vt,vt)-rad*rad sol,t1,t2=SolveTri(a,b,c) if sol==2: if t1<t2: t=t1 else: t=t2 elif sol==1: t=t1 else: t=HUGE_VAL return t
def Intersec(s, o, v): cent = np.array(s[0:3]) rad = s[3] vt = o - cent a = np.dot(v, v) b = 2 * np.dot(v, vt) c = np.dot(vt, vt) - rad * rad sol, t1, t2 = SolveTri(a, b, c) if sol == 2: if t1 < t2: t = t1 else: t = t2 elif sol == 1: t = t1 else: t = HUGE_VAL return t
def distmat(X,Y): n = len(X) m = len(Y) xx = ml.sum(X*X,axis=1) # #axis=1 是按行求和 print "xx:{}".format(xx) yy = ml.sum(Y*Y,axis=1) print "yy:{}".format(yy) xy = ml.dot(X,Y.T) #dot矩阵相乘 return tile(xx,(m,1)).T + tile(yy,(n,1)) - 2*xy #tile矩阵复制
def local_trans(self, joint_name, joint_angle): '''calculate local transformation of one joint :param str joint_name: the name of joint :param float joint_angle: the angle of joint in radians :return: transformation :rtype: 4x4 matrix ''' T = identity(4) # YOUR CODE HERE #print("Current joint: ", joint_name) j_cos = cos(joint_angle) j_sin = sin(joint_angle) # standard transformations (rotations only) # rotation around x-axis Rx = [[1, 0, 0, 0], [0, j_cos, -j_sin, 0], [0, j_sin, j_cos, 0], [0, 0, 0, 1]] # rotation around y-axis Ry = [[j_cos, 0, j_sin, 0], [0, 1, 0, 0], [-j_sin, 0, j_cos, 0], [0, 0, 0, 1]] # rotation around z-axis Rz = [[j_cos, j_sin, 0, 0], [-j_sin, j_cos, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] if joint_name in self.pitches: T = Ry elif joint_name in self.yaws: T = Rz elif joint_name in self.rolls: T = Rx elif joint_name in self.yaw_pitches: # combine yaw and pitch T = dot(Rz, Ry) else: print('ERROR: Unknown joint name "' + joint_name + '"') # T stays identity # add the offset in last collumn T[0][3] = self.offsets[joint_name][0] T[1][3] = self.offsets[joint_name][1] T[2][3] = self.offsets[joint_name][2] #print(np.round(T,2)) return T
def forward_kinematics(self, joints): '''forward kinematics :param joints: {joint_name: joint_angle} ''' for chain_joints in self.chains.values(): T = identity(4) for joint in chain_joints: angle = joints[joint] Tl = self.local_trans(joint, angle) # YOUR CODE HERE T = dot(T, Tl) self.transforms[joint] = T
def forward_kinematics(self, joints): '''forward kinematics :param joints: {joint_name: joint_angle} ''' for chain_joints in self.chains.values(): T = identity(4) for joint in chain_joints: angle = joints[joint] Tl = self.local_trans(joint, angle) # YOUR CODE HERE # "chain" all transformations by dot-multiplying them T = dot(T, Tl) self.transforms[joint] = T.copy()
def calc_phi(xys, ref_half_plane, view, cameraposor, laserpos, lasertheta): """Given an array of pixel pairs xys from a camera with view and cameraposor and laser with laserpos and lasertheta, calculate the laser inclination based on a known half-plane ref_half_plane. Throws a NoReferenceException if no pixels are in the reference half-plane.""" cref_pos = ddd.unrotate(ref_half_plane.pos - cameraposor.pos, cameraposor) cref_side = ddd.unrotate(ref_half_plane.side, cameraposor) cref_line = np.cross(cref_side, ddd.unrotate(ref_half_plane.normal, cameraposor), axis = 0) # TODO less copy-pasta cpos = np.array([cref_pos[1, 0], -cref_pos[2, 0]]) / cref_pos[0, 0] * ddd.view_number(view) \ + np.array([view.centerx, view.centery]) cline_ = cref_pos / cref_pos[0, 0] - cref_line / cref_line[0, 0] cside_ = np.array([cref_side[1, 0], -cref_side[2, 0]]) cside = np.array([cline_[2, 0], cline_[1, 0]]) if np.dot(cside, cside_) < 0: cside = - cside dxys = xys - cpos dot_products = np.array(np.mat([cside]) * np.mat(dxys).T)[0] good_xys = xys[dot_products >= 0] print("say "+str(np.average(good_xys[:,1]))) if len(good_xys) == 0: raise NoReferenceException() threepoints = ddd.threedize_plane(good_xys, view, cameraposor, ref_half_plane) return calc_phi_points(threepoints, laserpos, lasertheta)
def local_trans(self, joint_name, joint_angle): '''calculate local transformation of one joint :param str joint_name: the name of joint :param float joint_angle: the angle of joint in radians :return: transformation :rtype: 4x4 matrix ''' T = identity(4) # YOUR CODE HERE j_cos = cos(joint_angle) j_sin = sin(joint_angle) Mx = [[ 1, 0, 0, 0], [ 0, j_cos, -j_sin, 0], [ 0, j_sin, j_cos, 0], [ 0, 0, 0, 1]] My = [[ j_cos, 0, j_sin, 0], [ 0, 1, 0, 0], [ -j_sin, 0, j_cos, 0], [ 0, 0, 0, 1]] Mz = [[ j_cos, j_sin, 0, 0], [ -j_sin, j_cos, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1]] """ Pitch / Yaw / Roll""" if joint_name in ['HeadPitch', 'LShoulderPitch', 'LHipPitch', 'LKneePitch', 'LAnklePitch', 'RShoulderPitch', 'RHipPitch', 'RKneePitch', 'RAnklePitch']: T = My elif joint_name in ['HeadYaw', 'LElbowYaw', 'LWristYaw', 'RElbowYaw', 'RWristYaw']: T = Mz elif joint_name in ['LShoulderRoll', 'LElbowRoll', 'LHipRoll', 'LAnkleRoll', 'RShoulderRoll', 'RElbowRoll', 'RHipRoll', 'RAnkleRoll']: T = Mx elif joint_name in ['LHipYawPitch', 'RHipYawPitch']: T = dot(Mz, My) """joint length offset""" offsets = {'HeadYaw' : [0.00, 0.00, 126.50], 'HeadPitch' : [0.00, 0.00, 0.00], 'LShoulderPitch' : [0.00, 98.00, 100.00], 'LShoulderRoll' : [0.00, 0.00, 0.00], 'LElbowYaw' : [105.00, 15.00, 0.00], 'LElbowRoll' : [0.00, 0.00, 0.00], 'LWristYaw' : [55.95, 0.00, 0.00], 'RShoulderPitch': [0.00, -98.00, 100.00], 'RShoulderRoll': [0.00, 0.00, 0.00], 'RElbowYaw': [105.00, -15.00, 0.00], 'RElbowRoll': [0.00, 0.00, 0.00], 'RWristYaw': [55.95, 0.00, 0.00], 'LHipYawPitch' : [0.00, 50.00, -85.00], 'LHipRoll' : [0.00, 0.00, 0.00], 'LHipPitch' : [0.00, 0.00, 0.00], 'LKneePitch' : [0.00, 0.00, -100.00], 'LAnklePitch' : [0.00, 0.00, -102.90], 'LAnkleRoll' : [0.00, 0.00, 0.00], 'RHipYawPitch': [0.00, -50.00, -85.00], 'RHipRoll': [0.00, 0.00, 0.00], 'RHipPitch': [0.00, 0.00, 0.00], 'RKneePitch': [0.00, 0.00, -100.00], 'RAnklePitch': [0.00, 0.00, -102.90], 'RAnkleRoll': [0.00, 0.00, 0.00]} T[0][3] = offsets[joint_name][0] T[1][3] = offsets[joint_name][1] T[2][3] = offsets[joint_name][2] return T
def SPIRIT(A, lamb, energy, k0=1, holdOffTime=0, reorthog=False, evalMetrics="F"): A = np.mat(A) n = A.shape[1] totalTime = A.shape[0] Proj = npm.ones((totalTime, n)) * np.nan recon = npm.zeros((totalTime, n)) # initialize w_i to unit vectors W = npm.eye(n) d = 0.01 * npm.ones((n, 1)) m = k0 # number of eigencomponents relErrors = npm.zeros((totalTime, 1)) sumYSq = 0.0 E_t = [] sumXSq = 0.0 E_dash_t = [] res = {} k_hist = [] W_hist = [] anomalies = [] # incremental update W lastChangeAt = 0 for t in range(totalTime): k_hist.append(m) # update W for each y_t x = A[t, :].T # new data as column vector for j in range(m): W[:, j], d[j], x = updateW(x, W[:, j], d[j], lamb) Wj = W[:, j] # Grams smit reorthog if reorthog == True: W[:, :m], R = npm.linalg.qr(W[:, :m]) # compute low-D projection, reconstruction and relative error Y = W[:, :m].T * A[t, :].T # project to m-dimensional space xActual = A[t, :].T # actual vector of the current time xProj = W[:, :m] * Y # reconstruction of the current time Proj[t, :m] = Y.T recon[t, :] = xProj.T xOrth = xActual - xProj relErrors[t] = npm.sum(npm.power(xOrth, 2)) / npm.sum(npm.power(xActual, 2)) # update energy sumYSq = lamb * sumYSq + npm.sum(npm.power(Y, 2)) E_dash_t.append(sumYSq) sumXSq = lamb * sumXSq + npm.sum(npm.power(A[t, :], 2)) E_t.append(sumXSq) # Record RSRE if t == 0: top = 0.0 bot = 0.0 top = top + npm.power(npm.linalg.norm(xActual - xProj), 2) bot = bot + npm.power(npm.linalg.norm(xActual), 2) new_RSRE = top / bot if t == 0: RSRE = new_RSRE else: RSRE = npm.vstack((RSRE, new_RSRE)) ### Metric EVALUATION ### # deviation from truth if evalMetrics == "T": Qt = W[:, :m] if t == 0: res["subspace_error"] = npm.zeros((totalTime, 1)) res["orthog_error"] = npm.zeros((totalTime, 1)) res["angle_error"] = npm.zeros((totalTime, 1)) Cov_mat = npm.zeros([n, n]) # Calculate Covarentce Matrix of data up to time t Cov_mat = lamb * Cov_mat + npm.dot(xActual, xActual.T) # Get eigenvalues and eigenvectors WW, V = npm.linalg.eig(Cov_mat) # Use this to sort eigenVectors in according to deccending eigenvalue eig_idx = WW.argsort() # Get sort index eig_idx = eig_idx[::-1] # Reverse order (default is accending) # v_r = highest r eigen vectors (accoring to thier eigenvalue if sorted). V_k = V[:, eig_idx[:m]] # Calculate subspace error C = npm.dot(V_k, V_k.T) - npm.dot(Qt, Qt.T) res["subspace_error"][t, 0] = 10 * np.log10(npm.trace(npm.dot(C.T, C))) # frobenius norm in dB # Calculate angle between projection matrixes D = npm.dot(npm.dot(npm.dot(V_k.T, Qt), Qt.T), V_k) eigVal, eigVec = npm.linalg.eig(D) angle = npm.arccos(np.sqrt(max(eigVal))) res["angle_error"][t, 0] = angle # Calculate deviation from orthonormality F = npm.dot(Qt.T, Qt) - npm.eye(m) res["orthog_error"][t, 0] = 10 * np.log10(npm.trace(npm.dot(F.T, F))) # frobenius norm in dB # Energy thresholding ###################### # check the lower bound of energy level if sumYSq < energy[0] * sumXSq and lastChangeAt < t - holdOffTime and m < n: lastChangeAt = t m = m + 1 anomalies.append(t) # print 'Increasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq) # check the upper bound of energy level elif sumYSq > energy[1] * sumXSq and lastChangeAt < t - holdOffTime and m < n and m > 1: lastChangeAt = t m = m - 1 # print 'Decreasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq) W_hist.append(W[:, :m]) # set outputs # Grams smit reorthog if reorthog == True: W[:, :m], R = npm.linalg.qr(W[:, :m]) # Data Stores res2 = { "hidden": Proj, # Array for hidden Variables "E_t": np.array(E_t), # total energy of data "E_dash_t": np.array(E_dash_t), # hidden var energy "e_ratio": np.array(E_dash_t) / np.array(E_t), # Energy ratio "rel_orth_err": relErrors, # orthoX error "RSRE": RSRE, # Relative squared Reconstruction error "recon": recon, # reconstructed data "r_hist": k_hist, # history of r values "W_hist": W_hist, # history of Weights "anomalies": anomalies, } res.update(res2) return res
t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() k = [2.2e7, 3.124e7] # rate constant a_0 = 1e-3 # initial concentration of A a_1 = 2e-3 # initial concentration of C c = matlib.empty([t_1.size, 2]) c[:,0] = a_0 * matlib.exp(-k[0] * t_1) c[:,1] = a_1 * matlib.exp(-k[1] * t_1) # molar absorption of species A a = matlib.empty([2, 1]) a[0,0] = 1e3 a[1,0] = 1e3 y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0] fullLightVoltage = -0.0951192897786 y_1 = map(lambda y:fullLightVoltage*(10**-y), y_1) y_0 = [] for i in range(0, len(t_0)): y_0.append(fullLightVoltage + 0.01 * random.random() - 0.005) y = y_0 + y_1 t = t_0 + t_1
def gnmf_vb_poisson_mult_fast(x, a_tm, b_tm, a_ve, b_ve, EPOCH=1000, Method='vb', Update=np.inf, tie_a_ve='clamp', tie_b_ve='clamp', tie_a_tm='clamp', tie_b_tm='clamp', print_period=500): # Result initialiation g = dict() g['E_T'] = None g['E_logT'] = None g['E_V'] = None g['E_logV'] = None g['Bound'] = None g['a_ve'] = None g['b_ve'] = None g['a_tm'] = None g['b_tm'] = None logm = np.vectorize(math.log) W = x.shape[0] K = x.shape[1] I = b_tm.shape[1] M = ~np.isnan(x) X = np.zeros(x.shape) X[M] = x[M] t_init = np.random.gamma(a_tm, b_tm / a_tm) v_init = np.random.gamma(a_ve, b_ve / a_ve) L_t = t_init L_v = v_init E_t = t_init E_v = v_init Sig_t = t_init Sig_v = v_init B = np.zeros([1, EPOCH]) gammalnX = special.gammaln(X + 1) for e in range(1, EPOCH + 1): LtLv = L_t.dot(L_v) tmp = X / (LtLv) #check Tranpose Sig_t = L_t * (tmp.dot(L_v.T)) Sig_v = L_v * (L_t.T.dot(tmp)) alpha_tm = a_tm + Sig_t beta_tm = 1 / ((a_tm / b_tm) + M.dot(E_v.T)) E_t = alpha_tm * (beta_tm) alpha_ve = a_ve + Sig_v beta_ve = 1 / ((a_ve / b_ve) + E_t.T.dot(M)) E_v = alpha_ve * (beta_ve) # Compute the bound if (e % 10 == 1): print("*", end='') if (e % print_period == 1 or e == EPOCH): g['E_T'] = E_t g['E_logT'] = logm(L_t) g['E_V'] = E_v g['E_logV'] = logm(L_v) g['Bound'] = -np.sum(np.sum(M * (g['E_T'].dot(g['E_V'])) + gammalnX))\ + np.sum(np.sum(-X * ( ((L_t * g['E_logT']).dot(L_v) + L_t.dot(L_v * g['E_logV']))/(LtLv) - logm(LtLv) ) ))\ + np.sum(np.sum((-a_tm/b_tm)* g['E_T'] - special.gammaln(a_tm) + a_tm * logm(a_tm /b_tm)))\ + np.sum(np.sum((-a_ve/b_ve)* g['E_V'] - special.gammaln(a_ve) + a_ve * logm(a_ve /b_ve)))\ + np.sum(np.sum( special.gammaln(alpha_tm) + alpha_tm * logm(beta_tm) + 1))\ + np.sum(np.sum(special.gammaln(alpha_ve) + alpha_ve * logm(beta_ve) + 1 )) g['a_ve'] = a_ve g['b_ve'] = b_ve g['a_tm'] = a_tm g['b_tm'] = b_tm print( '\nBound = %f\t a_ve = %f \t b_ve = %f \t a_tm = %f \t b_tm = %f\n', g['Bound'], a_ve.flatten()[0], b_ve.flatten()[0], a_tm.flatten()[0], b_tm.flatten()[0]) if (e == EPOCH): break L_t = np.exp(special.psi(alpha_tm)) * beta_tm L_v = np.exp(special.psi(alpha_ve)) * beta_ve Z = None if (e > Update): if (not tie_a_tm == 'clamp'): Z = (E_t / b_tm) - (logm(L_t) - logm(b_tm)) if (tie_a_tm == 'clamp'): a_tm = gnmf_solvebynewton(Z, a_tm) elif (tie_a_tm == 'rows'): a_tm = gnmf_solvebynewton(np.sum(Z, 0) / W, a_tm) elif (tie_a_tm == 'cols'): a_tm = gnmf_solvebynewton(np.sum(Z, 1) / I, a_tm) elif (tie_a_tm == 'tie_all'): a_tm = gnmf_solvebynewton(np.sum(Z) / (W * I), a_tm) if (tie_b_tm == 'free'): b_tm = E_t elif (tie_b_tm == 'rows'): b_tm = M.repmat(np.sum(a_tm * E_t, 0) / np.sum(a_tm, 0), W, 1) elif (tie_b_tm == 'cols'): b_tm = M.repmat(np.sum(a_tm * E_t, 1) / np.sum(a_tm, 1), 1, I) elif (tie_b_tm == 'tie_all'): b_tm = (np.sum(a_tm * E_t) / np.sum(a_tm)) * np.ones([W, I]) if (not tie_a_ve == 'clamp'): Z = (E_v / b_ve) - (logm(L_v) - logm(b_ve)) if (tie_a_ve == 'clamp'): a_ve = gnmf_solvebynewton(Z, a_ve) elif (tie_a_ve == 'rows'): a_ve = gnmf_solvebynewton(np.sum(Z, 0) / I, a_ve) elif (tie_a_ve == 'cols'): a_ve = gnmf_solvebynewton(np.sum(Z, 1) / K, a_ve) elif (tie_a_ve == 'tie_all'): a_ve = gnmf_solvebynewton(np.sum(Z) / (I * K), a_ve) if (tie_b_ve == 'free'): b_ve = E_v elif (tie_b_ve == 'rows'): b_ve = M.repmat(np.sum(a_ve * E_v, 0) / np.sum(a_ve, 0), I, 1) elif (tie_b_tm == 'cols'): b_ve = M.repmat(np.sum(a_ve * E_v, 1) / np.sum(a_ve, 1), 1, K) elif (tie_b_tm == 'tie_all'): b_ve = (np.sum(a_ve * E_v) / np.sum(a_ve)) * np.ones([I, K]) return g
def SPIRIT(streams, energyThresh, lamb, evalMetrics): # Make if type(streams) == np.ndarray: streams_iter = iter(streams) # Max No. Streams if streams.ndim == 1: streams = np.expand_dims(streams, axis=1) num_streams = streams.shape[1] else: num_streams = streams.shape[1] count_over = 0 count_under = 0 #=============================================================================== # Initalise k, w and d, lamb #=============================================================================== k = 1 # Hidden Variables, initialise to one # Weights pc_weights = npm.zeros(num_streams) pc_weights[0, 0] = 1 # initialise outputs res = {} all_weights = [] k_hist = [] anomalies = [] x_dash = npm.zeros((1, num_streams)) Eng = mat([0.00000001, 0.00000001]) E_xt = 0 # Energy of X at time t E_rec_i = mat([0.000000000000001]) # Energy of reconstruction Y = npm.zeros(num_streams) timeSteps = streams.shape[0] #=============================================================================== # Main Loop #=============================================================================== for t in range(1, timeSteps + 1): # t = 1,...,200 k_hist.append(k) x_t_plus_1 = mat(streams_iter.next()) # Read in next signals d_i = E_rec_i * t # Step 1 - Update Weights pc_weights, y_t_i, error = track_W(x_t_plus_1, k, pc_weights, d_i, num_streams, lamb) # Record hidden variables padding = num_streams - k y_bar_t = npm.hstack((y_t_i, mat([nan] * padding))) Y = npm.vstack((Y, y_bar_t)) # Record Weights all_weights.append(pc_weights) # Record reconstrunted z and RSRE x_dash = npm.vstack((x_dash, y_t_i * pc_weights)) # Record RSRE if t == 1: top = 0.0 bot = 0.0 top = top + (norm(x_t_plus_1 - x_dash)**2) bot = bot + (norm(x_t_plus_1)**2) new_RSRE = top / bot if t == 1: RSRE = new_RSRE else: RSRE = npm.vstack((RSRE, new_RSRE)) ### FOR EVALUATION ### #deviation from truth if evalMetrics == 'T': Qt = pc_weights.T if t == 1: res['subspace_error'] = npm.zeros((timeSteps, 1)) res['orthog_error'] = npm.zeros((timeSteps, 1)) res['angle_error'] = npm.zeros((timeSteps, 1)) Cov_mat = npm.zeros([num_streams, num_streams]) # Calculate Covarentce Matrix of data up to time t Cov_mat = lamb * Cov_mat + npm.dot(x_t_plus_1, x_t_plus_1.T) # Get eigenvalues and eigenvectors W, V = eig(Cov_mat) # Use this to sort eigenVectors in according to deccending eigenvalue eig_idx = W.argsort() # Get sort index eig_idx = eig_idx[::-1] # Reverse order (default is accending) # v_r = highest r eigen vectors (accoring to thier eigenvalue if sorted). V_k = V[:, eig_idx[:k]] # Calculate subspace error C = npm.dot(V_k, V_k.T) - npm.dot(Qt, Qt.T) res['subspace_error'][t - 1, 0] = 10 * np.log10( npm.trace(npm.dot(C.T, C))) #frobenius norm in dB # Calculate angle between projection matrixes D = npm.dot(npm.dot(npm.dot(V_k.T, Qt), Qt.T), V_k) eigVal, eigVec = eig(D) angle = npm.arccos(np.sqrt(max(eigVal))) res['angle_error'][t - 1, 0] = angle # Calculate deviation from orthonormality F = npm.dot(Qt.T, Qt) - npm.eye(k) res['orthog_error'][t - 1, 0] = 10 * np.log10( npm.trace(npm.dot(F.T, F))) #frobenius norm in dB # Step 2 - Update Energy estimate E_xt = ((lamb * (t - 1) * E_xt) + norm(x_t_plus_1)**2) / t for i in range(k): E_rec_i[0, i] = ((lamb * (t - 1) * E_rec_i[0, i]) + (y_t_i[0, i]**2)) / t # Step 3 - Estimate the retained energy E_retained = npm.sum(E_rec_i, 1) # Record Energy Eng_new = npm.hstack((E_xt, E_retained[0, 0])) Eng = npm.vstack((Eng, Eng_new)) if E_retained < energyThresh[0] * E_xt: if k != num_streams: k = k + 1 # Initalise Ek+1 <-- 0 E_rec_i = npm.hstack((E_rec_i, mat([0]))) # Initialise W_i+1 new_weight_vec = npm.zeros(num_streams) new_weight_vec[0, k - 1] = 1 pc_weights = npm.vstack((pc_weights, new_weight_vec)) anomalies.append(t - 1) else: count_over += 1 elif E_retained > energyThresh[1] * E_xt: if k > 1: k = k - 1 # discard w_k and error pc_weights = delete(pc_weights, -1, 0) # Discard E_rec_i[k] E_rec_i = delete(E_rec_i, -1) else: count_under += 1 # Data Stores res2 = { 'hidden': Y, # Array for hidden Variables 'weights': all_weights, 'E_t': Eng[:, 0], # total energy of data 'E_dash_t': Eng[:, 1], # hidden var energy 'e_ratio': np.divide(Eng[:, 1], Eng[:, 0]), # Energy ratio 'RSRE': RSRE, # Relative squared Reconstruction error 'recon': x_dash, # reconstructed data 'r_hist': k_hist, # history of r values 'anomalies': anomalies } res.update(res2) return res, all_weights
def local_trans(self, joint_name, joint_angle): '''calculate local transformation of one joint :param str joint_name: the name of joint :param float joint_angle: the angle of joint in radians :return: transformation :rtype: 4x4 matrix ''' T = identity(4) # YOUR CODE HERE ja_sin = sin(joint_angle) ja_cos = cos(joint_angle) mRange = { 'HeadYaw': [0.00, 0.00, 126.50], 'HeadPitch': [0.00, 0.00, 0.00], 'LShoulderPitch': [0.00, 98.00, 100.00], 'LShoulderRoll': [0.00, 0.00, 0.00], 'LElbowYaw': [105.00, 15.00, 0.00], 'LElbowRoll': [0.00, 0.00, 0.00], 'LWristYaw': [55.95, 0.00, 0.00], 'RShoulderPitch': [0.00, -98.00, 100.00], 'RShoulderRoll': [0.00, 0.00, 0.00], 'RElbowYaw': [105.00, -15.00, 0.00], 'RElbowRoll': [0.00, 0.00, 0.00], 'RWristYaw': [55.95, 0.00, 0.00], 'LHipYawPitch': [0.00, 50.00, -85.00], 'LHipRoll': [0.00, 0.00, 0.00], 'LHipPitch': [0.00, 0.00, 0.00], 'LKneePitch': [0.00, 0.00, -100.00], 'LAnklePitch': [0.00, 0.00, -102.90], 'LAnkleRoll': [0.00, 0.00, 0.00], 'RHipYawPitch': [0.00, -50.00, -85.00], 'RHipRoll': [0.00, 0.00, 0.00], 'RHipPitch': [0.00, 0.00, 0.00], 'RKneePitch': [0.00, 0.00, -100.00], 'RAnklePitch': [0.00, 0.00, -102.90], 'RAnkleRoll': [0.00, 0.00, 0.00] } mX = [[1, 0, 0, 0], [0, ja_cos, -ja_sin, 0], [0, ja_sin, ja_cos, 0], [0, 0, 0, 1]] mY = [[ja_cos, 0, ja_sin, 0], [0, 1, 0, 0], [-ja_sin, 0, ja_cos, 0], [0, 0, 0, 1]] mZ = [[ja_cos, ja_sin, 0, 0], [-ja_sin, ja_cos, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] if joint_name in [ 'LShoulderRoll', 'LElbowRoll', 'LHipRoll', 'LAnkleRoll', 'RShoulderRoll', 'RElbowRoll', 'RHipRoll', 'RAnkleRoll' ]: T = mX elif joint_name in [ 'HeadPitch', 'LShoulderPitch', 'LHipPitch', 'LKneePitch', 'LAnklePitch', 'RShoulderPitch', 'RHipPitch', 'RKneePitch', 'RAnklePitch' ]: T = mY elif joint_name in [ 'HeadYaw', 'LElbowYaw', 'LWristYaw', 'RElbowYaw', 'RWristYaw' ]: T = mZ elif joint_name in ['LHipYawPitch', 'RHipYawPitch']: T = dot(mZ, mY) T[0][3] = mRange[joint_name][0] T[1][3] = mRange[joint_name][1] T[2][3] = mRange[joint_name][2] return T
def SPIRIT(A, lamb, energy, k0=1, holdOffTime=0, reorthog=False, evalMetrics='F'): A = np.mat(A) n = A.shape[1] totalTime = A.shape[0] Proj = npm.ones((totalTime, n)) * np.nan recon = npm.zeros((totalTime, n)) # initialize w_i to unit vectors W = npm.eye(n) d = 0.01 * npm.ones((n, 1)) m = k0 # number of eigencomponents relErrors = npm.zeros((totalTime, 1)) sumYSq = 0. E_t = [] sumXSq = 0. E_dash_t = [] res = {} k_hist = [] W_hist = [] anomalies = [] # incremental update W lastChangeAt = 0 for t in range(totalTime): k_hist.append(m) # update W for each y_t x = A[t, :].T # new data as column vector for j in range(m): W[:, j], d[j], x = updateW(x, W[:, j], d[j], lamb) Wj = W[:, j] # Grams smit reorthog if reorthog == True: W[:, :m], R = npm.linalg.qr(W[:, :m]) # compute low-D projection, reconstruction and relative error Y = W[:, :m].T * A[t, :].T # project to m-dimensional space xActual = A[t, :].T # actual vector of the current time xProj = W[:, :m] * Y # reconstruction of the current time Proj[t, :m] = Y.T recon[t, :] = xProj.T xOrth = xActual - xProj relErrors[t] = npm.sum(npm.power(xOrth, 2)) / npm.sum( npm.power(xActual, 2)) # update energy sumYSq = lamb * sumYSq + npm.sum(npm.power(Y, 2)) E_dash_t.append(sumYSq) sumXSq = lamb * sumXSq + npm.sum(npm.power(A[t, :], 2)) E_t.append(sumXSq) # Record RSRE if t == 0: top = 0.0 bot = 0.0 top = top + npm.power(npm.linalg.norm(xActual - xProj), 2) bot = bot + npm.power(npm.linalg.norm(xActual), 2) new_RSRE = top / bot if t == 0: RSRE = new_RSRE else: RSRE = npm.vstack((RSRE, new_RSRE)) ### Metric EVALUATION ### #deviation from truth if evalMetrics == 'T': Qt = W[:, :m] if t == 0: res['subspace_error'] = npm.zeros((totalTime, 1)) res['orthog_error'] = npm.zeros((totalTime, 1)) res['angle_error'] = npm.zeros((totalTime, 1)) Cov_mat = npm.zeros([n, n]) # Calculate Covarentce Matrix of data up to time t Cov_mat = lamb * Cov_mat + npm.dot(xActual, xActual.T) # Get eigenvalues and eigenvectors WW, V = npm.linalg.eig(Cov_mat) # Use this to sort eigenVectors in according to deccending eigenvalue eig_idx = WW.argsort() # Get sort index eig_idx = eig_idx[::-1] # Reverse order (default is accending) # v_r = highest r eigen vectors (accoring to thier eigenvalue if sorted). V_k = V[:, eig_idx[:m]] # Calculate subspace error C = npm.dot(V_k, V_k.T) - npm.dot(Qt, Qt.T) res['subspace_error'][t, 0] = 10 * np.log10( npm.trace(npm.dot(C.T, C))) #frobenius norm in dB # Calculate angle between projection matrixes D = npm.dot(npm.dot(npm.dot(V_k.T, Qt), Qt.T), V_k) eigVal, eigVec = npm.linalg.eig(D) angle = npm.arccos(np.sqrt(max(eigVal))) res['angle_error'][t, 0] = angle # Calculate deviation from orthonormality F = npm.dot(Qt.T, Qt) - npm.eye(m) res['orthog_error'][t, 0] = 10 * np.log10( npm.trace(npm.dot(F.T, F))) #frobenius norm in dB # Energy thresholding ###################### # check the lower bound of energy level if sumYSq < energy[ 0] * sumXSq and lastChangeAt < t - holdOffTime and m < n: lastChangeAt = t m = m + 1 anomalies.append(t) # print 'Increasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq) # check the upper bound of energy level elif sumYSq > energy[ 1] * sumXSq and lastChangeAt < t - holdOffTime and m < n and m > 1: lastChangeAt = t m = m - 1 # print 'Decreasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq) W_hist.append(W[:, :m]) # set outputs # Grams smit reorthog if reorthog == True: W[:, :m], R = npm.linalg.qr(W[:, :m]) # Data Stores res2 = { 'hidden': Proj, # Array for hidden Variables 'E_t': np.array(E_t), # total energy of data 'E_dash_t': np.array(E_dash_t), # hidden var energy 'e_ratio': np.array(E_dash_t) / np.array(E_t), # Energy ratio 'rel_orth_err': relErrors, # orthoX error 'RSRE': RSRE, # Relative squared Reconstruction error 'recon': recon, # reconstructed data 'r_hist': k_hist, # history of r values 'W_hist': W_hist, # history of Weights 'anomalies': anomalies } res.update(res2) return res
def regression_dataset(): a = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]) x = np.random.rand(100, 8) y = mlib.dot(x, a) + np.random.rand(100) / 10.0 return x, y
def local_trans(self, joint_name, joint_angle): '''calculate local transformation of one joint :param str joint_name: the name of joint :param float joint_angle: the angle of joint in radians :return: transformation :rtype: 4x4 matrix ''' T = identity(4) # YOUR CODE HERE s = sin(joint_angle) c = cos(joint_angle) motion = { 'HeadYaw' : [0.00, 0.00, 126.50], 'HeadPitch' : [0.00, 0.00, 0.00], 'LShoulderPitch' : [0.00, 98.00, 100.00], 'LShoulderRoll' : [0.00, 0.00, 0.00], 'LElbowYaw' : [105.00, 15.00, 0.00], 'LElbowRoll' : [0.00, 0.00, 0.00], 'LWristYaw' : [55.95, 0.00, 0.00], 'RShoulderPitch' : [0.00, -98.00, 100.00], 'RShoulderRoll' : [0.00, 0.00, 0.00], 'RElbowYaw' : [105.00, -15.00, 0.00], 'RElbowRoll' : [0.00, 0.00, 0.00], 'RWristYaw' : [55.95, 0.00, 0.00], 'LHipYawPitch' : [0.00, 50.00, -85.00], 'LHipRoll' : [0.00, 0.00, 0.00], 'LHipPitch' : [0.00, 0.00, 0.00], 'LKneePitch' : [0.00, 0.00, -100.00], 'LAnklePitch' : [0.00, 0.00, -102.90], 'LAnkleRoll' : [0.00, 0.00, 0.00], 'RHipYawPitch' : [0.00, -50.00, -85.00], 'RHipRoll' : [0.00, 0.00, 0.00], 'RHipPitch' : [0.00, 0.00, 0.00], 'RKneePitch' : [0.00, 0.00, -100.00], 'RAnklePitch' : [0.00, 0.00, -102.90], 'RAnkleRoll' : [0.00, 0.00, 0.00] } roll = [[1,0,0,0], [0,c,-s,0], [0,s,c,0], [0,0,0,1]] pitch = [[c,0,s,0], [0,1,0,0], [-s,0,c,0], [0,0,0,1]] yaw = [[c,s,0,0], [-s,c,0,0], [0,0,1,0], [0,0,0,1]] if joint_name in ['LShoulderRoll', 'LElbowRoll', 'LHipRoll', 'LAnkleRoll', 'RShoulderRoll', 'RElbowRoll', 'RHipRoll', 'RAnkleRoll']: T = roll elif joint_name in ['HeadPitch', 'LShoulderPitch', 'LHipPitch', 'LKneePitch', 'LAnklePitch', 'RShoulderPitch', 'RHipPitch', 'RKneePitch', 'RAnklePitch']: T = pitch elif joint_name in ['HeadYaw', 'LElbowYaw', 'LWristYaw', 'RElbowYaw', 'RWristYaw']: T = yaw elif joint_name in ['LHipYawPitch', 'RHipYawPitch']: T = dot(yaw, pitch) T[0][3] = motion[joint_name][0] T[1][3] = motion[joint_name][1] T[2][3] = motion[joint_name][2] return T
def SPIRIT(streams, energyThresh, lamb, evalMetrics): # Make if type(streams) == np.ndarray: streams_iter = iter(streams) # Max No. Streams if streams.ndim == 1: streams = np.expand_dims(streams, axis=1) num_streams = streams.shape[1] else: num_streams = streams.shape[1] count_over = 0 count_under = 0 #=============================================================================== # Initalise k, w and d, lamb #=============================================================================== k = 1 # Hidden Variables, initialise to one # Weights pc_weights = npm.zeros(num_streams) pc_weights[0, 0] = 1 # initialise outputs res = {} all_weights = [] k_hist = [] anomalies = [] x_dash = npm.zeros((1,num_streams)) Eng = mat([0.00000001, 0.00000001]) E_xt = 0 # Energy of X at time t E_rec_i = mat([0.000000000000001]) # Energy of reconstruction Y = npm.zeros(num_streams) timeSteps = streams.shape[0] #=============================================================================== # Main Loop #=============================================================================== for t in range(1, timeSteps + 1): # t = 1,...,200 k_hist.append(k) x_t_plus_1 = mat(streams_iter.next()) # Read in next signals d_i = E_rec_i * t # Step 1 - Update Weights pc_weights, y_t_i, error = track_W(x_t_plus_1, k, pc_weights, d_i, num_streams, lamb) # Record hidden variables padding = num_streams - k y_bar_t = npm.hstack((y_t_i, mat([nan] * padding))) Y = npm.vstack((Y,y_bar_t)) # Record Weights all_weights.append(pc_weights) # Record reconstrunted z and RSRE x_dash = npm.vstack((x_dash, y_t_i * pc_weights)) # Record RSRE if t == 1: top = 0.0 bot = 0.0 top = top + (norm(x_t_plus_1 - x_dash) ** 2 ) bot = bot + (norm(x_t_plus_1) ** 2) new_RSRE = top / bot if t == 1: RSRE = new_RSRE else: RSRE = npm.vstack((RSRE, new_RSRE)) ### FOR EVALUATION ### #deviation from truth if evalMetrics == 'T' : Qt = pc_weights.T if t == 1 : res['subspace_error'] = npm.zeros((timeSteps,1)) res['orthog_error'] = npm.zeros((timeSteps,1)) res['angle_error'] = npm.zeros((timeSteps,1)) Cov_mat = npm.zeros([num_streams,num_streams]) # Calculate Covarentce Matrix of data up to time t Cov_mat = lamb * Cov_mat + npm.dot(x_t_plus_1, x_t_plus_1.T) # Get eigenvalues and eigenvectors W , V = eig(Cov_mat) # Use this to sort eigenVectors in according to deccending eigenvalue eig_idx = W.argsort() # Get sort index eig_idx = eig_idx[::-1] # Reverse order (default is accending) # v_r = highest r eigen vectors (accoring to thier eigenvalue if sorted). V_k = V[:, eig_idx[:k]] # Calculate subspace error C = npm.dot(V_k , V_k.T) - npm.dot(Qt , Qt.T) res['subspace_error'][t-1,0] = 10 * np.log10(npm.trace(npm.dot(C.T , C))) #frobenius norm in dB # Calculate angle between projection matrixes D = npm.dot(npm.dot(npm.dot(V_k.T, Qt), Qt.T), V_k) eigVal, eigVec = eig(D) angle = npm.arccos(np.sqrt(max(eigVal))) res['angle_error'][t-1,0] = angle # Calculate deviation from orthonormality F = npm.dot(Qt.T , Qt) - npm.eye(k) res['orthog_error'][t-1,0] = 10 * np.log10(npm.trace(npm.dot(F.T , F))) #frobenius norm in dB # Step 2 - Update Energy estimate E_xt = ((lamb * (t-1) * E_xt) + norm(x_t_plus_1) ** 2) / t for i in range(k): E_rec_i[0, i] = ((lamb * (t-1) * E_rec_i[0, i]) + (y_t_i[0, i] ** 2)) / t # Step 3 - Estimate the retained energy E_retained = npm.sum(E_rec_i,1) # Record Energy Eng_new = npm.hstack((E_xt, E_retained[0,0])) Eng = npm.vstack((Eng, Eng_new)) if E_retained < energyThresh[0] * E_xt: if k != num_streams: k = k + 1 # Initalise Ek+1 <-- 0 E_rec_i = npm.hstack((E_rec_i, mat([0]))) # Initialise W_i+1 new_weight_vec = npm.zeros(num_streams) new_weight_vec[0, k-1] = 1 pc_weights = npm.vstack((pc_weights, new_weight_vec)) anomalies.append(t -1) else: count_over += 1 elif E_retained > energyThresh[1] * E_xt: if k > 1 : k = k - 1 # discard w_k and error pc_weights = delete(pc_weights, -1, 0) # Discard E_rec_i[k] E_rec_i = delete(E_rec_i, -1) else: count_under += 1 # Data Stores res2 = {'hidden' : Y, # Array for hidden Variables 'weights' : all_weights, 'E_t' : Eng[:,0], # total energy of data 'E_dash_t' : Eng[:,1], # hidden var energy 'e_ratio' : np.divide(Eng[:,1], Eng[:,0]), # Energy ratio 'RSRE' : RSRE, # Relative squared Reconstruction error 'recon' : x_dash, # reconstructed data 'r_hist' : k_hist, # history of r values 'anomalies' : anomalies} res.update(res2) return res, all_weights