oseiskar / simdkalman

Python Kalman filters vectorized as Single Instruction, Multiple Data
https://simdkalman.readthedocs.io/
MIT License
176 stars 36 forks source link

Data format - Kalman filter #10

Closed andrewczgithub closed 4 years ago

andrewczgithub commented 4 years ago

Hi @oseiskar

I was just trying to run a experiment with the dataset and I am getting the following error. I was wondering if you could assist.

The data set it -

measurements

masked_array( data=[[1.83 , 1.314 , 0.124 , ..., 2.072 , 0.959 , 0.093 ], [1.907 , 1.341 , 0.131 , ..., 2.109 , 0.972 , 0.1 ], [1.935 , 1.36 , 0.138 , ..., 2.141 , 1. , 0.105 ], ..., [4.864745, 2.99041 , 6.945452, ..., 5.772958, 1.830929, 0.58429 ], [4.87512 , 3.002997, 7.048876, ..., 5.792051, 1.817048, 0.578934], [4.887871, 3.013825, 7.171646, ..., 5.813568, 1.805832, 0.577737]], mask=False, fill_value=1e+20)

"""""""""

kf = simdkalman.KalmanFilter(A, np.linalg.inv(W_neg_sqrt @ W_neg_sqrt), C,np.linalg.inv(V_neg_sqrt @ V_neg_sqrt)) kf = kf.em(measurements, n_iter=10)

received the error -

ValueError Traceback (most recent call last) in 68 measurements[~(K & ~M_test)] = np.ma.masked 69 kf = simdkalman.KalmanFilter(A, np.linalg.inv(W_neg_sqrt @ W_neg_sqrt), C,np.linalg.inv(V_neg_sqrt @ V_neg_sqrt)) ---> 70 kf = kf.em(measurements, n_iter=10) 71 filtered_statemeans, = kf.smooth(measurements) 72 yhat_em = filtered_state_means @ C.T

~/tutorial-env/lib/python3.7/site-packages/simdkalman/kalmanfilter.py in em(self, data, n_iter, initial_value, initial_covariance, verbose) 732 gains = True, 733 log_likelihood = False, --> 734 verbose = verbose) 735 736 if verbose:

~/tutorial-env/lib/python3.7/site-packages/simdkalman/kalmanfilter.py in compute(self, data, n_test, initial_value, initial_covariance, smoothed, filtered, states, covariances, observations, likelihoods, gains, log_likelihood, verbose) 526 print('filtering %d/%d' % (j+1, n_measurements)) 527 --> 528 y = data[:,j,...].reshape((n_vars, n_obs, 1)) 529 530 tup = self.update(m, P, y, log_likelihood)

ValueError: cannot reshape array of size 119 into shape (119,48,1)

oseiskar commented 4 years ago

Hi! And sorry for the slow answer.

This package was not designed to support masked_array, which may create some issues here (not 100% if that's the main issue, though). If your data has missing values, they should be marked with np.nans (see example). I think this method should allow converting to such representation.

Also double-check the shape of your data is correct (as documented here). Multi-dimensional observation usage is demonstrated in this example

oseiskar commented 4 years ago

Closing due to inactivity