<jupyter_start><jupyter_text>This notebook creates the animations for the Multivariate Kalman Filter chapter. It is not really intended to be a readable part of the book, but of course you are free to look at the source code, and even modify it. However, if you are interested in running your own animations, I'll point you to the examples subdirectory of the book, which contains a number of python scripts that you can run and modify from an IDE or the command line. This module saves the animations to GIF files, which is quite slow and not very interactive.<jupyter_code>import filterpy.stats as stats
import numpy as np
from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import mkf_internal
import matplotlib.pyplot as plt
from gif_animate import animate

def ellipse_animate(frame):
    
    plt.cla()
    cov = frame/15.
    P = np.array([[2,cov],[cov,2]])
    stats.plot_covariance_ellipse((2,7), cov=P, facecolor='g', alpha=0.2, 
                              title='|2.0 {:.1f}|\n|{:.1f} 2.0|'.format(cov, cov))
fig = plt.figure()
animate('multivariate_ellipse.gif', ellipse_animate, 30, 125, figsize=(3, 3))<jupyter_output><empty_output><jupyter_text><jupyter_code>import numpy as np
from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from gif_animate import animate
import numpy.random as random
from DogSimulation import DogSimulation
import filterpy.stats
from filterpy.common import Q_discrete_white_noise
from filterpy.kalman import KalmanFilter

def dog_tracking_filter(R,Q=0,cov=1.):
    dog_filter = KalmanFilter (dim_x=2, dim_z=1)
    dog_filter.x = np.array([0, 0])   # initial state (location and velocity)
    dog_filter.F = np.array([[1,1],
                             [0,1]])  # state transition matrix
    dog_filter.H = np.array([[1,0]])  # Measurement function
    dog_filter.R *= R                 # measurement uncertainty
    dog_filter.P *= cov               # covariance matrix 
    if np.isscalar(Q):
        dog_filter.Q = Q_discrete_white_noise(2, var=Q)
    else:
        dog_filter.Q = Q
    return dog_filter

R = 5.
Q = .01
noise = 2.
P = 20.
dog = DogSimulation(measurement_variance=R, process_variance=Q)
f = dog_tracking_filter(R=R, Q=Q, cov=P)
random.seed(200)
zs = []
xs = []
def animate_track(frame):
    if frame > 30: return
    
    plt.gca().set_xlim(-100,100)
    if frame == 0:
        stats.plot_covariance_ellipse ((0, f.x[0]), cov=f.P, axis_equal=True, 
                                       facecolor='g', edgecolor=None, alpha=0.2)
        xs.append (f.x[0])
        
    z = dog.move_and_sense()
    zs.append(z)
    f.update(z)
    xs.append (f.x[0])
    
    stats.plot_covariance_ellipse ((frame+1, f.x[0]), cov=f.P, axis_equal=True, 
                                       facecolor='g', edgecolor=None, alpha=0.5,
                                       xlim=(-5,30), ylim=(-5,30))
    
    plt.plot(zs, color='r', linestyle='dashed')
    plt.plot(xs, color='b')
    f.predict()

animate('multivariate_track1.gif', animate_track, 37, 200, figsize=(5.5, 5.5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1091581868640148, 'fraction_numerical': 0.02528522972556275, 'mean_word_length': 3.1483375959079285, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '214091', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1085, 'n_words': 305, 'path': 'animations/multivariate_animations.ipynb', 'repo_name': 'Mayakshanesht/Kalman-and-bayesian-filters', 'size': '3243'}