def test_smoother_ref_traj2_1(): """ test_smoother_ref_traj2_1 """ traj = simple_traj2() theta = np.array([1.0, -1.0]) smoother_ref = TrajectorySmootherRef(traj, theta) smoother_ref.computeProbs() smoother_1 = TrajectorySmoother1(traj, theta) smoother_1.computeProbs() check_probs(smoother_1, smoother_ref)
def test_Z2(): """ Test of implementation 2. """ traj = simple_traj2() theta = np.array([0.0, -1.0]) choices = [0, 1] elts = LearningElements(traj, theta, choices) elts.computeLogZ() elts_ref = LearningElementsRef(traj, theta, choices) elts_ref.computeLogZ() assert(within(elts.logZ, elts_ref.logZ, 1e-5))
def test_filter_ref_traj2_1(): """ test_filter_ref_traj2_1 """ traj = simple_traj2() theta = np.array([-1.0, 1.0]) for k in range(traj.L): filter_ref = TrajectoryFilterRef(traj, theta, k) filter_ref.computeProbs() filter_1 = TrajectoryFilter1(traj, theta, k) filter_1.computeProbs() check_probs(filter_1, filter_ref)
def test_grad_Z2(): """ Test of implementation 2 of gradient. """ traj = simple_traj2() theta = np.array([0.0, -1.0]) choices = [0, 1] elts = LearningElements(traj, theta, choices) elts.computeGradientLogZ() elts_ref = LearningElementsRef(traj, theta, choices) elts_ref.computeGradientLogZ() g = elts.grad_logZ g_ref = elts_ref.grad_logZ assert(np.abs(g - g_ref).max() < 1e-3), (g, g_ref)
def test_hess_traj2_1(): """ test_hess_traj1_1 """ traj = simple_traj2() theta = np.array([0.0, -1.0]) choices = [0, 1] elts = LearningElements(traj, theta, choices) elts.computeHessianLogZ() elts_ref = LearningElementsRef(traj, theta, choices) elts_ref.computeHessianLogZ() h = elts.hess_logZ h_ref = elts_ref.hess_logZ assert(np.abs(h - h_ref).max() < 1e-3), (h, h_ref)
def test_viterbi_1_2(): """ test_viterbi_1_2 """ traj = simple_traj2() theta = np.array([1.0, -1.0]) viterbi_ref = TrajectoryViterbiRef(traj, theta) viterbi_ref.computeMostLikely() viterbi_1 = TrajectoryViterbi1(traj, theta) viterbi_1.computeMostLikely() assert len(viterbi_1.most_likely) == traj.L for l in range(traj.L): assert viterbi_1.most_likely[l] == viterbi_ref.most_likely[l] assert traj.num_choices[l] == len(viterbi_ref.most_likely_tree[l]) for i in range(traj.num_choices[l]): assert viterbi_ref.most_likely_tree[l][i] == \ viterbi_1.most_likely_tree[l][i]
def test_filter_ref_2(): """ test_filter_ref_2 """ traj = simple_traj2() theta = np.array([1.0, -1.0]) filter_0 = TrajectoryFilterRef(traj, theta, 0) filter_0.computeProbs() # The forward probabilities should equal the probabilities check_prob_fields(filter_0.forward, filter_0.probabilities) # Run the filter in inneficient smooting mode filter_L = TrajectoryFilterRef(traj, theta, traj.L) filter_L.computeProbs() smoother = TrajectorySmootherRef(traj, theta) smoother.computeProbs() check_prob_fields(filter_L.forward, smoother.forward) check_prob_fields(filter_L.backward, smoother.backward) check_prob_fields(filter_L.probabilities, smoother.probabilities)