Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍
https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
2.0 KiB
48 lines
2.0 KiB
4 years ago
|
import sys
|
||
4 years ago
|
sys.path.append("../../classes/")
|
||
4 years ago
|
import unittest
|
||
|
import numpy as np
|
||
|
|
||
4 years ago
|
import structure_graph.conditional_intensity_matrix as cim
|
||
4 years ago
|
|
||
|
|
||
|
class TestConditionalIntensityMatrix(unittest.TestCase):
|
||
|
|
||
|
@classmethod
|
||
|
def setUpClass(cls) -> None:
|
||
|
cls.state_res_times = np.random.rand(1, 3)[0]
|
||
|
cls.state_res_times = cls.state_res_times * 1000
|
||
|
cls.state_transition_matrix = np.random.randint(1, 10000, (3, 3))
|
||
|
for i in range(0, len(cls.state_res_times)):
|
||
|
cls.state_transition_matrix[i, i] = 0
|
||
|
cls.state_transition_matrix[i, i] = np.sum(cls.state_transition_matrix[i])
|
||
|
|
||
|
def test_init(self):
|
||
|
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
|
||
|
self.assertTrue(np.array_equal(self.state_res_times, c1.state_residence_times))
|
||
|
self.assertTrue(np.array_equal(self.state_transition_matrix, c1.state_transition_matrix))
|
||
|
self.assertEqual(c1.cim.dtype, np.float)
|
||
|
self.assertEqual(self.state_transition_matrix.shape, c1.cim.shape)
|
||
|
|
||
|
def test_compute_cim_coefficients(self):
|
||
|
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
|
||
|
c2 = self.state_transition_matrix.astype(np.float)
|
||
|
np.fill_diagonal(c2, c2.diagonal() * -1)
|
||
|
for i in range(0, len(self.state_res_times)):
|
||
|
for j in range(0, len(self.state_res_times)):
|
||
|
c2[i, j] = (c2[i, j] + 1) / (self.state_res_times[i] + 1)
|
||
|
c1.compute_cim_coefficients()
|
||
|
for i in range(0, len(c1.state_residence_times)):
|
||
|
self.assertTrue(np.isclose(np.sum(c1.cim[i]), 0.0, 1e-02, 1e-01))
|
||
|
for i in range(0, len(self.state_res_times)):
|
||
|
for j in range(0, len(self.state_res_times)):
|
||
|
self.assertTrue(np.isclose(c1.cim[i, j], c2[i, j], 1e-02, 1e-01))
|
||
|
|
||
4 years ago
|
def test_repr(self):
|
||
|
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
|
||
|
print(c1)
|
||
|
|
||
4 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|