class_docstr: Encapsulate the inner State Space Language Model for DTM.
  
  Some important attributes of this class:
  
      * `obs` is a matrix containing the document to topic ratios.
      * `e_log_prob` is a matrix containing the topic to word ratios.
      * `mean` contains the mean values to be used for inference for each word for a time slice.
      * `variance` contains the variance values to be used for inference of word in a time slice.
      * `fwd_mean` and`fwd_variance` are the forward posterior values for the mean and the variance.
      * `zeta` is an extra variational parameter with a value for each time slice.
class_docstr_tok: ["Encapsulate","the","inner","State","Space","Language","Model","for","DTM",".","Some","important","attributes","of","this","class",":","*","`","obs","`","is","a","matrix","containing","the","document","to","topic","ratios",".","*","`","e_log_prob","`","is","a","matrix","containing","the","topic","to","word","ratios",".","*","`","mean","`","contains","the","mean","values","to","be","used","for","inference","for","each","word","for","a","time","slice",".","*","`","variance","`","contains","the","variance","values","to","be","used","for","inference","of","word","in","a","time","slice",".","*","`","fwd_mean","`","and","`","fwd_variance","`","are","the","forward","posterior","values","for","the","mean","and","the","variance",".","*","`","zeta","`","is","an","extra","variational","parameter","with","a","value","for","each","time","slice","."]
docstr: Get the variance, based on the
  `Variational Kalman Filtering approach for Approximate Inference (section 3.1)
  <https://mimno.infosci.cornell.edu/info6150/readings/dynamic_topic_models.pdf>`_.
  
  This function accepts the word to compute variance for, along with the associated sslm class object,
  and returns the `variance` and the posterior approximation `fwd_variance`.
  
  Notes
  -----
  This function essentially computes Var[\beta_{t,w}] for t = 1:T
  
  .. :math::
  
      fwd\_variance[t] \equiv E((beta_{t,w}-mean_{t,w})^2 |beta_{t}\ for\ 1:t) =
      (obs\_variance / fwd\_variance[t - 1] + chain\_variance + obs\_variance ) *
      (fwd\_variance[t - 1] + obs\_variance)
  
  .. :math::
  
      variance[t] \equiv E((beta_{t,w}-mean\_cap_{t,w})^2 |beta\_cap_{t}\ for\ 1:t) =
      fwd\_variance[t - 1] + (fwd\_variance[t - 1] / fwd\_variance[t - 1] + obs\_variance)^2 *
      (variance[t - 1] - (fwd\_variance[t-1] + obs\_variance))
  
  Parameters
  ----------
  word: int
      The word's ID.
  chain_variance : float
      Gaussian parameter defined in the beta distribution to dictate how the beta values evolve over time.
  
  Returns
  -------
  (numpy.ndarray, numpy.ndarray)
      The first returned value is the variance of each word in each time slice, the second value is the
      inferred posterior variance for the same pairs.
