a
    EPÎjÙ+  ã                   @   s¦   d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ g d¢Zdd	„ Zd
d„ Zdd„ Zdd„ Zddd„Zddd„Zdd„ Zd dd„Zd!dd„Zd"dd„ZdS )#z`
This provides a small set of utilities in NumPyro that are used to diagnose posterior samples.
é    )ÚOrderedDict)ÚproductN)Ú
device_get)Útree_flattenÚtree_map)ÚautocorrelationÚautocovarianceÚeffective_sample_sizeÚgelman_rubinÚhpdiÚsplit_gelman_rubinÚprint_summaryc                 C   sz   | j d d… \}}| jddd�}|jdd�}||d  | }| j d dkrn| jdd�}|jddd�}|| }n|}||fS )Né   é   ©ÚaxisZddofr   ©r   )ÚshapeÚvarÚmean)ÚxÚCÚNZ	chain_varÚ
var_withinÚvar_estimatorZ
chain_meanZvar_between© r   úc/mounts/lovelace/software/anaconda3/envs/metadmg/lib/python3.9/site-packages/numpyro/diagnostics.pyÚ_compute_chain_variance_stats   s    
r   c                 C   s~   | j dksJ ‚| jd dks J ‚| jd dks2J ‚t| ƒ\}}tjddd�� t || ¡}W d  ƒ n1 sp0    Y  |S )aO  
    Computes R-hat over chains of samples ``x``, where the first dimension of
    ``x`` is chain dimension and the second dimension of ``x`` is draw dimension.
    It is required that ``x.shape[0] >= 2`` and ``x.shape[1] >= 2``.

    :param numpy.ndarray x: the input array.
    :return: R-hat of ``x``.
    :rtype: numpy.ndarray
    r   r   r   Úignore©ÚinvalidÚdivideN)Úndimr   r   ÚnpÚerrstateÚsqrt)r   r   r   Zrhatr   r   r   r
   +   s    
,r
   c                 C   sn   | j dksJ ‚| jd dks J ‚| jd d }tj| dd…d|…f | dd…| d…f gdd�}t|ƒ}|S )aC  
    Computes split R-hat over chains of samples ``x``, where the first dimension
    of ``x`` is chain dimension and the second dimension of ``x`` is draw dimension.
    It is required that ``x.shape[1] >= 4``.

    :param numpy.ndarray x: the input array.
    :return: split R-hat of ``x``.
    :rtype: numpy.ndarray
    r   r   é   Nr   r   )r"   r   r#   Úconcatenater
   )r   ZN_halfZ	new_inputZ
split_rhatr   r   r   r   >   s    
4r   c                 C   sl   | dkr| S | }|d dkr&|d }q|d dkr<|d }q&|d dkrR|d }q<|dkr^| S | d7 } qd S )Nr   r   é   é   r   r   )ÚtargetÚmr   r   r   Ú_fft_next_fast_lenQ   s    


r,   c           	      C   sÞ   | j | }t|ƒ}d| }t | |d¡} | | jddd� }tjj||dd�}|t |¡ }tjj||dd�}|dd|…f }|t 	|dd¡ }tj
d	d	d
��$ ||ddd…f  }W d  ƒ n1 sÆ0    Y  t ||d¡S )zø
    Computes the autocorrelation of samples at dimension ``axis``.

    :param numpy.ndarray x: the input array.
    :param int axis: the dimension to calculate autocorrelation.
    :return: autocorrelation of ``x``.
    :rtype: numpy.ndarray
    r   éÿÿÿÿT©r   Zkeepdims)Únr   .Ng        r   r   r   )r   r,   r#   Úswapaxesr   ZfftZrfftÚ	conjugateZirfftZaranger$   )	r   r   r   ÚMZM2Zcentered_signalZfreqvecZfreqvec_gramZautocorrr   r   r   r   c   s    
