class_docstr: The Agent class, the highest-level API that wraps together processes for action, perception, and learning under active inference.
  
  The basic usage is as follows:
  
  >>> my_agent = Agent(A = A, B = C, <more_params>)
  >>> observation = env.step(initial_action)
  >>> qs = my_agent.infer_states(observation)
  >>> q_pi, G = my_agent.infer_policies()
  >>> next_action = my_agent.sample_action()
  >>> next_observation = env.step(next_action)
  
  This represents one timestep of an active inference process. Wrapping this step in a loop with an ``Env()`` class that returns
  observations and takes actions as inputs, would entail a dynamic agent-environment interaction.
class_docstr_tok: ["The","Agent","class",",","the","highest-level","API","that","wraps","together","processes","for","action",",","perception",",","and","learning","under","active","inference",".","The","basic","usage","is","as","follows",":",">",">",">","my_agent","=","Agent","(","A","=","A",",","B","=","C",",","<","more_params",">",")",">",">",">","observation","=","env.step","(","initial_action",")",">",">",">","qs","=","my_agent.infer_states","(","observation",")",">",">",">","q_pi",",","G","=","my_agent.infer_policies","(",")",">",">",">","next_action","=","my_agent.sample_action","(",")",">",">",">","next_observation","=","env.step","(","next_action",")","This","represents","one","timestep","of","an","active","inference","process",".","Wrapping","this","step","in","a","loop","with","an","``","Env","(",")","``","class","that","returns","observations","and","takes","actions","as","inputs",",","would","entail","a","dynamic","agent-environment","interaction","."]
docstr: Both sets and returns the penultimate belief before the first timestep of the backwards inference horizon. 
  In the case that the inference horizon includes the first timestep of the simulation, then the ``latest_belief`` is
  simply the first belief of the whole simulation, or the prior (``self.D``). The particular structure of the ``latest_belief``
  depends on the value of ``self.edge_handling_params['use_BMA']``.
  
  Returns
  ---------
  latest_belief: ``numpy.ndarray`` of dtype object
      Penultimate posterior beliefs over hidden states at the timestep just before the first timestep of the inference horizon. 
      Depending on the value of ``self.edge_handling_params['use_BMA']``, the shape of this output array will differ.
      If ``self.edge_handling_params['use_BMA'] == True``, then ``latest_belief`` will be a Bayesian model average 
      of beliefs about hidden states, where the average is taken with respect to posterior beliefs about policies.
      Otherwise, `latest_belief`` will be the full, policy-conditioned belief about hidden states, and will have indexing structure
      policies->factors, such that ``latest_belief[p_idx][f_idx]`` refers to the penultimate belief about marginal factor ``f_idx``
      under policy ``p_idx``.
docstr_tok: ["Both","sets","and","returns","the","penultimate","belief","before","the","first","timestep","of","the","backwards","inference","horizon",".","In","the","case","that","the","inference","horizon","includes","the","first","timestep","of","the","simulation",",","then","the","``","latest_belief","``","is","simply","the","first","belief","of","the","whole","simulation",",","or","the","prior","(","``","self.D","``",")",".","The","particular","structure","of","the","``","latest_belief","``","depends","on","the","value","of","``","self.edge_handling_params","[","'use_BMA","'","]","``",".","Returns","--","--","--","--","-","latest_belief",":","``","numpy.ndarray","``","of","dtype","object","Penultimate","posterior","beliefs","over","hidden","states","at","the","timestep","just","before","the","first","timestep","of","the","inference","horizon",".","Depending","on","the","value","of","``","self.edge_handling_params","[","'use_BMA","'","]","``",",","the","shape","of","this","output","array","will","differ",".","If","``","self.edge_handling_params","[","'use_BMA","'","]","==","True","``",",","then","``","latest_belief","``","will","be","a","Bayesian","model","average","of","beliefs","about","hidden","states",",","where","the","average","is","taken","with","respect","to","posterior","beliefs","about","policies",".","Otherwise",",","`","latest_belief","``","will","be","the","full",",","policy-conditioned","belief","about","hidden","states",",","and","will","have","indexing","structure","policies-",">","factors",",","such","that","``","latest_belief","[","p_idx","]","[","f_idx","]","``","refers","to","the","penultimate","belief","about","marginal","factor","``","f_idx","``","under","policy","``","p_idx","``","."]
code: def set_latest_beliefs(self, last_belief=None):
      """
      Both sets and returns the penultimate belief before the first timestep of the backwards inference horizon.
      In the case that the inference horizon includes the first timestep of the simulation, then the ``latest_belief`` is
      simply the first belief of the whole simulation, or the prior (``self.D``). The particular structure of the ``latest_belief``
      depends on the value of ``self.edge_handling_params['use_BMA']``.
  
      Returns
      ---------
      latest_belief: ``numpy.ndarray`` of dtype object
          Penultimate posterior beliefs over hidden states at the timestep just before the first timestep of the inference horizon.
          Depending on the value of ``self.edge_handling_params['use_BMA']``, the shape of this output array will differ.
          If ``self.edge_handling_params['use_BMA'] == True``, then ``latest_belief`` will be a Bayesian model average
          of beliefs about hidden states, where the average is taken with respect to posterior beliefs about policies.
          Otherwise, `latest_belief`` will be the full, policy-conditioned belief about hidden states, and will have indexing structure
          policies->factors, such that ``latest_belief[p_idx][f_idx]`` refers to the penultimate belief about marginal factor ``f_idx``
          under policy ``p_idx``.
      """
  
      if last_belief is None:
          last_belief = utils.obj_array(len(self.policies))
          for p_i, _ in enumerate(self.policies):
              last_belief[p_i] = copy.deepcopy(self.qs[p_i][0])
  
      begin_horizon_step = self.curr_timestep - self.inference_horizon
      if self.edge_handling_params["use_BMA"] and (
          begin_horizon_step >= 0
      ):
          if hasattr(self, "q_pi_hist"):
              self.latest_belief = inference.average_states_over_policies(
                  last_belief, self.q_pi_hist[begin_horizon_step]
              )  # average the earliest marginals together using contemporaneous posterior over policies (`self.q_pi_hist[0]`)
          else:
              self.latest_belief = inference.average_states_over_policies(
                  last_belief, self.q_pi
              )  # average the earliest marginals together using posterior over policies (`self.q_pi`)
      else:
          self.latest_belief = last_belief
  
      return self.latest_belief
  
imports: import warnings
  import numpy
  from pymdp import inference, control, learning
  from pymdp import utils, maths
  import copy
label_desc: Use image node_id 9 for calling the Agent obj's underlying member method code with example usage: obj.set_latest_beliefs(last_belief) and returns: self