示例#1
0
 def testInitializeEvaluatoin(self):
     self.builder1 = self.builder1 + self.trend + self.dynamic
     self.builder1.dynamicComponents['dynamic'].updateEvaluation(8)
     self.builder1.initialize()
     print(self.builder1.model.evaluation, mt.matrixAddByCol(
         self.trend.evaluation, self.dynamic.evaluation))
     self.assertAlmostEqual(np.sum(
         np.abs(self.builder1.model.evaluation -
                mt.matrixAddByCol(self.trend.evaluation,
                                  self.dynamic.evaluation))), 0.0)
示例#2
0
    def testInitialize(self):
        self.builder1 = self.builder1 + self.trend + self.dynamic \
                        + self.autoReg

        self.builder1.initialize()
        self.assertAlmostEqual(np.sum(
            np.abs(self.builder1.model.evaluation
                   - mt.matrixAddByCol(mt.matrixAddByCol(
                       self.trend.evaluation,
                       self.dynamic.evaluation),
                        self.autoReg.evaluation))), 0.0)
示例#3
0
    def testInitialize(self):
        self.builder1 = self.builder1 + self.trend + self.dynamic \
                        + self.autoReg

        self.builder1.initialize(data=self.data)
        self.assertAlmostEqual(np.sum(
            np.abs(self.builder1.model.evaluation
                   - mt.matrixAddByCol(mt.matrixAddByCol(
                       self.trend.evaluation,
                       self.dynamic.evaluation),
                        self.autoReg.evaluation))), 0.0)
示例#4
0
    def testUpdate(self):
        self.builder1 = self.builder1 + self.trend + self.dynamic \
                        + self.autoReg

        self.builder1.initialize()
        self.builder1.updateEvaluation(2)
        self.assertAlmostEqual(np.sum(
            np.abs(self.builder1.model.evaluation
                   - mt.matrixAddByCol(mt.matrixAddByCol(
                       self.trend.evaluation,
                       np.matrix([self.features[2]])),
                                       np.matrix(self.autoReg.features[2])))), 0.0)
示例#5
0
    def testUpdate(self):
        self.builder1 = self.builder1 + self.trend + self.dynamic \
                        + self.autoReg

        self.builder1.initialize(data=self.data)
        self.builder1.updateEvaluation(2, self.data)
        self.assertAlmostEqual(np.sum(
            np.abs(self.builder1.model.evaluation
                   - mt.matrixAddByCol(mt.matrixAddByCol(
                       self.trend.evaluation,
                       np.matrix([self.features[2]])),
                                       np.matrix(self.autoReg.evaluation)))), 0.0)
示例#6
0
 def testInitializeEvaluatoin(self):
     self.builder1 = self.builder1 + self.trend + self.dynamic
     self.builder1.dynamicComponents['dynamic'].updateEvaluation(8)
     self.builder1.initialize(data=self.data)
     self.assertAlmostEqual(np.sum(
         np.abs(self.builder1.model.evaluation -
                mt.matrixAddByCol(self.trend.evaluation,
                                  self.dynamic.evaluation))), 0.0)
示例#7
0
    def initialize(self, data=[], noise=1):
        """ Initialize the model. It construct the baseModel by assembling all
        quantities from the components.

        Args:
            noise: the initial guess of the variance of the observation noise.
        """
        if len(self.staticComponents) == 0 and \
           len(self.dynamicComponents) == 0 and \
           len(self.automaticComponents) == 0:

            raise NameError('The model must contain at least' +
                            ' one component')

        # construct transition, evaluation, prior state, prior covariance
        if self._printInfo:
            print('Initializing models...')
        transition = None
        evaluation = None
        state = None
        sysVar = None
        self.discount = np.array([])

        # first construct for the static components
        # the evaluation will be treated separately for static or dynamic
        # as the latter one will change over time
        currentIndex = 0  # used for compute the index
        for i in self.staticComponents:
            comp = self.staticComponents[i]
            transition = mt.matrixAddInDiag(transition, comp.transition)
            evaluation = mt.matrixAddByCol(evaluation,
                                           comp.evaluation)
            state = mt.matrixAddByRow(state, comp.meanPrior)
            sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
            self.discount = np.concatenate((self.discount, comp.discount))
            self.componentIndex[i] = (currentIndex, currentIndex + comp.d - 1)
            currentIndex += comp.d

        # if the model contains the dynamic part, we add the dynamic components
        if len(self.dynamicComponents) > 0:
            self.dynamicEvaluation = None
            for i in self.dynamicComponents:
                comp = self.dynamicComponents[i]
                comp.updateEvaluation(0)
                transition = mt.matrixAddInDiag(transition, comp.transition)
                evaluation = mt.matrixAddByCol(evaluation,
                                               comp.evaluation)
                state = mt.matrixAddByRow(state, comp.meanPrior)
                sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
                self.discount = np.concatenate((self.discount, comp.discount))
                self.componentIndex[i] = (currentIndex,
                                          currentIndex + comp.d - 1)
                currentIndex += comp.d

        # if the model contains the automatic dynamic part, we add
        # them to the builder
        if len(self.automaticComponents) > 0:
            self.automaticEvaluation = None
            for i in self.automaticComponents:
                comp = self.automaticComponents[i]
                comp.updateEvaluation(0, data)
                transition = mt.matrixAddInDiag(transition, comp.transition)
                evaluation = mt.matrixAddByCol(evaluation,
                                               comp.evaluation)
                state = mt.matrixAddByRow(state, comp.meanPrior)
                sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
                self.discount = np.concatenate((self.discount, comp.discount))
                self.componentIndex[i] = (currentIndex,
                                          currentIndex + comp.d - 1)
                currentIndex += comp.d

        self.statePrior = state
        self.sysVarPrior = sysVar
        self.noiseVar = np.matrix(noise)
        self.model = baseModel(transition=transition,
                               evaluation=evaluation,
                               noiseVar=np.matrix(noise),
                               sysVar=sysVar,
                               state=state,
                               df=self.initialDegreeFreedom)
        self.model.initializeObservation()

        # compute the renew period
        if self.renewDiscount is None:
            self.renewDiscount = np.min(self.discount)

        if self.renewDiscount < 1.0 - 1e-8:
            self.renewTerm = np.log(0.001 * (1 - self.renewDiscount)) \
                             / np.log(self.renewDiscount)

        self.initialized = True
        if self._printInfo:
            print('Initialization finished.')
