B
    d"                 @   s>   d Z ddlZddlmZ dd Zdd Zdd	d
Zdd ZdS )z`
This modules provides functions for estimating the marginal
likelihood or evidence of a model.
    N)	integratec             C   sb   t | }t| }d}x*t| D ]\}}|t| | | 7 }q W t|}||t| 7 }|S )a  Returns the log evidence via the prior arithmetic mean estimator (AME).

    The logarithm form of AME is used. This is the most basic
    evidence estimator, and often requires O(billions) of samples
    from the prior.

    Parameters
    ----------
    log_likelihood : 1d array of floats
        The log likelihood of the data sampled from the prior
        distribution.

    Returns
    -------
    float :
        Estimation of the log of the evidence.
    g        )lennumpymax	enumerateexplog)log_likelihoodnum_sampleslogl_maxlog_evidencei_ r   e/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/inference/evidence.pyarithmetic_mean_estimator   s    

r   c             C   sr   t | }td|  }d}x.t| D ]"\}}|td| |  | 7 }q$W dt| }||7 }|t|7 }|S )a  Returns the log evidence via posterior harmonic mean estimator (HME).

    The logarithm form of HME is used. This method is not
    recommended for general use. It is very slow to converge,
    formally, has infinite variance, and very error prone.

    Not recommended for general use.

    Parameters
    ----------
    log_likelihood : 1d array of floats
        The log likelihood of the data sampled from the posterior
        distribution.

    Returns
    -------
    float :
        Estimation of the log of the evidence.
    g      g        )r   r   r   r   r   r   )r	   r
   r   r   r   r   r   r   r   harmonic_mean_estimator6   s    r   simpsonsc                s  dddg}||kr"t d||f t|}|| }| tt|td  ftjdd}|dkr~t||}|dkrd}xhtt|d D ]T | d  |   }d	|d
  }	t	 d  }
|
t	  8 }
||	|
 8 }qW ||7 }n|dkrt
j||dd}ttd }g }x<td D ],\ }| fddtt|D  q<W |dkrxntd D ]\ }t|  || < qW n>|dkrx2td D ]"\ }t
j|  |dd| < qW t|tttd  }||fS )aw  Returns the log evidence of the model via thermodynamic integration.
    Also returns an estimated standard deviation for the log evidence.

    Current options are integration through the trapezoid rule, a
    first-order corrected trapezoid rule, and Simpson's rule.

    Parameters
    ----------
    log_likelihood : 3d array of shape (betas, walker, iteration)
        The log likelihood for each temperature separated by
        temperature, walker, and iteration.

    betas : 1d array
        The inverse temperatures used in the MCMC.

    method : {"trapzoid", "trapezoid_corrected", "simpsons"},
             optional.
        The numerical integration method to use for the
        thermodynamic integration. Choices include: "trapezoid",
        "trapezoid_corrected", "simpsons", for the trapezoid rule,
        the first-order correction to the trapezoid rule, and
        Simpson's rule. [Default = "simpsons"]

    Returns
    -------
    log_evidence : float
        Estimation of the log of the evidence.

    mcmc_std : float
        The standard deviation of the log evidence estimate from
        Monte-Carlo spread.
    	trapezoidtrapezoid_correctedr   z$Method %s not supported. Expected %sr      )Zaxis)r   r   gUUUUUU?g       @last)Zevenc                s   g | ]}|   qS r   r   ).0x)r   r	   r   r   
<listcomp>   s    z-thermodynamic_integration.<locals>.<listcomp>)
ValueErrorr   argsortreshaper   flattenaverageZtrapzrangevarr   Zsimpszerosr   appendZstdsqrtfloat)r	   betasmethodZmethod_listorderZaverage_loglr   Zvar_correction
delta_betaZpre_fac_varZvar_diffZti_vecZlogl_per_sampr   mcmc_stdr   )r   r	   r   thermodynamic_integrationX   sL    #



(

 r+   c             C   sz  t |ddd }|| }| | } t | t|t| d  f} t t|d }x|tt|d D ]h}|| ||d   }t | |d  }|| }|| |d  |  }t t 	t 
|}|| ||< qhW t |}	|	}
d}xhtt|d D ]T}|| ||d   }|| |d   ||  }t 
|d }t |d }||7 }qW |tt| d d  }t |}|
|fS )a  Returns the log evidence of the model via stepping stone algorithm.
    Also returns an estimated standard deviation for the log evidence.

    Parameters
    ----------
    log_likelihood : 3d array of shape (betas, walker, iteration)
        The log likelihood for each temperature separated by
        temperature, walker, and iteration.

    betas          : 1d array
                     The inverse temperatures used in the MCMC.

    Returns
    -------
    log_evidence : float
        Estimation of the log of the evidence.
    mcmc_std : float
        The standard deviation of the log evidence estimate from
        Monte-Carlo spread.
    Nr   r   g      ?   g       @)r   r   r   r   r   r"   r    r   r   r   r   sumr%   r$   )r	   r&   r(   Z	log_rk_pbr   r)   Zmax_logl_pbZval_1Zval_2Zlog_rkr   r*   Zpre_factvalr   r   r   stepping_stone_algorithm   s4    

r0   )r   )__doc__r   Zscipyr   r   r   r+   r0   r   r   r   r   <module>   s   #
i