@staticmethod<EOL><INDENT>def _kalman_update_step(k,   p_m , p_P, p_meas_model_callable, measurement, calc_log_likelihood= False,<EOL>calc_grad_log_likelihood=False, p_dm = None, p_dP = None):<DEDENT>
<EOL>m_pred = p_m <EOL>P_pred = p_P <EOL>H = p_meas_model_callable.Hk(k, m_pred, P_pred)<EOL>R = p_meas_model_callable.Rk(k)<EOL>time_series_no = p_m.shape[<NUM_LIT:1>] <EOL>log_likelihood_update=None; dm_upd=None; dP_upd=None; d_log_likelihood_update=None<EOL>v = measurement-p_meas_model_callable.f_h(k, m_pred, H)<EOL>S = H.dot(P_pred).dot(H.T) + R<EOL>if measurement.shape[<NUM_LIT:0>]==<NUM_LIT:1>: <EOL><INDENT>if (S < <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>" % k )<EOL><DEDENT>K = P_pred.dot(H.T) / S<EOL>if calc_log_likelihood:<EOL><INDENT>log_likelihood_update = -<NUM_LIT:0.5> * ( np.log(<NUM_LIT:2>*np.pi) + np.log(S) +<EOL>v*v / S)<EOL>if np.any(np.isnan(log_likelihood_update)): <EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>LL = None; islower = None<EOL><DEDENT>else:<EOL><INDENT>LL,islower = linalg.cho_factor(S)<EOL>K = linalg.cho_solve((LL,islower), H.dot(P_pred.T)).T<EOL>if calc_log_likelihood:<EOL><INDENT>log_likelihood_update = -<NUM_LIT:0.5> * ( v.shape[<NUM_LIT:0>]*np.log(<NUM_LIT:2>*np.pi) +<EOL><NUM_LIT:2>*np.sum( np.log(np.diag(LL)) ) +np.sum((linalg.cho_solve((LL,islower),v)) * v, axis = <NUM_LIT:0>) ) <EOL><DEDENT><DEDENT>if calc_grad_log_likelihood:<EOL><INDENT>dm_pred_all_params = p_dm <EOL>dP_pred_all_params = p_dP<EOL>param_number = p_dP.shape[<NUM_LIT:2>]<EOL>dH_all_params = p_meas_model_callable.dHk(k)<EOL>dR_all_params = p_meas_model_callable.dRk(k)<EOL>dm_upd = np.empty(dm_pred_all_params.shape)<EOL>dP_upd = np.empty(dP_pred_all_params.shape)<EOL>d_log_likelihood_update = np.empty((param_number,time_series_no))<EOL>for param in range(param_number):<EOL><INDENT>dH = dH_all_params[:,:,param]<EOL>dR = dR_all_params[:,:,param]<EOL>dm_pred = dm_pred_all_params[:,:,param]<EOL>dP_pred = dP_pred_all_params[:,:,param]<EOL>dv = - np.dot( dH, m_pred) -  np.dot( H, dm_pred)<EOL>dS = np.dot(dH, np.dot( P_pred, H.T))<EOL>dS += dS.T<EOL>dS += np.dot(H, np.dot( dP_pred, H.T)) + dR<EOL>if LL is not None: <EOL><INDENT>tmp1 = linalg.cho_solve((LL,islower), H).T<EOL>tmp2 = linalg.cho_solve((LL,islower), dH).T<EOL>tmp3 = linalg.cho_solve((LL,islower), dS).T<EOL><DEDENT>else: <EOL><INDENT>tmp1 = H.T / S<EOL>tmp2 = dH.T / S<EOL>tmp3 = dS.T / S<EOL><DEDENT>dK = np.dot( dP_pred, tmp1) + np.dot( P_pred, tmp2) -np.dot( P_pred, np.dot( tmp1, tmp3 ) )<EOL>dm_upd[:,:,param] = dm_pred + np.dot(dK, v) + np.dot(K, dv)<EOL>dP_upd[:,:,param] = -np.dot(dK, np.dot(S, K.T))<EOL>dP_upd[:,:,param] += dP_upd[:,:,param].T<EOL>dP_upd[:,:,param] += dP_pred - np.dot(K , np.dot( dS, K.T))<EOL>dP_upd[:,:,param] = <NUM_LIT:0.5>*(dP_upd[:,:,param] + dP_upd[:,:,param].T) <EOL>if LL is not None: <EOL><INDENT>tmp5 = linalg.cho_solve((LL,islower), v)<EOL><DEDENT>else: <EOL><INDENT>tmp5 = v / S<EOL><DEDENT>d_log_likelihood_update[param,:] = -(<NUM_LIT:0.5>*np.sum(np.diag(tmp3)) +np.sum(tmp5*dv, axis=<NUM_LIT:0>) - <NUM_LIT:0.5> * np.sum(tmp5 * np.dot(dS, tmp5), axis=<NUM_LIT:0>) )<EOL><DEDENT><DEDENT>m_upd = m_pred + K.dot( v )<EOL>P_upd = K.dot(S).dot(K.T)<EOL>P_upd = <NUM_LIT:0.5>*(P_upd + P_upd.T)<EOL>P_upd =  P_pred - P_upd<EOL>return m_upd, P_upd, log_likelihood_update, dm_upd, dP_upd, d_log_likelihood_update<EOL>
Input:

k: int
      Iteration No. Starts at 0. Total number of iterations equal to the
      number of measurements.

m_P: matrix of size (state_dim, time_series_no)
     Mean value from the previous step. For "multiple time series mode"
        it is matrix, second dimension of which correspond to different
        time series.

p_P:
     Covariance matrix from the prediction step.

p_meas_model_callable: object

measurement: (measurement_dim, time_series_no) matrix
    One measurement used on the current update step. For
    "multiple time series mode" it is matrix, second dimension of
    which correspond to different time series.

calc_log_likelihood: boolean
    Whether to calculate marginal likelihood of the state-space model.

calc_grad_log_likelihood: boolean
        Whether to calculate gradient of the marginal likelihood
        of the state-space model. If true then the next parameter must
        provide the extra parameters for gradient calculation.

p_dm: 3D array (state_dim, time_series_no, parameters_no)
        Mean derivatives from the prediction step. For "multiple time series mode"
        it is 3D array, second dimension of which correspond to different
        time series.

p_dP: array
    Covariance derivatives from the prediction step.

Output:
----------------------------
m_upd, P_upd, dm_upd, dP_upd: metrices, 3D objects
    Results of the prediction steps.

log_likelihood_update: double or 1D array
    Update to the log_likelihood from this step

d_log_likelihood_update: (grad_params_no, time_series_no) matrix
    Update to the gradient of log_likelihood, "multiple time series mode"
    adds extra columns to the gradient.