示例#8
0
    def initialize(self, data=[], noise=1):
        """ Initialize the model. It construct the baseModel by assembling all
        quantities from the components.

        Args:
            noise: the initial guess of the variance of the observation noise.
        """
        if len(self.staticComponents) == 0 and \
           len(self.dynamicComponents) == 0 and \
           len(self.automaticComponents) == 0:

            raise NameError('The model must contain at least' +
                            ' one component')

        # construct transition, evaluation, prior state, prior covariance
        if self._printInfo:
            print('Initializing models...')
        transition = None
        evaluation = None
        state = None
        sysVar = None
        self.discount = np.array([])

        # first construct for the static components
        # the evaluation will be treated separately for static or dynamic
        # as the latter one will change over time
        currentIndex = 0  # used for compute the index
        for i in self.staticComponents:
            comp = self.staticComponents[i]
            transition = mt.matrixAddInDiag(transition, comp.transition)
            evaluation = mt.matrixAddByCol(evaluation, comp.evaluation)
            state = mt.matrixAddByRow(state, comp.meanPrior)
            sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
            self.discount = np.concatenate((self.discount, comp.discount))
            self.componentIndex[i] = (currentIndex, currentIndex + comp.d - 1)
            currentIndex += comp.d

        # if the model contains the dynamic part, we add the dynamic components
        if len(self.dynamicComponents) > 0:
            self.dynamicEvaluation = None
            for i in self.dynamicComponents:
                comp = self.dynamicComponents[i]
                comp.updateEvaluation(0)
                transition = mt.matrixAddInDiag(transition, comp.transition)
                evaluation = mt.matrixAddByCol(evaluation, comp.evaluation)
                state = mt.matrixAddByRow(state, comp.meanPrior)
                sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
                self.discount = np.concatenate((self.discount, comp.discount))
                self.componentIndex[i] = (currentIndex,
                                          currentIndex + comp.d - 1)
                currentIndex += comp.d

        # if the model contains the automatic dynamic part, we add
        # them to the builder
        if len(self.automaticComponents) > 0:
            self.automaticEvaluation = None
            for i in self.automaticComponents:
                comp = self.automaticComponents[i]
                comp.updateEvaluation(0, data)
                transition = mt.matrixAddInDiag(transition, comp.transition)
                evaluation = mt.matrixAddByCol(evaluation, comp.evaluation)
                state = mt.matrixAddByRow(state, comp.meanPrior)
                sysVar = mt.matrixAddInDiag(sysVar, comp.covPrior)
                self.discount = np.concatenate((self.discount, comp.discount))
                self.componentIndex[i] = (currentIndex,
                                          currentIndex + comp.d - 1)
                currentIndex += comp.d

        self.statePrior = state
        self.sysVarPrior = sysVar
        self.noiseVar = np.matrix(noise)
        self.model = baseModel(transition=transition,
                               evaluation=evaluation,
                               noiseVar=np.matrix(noise),
                               sysVar=sysVar,
                               state=state,
                               df=self.initialDegreeFreedom)
        self.model.initializeObservation()

        # compute the renew period
        if self.renewDiscount is None:
            self.renewDiscount = np.min(self.discount)

        if self.renewDiscount < 1.0 - 1e-8:
            self.renewTerm = np.log(0.001 * (1 - self.renewDiscount)) \
                             / np.log(self.renewDiscount)

        self.initialized = True
        if self._printInfo:
            print('Initialization finished.')