B
    zd!                 @   sX   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ ddl	m
Z
 G dd de
ZdS )	    N)create_bit_generators)Chain)	ChainDatadetect_dtypes   )BaseSamplerc               @   s4   e Zd ZdZdddZdd Zdd Zdd	d
ZdS )MetropolisHastingsSamplera  A standard Metropolis-Hastings sampler.

    Parameters
    ----------
    parameters : tuple or list
        Names of the parameters to sample.
    model : object
        Model object.
    nchains : int
        The number of chains to create. Must be greater than zero.
    proposals : list, optional
        List of proposals to use. Any parameters that do not have a proposal
        provided will use the ``default_propsal``.
    default_proposal : an epsie.Proposal class, optional
        The default proposal to use for parameters not in ``proposals``.
        Default is :py:class:`epsie.proposals.Normal`.
    default_proposal_args : dict, optional
        Dictionary of arguments to pass to the default proposal.
    seed : int, optional
        Seed for the random number generator. If None provided, will create
        one.
    pool : Pool object, optional
        Specify a process pool to use for parallelization. Default is to use a
        single core.
    Nc	       	      C   s4   || _ || _| ||| || _|| _| | d S )N)
parametersmodelZset_proposalsseedpoolcreate_chains)	selfr	   r
   nchains	proposalsZdefault_proposalZdefault_proposal_argsr   r    r   e/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/epsie/samplers/mhsampler.py__init__5   s    z"MetropolisHastingsSampler.__init__c                s:   |dk rt dt| jd} fddt|D  _dS )zCreates a list of :py:class:`chain.Chain`.

        Parameters
        ----------
        nchains : int
            The number of Markov chains to create.
        r   znchains must be >= 1)r   c          	      s2   g | ]*\}}t  j jd d  jD ||dqS )c             S   s   g | ]}t |qS r   )copydeepcopy).0pr   r   r   
<listcomp>M   s    zFMetropolisHastingsSampler.create_chains.<locals>.<listcomp>.<listcomp>)Zbit_generatorZchain_id)r   r	   r
   r   )r   Zcidbg)r   r   r   r   K   s   z;MetropolisHastingsSampler.create_chains.<locals>.<listcomp>N)
ValueErrorr   r   	enumerateZ_chains)r   r   Zbitgensr   )r   r   r   ?   s
    
z'MetropolisHastingsSampler.create_chainsc             C   sb   t | jd |}tt| t|d}|| j x$t| jD ]\}}t ||||< q@W |	 S )a1  Concatenates dictionary attributes over all of the chains.

        This is a convenience function used by properties such as
        ``current_positions`` to gather all of the dictionary attributes from
        the chains.

        Parameters
        ----------
        attr : str
            The name of the attribute to get from the chains. The attribute
            is assumed to return a dictionary.

        Returns
        -------
        dict :
            Dictionary mapping parameters to arrays. The arrays have shape
            ``nchains``.
        r   )Zdtypes)
getattrchainsr   listkeysr   extendr   r   asdict)r   attrdoutiichainr   r   r   _concatenate_dictsQ   s    z,MetropolisHastingsSampler._concatenate_dictsc                sF   dkr"t t fdd| j}nt t fdd| j}t|S )a  Concatenates the given attribute over all of the chains.

        This is a convenience function used by properties such as ``positions``
        to gather all of the array attributes from the chains.

        Parameters
        ----------
        attr : str
            The name of the attribute to get from the chains. The attribute
            is assumed to return a (structred) array.
        item : str or array index, optional
            Get a particular item from the (structred) array from each chain
            before concatenating.

        Returns
        -------
        array :
            The returned array has shape ``nchains x niterations``.
        Nc                s
   t |  S )N)r   )x)r"   r   r   <lambda>       z?MetropolisHastingsSampler._concatenate_arrays.<locals>.<lambda>c                s   t |   S )N)r   )r(   )r"   itemr   r   r)      r*   )r   mapr   numpystack)r   r"   r+   Zarrsr   )r"   r+   r   _concatenate_arraysl   s    z-MetropolisHastingsSampler._concatenate_arrays)NNNNN)N)__name__
__module____qualname____doc__r   r   r'   r/   r   r   r   r   r      s     
r   )r-   r   Zepsier   Zepsie.chainr   Zepsie.chain.chaindatar   r   baser   r   r   r   r   r   <module>   s   