示例#1
0
文件: easir.py 项目: sdwfrost/pyepi
    def predict_with_filter(self):
        F = self.filter

        while self.epoch < self.epochs - 1:
            X = as_matrix(
                [self.current_Ss, self.current_Is, self.alphas, self.betas])

            F.fit(X)
            y = tile(as_matrix(self.CDC_obs[self.epoch]), self.num_enbs)
            F.step(y, predict_P=False)
            self.weights.append(F.weights)

            x_post = F.x_post
            x_mean = mean(X, axis=1)
            x_post_mean = mean(x_post, axis=1)
            x_post = self.inflate * (X - tile(x_mean, self.num_enbs)) + \
                     tile(x_post_mean, self.num_enbs)
            for j in range(self.num_enbs):
                self.current_Ss[j] = self.check_bounds(x_post[0, j])
                self.current_Is[j] = self.check_bounds(x_post[1, j])
                self.alphas[j] = self.check_bounds(x_post[2, j], inf)
                self.betas[j] = self.check_bounds(x_post[3, j], inf)

            self.update_states()
            self.epoch += 1

        self.get_score()
示例#2
0
    def predict_with_filter(self):
        F = self.filter

        while self.epoch < self.epochs - 1:
            X = as_matrix(
                [self.current_Ss, self.current_Is, self.alphas, self.betas])

            F.fit(X)
            y = tile(as_matrix(self.CDC_obs[self.epoch]), self.num_enbs)
            F.step(y, predict_P=False)

            x_post, weights, num_res = Bass.step(X, F.x_post, self.weights[-1],
                                                 self.CDC_obs[self.epoch],
                                                 self.err_bnd, F.V)
            self.num_resample += num_res
            self.weights.append(weights)
            for j in range(self.num_enbs):
                self.current_Ss[j] = self.check_bounds(x_post[0, j])
                self.current_Is[j] = self.check_bounds(x_post[1, j])
                self.alphas[j] = self.check_bounds(x_post[2, j], inf)
                self.betas[j] = self.check_bounds(x_post[3, j], inf)

            self.update_states()
            self.epoch += 1

        self.get_score()
        self.mean_resample = self.num_resample / self.epoch
示例#3
0
文件: easir.py 项目: weipeng/pyepi
    def predict_with_filter(self):
        F = self.filter

        while self.epoch < self.epochs - 1:
            X = as_matrix([self.current_Ss, self.current_Is, 
                           self.alphas, self.betas])
        
            F.fit(X)
            y = tile(as_matrix(self.CDC_obs[self.epoch]), 
                     self.num_enbs)
            F.step(y, predict_P=False)
            self.weights.append(F.weights)

            x_post = F.x_post
            x_mean = mean(X, axis=1)
            x_post_mean = mean(x_post, axis=1)
            x_post = self.inflate * (X - tile(x_mean, self.num_enbs)) + \
                     tile(x_post_mean, self.num_enbs)
            for j in range(self.num_enbs):
                self.current_Ss[j] = self.check_bounds(x_post[0, j])
                self.current_Is[j] = self.check_bounds(x_post[1, j])
                self.alphas[j] = self.check_bounds(x_post[2, j], inf)
                self.betas[j] = self.check_bounds(x_post[3, j], inf)

            self.update_states()
            self.epoch += 1

        self.get_score()
示例#4
0
    def _init_filter(self):
        num_states = 2
        num_obs = 1
        A = as_matrix([[1, -self.alpha], [0, 1 + self.alpha - self.beta]])

        B = as_matrix([0, 1])

        Cov = eye(num_states, dtype=data_type) * 0.0001

        V = Cov.copy()
        W = eye(num_obs, dtype=data_type) * 0.0001
示例#5
0
文件: ensir.py 项目: weipeng/pyepi
    def _init_filter(self):
        num_states = 4
        num_obs = 1
        
        A = None
        B = self.construct_B(with_param=True)

        V = as_matrix(eye(num_states, dtype=data_type)) * 0.0001
        W = as_matrix(eye(num_obs, dtype=data_type)) * 0.0001

        self.filter = EnsembleKalmanFilter(self.num_enbs, num_states, num_obs, A, B, V, W)
示例#6
0
    def _init_filter(self):
        num_states = 4
        num_obs = 1

        A = None
        B = self.construct_B(with_param=True)

        V = as_matrix(eye(num_states, dtype=data_type)) * 0.0001
        W = as_matrix(eye(num_obs, dtype=data_type)) * 0.0001

        self.filter = EnsembleKalmanFilter(self.num_enbs, num_states, num_obs,
                                           A, B, V, W)
示例#7
0
文件: sir.py 项目: weipeng/pyepi
    def _init_filter(self):
        num_states = 2
        num_obs = 1
        A = as_matrix([[1, -self.alpha], 
                       [0, 1 + self.alpha - self.beta]])

        B = as_matrix([0, 1])

        Cov = eye(num_states, dtype=data_type) * 0.0001

        V = Cov.copy()
        W = eye(num_obs, dtype=data_type) * 0.0001
        
        self.filter = KalmanFilter(num_states, num_obs, A, B, V, W, Cov)
