def testAlter(self): dlm4 = dlm(self.data) dlm4 + self.trend1 + dynamic(features=self.features, discount=1) dlm4.fitForwardFilter() # the filtered step range should be (0, 19) self.assertEqual(dlm4.result.filteredSteps, [0, 19]) dlm4.alter(date=15, data=1, component='main') self.assertEqual(dlm4.result.filteredSteps, [0, 14]) dlm4.fitForwardFilter() newData = [0] * 9 + [1] + [0] * 10 newData[15] = 1 dlm5 = dlm(newData) dlm5 + self.trend1 + dynamic(features=self.features, discount=1) dlm5.fitForwardFilter() # The two chain should have the same filtered obs self.assertAlmostEqual(np.sum(np.array(dlm4.result.filteredObs) - \ np.array(dlm5.result.filteredObs)), 0.0) # test alter the feature dlm4.alter(date=0, data=[1, 1], component='dynamic') self.assertAlmostEqual( dlm4.builder.dynamicComponents['dynamic'].features[0], [1, 1])
def testPredictionNotChangeModel(self): timeSeries = [1, 2, 1, 5, 3, 5, 4, 8, 1, 2] dlm1 = dlm(timeSeries) + trend(degree=2, discount=0.95) dlm1.fitForwardFilter() (obs1, var1) = dlm1.predictN(N=1, date=dlm1.n - 1) dlm2 = dlm([]) + trend(degree=2, discount=0.95) for d in timeSeries: dlm2.append([d], component='main') dlm2.fitForwardFilter() (obs2, var2) = dlm2.predictN(N=1, date=dlm2.n - 1) self.assertAlmostEqual(obs1, obs2) self.assertAlmostEqual(var1, var2)
def testAppendDynamic(self): # we feed the data to dlm4 via two segments dlm4 = dlm(self.data[0:11]) dlm4 + self.trend1 + dynamic(features=self.features[0:11], discount=1) dlm4.fitForwardFilter() dlm4.append(self.data[11:20]) dlm4.append(self.features[11:20], component='dynamic') dlm4.fitForwardFilter() # we feed the data to dlm5 all at once dlm5 = dlm(self.data) dlm5 + self.trend1 + dynamic(features=self.features, discount=1) dlm5.fitForwardFilter() self.assertAlmostEqual( np.sum( np.array(dlm4.result.filteredObs) - np.array(dlm5.result.filteredObs)), 0.0)
def testPopout(self): dlm4 = dlm(self.data) dlm4 + self.trend1 + dynamic(features=self.features, discount=1) dlm4.fitForwardFilter() # the filtered step range should be (0, 19) self.assertEqual(dlm4.result.filteredSteps, [0, 19]) # pop out the first date, the filtered range should be (0, -1) dlm4.popout(0) self.assertEqual(dlm4.result.filteredSteps, [0, -1]) dlm4.fitForwardFilter() dlm5 = dlm(self.data[1:20]) dlm5 + self.trend1 + dynamic(features=self.features[1:20], discount=1) dlm5.fitForwardFilter() # The two chain should have the same filtered obs self.assertAlmostEqual( np.sum( np.array(dlm4.result.filteredObs) - np.array(dlm5.result.filteredObs)), 0.0)
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.data5 = range(100) self.features = np.random.random((20, 2)).tolist() self.trend0 = trend(degree=0, discount=1.0, w=1.0) self.trend1 = trend(degree=0, discount=1.0) self.dlm1 = dlm(self.data) self.dlm2 = dlm(self.data) self.dlm3 = dlm([-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]) self.dlm4 = dlm([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) self.dlm5 = dlm(self.data5) self.dlm6 = dlm(self.data5) self.dlm1 + trend(degree=0, discount=1, w=1.0) self.dlm2 + trend(degree=0, discount=1e-12, w=1.0) self.dlm3 + seasonality(period=2, discount=1, w=1.0) self.dlm4 + dynamic(features=[[0] for i in range(5)] + [[1] for i in range(5)], discount=1, w=1.0) self.dlm5 + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=1, discount=1, w=1.0) self.dlm6 + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=2, discount=1, w=1.0) self.dlm1.evolveMode('dependent') self.dlm2.evolveMode('dependent') self.dlm3.evolveMode('dependent') self.dlm4.evolveMode('dependent') self.dlm5.evolveMode('dependent') self.dlm6.evolveMode('dependent')
def testAppend(self): dlm4 = dlm(self.data[0:11]) dlm4 + self.trend0 dlm4.evolveMode('dependent') dlm4.fitForwardFilter() self.assertEqual(dlm4.n, 11) dlm4.append(self.data[11:20]) self.assertEqual(dlm4.n, 20) dlm4.fitForwardFilter() self.dlm1.fitForwardFilter() self.assertAlmostEqual( np.sum( np.array(dlm4.result.filteredObs) - np.array(self.dlm1.result.filteredObs)), 0.0)