nessai.proposal.importance
==========================

.. py:module:: nessai.proposal.importance

.. autoapi-nested-parse::

   Proposals specifically for use with the importance based nested sampler.

   ..
       !! processed by numpydoc !!


Classes
-------

.. autoapisummary::

   nessai.proposal.importance.ImportanceFlowProposal


Module Contents
---------------

.. py:class:: ImportanceFlowProposal(model: nessai.model.Model, output: str, rng: Optional[numpy.random.Generator] = None, flow_config: dict = None, training_config: dict = None, reparameterisation: str = 'logit', weighted_kl: bool = True, reset_flow: Union[bool, int] = True, clip: bool = False, plot_training: bool = False)

   Bases: :py:obj:`nessai.proposal.base.Proposal`


   
   Flow-based proposal for importance-based nested sampling.


   :Parameters:

       **model** : :obj:`nessai.model.Model`
           User-defined model.

       **rng** : :obj:`numpy.random.Generator`, optional
           Random number generator. If not provided, a new generator is created.

       **output** : str
           Output directory.

       **flow_config** : dict
           Configuration for the flow.

       **reparameterisation** : str
           Reparameterisation to use. If None, only the unit-hypercube
           reparameterisation is used.

       **weighted_kl** : bool
           If true, use the weights (prior / meta-proposal) when training the
           next flow.

       **rest_flow** : Union[bool, int]
           When to reset the flow. If False, the flow is not reset. If True the
           flow is reset every time a new flow is added. If an integer, every
           nth flow is reset.

       **clip** : bool
           If true the samples generated by flow will be clipped to [0, 1] before
           being mapped back from the unit-hypercube. This is only needed when
           the mapping cannot be defined outside of [0, 1]. In cases where it
           can, these points will be rejected when the prior bounds are checked.

       **plot_training** : bool
           If True, produce plots during training. If False, no plots are
           produced.














   ..
       !! processed by numpydoc !!

   .. py:property:: flow_config
      :type: dict


      
      Return the configuration for the flow
















      ..
          !! processed by numpydoc !!


   .. py:property:: weights
      :type: dict


      
      Dictionary containing the weights for each proposal
















      ..
          !! processed by numpydoc !!


   .. py:property:: weights_array
      :type: numpy.ndarray


      
      Array of weights for each proposal
















      ..
          !! processed by numpydoc !!


   .. py:property:: n_proposals
      :type: int


      
      Current number of proposals in the meta proposal
















      ..
          !! processed by numpydoc !!


   .. py:method:: initialise()

      
      Initialise the proposal
















      ..
          !! processed by numpydoc !!


   .. py:method:: verify_rescaling(n: int = 1000, rtol: float = 1e-08, atol: float = 1e-08) -> None

      
      Verify the rescaling is invertible.

      Uses :code:`numpy.allclose`, see numpy documentation for more details.

      :Parameters:

          **n** : int
              Number of samples to test.

          **atol** : float
              The absolute tolerance.

          **rtol** : float
              The relative tolerance.














      ..
          !! processed by numpydoc !!


   .. py:method:: to_prime(x: numpy.ndarray) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Convert samples from the unit hypercube to samples in x'-space


      :Parameters:

          **x_prime**
              Unstructured array of samples in the unit hypercube



      :Returns:

          x
              Unstructured array of samples in x'-space

          log_j
              Corresponding log-Jacobian determinant.











      ..
          !! processed by numpydoc !!


   .. py:method:: from_prime(x_prime: numpy.ndarray) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Convert samples the x'-space to samples in the unit hypercube.


      :Parameters:

          **x_prime**
              Unstructured array of samples.



      :Returns:

          x
              Unstructured array of samples in the unit hypercube.

          log_j
              Corresponding log-Jacobian determinant.











      ..
          !! processed by numpydoc !!


   .. py:method:: rescale(x: numpy.ndarray) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Convert to the space in which the flow is trained.

      Returns an unstructured array.















      ..
          !! processed by numpydoc !!


   .. py:method:: inverse_rescale(x_prime: numpy.ndarray) -> numpy.ndarray

      
      Convert from the space in which the flow is trained.

      Returns a structured array.















      ..
          !! processed by numpydoc !!


   .. py:method:: update_proposal_weights(weights: dict) -> None

      
      Method to update the proposal weights dictionary.









      :Raises:

          RuntimeError
              If the weights do not sum to 1 are the update.







      ..
          !! processed by numpydoc !!


   .. py:method:: train(samples: numpy.ndarray, plot: bool = False, output: Union[str, None] = None, weights: numpy.ndarray = None, **kwargs) -> None

      
      Train the proposal with a set of samples.


      :Parameters:

          **samples** :  numpy.ndarray
              Array of samples for training.

          **plot** : bool
              Flag to enable or disable plotting.

          **output** : Union[str, None]
              Output directory to use instead of default output. If None the
              default that was set when the class what initialised is used.

          **kwargs**
              Key-word arguments passed to                 :py:meth:`nessai.flowmodel.FlowModel.train`.














      ..
          !! processed by numpydoc !!


   .. py:method:: compute_log_Q(x_prime: numpy.ndarray, log_j: Optional[numpy.ndarray] = None) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Compute the log meta proposal (log Q) for an array of points.


      :Parameters:

          **x_prime** : numpy.ndarray
              Array of samples in the unit hypercube.

          **log_j** : Optional[numpy.ndarray]
              Log-Jacobian determinant of the prime samples. Must be supplied if
              proposal includes flows.



      :Returns:

          **log_Q** : numpy.ndarray
              Value of the meta-proposal in the prime space.

          **log_q** : numpy.ndarray
              Array of values for flow in the flow in the proposal. Array will
              have shape (# samples, # flows)











      ..
          !! processed by numpydoc !!


   .. py:method:: draw(n: int, flow_number: Optional[int] = None) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Draw n new points.


      :Parameters:

          **n** : int
              Number of points to draw.

          **flow_number** : Optional[int]
              Specifies which flow to use. If not specified the last flow will
              be used.



      :Returns:

          np.ndarray
              Array of new points.

          np.ndarray
              Log-proposal probabilities (log_q)











      ..
          !! processed by numpydoc !!


   .. py:method:: update_log_q(samples: numpy.ndarray, log_q: numpy.ndarray) -> numpy.ndarray

      
      Update the array of proposal probabilities for a set of samples
















      ..
          !! processed by numpydoc !!


   .. py:method:: compute_meta_proposal_from_log_q(log_q)

      
      Compute the meta-proposal from an array of proposal         log-probabilities
















      ..
          !! processed by numpydoc !!


   .. py:method:: compute_meta_proposal_samples(samples: numpy.ndarray) -> numpy.ndarray

      
      Compute the meta proposal Q for a set of samples.

      Includes any rescaling that has been configured.




      :Returns:

          **log_meta_proposal** : numpy.ndarray
              Array of meta-proposal log probabilities (log Q)

          **log_q** : numpy.ndarray
              Array of log q for each flow.











      ..
          !! processed by numpydoc !!


   .. py:method:: get_proposal_log_prob(it: int) -> Callable

      
      Get a pointer to the function for ith proposal.
















      ..
          !! processed by numpydoc !!


   .. py:method:: compute_kl_between_proposals(x: numpy.ndarray, p_it: Optional[int] = None, q_it: Optional[int] = None) -> float

      
      Compute the KL divergence between two proposals.

      Samples should be drawn from p. If proposals aren't specified the
      current and previous proposals are used.















      ..
          !! processed by numpydoc !!


   .. py:method:: draw_from_prior(n: int) -> Tuple[numpy.ndarray, numpy.ndarray]

      
      Draw from the prior
















      ..
          !! processed by numpydoc !!


   .. py:method:: draw_from_flows(n: int, weights=None, counts=None) -> Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

      
      Draw n points from all flows (g).


      :Parameters:

          **n** : int
              Number of points

          **weights**
              Array of (normalised) weights. Ignored if counts is specified.

          **counts**
              Number of samples to draw from each proposal.














      ..
          !! processed by numpydoc !!


   .. py:method:: resume(model, flow_config, weights_path=None)

      
      Resume the proposal
















      ..
          !! processed by numpydoc !!


