def interaction(self): # mu mu = np.zeros((self.mode_num, self.mode_num)) for i in range(self.mode_num): for j in range(self.mode_num): pij = (1 - (self.mode_num - 1) * self.f) if (i == j) else self.f mu[i, j] = pij * self.mode_dist[i] c = np.sum(mu, 0) mu = mu / c pos = np.isnan(mu) mu[pos] = 0 mode_particle_dict = {} for j in range(self.mode_num): particles = [] for i in range(self.mode_num): for p in self.mode_particle_dict[j]: _p = p.clone() _p.set_weigth(_p.weight * mu[i, j]) particles.append(_p) mode_particle_dict[j] = resample(particles, self.N) self.mode_particle_dict = mode_particle_dict return c
def pf_identify_fault_para(self, window=2.0): i = self.detect_para_fault() self.para_fault_id.append(i) window_len = int(window/self.hsw.step_len) if i!=0 and (np.array(self.para_fault_id[-window_len:])!=0).all() and \ abs(self.t-self.latest_sp)>3 and not self.particle_para_estimation: self.particle_para_estimation = True i -= 1 self.N = self.Nmax particles = self.tracjectory[-1] self.detect_time = self.t # detect time self.fault_time = self.t - window self.fault_para_flag[i] = 1 particles = resample(particles, self.Nmax) for p in particles: magnitude = np.random.uniform(0.05, 0.5) init_para = np.zeros(len(self.hsw.para_faults())) init_para[i] = magnitude p.set_para(init_para) self.tracjectory[-1] = particles if not self.particle_para_estimation: fault_para = self.fault_para else: particles = self.tracjectory[-1] fault_para = np.sum([p.weight*p.para for p in particles], 0) self.para.append(fault_para)
def inverse_transform(self, X): """ Inverse transform of sampled data X. It requires original_shape to be set (via set_params). """ if self.original_rows is None: raise ValueError("original shape is not set!") newshape = (self.original_rows, X.shape[1]) return resample(X, newshape, self.method, self.centre, self.minusone)
def transform(self, X, y=None): """ Resample the matrix X. X is of shape [n_samples, n_features]. It has to have at least 2 dimensions. X can be a list of matrices, in which case tranforms is applied to each of them independently and it returns a list of discretized matrices. """ if type(X) is list: ret = [] for x in X: newshape = (self.newshape, x.shape[1]) ret.append(resample(x, newshape, self.method, self.centre, self.minusone) ) return ret else: newshape = (self.newshape, X.shape[1]) return resample(X, newshape, self.method, self.centre, self.minusone)
def step(self, particle, obs): self.t += self.hsw.step_len particle_ip1 = [] for ptc in particle: p = self.step_particle(ptc, obs) particle_ip1.append(p) normalize(particle_ip1, 0) re_particle_ip1 = resample(particle_ip1, self.N) ave_state = sum([p.weight * p.state for p in re_particle_ip1]) max_mode = [p.mode for p in re_particle_ip1] num_counter = Counter(max_mode) max_mode = num_counter.most_common(1)[0][0] self.tracjectory.append(re_particle_ip1) self.state.append(ave_state) self.mode.append(max_mode)
def step(self, obs): self.t += self.hsw.step_len mode_i0 = self.mode0 if not self.state else self.mode[len( self.state) - 1] # lastest mode particle_ip1 = {} for m in range(self.mode_num): particle_ip1[m] = [] particle = self.mode_particle_dict[m] for ptc in particle: p = self.step_particle(ptc, obs, mode_i0) particle_ip1[m].append(p) weight = [ sum([p.weight for p in particle_ip1[m]]) for m in particle_ip1 ] weight = [w / sum(weight) for w in weight] w_max = max(weight) # maximal weight m_opt = weight.index(w_max) # optimal mode new_particle_ip1 = {} for m in range(self.mode_num): if weight[m] == 0 or (w_max / weight[m] > self.r): copy_ptc = [] for p in particle_ip1[m_opt]: _p = p.clone() _p.mode = m copy_ptc.append(_p) new_particle_ip1[m] = copy_ptc else: new_particle_ip1[m] = particle_ip1[m] m_opt = m_opt if weight[mode_i0] == 0 or ( weight[m_opt] / weight[mode_i0] > self.r) else mode_i0 for m in range(self.mode_num): normalize(new_particle_ip1[m], 0.0001) new_particle_ip1[m] = resample(new_particle_ip1[m]) ave_state = sum([p.weight * p.state for p in new_particle_ip1[m_opt]]) self.tracjectory.append(new_particle_ip1[m_opt]) self.state.append(ave_state) self.mode.append(m_opt) self.mode_particle_dict = new_particle_ip1
def step(self, particles, obs, mode): ''' particles: particle list ''' self.t += self.hsw.step_len obs_conf = self.obs_conf if (self.fault_para_flag==0).all() else 0.0 mode_i0 = self.mode0 if not self.state else self.mode[len(self.state)-1] self.latest_sp = self.latest_sp if mode_i0==mode else self.t particles_ip1 = [] res = np.zeros(len(self.hsw.obs_sigma)) for ptc in particles: p, r = self.step_particle(ptc, obs, mode_i0, mode) particles_ip1.append(p) res += r self.last_likelihood = sum([ptc.weight for ptc in particles_ip1]) normalize(particles_ip1, obs_conf) re_particles_ip1 = resample(particles_ip1, self.N) ave_state = self.ave_state(re_particles_ip1) self.tracjectory.append(re_particles_ip1) self.state.append(ave_state) self.res.append(res) self.process_fault(res)