<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Linear Model
\begin{equation}
z_n=0.5z_{n-1}+\xi_{n-1}
\end{equation}
with
$\xi_{n-1}\sim N(0,B)$ and $z_{0}\sim N(0,0.4)$
Observations
\begin{equation}
y_n=z_n+\eta_n
\end{equation}
$\eta_{n-1}\sim N(0,R)$
Kalman filter
Forecast formulas
Step1:  Lorenz equations
\begin{align}
\dot{x}&=\sigma(y-x)\
\dot{y}&=x(\rho-z)-y\
\dot{z}&=xy-\beta z
\end{align}
Ensemble Kalman Filter
\begin{equation}
z^i_{n+1}=\hat{z}^i_{n+1}-K_{n+1}(H\hat{z}^i_{n+1}-\tilde{y}^i_{n+1})
\end{equation}
\begin{align}
m_{n}&\approx\frac{1}{M}\sum^M_{i=1}z^i_{n}\
C_{n}&\approx\frac{1}{M}\sum^M_{i=1}(z^i_{n}-m_{n})(z^i_{n}-m_{n})^{\top}
\end{align}
Exercise
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123541312)
n = 100
z = np.zeros((n))
m = np.zeros((n))
mh = np.zeros((n))
y = np.zeros((n))
C = np.zeros((n))
Ch = np.zeros((n))
K = np.zeros((n))
A = .5
B = .2
C[0] = .4
R = .01
H = 1
zeta = np.random.normal(0, B, n)
nu = np.random.normal(0, R, n)
z[0] = np.random.normal(0, C[0])
#m[0] = z[0] + np.random.normal(0, C[0])
#y[0] = A * z[0] + np.random.normal(0, R)
#y = np.random.normal(0, R, n)
for i in range(1,n):
    z[i] = A * z[i-1] + zeta[i-1]
    y[i] = z[i] + nu[i]
    mh[i] = A * m[i-1]
    Ch[i] = A * C[i-1] * (A) + B
    tmp = (R + H * Ch[i] * H)**-1
    
    K[i] = Ch[i] * H * tmp
    
    m[i] = mh[i] - K[i] * (H * mh[i] - y[i])
    C[i] = Ch[i] - K[i] * H * Ch[i]
    
fig, ax = plt.subplots()
ax.plot(np.arange(n), z, label=r'$z$')
ax.plot(np.arange(n), m, label=r'$m$')
ax.legend()
plt.show()
Explanation: Linear Model
\begin{equation}
z_n=0.5z_{n-1}+\xi_{n-1}
\end{equation}
with
$\xi_{n-1}\sim N(0,B)$ and $z_{0}\sim N(0,0.4)$
Observations
\begin{equation}
y_n=z_n+\eta_n
\end{equation}
$\eta_{n-1}\sim N(0,R)$
Kalman filter
Forecast formulas:
\begin{align}
\hat{m}{n+1}&=Am_n\
\hat{C}{n+1}&=AC_nA^{\top}+B
\end{align}
Analysis formulas
\begin{align}
m_{n+1}&=\hat{m}{n+1}-K{n+1}(H\hat{m}{n+1}-y{n+1})\
C_{n+1}&=\hat{C}{n+1}-K{n+1}H\hat{C}_{n+1}
\end{align}
with Kalman gain
\begin{equation}
K_{n+1}=\hat{C}{n+1}H^{\top}(R+H\hat{C}{n+1}H^{\top})^{-1}
\end{equation}
Exercise: Please implement the Kalman filter for the example above
End of explanation
print(np.random.normal(0,.5))
print(np.random.normal(0,.5))
print(np.random.normal(0,.5))
print(np.random.normal(0,.5))
print(np.random.normal(0,.5))
print(np.random.normal(0,.5))
Explanation: Lorenz equations
\begin{align}
\dot{x}&=\sigma(y-x)\
\dot{y}&=x(\rho-z)-y\
\dot{z}&=xy-\beta z
\end{align}
Ensemble Kalman Filter
\begin{equation}
z^i_{n+1}=\hat{z}^i_{n+1}-K_{n+1}(H\hat{z}^i_{n+1}-\tilde{y}^i_{n+1})
\end{equation}
\begin{align}
m_{n}&\approx\frac{1}{M}\sum^M_{i=1}z^i_{n}\
C_{n}&\approx\frac{1}{M}\sum^M_{i=1}(z^i_{n}-m_{n})(z^i_{n}-m_{n})^{\top}
\end{align}
Exercise: Please implement the Ensemble Kalman filter for the Lorenz equation
End of explanation
<END_TASK>