class_docstr: The main modeling part of a dynamic linear model. It allows the users to
  custemize their own model. User can add, delete any components like trend
  or seasonality to the builder, or view the existing components. Builder
  will finally assemble all components to a big model for further training
  and inference.
  
  Attributes:
      model: the model structure from @baseModel, stores all the necessary
      quantities initialized: indicates whether the model has been built
      staticComponents: stores all the static components (trend and
                        seasonality)
      dynamicComponents: stores all the dynamic components
      componentIndex: the location of each component in the latent states
      statePrior: the prior mean of the latent state
      sysVarPrior: the prior of the covariance of the latent states
      noiseVar: the prior of the observation noise
      initialDegreeFreedom: the initial degree of freedom.
      discount: the discounting factor, please refer to @kalmanFilter for
                more details
      renewTerm: used for aiding the stability of the model. The renewTerm
                 is computed according to the discount fact. When the filter
                 goes over certain steps, the information contribution of the
                 previous data has decayed to minimum. We then ignore those
                 days and refit the time series starting from current -
                 renewTerm. Thus, the effective sample size of the dlm is
                 twice renewTerm. When discount = 1, there will be no
                 renewTerm, since all the information will be passed along.
      renewDiscount: the minimum discount of seasonality component,
                     or if there is no seasonality, this will be the minimum
                     discount of all components. Used for computing
                     renewTerm.
  
  Methods:
      add: add new component
      ls:  list out all components
      delete: delete a specific component by its name
      initialize: assemble all the component to construt a big model
      updateEvaluation: update the valuation matrix of the big model
class_docstr_tok: ["The","main","modeling","part","of","a","dynamic","linear","model",".","It","allows","the","users","to","custemize","their","own","model",".","User","can","add",",","delete","any","components","like","trend","or","seasonality","to","the","builder",",","or","view","the","existing","components",".","Builder","will","finally","assemble","all","components","to","a","big","model","for","further","training","and","inference",".","Attributes",":","model",":","the","model","structure","from","@","baseModel",",","stores","all","the","necessary","quantities","initialized",":","indicates","whether","the","model","has","been","built","staticComponents",":","stores","all","the","static","components","(","trend","and","seasonality",")","dynamicComponents",":","stores","all","the","dynamic","components","componentIndex",":","the","location","of","each","component","in","the","latent","states","statePrior",":","the","prior","mean","of","the","latent","state","sysVarPrior",":","the","prior","of","the","covariance","of","the","latent","states","noiseVar",":","the","prior","of","the","observation","noise","initialDegreeFreedom",":","the","initial","degree","of","freedom",".","discount",":","the","discounting","factor",",","please","refer","to","@","kalmanFilter","for","more","details","renewTerm",":","used","for","aiding","the","stability","of","the","model",".","The","renewTerm","is","computed","according","to","the","discount","fact",".","When","the","filter","goes","over","certain","steps",",","the","information","contribution","of","the","previous","data","has","decayed","to","minimum",".","We","then","ignore","those","days","and","refit","the","time","series","starting","from","current","-","renewTerm",".","Thus",",","the","effective","sample","size","of","the","dlm","is","twice","renewTerm",".","When","discount","=","1",",","there","will","be","no","renewTerm",",","since","all","the","information","will","be","passed","along",".","renewDiscount",":","the","minimum","discount","of","seasonality","component",",","or","if","there","is","no","seasonality",",","this","will","be","the","minimum","discount","of","all","components",".","Used","for","computing","renewTerm",".","Methods",":","add",":","add","new","component","ls",":","list","out","all","components","delete",":","delete","a","specific","component","by","its","name","initialize",":","assemble","all","the","component","to","construt","a","big","model","updateEvaluation",":","update","the","valuation","matrix","of","the","big","model"]
docstr: 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.
docstr_tok: ["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","."]
code: 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
      self._logger.info("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
      self._logger.info("Initialization finished.")
  
file_path: pydlm/pydlm/modeler/builder.py
imports: from pydlm.base.baseModel import baseModel
  from pydlm.modeler.matrixTools import matrixTools
  from copy import deepcopy
  import logging
  import numpy
label_desc: Use image node_id 6 for calling the builder obj's underlying member method code with example usage: obj.initialize(data, noise) without return types