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 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]) # pop out the first date, the filtered range should be (0, -1) 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)
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 _copyToFeatures(self, name, filterType): current = self.dlms[name] # if the feature has not been created, # we need to first add it as a feature if 'mvdlmFeatures' not in current.dynamicComponents: # initialize the new feature newFeature = [[0] * (len(self.order) - 1) for i in range(self.n)] current.add( dynamic(features=newFeature, name='mvdlmFeatures', discount=1.0)) # fetch the features theFeature = current.dynamicComponents['mvdlmFeatures'].features # update the features for i in range(self.n): count = 0 for otherdlm in self.order: if otherdlm != name: if filterType == 'forwardFilter': theFeature[i][count] = self.dlms[otherdlm] \ .result.filteredObs[i] elif filterType == 'backwardSmoother': theFeature[i][count] = self.dlms[otherdlm] \ .result.smoothedObs[i] elif filterType == 'original': if self.dlms[otherdlm].data[i] is not None: theFeature[i][count] = self.dlms[otherdlm].data[i] else: theFeature[i][count] = 0.0 count += 1
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 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.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.dlm6 = 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.dlm6 + trend(degree=0, discount=1, w=1.0) + \ autoReg(degree=2, 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') self.dlm6.evolveMode('dependent')
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 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 + 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 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 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.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 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.features = np.matrix(np.random.rand(10, 2)).tolist() self.newDynamic = dynamic(features=self.features, w=1.0)
def testInputNumpyMatrix(self): dynamic(features=np.random.rand(10, 2), w=1.0) pass