Kalman Filter update function for multivariable regression {PYTHON]

40 Views Asked by At

I want to fix update function to multivariable case. my update function dont work with multiple values [x2,x3] I need update function whoich takes row and return kalman filter values.

class KalmanFilterRegression3 :
    
    def __init__(self,df):
        self.y=df['x1']
        self.x2=df['x2']
        self.x3=df['x3']

        
        delta = 1e-3
        trans_cov = delta / (1 - delta) * np.eye(3)  # How much random walk wiggles
        obs_mat = np.expand_dims(np.vstack([ self.x2,  self.x3, np.ones(len(self.x2))]).T, axis=1)
        
        self.kf = KalmanFilter(n_dim_obs=1, n_dim_state=3,  # y is 1-dimensional, (alpha, beta1, beta2) is 3-dimensional
                       initial_state_mean=[0, 0, 0],
                       initial_state_covariance=np.eye(3),  # Initial covariance matrix for state parameters
                       transition_matrices=np.eye(3),  # Transition matrix for state parameters
                       observation_matrices=obs_mat,  # Observation matrix for predicting observations
                       observation_covariance=2,  # Observation noise covariance
                       transition_covariance=trans_cov)  # Process noise covariance
    
    
        self.state_means, self.state_covs = self.kf.filter(self.y.values)
        
    
    def update(self,row,idx):
        y=row['x1']
        x2=row['x2']
        x3=row['x3']
        


        obs_mat = np.asarray([x2,x3, 1])
        state_means=self.state_means
        state_covs=self.state_covs
        state_means[id], state_covs[id] = self.kf.filter_update(state_means[idx-1], 
                                                            state_covs[idx-1], 
                                                            observation = y, 
                                                            observation_matrix=obs_mat)


        slope=history_state_means[idx, 0]
        print ("SLOPE", slope)
    
        return state_means

I want to fix update function to multivariable case.

0

There are 0 best solutions below