B
    z‹d*  ã               @   sT   d dl mZmZmZmZ ddddgZd dlZdd„ Zdd	d„Z	ddd„Z
dd„ ZdS )é    )ÚdivisionÚprint_functionÚabsolute_importÚunicode_literalsÚ_ladderÚget_acfÚget_integrated_actÚ&thermodynamic_integration_log_evidenceNc             C   s    t  | ¡} | ddd…  ¡  | S )zM
    Convert an arbitrary iterable of floats into a sorted numpy array.

    Néÿÿÿÿ)ÚnpÚarrayÚsort)Úbetas© r   úY/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/ptemcee/util.pyr      s    
Fc             C   sÌ   t  | ¡} tdƒgt| jƒ }|rTtdt  t  | j| ¡¡ ƒ}td|ƒ||< | } n
| j| }t jj| t j	| |d d| |d}td|ƒ||< t jj
|t  |¡ |dt|ƒ j}d||< ||t|ƒ  S )a÷  
    Estimate the autocorrelation function of a time series using the FFT.

    :param x:
        The time series. If multidimensional, set the time axis using the
        ``axis`` keyword argument and the function will be computed for every
        other axis.

    :param axis: (optional)
        The time axis of ``x``. Assumed to be the first axis if not specified.

    :param fast: (optional)
        If ``True``, only use the largest ``2^n`` entries for efficiency.
        (default: False)

    Né   r   )Úaxis)Únr   )r   Z
atleast_1dÚsliceÚlenÚshapeÚintÚfloorÚlog2ZfftZmeanZifftÚ	conjugateÚtupleÚreal)Úxr   ÚfastÚmr   ÚfZacfr   r   r   r      s    

$$é2   c             C   sz   t | ||d}t|jƒdkr6ddt |d|… ¡  S tdƒgt|jƒ }td|ƒ||< ddtj|t|ƒ |d  }|S )aË  
    Estimate the integrated autocorrelation time of a time series.

    See `Sokal's notes <http://www.stat.unc.edu/faculty/cji/Sokal.pdf>`_ on
    MCMC and sample estimators for autocorrelation times.

    :param x:
        The time series. If multidimensional, set the time axis using the
        ``axis`` keyword argument and the function will be computed for every
        other axis.

    :param axis: (optional)
        The time axis of ``x``. Assumed to be the first axis if not specified.

    :param window: (optional)
        The size of the window to use. (default: 50)

    :param fast: (optional)
        If ``True``, only use the largest ``2^n`` entries for efficiency.
        (default: False)

    )r   r   é   r   N)r   )r   r   r   r   Úsumr   r   )r   r   Zwindowr   r    r   Útaur   r   r   r   ;   s    c             C   s  t | ƒt |ƒkrtdƒ‚t | ¡ddd… }| | } || }t | ¡}| d dkr°t |dgf¡} t |ddd… dgf¡}t |ddd… |d gf¡}t ||d gf¡}n8t |ddd… dgf¡}t |ddd… |d gf¡}t || ¡ }t ||¡ }|t || ¡fS )a  
    Thermodynamic integration estimate of the evidence.

    :param betas: The inverse temperatures to use for the quadrature.

    :param logls:  The mean log-likelihoods corresponding to ``betas`` to use for
        computing the thermodynamic evidence.

    :return ``(logZ, dlogZ)``: Returns an estimate of the
        log-evidence and the error associated with the finite
        number of temperatures at which the posterior has been
        sampled.

    The evidence is the integral of the un-normalized posterior
    over all of parameter space:

    .. math::

        Z \equiv \int d\theta \, l(\theta) p(\theta)

    Thermodymanic integration is a technique for estimating the
    evidence integral using information from the chains at various
    temperatures.  Let

    .. math::

        Z(\beta) = \int d\theta \, l^\beta(\theta) p(\theta)

    Then

    .. math::

        \frac{d \log Z}{d \beta}
        = \frac{1}{Z(\beta)} \int d\theta l^\beta p \log l
        = \left \langle \log l \right \rangle_\beta

    so

    .. math::

        \log Z(1) - \log Z(0)
        = \int_0^1 d\beta \left \langle \log l \right\rangle_\beta

    By computing the average of the log-likelihood at the
    difference temperatures, the sampler can approximate the above
    integral.
    z6Need the same number of log(L) values as temperatures.Nr
   r   r   )r   Ú
ValueErrorr   ZargsortÚcopyZconcatenateZtrapzÚabs)r   ZloglsÚorderZbetas0Zbetas2Zlogls2ZlogZZlogZ2r   r   r   r	   a   s     0
)r   F)r   r!   F)Ú
__future__r   r   r   r   Ú__all__Únumpyr   r   r   r   r	   r   r   r   r   Ú<module>   s   
%
&