def imf_calc(self): theta = self.const.theta_0 M = np.zeros((self.const.s, self.const.s)) for k in range(self.const.N): print(f"WE SURVIVED {k} ITERATIONS") self.const.a = np.dot(self.model.Psi(theta), self.const.U[k]) a_gr = self.a_gr(k) if k == 0: x_A_tk1 = np.reshape([np.dot(self.model.F(theta), self.model.mu_x(theta)) + self.const.a] + \ [np.dot(self.model.F_grad(theta, i), self.model.mu_x(theta)) + np.dot(self.model.F(theta), self.model.mu_x_grad(theta, i)) + a_gr[i] for i in range(self.const.s)], (self.const.s + 1, self.const.m)) sigma_A_tk1 = np.zeros((self.const.s + 1, self.const.s + 1)) E_x_hat = x_A_tk1 E_x_hat_x_hat_t = sigma_A_tk1 + np.dot(x_A_tk1, T(x_A_tk1)) for i in range(self.const.s): for j in range(self.const.s): M[i][j] += TCD(self.model.H_grad(theta, i), self.C(0), E_x_hat_x_hat_t, T(self.C(0)), T(self.model.H_grad(theta, j)), inv(self.kalman.B(theta))) + \ TCD(self.model.H_grad(theta, i), self.C(0), E_x_hat_x_hat_t, T(self.C(j)), T(self.model.H(theta)), inv(self.kalman.B(theta))) + \ TCD(self.model.H_grad(theta, i), self.C(0), E_x_hat, T(self.model.A_grad(theta, j)), inv(self.kalman.B(theta))) + \ TCD(self.model.H(theta), self.C(i), E_x_hat_x_hat_t, T(self.C(0)), T(self.model.H_grad(theta, j)), inv(self.kalman.B(theta))) + \ TCD(self.model.H(theta), self.C(i), E_x_hat_x_hat_t, T(self.C(j)), T(self.model.H(theta)), inv(self.kalman.B(theta))) + \ TCD(self.model.H(theta), self.C(i), E_x_hat, T(self.model.A_grad(theta, j)), inv(self.kalman.B(theta))) + \ TCD(self.model.A_grad(theta, i), T(E_x_hat), T(self.C(0)), T(self.model.H_grad(theta, i)), inv(self.kalman.B(theta))) + \ TCD(self.model.A_grad(theta, i), T(E_x_hat), T(self.C(j)), T(self.model.H(theta)), inv(self.kalman.B(theta))) + \ TCD(self.model.A_grad(theta, i), T(self.model.A_grad(theta, j)), inv(self.kalman.B(theta))) + \ 0.5 * TCD(self.kalman.B_tk1_grad(theta, i), inv(self.kalman.B(theta)), self.kalman.B_tk1_grad(theta, j), inv(self.kalman.B(theta))) if k != self.const.N - 1: x_A_tk1 = np.reshape( (T(np.dot(self.F_A_tk(), x_A_tk1)) + self.a_A_tk()), (self.const.s + 1, self.const.m)) sigma_A_tk1 = combo_dot( self.F_A_tk(), sigma_A_tk1, T(self.F_A_tk())) + combo_dot( self.K_A_tk(), self.kalman.B(theta), T(self.K_A_tk())) a = np.hstack(self.kalman.P_tk1_tk1(theta))[0] b = np.hstack([ self.kalman.P_tk1_tk1_grad(theta, i) for i in range(self.const.s) ])[0] self.kalman.model.P_tk_tk = lambda theta: a self.kalman.model.P_tk_tk_grad = lambda theta, i: b[i] return M
def K(self, theta): return np.dot(np.dot(self.P_tk1_tk(theta), T(self.model.H(theta))), inv(self.B(theta)))
def K_tk1_grad(self, theta, i): return np.dot(np.dot(self.P_tk1_tk_grad(theta, i), T(self.model.H(theta))) + np.dot(self.P_tk1_tk(theta), T(self.model.H_grad(theta, i))) - np.dot(np.dot(np.dot(self.P_tk1_tk(theta), T(self.model.H(theta))), inv(self.B(theta))), self.B_tk1_grad(theta, i)), inv(self.B(theta)))
def test_T_1(): assert np.array_equal(inv(2), 0.5)
def test_T_5(): assert np.array_equal(inv(npa), npa_inv)
def test_T_4(): assert np.array_equal(inv(a), a_inv)
def test_T_3(): assert np.array_equal(inv([1, 2]), [1, 0.5])
def test_T_2(): assert np.array_equal(inv([2]), [0.5])