0

I came across a problem when I was using pykalman 0.9.5 in Python 3.6.3

Refer to the code below, why are the results from kf2 and kf3 are different while the results from kf1 and kf3 are identical?

The difference of process between kf2 and kf3 is that I merely split the iteration into running 2 times of the function for kf2.

Thanks for everyone looking into it.

>>>pri_mean[:10]
array([ 2827.2222,  2829.6   ,  2831.    ,  2832.1   ,  2833.1   ,  2835.3   ,  2833.9   ,
        2833.8   ,  2833.6   ,  2833.    ])

>>>kf1 = KalmanFilter()
>>>kf1 = kf1.em(pri_mean, 10, em_vars='all')
>>>print(kf1.transition_matrices, kf1.transition_offsets, kf1.transition_covariance)
[[ 0.99741876]] [ 10.04426882] [[ 2896.92752373]]
>>>kf2 = kf1.em(pri_mean, 10, em_vars='all')
>>>print(kf2.transition_matrices, kf2.transition_offsets, kf2.transition_covariance)
[[ 0.99364606]] [ 20.02260806] [[ 2600.94151188]]

>>>kf3 = KalmanFilter()
>>>kf3 = kf3.em(pri_mean, 20, em_vars='all')
>>>print(kf3.transition_matrices, kf3.transition_offsets, kf3.transition_covariance)
[[ 0.99741876]] [ 10.04426882] [[ 2896.92752373]]
Star
  • 3,222
  • 5
  • 32
  • 48
Gabriel
  • 161
  • 2
  • 11

1 Answers1

0

I edit the answer, since I misunderstood the question. I think the problem is that you are missing the keyword n_iter

See this code:

kf1 = KalmanFilter()
kf1 = kf1.em(pri_mean, n_iter=10, em_vars='all')
print(kf1.transition_matrices, kf1.transition_offsets, kf1.transition_covariance)
kf1 = kf1.em(pri_mean, n_iter=10, em_vars='all')
print(kf1.transition_matrices, kf1.transition_offsets, kf1.transition_covariance)
kf1 = KalmanFilter()
kf1 = kf1.em(pri_mean, n_iter=20, em_vars='all')
print(kf1.transition_matrices, kf1.transition_offsets, kf1.transition_covariance)

I create one filter through KalmanFilter(), loop 10 iterations and print, then 10 more and print. This is equivalent as calling .em() with 20 iterations straight away.

Would produce the following output

[[ 0.95500561]] [ 113.29118228] [[ 6431.66262464]]
[[ 0.93636512]] [ 119.32378005] [[ 249.67547612]]
[[ 0.93636512]] [ 119.32378005] [[ 249.67547612]]
drublackberry
  • 238
  • 1
  • 7
  • Hi drublackberry, thanks for your answer. I overwrote kf1 on purpose because I want to compare kf2, which just iterates 20 times in total just like kf3, and kf3 itself, which iterates 20 times in one function. Should the results between calling em(n_iter=10) two times and calling em(n_iter=20) one time being the same? I am confused. – Gabriel Dec 21 '17 at 11:31
  • Sorry @Gabriel_F I misunderstood. I edited the answer so now should work. – drublackberry Dec 21 '17 at 17:34
  • So it did! However, I still don't understand. Which part did I misunderstand? In my example, kf1 and k3 should not affect each other comparing to your first 'kf1' instance and second 'kf1' instance. – Gabriel Dec 22 '17 at 01:09
  • Simpler than that, you forgot to tell your call that 10 or 20 were `n_iter`. Your sequence of instantiation should work just fine. – drublackberry Dec 22 '17 at 08:08
  • Sorry for the delay. I just tried it and got what I want like you said. Thanks a lot. What a stupid mistake. – Gabriel Dec 24 '17 at 09:01