コード例 #1
0
ファイル: particlefilter.py プロジェクト: sdwfrost/pyepi
    def step(self, y, predict_P=False, index=1):
        X = self.x_prior
        states = X[:2, :]
        params = X[2:, :]

        s_std = std(X[index].A1)

        tmp_ws = as_array([norm.pdf(y, x[0, index], s_std) for x in states.T])
        n_weights = self.weights * tmp_ws
        sum_weights = n_weights.sum()

        if sum_weights != 0:
            n_weights /= sum_weights
            neff = 1.0 / (n_weights**2).sum()

        if sum_weights == 0 or neff < self.num_part / 2.0:
            idx = choice(range(X.shape[1]), X.shape[1], p=self.weights)
            self.weights = tile(as_array(1.0 / self.num_part), self.num_part)

            self.x_post = X[:, idx]
        else:
            self.x_post = X
            self.weights = n_weights

        p_mean = average(params, axis=1, weights=self.weights).A1
        p_cov = cov(params, aweights=self.weights)
        self.x_post[2:, :] = multivariate_normal(p_mean, p_cov, X.shape[1]).T

        for i, x in enumerate(self.x_post[2:, :].T):
            if x.any() < 0:
                while True:
                    new = multivariate_normal(p_mean, p_cov, 1).T
                    if new.all() > 0 and new[0, 1] > new[0, 2]:
                        self.x_post[2:, i] = new
                        break
コード例 #2
0
ファイル: particlefilter.py プロジェクト: weipeng/pyepi
    def step(self, y, predict_P=False, index=1):
        X = self.x_prior
        states = X[:2, :]
        params = X[2:, :]
        
        s_std = std(X[index].A1) 
        
        tmp_ws = as_array([norm.pdf(y, x[0, index], s_std) for x in states.T])
        n_weights = self.weights * tmp_ws
        sum_weights = n_weights.sum()
    
        if sum_weights != 0:
            n_weights /= sum_weights
            neff = 1.0 / (n_weights ** 2).sum() 
        
        if sum_weights == 0 or neff < self.num_part/2.0:
            idx = choice(range(X.shape[1]), X.shape[1], p=self.weights)
            self.weights = tile(as_array(1.0 / self.num_part), self.num_part)
            
            self.x_post = X[:, idx]
        else:
            self.x_post = X
            self.weights = n_weights

        p_mean = average(params, axis=1, weights=self.weights).A1
        p_cov = cov(params, aweights=self.weights)
        self.x_post[2:, :] = multivariate_normal(p_mean, p_cov, X.shape[1]).T
 
        for i, x in enumerate(self.x_post[2:, :].T):
            if x.any() < 0:
                while True:
                    new = multivariate_normal(p_mean, p_cov, 1).T
                    if new.all() > 0 and new[0, 1] > new[0, 2]:
                        self.x_post[2:, i] = new
                        break
コード例 #3
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]
コード例 #4
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]
コード例 #5
0
    def step(self, y, predict_P=False, index=1):
        X = self.x_prior
        states = X[:2, :]
        params = X[2:, :]
        print('A1', X[index].A1.shape)
        s_std = std(X[index].A1)

        tmp_ws = as_array([norm.pdf(y, x[0, index], s_std) for x in states.T])
        n_weights = self.weights * tmp_ws
        sum_weights = n_weights.sum()
        if sum_weights != 0:
            n_weights /= sum_weights
            neff = 1.0 / (n_weights**2).sum()

        if sum_weights == 0 or neff < self.num_part / 2.0:
            print('index', index, 'resamples')
            idx = choice(range(X.shape[1]), X.shape[1], p=self.weights)
            self.weights = tile(as_array(1.0 / self.num_part), self.num_part)
            self.x_post = X[:, idx]

        else:
            self.x_post = X
            self.weights = n_weights

        print('weight:', self.weights.shape, 'n_weight', n_weights.sum(),
              'curr weight', self.weights.sum())
        p_mean = average(params, axis=1, weights=self.weights).A1
        p_cov = cov(params, aweights=self.weights)
        self.x_post[2:, :] = multivariate_normal(p_mean, p_cov, X.shape[1]).T

        for i, x in enumerate(self.x_post[
                2:, :].T):  #iterate over all parameter post estimat.
            # resample any particle parameters (alpha,beta)
            if x.any() < 0:
                while True:
                    new = multivariate_normal(p_mean, p_cov, 1).T
                    if new.all() > 0 and new[0, 1] > new[0, 2]:
                        self.x_post[2:, i] = new
                        break
コード例 #6
0
    def get_score(self):
        I_mat = as_array(self.Is)
        for i, w in enumerate(self.weights):
            I_mat[i] *= w

        self.IS = sum(I_mat, axis=1)

        time_gap = 1  # self.epochs / 52
        idx = [x for x in range(self.epochs) if not x % time_gap]

        self.score = RSS(self.CDC_obs, self.IS[idx])
        self.scores = {}
        self.scores['SSE'] = self.score
        self.scores['RMSE'] = RMSE(self.CDC_obs, self.IS[idx])
        self.scores['MSPE'] = MSPE(self.CDC_obs, self.IS[idx])
        self.scores['CORR'] = corrcoef(self.CDC_obs, self.IS[idx])[0, 1]
        return self.score
コード例 #7
0
ファイル: ensir.py プロジェクト: weipeng/pyepi
    def get_score(self):
        I_mat = as_array(self.Is)
        for i, w in enumerate(self.weights):
            I_mat[i] *= w 

        self.IS = sum(I_mat, axis=1)

        time_gap = self.epochs / 52
        idx = [x for x in range(self.epochs) if not x % time_gap]

        self.score = RSS(self.CDC_obs, self.IS[idx])
        self.scores = {}
        self.scores['SSE'] = self.score
        self.scores['RMSE'] = RMSE(self.CDC_obs, self.IS[idx])
        self.scores['MSPE'] = MSPE(self.CDC_obs, self.IS[idx])
        self.scores['CORR'] = corrcoef(self.CDC_obs, self.IS[idx])[0, 1]
        return self.score