示例#8
0
    def predict_with_filter(self, params):
        F = self.filter

        while self.epoch < self.epochs - 1:
            X = as_matrix(
                [self.current_Ss, self.current_Is, self.alphas, self.betas])

            F.fit(X)
            y = self.CDC_obs[self.epoch]
            F.step(y, predict_P=False)
            self.weights.append(F.weights)

            x_post = F.x_post
            for j in range(self.num_enbs):
                self.current_Ss[j] = self.check_bounds(x_post[0, j])
                self.current_Is[j] = self.check_bounds(x_post[1, j])
                self.alphas[j] = self.check_bounds(x_post[
                    2,
                    j])  #, inf)    # self.check_bounds(x_post[2, j]) #, inf)
                self.betas[j] = self.check_bounds(x_post[3, j])  #, inf)
                #print(self.alphas)

            self.update_states()
            self.epoch += 1
            self.alpha_list.append(mean(self.alphas))
            self.beta_list.append(mean(self.betas))
        self.get_score()
示例#9
0
文件: bass.py 项目: weipeng/pyepi
    def step(X, X_out, w, y, err, R, index=1):
        s_std = std(X[index].A1)  
        tmp_ws = as_array([norm.pdf(y, x[0, index], s_std) for x in X.T])
        # It is strange that the numerical instability was never found in
        # the python 2.7 version. 
        # The following two lines are just an ad-hoc solution
        if (tmp_ws == 0).all(): 
            tmp_ws = w
        else:
            tmp_ws *= w 
            tmp_ws /= tmp_ws.sum()

        s_idx = where(tmp_ws >= err)[0]
        l_idx = where(tmp_ws < err)[0]

        if l_idx.shape[0] > 0:
            s_ws = tmp_ws[s_idx] / tmp_ws[s_idx].sum()
            noise = multivariate_normal(zeros(X.shape[0]), R, l_idx.shape[0])
            idx = choice(s_idx, l_idx.shape[0], p=s_ws)
            X_out[:, l_idx] = X_out[:, idx] + as_matrix(noise).T
            tmp_ws[l_idx] = tmp_ws[idx]
            w[:] = tmp_ws / tmp_ws.sum()
        else:
            w[:] = tmp_ws[:]

        return X_out, w, l_idx.shape[0]
示例#10
0
    def step(X, X_out, w, y, err, R, index=1):
        s_std = std(X[index].A1)
        tmp_ws = as_array([norm.pdf(y, x[0, index], s_std) for x in X.T])
        # It is strange that the numerical instability was never found in
        # the python 2.7 version.
        # The following two lines are just an ad-hoc solution
        if (tmp_ws == 0).all():
            tmp_ws = w
        else:
            tmp_ws *= w
            tmp_ws /= tmp_ws.sum()

        s_idx = where(tmp_ws >= err)[0]
        l_idx = where(tmp_ws < err)[0]

        if l_idx.shape[0] > 0:
            s_ws = tmp_ws[s_idx] / tmp_ws[s_idx].sum()
            noise = multivariate_normal(zeros(X.shape[0]), R, l_idx.shape[0])
            idx = choice(s_idx, l_idx.shape[0], p=s_ws)
            X_out[:, l_idx] = X_out[:, idx] + as_matrix(noise).T
            tmp_ws[l_idx] = tmp_ws[idx]
            w[:] = tmp_ws / tmp_ws.sum()
        else:
            w[:] = tmp_ws[:]

        return X_out, w, l_idx.shape[0]
示例#11
0
    def __init__(self, num_states, num_obs, A, B, V, W, P=None):
        self.num_states = num_states
        self.num_obs = num_obs

        self.A = None if A is None else as_matrix(A)
        self.B = None if B is None else as_matrix(B)
        self.V = as_matrix(V) 
        self.W = as_matrix(W) 
        
        self.x_prior = as_matrix(zeros(num_states)).T
        self.x_post = as_matrix(zeros(num_states)).T
        self.P_prior = P if P is not None else \
                       as_matrix(zeros((num_states, num_obs)))
        self.P_post = P if P is not None else \
                      as_matrix(zeros((num_states, num_obs)))

        self.K = as_matrix(zeros((num_states, num_obs)))
        self.I = as_matrix(eye(num_states))
示例#12
0
    def predict_with_filter(self, params):
        if not self.filtering or not self.filter:
            raise Exception('The filtering flag must be set True, \
                             and the filter needs to be inialized')

        F = self.filter
        while self.epoch < self.epochs - 1:
            x = as_matrix([self.s, self.i]).T
            F.fit(x, params, refit=True)
            y = as_matrix([self.CDC_obs[self.epoch]]).T
            F.step(y)

            self.s = self.check_bounds(F.x_post[0, 0])
            self.i = self.check_bounds(F.x_post[1, 0])
            self.update_states()
            self.epoch += 1

        self.get_score()
        self.score
示例#13
0
文件: sir.py 项目: weipeng/pyepi
    def predict_with_filter(self):
        if not self.filtering or not self.filter:
            raise Exception('The filtering flag must be set True, \
                             and the filter needs to be inialized')

        F = self.filter 
        while self.epoch < self.epochs - 1:
            x = as_matrix([self.s, self.i]).T
            F.fit(x)
            y = as_matrix([self.CDC_obs[self.epoch]]).T
            F.step(y)

            self.s = self.check_bounds(F.x_post[0, 0])
            self.i = self.check_bounds(F.x_post[1, 0])
            self.update_states()
            self.epoch += 1
            
        self.get_score()
        self.score 
示例#14
0
 def construct_B(self, with_param=False):
     B = as_matrix([0, 1, 0, 0]) if with_param else as_matrx([0, 1])
     return B
示例#15
0
文件: sir.py 项目: weipeng/pyepi
 def construct_B(self, with_param=False):
     B = as_matrix([0, 1, 0, 0]) if with_param else as_matrx([0, 1])
     return B