def initializeFromBuilder(self, data, exported_builder): # Copy the components self.staticComponents = deepcopy(exported_builder.staticComponents) self.automaticComponents = deepcopy( exported_builder.automaticComponents) self.dynamicComponents = deepcopy(exported_builder.dynamicComponents) self.componentIndex = deepcopy(exported_builder.componentIndex) self.discount = deepcopy(exported_builder.discount) self.initialDegreeFreedom = exported_builder.model.df # Copy the model states self.statePrior = exported_builder.model.state self.sysVarPrior = exported_builder.model.sysVar self.noiseVar = exported_builder.model.noiseVar self.transition = exported_builder.transition self.evaluation = exported_builder.evaluation # update the evaluation to the current. self.updateEvaluation(step=0, data=data) self.model = baseModel(transition=self.transition, evaluation=self.evaluation, noiseVar=self.noiseVar, sysVar=self.sysVarPrior, state=self.statePrior, 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.initilized = True self.initialized = True if self._printInfo: print('Initialization finished.')
def initializeFromBuilder(self, data, exported_builder): # Copy the components self.staticComponents = deepcopy(exported_builder.staticComponents) self.automaticComponents = deepcopy(exported_builder.automaticComponents) self.dynamicComponents = deepcopy(exported_builder.dynamicComponents) self.componentIndex = deepcopy(exported_builder.componentIndex) self.discount = deepcopy(exported_builder.discount) self.initialDegreeFreedom = exported_builder.model.df # Copy the model states self.statePrior = exported_builder.model.state self.sysVarPrior = exported_builder.model.sysVar self.noiseVar = exported_builder.model.noiseVar self.transition = exported_builder.transition self.evaluation = exported_builder.evaluation # update the evaluation to the current. self.updateEvaluation(step=0, data=data) self.model = baseModel(transition=self.transition, evaluation=self.evaluation, noiseVar=self.noiseVar, sysVar=self.sysVarPrior, state=self.statePrior, 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.')
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.')