2r   c                 C   s   t | |ƒ| j|dd� S )zõ
    Computes the autocovariance of samples at dimension ``axis``.

    :param numpy.ndarray x: the input array.
    :param int axis: the dimension to calculate autocovariance.
    :return: autocovariance of ``x``.
    :rtype: numpy.ndarray
    Tr.   )r   r   )r   r   r   r   r   r   ˆ   s    	r   c           	      C   sî   | j dksJ ‚| jd dks J ‚t| dd�}t| ƒ\}}d||jdd� |  }d|d< |ddd…df |ddd…df  }|dd… }tj|tjjtj	|dd…df ddd	�dd�gdd�}d
d|j
dd�  }t | jdd… ¡| }|S )aÇ  
    Computes effective sample size of input ``x``, where the first dimension of
    ``x`` is chain dimension and the second dimension of ``x`` is draw dimension.

    **References:**

    1. *Introduction to Markov Chain Monte Carlo*,
       Charles J. Geyer
    2. *Stan Reference Manual version 2.18*,
       Stan Development Team

    :param numpy.ndarray x: the input array.
    :return: effective sample size of ``x``.
    :rtype: numpy.ndarray
    r   r   r   g      ð?r   Nr-   .)Za_minZa_maxg      ð¿g       @)r"   r   r   r   r   r#   r'   ÚminimumÚ
accumulateZclipÚsumÚprod)	r   Z	gamma_k_cr   r   Zrho_kZRho_kZRho_initÚtauÚn_effr   r   r   r	   ”   s"    $&þûr	   çÍÌÌÌÌÌì?c                 C   sÀ   t  | |d¡} t j| dd�}| jd }t|| ƒ}|d|| … }||d… }|| }|jdd�}	|	| }
t j||	d dd�}t  ||d¡}t j||
d dd�}t  ||d¡}t j||g|d�S )a¢  
    Computes "highest posterior density interval" (HPDI) which is the narrowest
    interval with probability mass ``prob``.

    :param numpy.ndarray x: the input array.
    :param float prob: the probability mass of samples within the interval.
    :param int axis: the dimension to calculate hpdi.
    :return: quantiles of ``x`` at ``(1 - prob) / 2`` and
        ``(1 + prob) / 2``.
    :rtype: numpy.ndarray
    r   r   N©N.)r#   r0   Úsortr   ÚintZargminZtake_along_axisr'   )r   Úprobr   Zsorted_xZmassZindex_lengthZintervals_leftZintervals_rightZintervals_lengthZindex_startZ	index_endZhpd_leftZ	hpd_rightr   r   r   r   Ã   s    
r   Tc              
   C   s  |st dd„ | ƒ} t| tƒs6dd„ tt| ƒd ƒD ƒ} i }|  ¡ D ]Ð\}}t|ƒ}t |d|j	dd…  ¡}|j
dd	�}|jdd
d�}tj|dd	�}	t||d�}
t|ƒ}t|ƒ}d dd
|  ¡}d dd
|  ¡}td|fd|fd|	f||
d f||
d
 fd|fd|fgƒ||< qB|S )ay  
    Returns a summary table displaying diagnostics of ``samples`` from the
    posterior. The diagnostics displayed are mean, standard deviation, median,
    the 90% Credibility Interval :func:`~numpyro.diagnostics.hpdi`,
    :func:`~numpyro.diagnostics.effective_sample_size`, and
    :func:`~numpyro.diagnostics.split_gelman_rubin`.

    :param samples: a collection of input samples with left most dimension is chain
        dimension and second to left most dimension is draw dimension.
    :type samples: dict or numpy.ndarray
    :param float prob: the probability mass of samples within the HPDI interval.
    :param bool group_by_chain: If True, each variable in `samples` will be treated
        as having shape `num_chains x num_samples x sample_shape`. Otherwise, the
        corresponding shape will be `num_samples x sample_shape` (i.e. without
        chain dimension).
    c                 S   s   | d S ©Nr:   r   ©r   r   r   r   Ú<lambda>ñ   ó    zsummary.<locals>.<lambda>c                 S   s   i | ]\}}d   |¡|“qS ©zParam:{}©Úformat©Ú.0ÚiÚvr   r   r   Ú
