def calculateMostLikely(self): """ Calculates the indices of the most likely trajectory. The result alternates between point indices and path indices and correspond to the candidate points and candidate paths. """ # a LearningTrajectory is the basic data structure that stores the # features (scores candidate states candidate paths), and transitions # (indices to look up those states or paths). traj = LearningTrajectory(self.features, self.transitions) # The viterbi is a specific algorithm that calculates the most likely # states and most likley paths. The key output are the indices noted # below, which can be used to look up the specific candidate states # and candidate paths (although those must be stored externally. # There is one index for each feature. viterbi = TrajectoryViterbi1(traj, self.THETA) try: viterbi.computeAssignments() except (ValueError): for f in self.features: print(f) for t in self.transitions: print(t) print(self.THETA) raise # The indexes of the most likely elements of the trajectory # Point indexes and path indexes are interleaved. self.most_likely_indices = viterbi.assignments
def test_viterbi_1_7(): """ test_viterbi_1_7 """ traj = simple_traj7() theta = np.array([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]
Note: these weights have not be tuned in any way and are here for demonstration only. """ # NOTES (gde): # # theta determines the relative weights of the pathscores versus # the pointscores. What are recommended values? # Also beware of units! theta = np.array([1, 1]) """ Viterbi filter (most likely elements) """ # NOTES (gde): # # The viterbi is a specific algorithm that calculates the most likely # states and most likley paths. The key output are the indices noted # below, which can be used to look up the specific candidate states # and candidate paths (although those must be stored externally. # There is one index for each feature. viterbi = TrajectoryViterbi1(traj, theta) viterbi.computeAssignments() # The indexes of the most likely elements of the trajectory # Point indexes and path indexes are interleaved. most_likely_indexes = viterbi.assignments """ Alpha-beta smoother (distributions) """ # NOTES (gde): # # The smoother is a different algorithm that gives probabilities instead # of the most likley. smoother = TrajectorySmoother1(traj, theta) smoother.computeProbabilities() probabilities = smoother.probabilities