docstr_tok: ["Get","the","variance",",","based","on","the","`","Variational","Kalman","Filtering","approach","for","Approximate","Inference","(","section","3.1",")","<","https",":","\/\/mimno.infosci.cornell.edu\/info6150\/readings\/dynamic_topic_models.pdf",">","`","_",".","This","function","accepts","the","word","to","compute","variance","for",",","along","with","the","associated","sslm","class","object",",","and","returns","the","`","variance","`","and","the","posterior","approximation","`","fwd_variance","`",".","Notes","--","--","-","This","function","essentially","computes","Var","[","\\beta_","{","t",",","w","}","]","for","t","=","1",":","T","..",":","math",":",":","fwd\\_variance","[","t","]","\\equiv","E","(","(","beta_","{","t",",","w","}","-mean_","{","t",",","w","}",")","^2","|beta_","{","t","}","\\","for\\","1",":","t",")","=","(","obs\\_variance","\/","fwd\\_variance","[","t","-","1","]","+","chain\\_variance","+","obs\\_variance",")","*","(","fwd\\_variance","[","t","-","1","]","+","obs\\_variance",")","..",":","math",":",":","variance","[","t","]","\\equiv","E","(","(","beta_","{","t",",","w","}","-mean\\_cap_","{","t",",","w","}",")","^2","|beta\\_cap_","{","t","}","\\","for\\","1",":","t",")","=","fwd\\_variance","[","t","-","1","]","+","(","fwd\\_variance","[","t","-","1","]","\/","fwd\\_variance","[","t","-","1","]","+","obs\\_variance",")","^2","*","(","variance","[","t","-","1","]","-","(","fwd\\_variance","[","t-1","]","+","obs\\_variance",")",")","Parameters","--","--","--","--","--","word",":","int","The","word","'s","ID",".","chain_variance",":","float","Gaussian","parameter","defined","in","the","beta","distribution","to","dictate","how","the","beta","values","evolve","over","time",".","Returns","--","--","--","-","(","numpy.ndarray",",","numpy.ndarray",")","The","first","returned","value","is","the","variance","of","each","word","in","each","time","slice",",","the","second","value","is","the","inferred","posterior","variance","for","the","same","pairs","."]
code: def compute_post_variance(self, word, chain_variance):
      r"""Get the variance, based on the
      `Variational Kalman Filtering approach for Approximate Inference (section 3.1)
      <https://mimno.infosci.cornell.edu/info6150/readings/dynamic_topic_models.pdf>`_.
  
      This function accepts the word to compute variance for, along with the associated sslm class object,
      and returns the `variance` and the posterior approximation `fwd_variance`.
  
      Notes
      -----
      This function essentially computes Var[\beta_{t,w}] for t = 1:T
  
      .. :math::
  
          fwd\_variance[t] \equiv E((beta_{t,w}-mean_{t,w})^2 |beta_{t}\ for\ 1:t) =
          (obs\_variance / fwd\_variance[t - 1] + chain\_variance + obs\_variance ) *
          (fwd\_variance[t - 1] + obs\_variance)
  
      .. :math::
  
          variance[t] \equiv E((beta_{t,w}-mean\_cap_{t,w})^2 |beta\_cap_{t}\ for\ 1:t) =
          fwd\_variance[t - 1] + (fwd\_variance[t - 1] / fwd\_variance[t - 1] + obs\_variance)^2 *
          (variance[t - 1] - (fwd\_variance[t-1] + obs\_variance))
  
      Parameters
      ----------
      word: int
          The word's ID.
      chain_variance : float
          Gaussian parameter defined in the beta distribution to dictate how the beta values evolve over time.
  
      Returns
      -------
      (numpy.ndarray, numpy.ndarray)
          The first returned value is the variance of each word in each time slice, the second value is the
          inferred posterior variance for the same pairs.
  
      """
      INIT_VARIANCE_CONST = 1000
  
      T = self.num_time_slices
      variance = self.variance[word]
      fwd_variance = self.fwd_variance[word]
      # forward pass. Set initial variance very high
      fwd_variance[0] = chain_variance * INIT_VARIANCE_CONST
      for t in range(1, T + 1):
          if self.obs_variance:
              c = self.obs_variance / (
                  fwd_variance[t - 1]
                  + chain_variance
                  + self.obs_variance
              )
          else:
              c = 0
          fwd_variance[t] = c * (fwd_variance[t - 1] + chain_variance)
  
      # backward pass
      variance[T] = fwd_variance[T]
      for t in range(T - 1, -1, -1):
          if fwd_variance[t] > 0.0:
              c = np.power(
                  (
                      fwd_variance[t]
                      / (fwd_variance[t] + chain_variance)
                  ),
                  2,
              )
          else:
              c = 0
          variance[t] = (c * (variance[t + 1] - chain_variance)) + (
              (1 - c) * fwd_variance[t]
          )
  
      return variance, fwd_variance
  
imports: import logging
  import numpy
  from scipy.special import digamma, gammaln
  from scipy import optimize
  from gensim import utils, matutils
  from gensim.models import ldamodel
label_desc: Use image node_id 3 for calling the sslm obj's underlying member method code with example usage: obj.compute_post_variance(word, chain_variance) and returns: variance,  fwd_variance