<dictcomp>ó   s   zsummary.<locals>.<dictcomp>r   )r-   r   Nr   r   r   )r=   z{:.1f}%é2   r   ÚstdÚmedianr8   Úr_hat)r   Ú
isinstanceÚdictÚ	enumerater   Úitemsr   r#   Zreshaper   r   rK   rL   r   r	   r   rD   r   )Úsamplesr=   Úgroup_by_chainÚsummary_dictÚnameÚvalueZ
value_flatr   rK   rL   Zhpdr8   rM   Z	hpd_lowerZ	hpd_upperr   r   r   Úsummaryß   s:    
ÿ

ùÿ
rW   c              	      sl  |st dd„ | ƒ} t| tƒs6dd„ tt| ƒd ƒD ƒ} t| |dd�}dd„ |  ¡ D ƒ}tttd	d„ | 	¡ ƒƒd
ƒ}dt
|ƒ d }|d }dgtt| 	¡ ƒd  ¡ ƒ }tƒ  t|j|Ž ƒ |d }	| ¡ D ]”\}
}|d j}t|ƒdk�rt|	j|
g| 	¡ ¢R Ž ƒ qÌttt|ƒŽ D ]H‰ d d tt
ˆ ƒ¡¡}t|	j|
| g‡ fdd„| 	¡ D ƒ¢R Ž ƒ �qqÌtƒ  dS )ax  
    Prints a summary table displaying diagnostics of ``samples`` from the
    posterior. The diagnostics displayed are mean, standard deviation, median,
    the 90% Credibility Interval :func:`~numpyro.diagnostics.hpdi`,
    :func:`~numpyro.diagnostics.effective_sample_size`, and
    :func:`~numpyro.diagnostics.split_gelman_rubin`.

    :param samples: a collection of input samples with left most dimension is chain
        dimension and second to left most dimension is draw dimension.
    :type samples: dict or numpy.ndarray
    :param float prob: the probability mass of samples within the HPDI interval.
    :param bool group_by_chain: If True, each variable in `samples` will be treated
        as having shape `num_chains x num_samples x sample_shape`. Otherwise, the
        corresponding shape will be `num_samples x sample_shape` (i.e. without
        chain dimension).
    c                 S   s   | d S r>   r   r?   r   r   r   r@   #  rA   zprint_summary.<locals>.<lambda>c                 S   s   i | ]\}}d   |¡|“qS rB   rC   rE   r   r   r   rI   %  s   z!print_summary.<locals>.<dictcomp>r   T)rS   c                 S   s<   i | ]4\}}||d  d  tdd„ |jdd… ƒ¡ d “qS )ú[ú,c                 S   s   t | d ƒS )Nr   )Ústrr?   r   r   r   r@   +  rA   z*print_summary.<locals>.<dictcomp>.<lambda>r   Nú])ÚjoinÚmapr   )rF   ÚkrH   r   r   r   rI   *  s   ÿc                 S   s   t | ƒS )N)Úlenr?   r   r   r   r@   .  rA   é
   z{:>Ú}z* {:>9} {:>9} {:>9} {:>9} {:>9} {:>9} {:>9}Ú z? {:>9.2f} {:>9.2f} {:>9.2f} {:>9.2f} {:>9.2f} {:>9.2f} {:>9.2f}r   z[{}]rY   c                    s   g | ]}|ˆ  ‘qS r   r   )rF   rH   ©Úidxr   r   Ú
<listcomp>@  rA   z!print_summary.<locals>.<listcomp>N)r   rN   rO   rP   r   rW   rQ   Úmaxr]   ÚvaluesrZ   ÚlistÚkeysÚprintrD   r   r_   r   Úranger\   )rR   r=   rS   rT   Z	row_namesÚmax_lenZname_formatZheader_formatÚcolumnsZ
row_formatrU   Z
stats_dictr   Zidx_strr   rc   r   r     s>    
ÿþ
ÿÿÿ
r   )r   )r   )r9   r   )r9   T)r9   T)Ú__doc__Úcollectionsr   Ú	itertoolsr   Únumpyr#   Zjaxr   Zjax.tree_utilr   r   Ú__all__r   r
   r   r,   r   r   r	   r   rW   r   r   r   r   r   Ú<module>   s    
%
/

2