def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 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(range(100)) 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, data=range(100), 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')
def testAlter(self): dlm4 = dlm(self.data) dlm4 + trend(degree = 1, discount = 1) + 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 + trend(degree = 1, discount = 1) + 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 setUp(self): self.data = [0] * 9 + [1] + [0] * 10 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(range(100)) self.dlm6 = _dlm(range(100)) self.dlm1.builder + trend(degree=1, discount=1, w=1.0) self.dlm2.builder + trend(degree=1, discount=1e-12, w=1.0) self.dlm3.builder + seasonality(period=2, discount=1, w=1.0) self.dlm4.builder + dynamic( features=[[0] for i in range(5)] + [[1] for i in range(5)], discount=1, w=1.0) self.dlm5.builder + trend(degree=1, discount=1, w=1.0) + \ autoReg(degree=1, data=range(100), discount=1, w=1.0) self.dlm6.builder + trend(degree=1, discount=0.9, w=1.0) + \ seasonality(period=2, discount=0.8, w=1.0) + \ autoReg(degree=3, data=range(100), discount=1.0) self.dlm1._initialize() self.dlm2._initialize() self.dlm3._initialize() self.dlm4._initialize() self.dlm5._initialize() self.dlm6._initialize() self.dlm1.options.innovationType = 'whole' self.dlm2.options.innovationType = 'whole' self.dlm3.options.innovationType = 'whole' self.dlm4.options.innovationType = 'whole' self.dlm5.options.innovationType = 'whole' self.dlm6.options.innovationType = 'whole'
def setUp(self): self.kf1 = kalmanFilter(discount=[1]) self.kf0 = kalmanFilter(discount=[1e-10]) self.kf11 = kalmanFilter(discount=[1, 1]) self.trend0 = trend(degree=0, discount=1, w=1.0) self.trend0_90 = trend(degree=0, discount=0.9, w=1.0) self.trend0_98 = trend(degree=0, discount=0.98, w=1.0, name='a') self.trend1 = trend(degree=1, discount=1, w=1.0)
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.features = np.random.random((20, 2)).tolist() 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.dlm1 + trend(degree=1, discount=1) self.dlm2 + trend(degree=1, discount=1e-12) self.dlm3 + seasonality(period=2, discount=1)
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.dlm1 = _dlm(self.data) self.dlm2 = _dlm(self.data) self.dlm1.builder + trend(degree=0, discount=1, w=1.0) self.dlm2.builder + trend(degree=0, discount=1e-12, w=1.0) self.dlm1._initialize() self.dlm2._initialize() self.dlm1.options.innovationType='whole' self.dlm2.options.innovationType='whole'
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.dlm1 = _dlm(self.data) self.dlm2 = _dlm(self.data) self.dlm1.builder + trend(degree=0, discount=1, w=1.0) self.dlm2.builder + trend(degree=0, discount=1e-12, w=1.0) self.dlm1._initialize() self.dlm2._initialize() self.dlm1.options.innovationType = 'whole' self.dlm2.options.innovationType = 'whole'
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 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.dlm1.builder + trend(degree=1, discount=1) self.dlm2.builder + trend(degree=1, discount=1e-12) self.dlm3.builder + seasonality(period=2, discount=1) self.dlm1._initialize() self.dlm2._initialize() self.dlm3._initialize()
def testEvolveMode(self): dlm = builder() dlm.add(trend(degree=1, discount=0.9, w=1.0)) dlm.add(trend(degree=1, discount=0.98, name='a', w=1.0)) dlm.initialize() kf2 = kalmanFilter(discount=[0.9, 0.98], updateInnovation='component', index=dlm.componentIndex) kf2.forwardFilter(dlm.model, 1.0) self.assertAlmostEqual(dlm.model.innovation[0, 1], 0.0) self.assertAlmostEqual(dlm.model.innovation[1, 0], 0.0)
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 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 setUp(self): self.data5 = range(100) self.dlm5 = _dlmGet(self.data5) self.dlm5.builder + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=1, discount=1, w=1.0) self.dlm5._initialize() self.dlm5.options.innovationType = 'whole'
def testAppendAutomatic(self): # we feed the data to dlm4 via two segments dlm4 = dlm(self.data[0:11]) dlm4 + trend(degree=1, discount=1) + autoReg( degree=3, data=self.data[0:11], discount=1) dlm4.fitForwardFilter() dlm4.append(self.data[11:20]) dlm4.fitForwardFilter() # we feed the data to dlm5 all at once dlm5 = dlm(self.data) dlm5 + trend(degree=1, discount=1) + autoReg( degree=3, data=self.data, discount=1) dlm5.fitForwardFilter() self.assertAlmostEqual(np.sum(np.array(dlm4.result.filteredObs) - \ np.array(dlm5.result.filteredObs)), 0.0)
def setUp(self): self.data5 = range(100) self.dlm5 = _dlmGet(self.data5) self.dlm5.builder + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=1, discount=1, w=1.0) self.dlm5._initialize() self.dlm5.options.innovationType='whole'
def testForwardFilter(self): dlm = builder() dlm.add(trend(degree=1, discount=1, w=1.0)) dlm.initialize() self.kf1.predict(dlm.model) self.assertAlmostEqual(dlm.model.prediction.obs, 0) # the prior on the mean is zero, but observe 1, with # discount = 1, one should expect the filterd mean to be 0.5 self.kf1.forwardFilter(dlm.model, 1) self.assertAlmostEqual(dlm.model.obs, 0.5) self.assertAlmostEqual(dlm.model.prediction.obs, 0) self.assertAlmostEqual(dlm.model.sysVar, 0.375) self.kf1.predict(dlm.model) self.assertAlmostEqual(dlm.model.obs, 0.5) self.assertAlmostEqual(dlm.model.prediction.obs, 0.5) dlm.initialize() self.kf0.predict(dlm.model) self.assertAlmostEqual(dlm.model.prediction.obs, 0) # the prior on the mean is zero, but observe 1, with discount = 0 # one should expect the filtered mean close to 1 self.kf0.forwardFilter(dlm.model, 1) self.assertAlmostEqual(dlm.model.obs[0, 0], 1) self.assertAlmostEqual(dlm.model.prediction.obs[0, 0], 0) self.assertAlmostEqual(dlm.model.sysVar[0, 0], 0.5) self.kf0.predict(dlm.model) self.assertAlmostEqual(dlm.model.obs[0, 0], 1) self.assertAlmostEqual(dlm.model.prediction.obs[0, 0], 1)
def setUp(self): self.data = np.random.rand(10).tolist() self.features = np.random.rand(10, 2).tolist() self.trend = trend(degree=2, w=1.0) self.seasonality = seasonality(period=7, w=1.0) self.dynamic = dynamic(self.features, w=1.0) self.autoReg = autoReg(degree=3, w=1.0) self.builder1 = builder()
def testAppendDynamic(self): # we feed the data to dlm4 via two segments dlm4 = dlm(self.data[0:11]) dlm4 + trend(degree = 1, discount = 1) + 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 + trend(degree = 1, discount = 1) + 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 testMissingEvaluation(self): dlm = builder() dlm.add(trend(degree=1, discount=1, w=1.0)) dlm.initialize() dlm.model.evaluation = np.matrix([[None]]) self.kf1.forwardFilter(dlm.model, 1.0, dealWithMissingEvaluation = True) self.assertAlmostEqual(dlm.model.obs, 0.0) self.assertAlmostEqual(dlm.model.transition, 1.0)
def testAdd(self): trend2 = trend(2, name='trend2') self.dlm1 = self.dlm1 + trend2 self.assertEqual(self.dlm1.builder.staticComponents['trend2'], trend2) dynamic2 = dynamic(features=self.features, name='d2') self.dlm1 = self.dlm1 + dynamic2 self.assertEqual(self.dlm1.builder.dynamicComponents['d2'], dynamic2) ar3 = autoReg(degree=3, data=self.data, name='ar3') self.dlm1 = self.dlm1 + ar3 self.assertEqual(self.dlm1.builder.automaticComponents['ar3'], ar3)
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.data5 = range(100) self.dlm1 = _dlmTune(self.data) self.dlm2 = _dlmTune(self.data) self.dlm6 = _dlmTune(self.data5) self.dlm7 = _dlmTune([0, 1, None, 1, 0, 1, -1]) self.dlm1.builder + trend(degree=0, discount=1, w=1.0) self.dlm2.builder + trend(degree=0, discount=1e-12, w=1.0) self.dlm6.builder + trend(degree=0, discount=0.9, w=1.0) + \ seasonality(period=2, discount=0.8, w=1.0) + \ autoReg(degree=3, discount=1.0) self.dlm7.builder + trend(degree=0, discount=1, w=1.0) self.dlm1._initialize() self.dlm2._initialize() self.dlm6._initialize() self.dlm7._initialize() self.dlm1.options.innovationType='whole' self.dlm2.options.innovationType='whole' self.dlm6.options.innovationType='whole' self.dlm7.options.innovationType='whole'
def testAdd(self): trend2 = trend(2, name='trend2') self.dlm1 = self.dlm1 + trend2 self.assertEqual(self.dlm1.builder.staticComponents['trend2'], trend2) dynamic2 = dynamic(features=self.features, name='d2') self.dlm1 = self.dlm1 + dynamic2 self.assertEqual(self.dlm1.builder.dynamicComponents['d2'], dynamic2) ar3 = autoReg(degree=3, name='ar3') self.dlm1 = self.dlm1 + ar3 self.assertEqual(self.dlm1.builder.automaticComponents['ar3'], ar3)
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 + trend(degree=1, discount=1) 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)
def testPopout(self): dlm4 = dlm(self.data) dlm4 + trend(degree = 1, discount = 1) + 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 + trend(degree = 1, discount = 1) + 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 testBackwardSmoother(self): dlm = builder() dlm.add(trend(degree=1, discount=1, w=1.0)) dlm.initialize() # with mean being 0 and observe 1 and 0 consectively, one shall # expect the smoothed mean at 1 will be 1/3, for discount = 1 self.kf1.forwardFilter(dlm.model, 1) self.kf1.forwardFilter(dlm.model, 0) self.kf1.backwardSmoother(dlm.model, \ np.matrix([[0.5]]), \ np.matrix([[0.375]])) self.assertAlmostEqual(dlm.model.obs[0, 0], 1.0/3) self.assertAlmostEqual(dlm.model.sysVar[0, 0], 0.18518519)
def testBackwardSmootherMultiDim(self): dlm = builder() dlm.add(trend(degree=2, discount=1, w=1.0)) dlm.initialize() self.kf11.forwardFilter(dlm.model, 1) state1 = dlm.model.state cov1 = dlm.model.sysVar self.kf11.forwardFilter(dlm.model, -1) self.kf11.backwardSmoother(dlm.model, \ rawState = state1, \ rawSysVar = cov1) self.assertAlmostEqual(dlm.model.obs[0, 0], 0.0)
def testMissingData(self): dlm = builder() dlm.add(trend(degree=1, discount=1, w=1.0)) dlm.initialize() self.kf0.forwardFilter(dlm.model, 1) self.assertAlmostEqual(dlm.model.obs[0, 0], 1.0) self.assertAlmostEqual(dlm.model.obsVar[0, 0], 1.0) self.kf0.forwardFilter(dlm.model, None) self.assertAlmostEqual(dlm.model.obs[0, 0], 1.0) self.assertAlmostEqual(dlm.model.obsVar[0, 0]/1e10, 0.5) self.kf0.forwardFilter(dlm.model, None) self.assertAlmostEqual(dlm.model.obs[0, 0], 1.0) self.assertAlmostEqual(dlm.model.obsVar[0, 0]/1e10, 0.5) self.kf0.forwardFilter(dlm.model, 0) self.assertAlmostEqual(dlm.model.obs[0, 0], 0.0)
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.data5 = range(100) self.dlm3 = _dlmPredict([-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]) self.dlm4 = _dlmPredict([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) self.dlm5 = _dlmPredict(self.data5) self.dlm3.builder + seasonality(period=2, discount=1, w=1.0) self.dlm4.builder + dynamic(features=[[0] for i in range(5)] + [[1] for i in range(5)], discount=1, w=1.0) self.dlm5.builder + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=1, discount=1, w=1.0) self.dlm3._initialize() self.dlm4._initialize() self.dlm5._initialize() self.dlm3.options.innovationType='whole' self.dlm4.options.innovationType='whole' self.dlm5.options.innovationType='whole'
def setUp(self): self.data = [0] * 9 + [1] + [0] * 10 self.data5 = range(100) self.dlm3 = _dlmPredict([-1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]) self.dlm4 = _dlmPredict([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) self.dlm5 = _dlmPredict(self.data5) self.dlm3.builder + seasonality(period=2, discount=1, w=1.0) self.dlm4.builder + dynamic( features=[[0] for i in range(5)] + [[1] for i in range(5)], discount=1, w=1.0) self.dlm5.builder + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=1, discount=1, w=1.0) self.dlm3._initialize() self.dlm4._initialize() self.dlm5._initialize() self.dlm3.options.innovationType = 'whole' self.dlm4.options.innovationType = 'whole' self.dlm5.options.innovationType = 'whole'
def testDelete(self): trend2 = trend(2, name='trend2') self.dlm1 = self.dlm1 + trend2 self.dlm1.delete('trend2') self.assertEqual(len(self.dlm1.builder.staticComponents), 1)
def setUp(self): self.features = np.random.rand(10, 2).tolist() self.trend = trend(degree = 3) self.seasonality = seasonality(period = 7) self.dynamic = dynamic(self.features) self.builder1 = builder()
def setUp(self): self.mydlm = dlm(np.random.random(100)) + trend(2, discount=0.95) self.mytuner = modelTuner()
def testPlot(self): dlm1 = dlm(range(100)) + trend(1) dlm1.fit() dlm1.plot()