示例#1
0
    def test_cross_function_d(self):
        """ Test case d: outer-producting a vector and a sequence of vectors:
        Options:
        - First vector is a Categorical, second sequence of vectors is a numpy ndarray
        (dtype = object)
        - First vector is a Categorical, second sequence of vectors is a Categorical
        (where self.IS_AOA = True))
        """
        array_path = os.path.join(os.getcwd(), DATA_PATH + "cross_d.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        random_vec = Categorical(values=mat_contents["random_vec"])
        states = mat_contents["s"]
        for i in range(len(states)):
            states[i] = states[i].squeeze()

        # First way, where first array is a Categorical, second array is a numpy ndarray
        # (dtype = object)
        result_1a_py = random_vec.cross(states, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1a_py).all())

        # Second way, where first array is a Categorical, second array is a Categorical
        # (where self.IS_AOA = True)
        states = Categorical(values=states[0])
        result_1b_py = random_vec.cross(states, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1b_py).all())
示例#2
0
    def test_cross_function_b(self):
        """Test case b: outer-producting two vectors together:
        Options:
        - both vectors are stored in single Categorical (with two entries, where self.AoA == True)
        - first vector is a Categorical (self.AoA = False) and second array is a numpy ndarray
        (non-object array)
        - first vector is a Categorical, second vector is also Categorical
        """
        array_path = os.path.join(os.getcwd(), DATA_PATH + "cross_b.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]

        # first way, where both arrays as stored as two entries in a single AoA Categorical
        states = Categorical(values=mat_contents["s"][0])
        result_1_py = states.cross(return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        # second way (type 1), where first array is a Categorical, second array is a
        # straight numpy array
        states_first_factor = Categorical(values=mat_contents["s"][0][0])
        states_second_factor = mat_contents["s"][0][1]
        result_2a_py = states_first_factor.cross(states_second_factor,
                                                 return_numpy=True)
        self.assertTrue(np.isclose(result_2, result_2a_py).all())

        # second way (type 2), where first array is a Categorical, second array
        # is another Categorical
        states_first_factor = Categorical(values=mat_contents["s"][0][0])
        states_second_factor = Categorical(values=mat_contents["s"][0][1])
        result_2b_py = states_first_factor.cross(states_second_factor,
                                                 return_numpy=True)
        self.assertTrue(np.isclose(result_2, result_2b_py).all())
示例#3
0
    def test_cross_function_e(self):
        """Test case e: outer-producting two sequences of vectors:
        Options:
        - First sequence is an AoA Categorical, second sequence is a numpy ndarray
        (dtype = object)
        - First sequence is NOT an AoA Categorical, second sequence is a numpy ndarray
        (dtype = object)
        - First sequence is an AoA Categorical, second sequence is an AoA Categorical
        - First sequence is NOT an AoA Categorical, second sequence is an AoA Categorical
        """
        array_path = os.path.join(os.getcwd(), DATA_PATH + "cross_e.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]

        s2 = mat_contents["s2"]
        s2_new = np.empty(s2.shape[1], dtype=object)
        for i in range(len(s2_new)):
            s2_new[i] = s2[0][i].squeeze()

        # First way (type 1), first sequence is a Categorical (self.AOA = True) and
        # second sequence is a numpy ndarray (dtype = object)
        s1 = Categorical(values=mat_contents["s1"][0])
        result_1aa_py = s1.cross(s2_new, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1aa_py).all())

        # First way (type 2), first sequence is a Categorical (self.AOA = False) and
        # second sequence is a numpy ndarray (dtype = object)
        s1 = Categorical(values=mat_contents["s1"][0][0])
        result_1ab_py = s1.cross(s2_new, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1ab_py).all())

        s2_new = Categorical(values=mat_contents["s2"][0])
        # Second way (type 1), first sequence is a Categorical (self.AOA = True)
        # and second sequence is a Categorical
        s1 = Categorical(values=mat_contents["s1"][0])
        result_2aa_py = s1.cross(s2_new, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_2aa_py).all())

        # Second way (type 2), first sequence is a Categorical (self.AOA = False)
        # and second sequence is a Categorical
        s1 = Categorical(values=mat_contents["s1"][0][0])
        result_2ab_py = s1.cross(s2_new, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_2ab_py).all())
示例#4
0
    def test_cross_function_a(self):
        """Test case a: passing in a single-factor hidden state vector -
        under both options of Categorical: where self.AOA is True or False """
        array_path = os.path.join(os.getcwd(), DATA_PATH + "cross_a.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]

        states = np.empty(1, dtype=object)
        states[0] = mat_contents["s"][0, 0].squeeze()
        # This would create a 1-dimensional array of arrays (namely, self.IS_AOA == True)
        states = Categorical(values=states)
        result_1_py = states.cross(return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        # this creates a simple single-factor Categorical (namely, self.IS_AOA == False)
        states = Categorical(values=mat_contents["s"][0, 0].squeeze())
        result_2_py = states.cross(return_numpy=True)
        self.assertTrue(np.isclose(result_2, result_2_py).all())
示例#5
0
    def test_cross_function_c(self):
        """Test case c: outer-producting a vector and a matrix together:
        Options:
        - First vector is a Categorical, and the matrix argument is a numpy ndarray
        (non-object array)
        - First vector is a Categorical, and the matrix argument is also a Categorical
        """
        array_path = os.path.join(os.getcwd(), DATA_PATH + "cross_c.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        random_vec = Categorical(values=mat_contents["random_vec"])

        # first way, where first array is a Categorical, second array is a numpy ndarray
        random_matrix = mat_contents["random_matrix"]
        result_1a_py = random_vec.cross(random_matrix, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1a_py).all())

        # second way, where first array is a Categorical, second array is a Categorical
        random_matrix = Categorical(values=mat_contents["random_matrix"])
        result_1b_py = random_vec.cross(random_matrix, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1b_py).all())
示例#6
0
    qs_past = qs.copy()

    s_t = env.set_state(s[t + 1])

    # evoke observation, given the state
    obs = gp_likelihood[:, s_t]

    # turn observation into an index
    obs = np.where(obs)[0][0]

    # get transition likelihood
    B = b.mean()

    # infer new hidden state
    qs = maths.softmax(A[obs, :].log() + B.dot(qs_past).log(),
                       return_numpy=False)

    # update beliefs about the transition likelihood (i.e. 'learning')

    b += qs.cross(qs_past, return_numpy=True)  # update transition likelihood

    # print information
    print("Time step {} Location {}".format(t, env.state))

    # env.render()
    plot_beliefs(qs, "Beliefs (Qs) at time {}".format(t))

B = b.mean()
plot_likelihood(B, 'Final transition likelihood')