B
    ú‹dÇž  ã            
   @   sÂ   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ dd	d
ddgZedddddddddgƒZdd„ Zdd	„ Zdd„ Zd#dd„Zdd„ Zdd„ Zd d
„ Zd!d„ Zd"d„ ZdS )$zÀ
Functions for proposing new live points used by
:class:`~dynesty.sampler.Sampler` (and its children from
:mod:`~dynesty.nestedsamplers`) and
:class:`~dynesty.dynamicsampler.DynamicSampler`.

é    )Ú
namedtupleN)Úlinalgé   )Ú	unitcheckÚapply_reflectÚget_random_generator)Ú
randsphereÚsample_unifÚsample_rwalkÚsample_sliceÚsample_rsliceÚsample_hsliceÚSamplerArgumentÚuÚloglstarÚaxesÚscaleÚprior_transformÚloglikelihoodÚrseedÚkwargsc             C   s:   |   t | j¡¡}|  t |¡¡}d}d}| j||||fS )aƒ  
    Evaluate a new point sampled uniformly from a bounding proposal
    distribution. Parameters are zipped within `args` to utilize
    `pool.map`-style functions.

    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample.

    loglstar : float
        Ln(likelihood) bound. **Not applicable here.**

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new points. **Not applicable here.**

    scale : float
        Value used to scale the provided axes. **Not applicable here.**

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.
        **Not applicable here.**

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube. **For
        uniform sampling this is the same as the initial input position.**

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample. For uniform
        sampling this is `1` by construction.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`. **Not
        applicable for uniform sampling.**

    r   N)r   ÚnpÚasarrayr   r   )ÚargsÚvÚloglÚncÚblob© r   ú]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/dynesty/sampling.pyr	      s
    9c          	   C   s.   t | jƒ}t| j| j| j| j| j| j|| j	ƒS )aÔ  
    Return a new live point proposed by random walking away from an
    existing live point.

    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample. **This is a copy of an existing live
        point.**

    loglstar : float
        Ln(likelihood) bound.

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new points. For random walks new positions are
        proposed using the :class:`~dynesty.bounding.Ellipsoid` whose
        shape is defined by axes.

    scale : float
        Value used to scale the provided axes.

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube.

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`.

    )
r   r   Úgeneric_random_walkr   r   r   r   r   r   r   )r   Úrstater   r   r   r
   _   s    5
c             C   sú   |  d¡}|  d¡}	|  d¡}
t| ƒ}|jd }|  dd¡}d}d}d}x~||k rÆt| ||||||	|
|d	\}}|r†|d7 }|d7 }qJ||ƒ}||ƒ}|d7 }||kr¼|} |}|}|d7 }qJ|d7 }qJW |dkrà|| ƒ}||ƒ}|||d	œ}| ||||fS )
a“  
    Generic random walk step
    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample. **This is a copy of an existing live
        point.**

    loglstar : float
        Ln(likelihood) bound.

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new points. For random walks new positions are
        proposed using the :class:`~dynesty.bounding.Ellipsoid` whose
        shape is defined by axes.

    scale : float
        Value used to scale the provided axes.

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube.

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`.

    Ú
nonboundedÚperiodicÚ
reflectiver   Úwalksé   )r!   r#   r$   r"   r   )ÚacceptZrejectr   )ÚgetÚlenÚshapeÚpropose_ball_point)r   r   r   r   r   r   r!   r   r"   r#   r$   ÚnÚ	n_clusterr%   ZnacceptZnrejectÚncallÚu_propZfailÚv_propÚ	logl_propr   r   r   r   r   r   r    š   sJ    4





r    c	             C   sª   | d|… }	|  || ¡}
t |¡}|
||d…< t||d}t ||¡}|	||  |d|…< |dk	rxt || d¡||< |dk	rt|| ƒ||< t||ƒr¢|dfS dS dS )zç
    Here we are proposing points uniformly within an n-d ellipsoid.
    We are only trying once.
    We return the tuple with
    1) proposed point or None
    2) failure flag (if True, the generated point was outside bounds)
    N)r!   r   F)NT)Úrandomr   Úzerosr   ÚdotÚmodr   r   )r   r   r   r,   r-   r!   r#   r$   r"   Z	u_clusterZu_non_clusterr/   ZdrZdur   r   r   r+     s    

r+   c             C   s¬   || }}|}	|}
d}x|| dkr¦|| d }d|  k rD| ks^n | |  k rZdkrbn nd}| |k r||}||ƒd }
n|}||ƒd }	|r||	kr||
krdS qW dS )aä  
    Acceptance test of slice sampling when doubling mode is used.
    This is an exact implementation of algorithm 6 of Neal 2003
    here w=1 and x0=0 as we are working in the
    coordinate system of F(A) = f(x0+A*w)

    Arguments are
    1) candidate location x1
    2) wrapped logl function (see generic_slice_step)
    3) threshold logl value
    4) left edge of the full interval
    5) right edge of the full interval
    6) value at left edge
    7) value at right edge
    Fgš™™™™™ñ?g       @r   Tr   r   )Úx1ÚFr   ÚLÚRÚfLÚfRZlhatZrhatZf_lhatZf_rhatÚDÚMr   r   r   Ú_slice_doubling_accept?  s     
.r>   c       !         sf  d\‰}}	d}
t ˆƒ}| ¡ }t ˆ ¡}t |¡d }||krH|| }nd}ˆ | ‰ ‡ ‡‡‡‡‡fdd„}| }d| }||ƒd }||ƒd }d}|sx(||krÂ|d8 }||ƒd }|d7 }qœW x(||krì|d7 }||ƒd }|d7 }qÆW ||
kr”d}t d	|
› d
¡ n„d}xn||ks*||kr‚| ¡ }|dk rV||| 8 }||ƒd }n||| 7 }||ƒd }||7 }|d9 }qW |}|}|}|}x¶|| }|| ¡ |  }||ƒ\}}|	d7 }	||krì|rèt|||||||ƒrìP nZ|dk rü|}nJ|dkr|}n:t	dˆ› d|› d|› d|› d|› d|› d|› dˆ › dƒ‚q–W ˆ|ƒ} || |ˆ||	|fS )a  
    Do a slice generic slice sampling step along a specified dimension

    Arguments
    u: ndarray (ndim sized)
        Starting point in unit cube coordinates
        It MUST satisfy the logl>loglstar criterion
    direction: ndarray (ndim sized)
        Step direction vector
    nonperiodic: ndarray(bool)
        mask for nonperiodic variables
    loglstar: float
        the critical value of logl, so that new logl must be >loglstar
    loglikelihood: function
    prior_transform: function
    rstate: random state
    )r   r   r   iè  g       @r   c                s<   ˆ| ˆ   }t |ˆƒr$ˆˆ|ƒƒ}ntj }ˆd7 ‰||fS )Nr   )r   r   Úinf)ÚxZu_newr   )Ú	directionr   r   Únonperiodicr   r   r   r   r7   ‡  s    
zgeneric_slice_step.<locals>.FFTz1The slice sample interval was expanded more than z timesg      à?é   r   zRSlice sampler has failed to find a valid point. Some useful output quantities:
u: z
nstep_left: z
nstep_right: z
nstep_hat: z	
u_prop: z
loglstar: z
logl_prop: z
direction: Ú
)
r)   r2   r   Únormr   ÚsqrtÚwarningsÚwarnr>   ÚRuntimeError)!r   rA   rB   r   r   r   Údoublingr!   ÚnexpandÚ	ncontractZnexpand_thresholdr,   Zrand0ZdirlenÚmaxlenZdirnormr7   Znstep_lZnstep_rÚlogl_lÚlogl_rÚexpansion_warningÚKÚVr8   r9   r:   r;   Z	nstep_hatZ
nstep_propr/   r1   r0   r   )rA   r   r   rB   r   r   r   Úgeneric_slice_stepd  sr    








@rS   c             C   s@  | j | j| j| j| j| j| jf\}}}}}}}t| jƒ}| 	dd¡}	| 	dd¡}
t
|ƒ}|jd |ksjt‚| 	dd¡}d}d}d}||j }d}x”t|ƒD ]ˆ}t |¡}| |¡ xn|D ]f}|| }t|||	||||
|ƒ\}}}}}}}|}||7 }||7 }||7 }|r¸|
s¸d}d}
t d	¡ q¸W qšW |||d
œ}|||||fS )aF  
    Return a new live point proposed by a series of random slices
    away from an existing live point. Standard "Gibs-like" implementation where
    a single multivariate "slice" is a combination of `ndim` univariate slices
    through each axis.

    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample. **This is a copy of an existing live
        point.**

    loglstar : float
        Ln(likelihood) bound.

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new points. For slices new positions are
        proposed along the arthogonal basis defined by :data:`axes`.

    scale : float
        Value used to scale the provided axes.

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube.

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`.

    rB   NÚslice_doublingFr   Úslicesé   Tz<Enabling doubling strategy of slice sampling from Neal(2003))rK   rL   Úexpansion_warning_set)r   r   r   r   r   r   r   r   r   r(   r)   r*   ÚAssertionErrorÚTÚranger   ZarangeÚshufflerS   rG   rH   )r   r   r   r   r   r   r   r   r!   rB   rJ   r,   rU   r   rK   rL   rW   Ú_ZidxsÚidxÚaxisr/   r0   r1   Únc1Únexpand1Ú
ncontract1rP   r   r   r   r   r   ä  sD    7




c          
   C   s6  | j | j| j| j| j| j| jf\}}}}}}}t| jƒ}| 	dd¡}	| 	dd¡}
t
|ƒ}|jd |ksjt‚| 	dd¡}d}d}d}d}x”t|ƒD ]ˆ}|j|d}|t |¡ }t ||¡| }t|||	||||
|ƒ\}}}}}}}|}||7 }||7 }||7 }|r|
sd	}
d	}t d
¡ qW |||dœ}|||||fS )aÌ  
    Return a new live point proposed by a series of random slices
    away from an existing live point. Standard "random" implementation where
    each slice is along a random direction based on the provided axes.

    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample. **This is a copy of an existing live
        point.**

    loglstar : float
        Ln(likelihood) bound.

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new slice directions.

    scale : float
        Value used to scale the provided axes.

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube.

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`.

    rB   NrT   Fr   rU   rV   )ÚsizeTz<Enabling doubling strategy of slice sampling from Neal(2003))rK   rL   rW   )r   r   r   r   r   r   r   r   r   r(   r)   r*   rX   rZ   Ústandard_normalr   rE   r   r4   rS   rG   rH   )r   r   r   r   r   r   r   r   r!   rB   rJ   r,   rU   r   rK   rL   rW   r\   ÚdrhatrA   r/   r0   r1   r_   r`   ra   rP   r   r   r   r   r   Q  s@    5
c       G      C   sz  | j | j| j| j| j| j| jf\}}}}}}}t| jƒ}| 	dd¡}	t
|ƒ}
|jd t
|ƒksbt‚| 	dd¡}| 	dd¡}| 	dd¡}| 	d	d
¡}d}d}d}d}d}
x¶t|ƒD 
]¨}g g g   }}}|j|
d}|t |¡ }t ||¡| d }t |¡}| ¡ }| ¡ }|| d| d| ¡| 8 }|| d| d| ¡| 7 }| t |¡¡ | t |¡¡ | t |¡¡ d\}}t |¡}d}xÆ||krV| t |¡¡ dg  } }!xÖ|| d| d| ¡| 7 }t||	ƒr|t |¡ƒ}"|t |"¡ƒ}#|d7 }|d7 }|d7 }ntj }#|#|k rh|r>d}| ¡  P nt |¡} |#}$t t|$ƒ¡rbd
}nd}nd
}|! t |¡¡ | dk	r¸P q¸W t
|ƒt
|ƒd krðt
|!ƒdkrÆ|!| t
|!ƒ¡ }!n
t |¡}!| t |!¡¡ | t | ¡¡ |røP | |$ }}#|dkrút |
¡}%xàt|
ƒD ]Ô}&t |¡t |¡ }'}(|(|&  d7  < t|(|	ƒrv|t |(¡ƒ})|t |)¡ƒj}*ntj }*d}|d7 }|'|&  d8  < t|'|	ƒrÆ|t |'¡ƒ}+|t |+¡ƒj},ntj },d}|rÚP |d7 }|*|, d |%|&< q W nè||"ƒ}%|rÚg }-x¶t|
ƒD ]ª}&t |¡t |¡ }'}(|(|&  d7  < t|(|	ƒr\|t |(¡ƒ})nd}t |"¡})|'|&  d8  < t|'|	ƒr–|t |'¡ƒ}+nd}t |"¡})|r¬P |- |)|+ d ¡ qW t |-¡}-t |-|%¡}%|d7 }|d|% t ||%¡ t |%¡d   }.t |.|¡}/|/t |.¡t |¡  }/|/dk r>d}P n|.}d} d}|d7 }q’W d\}}t |¡ }t |¡}d}xÆ||k	rB| t |¡¡ dg  } }!xÖ|| d| d| ¡| 7 }t||	ƒr|t |¡ƒ}0|t |0¡ƒ}1|d7 }|d7 }|d7 }ntj }1|1|k rT|r*d}| ¡  P nt |¡} |1}$t t|$ƒ¡rNd
}nd}nd
}|! t |¡¡ | dk	r¤P q¤W t
|ƒt
|ƒd krÜt
|!ƒdkr²|!| t
|!ƒ¡ }!n
t |¡}!| t |!¡¡ | t | ¡¡ |räP | |$ }}1|dkræt |
¡}%xàt|
ƒD ]Ô}&t |¡t |¡ }2}3|3|&  d7  < t|3|	ƒrb|t |3¡ƒ}4|t |4¡ƒj}5ntj }5d}|d7 }|2|&  d8  < t|2|	ƒr²|t |2¡ƒ}6|t |6¡ƒj}7ntj }7d}|rÆP |d7 }|5|7 d |%|&< qW nè||0ƒ}%|rÆg }-x¶t|
ƒD ]ª}&t |¡t |¡ }2}3|3|&  d7  < t|3|	ƒrH|t |3¡ƒ}4nd}t |0¡}4|2|&  d8  < t|2|	ƒr‚|t |2¡ƒ}6nd}t |0¡}4|r˜P |- |4|6 d ¡ qW t |-¡}-t |-|%¡}%|d7 }|d|% t ||%¡ t |%¡d   }.t |.|¡}/|/t |.¡t |¡  }/|/dk 	r*d}P n|.}d} d}|d7 }q~W t
|ƒdk	rp| d¡ | d¡ | d¡ t |¡t |¡t |¡  }}}t
|ƒ}8tj|8td}9x2t t!||ƒƒD ] \}&\}:};t |;|: ¡|9|&< 	q¸W t |9¡}<xrt "|9d|< k ¡
r t#d|› d|› d|› d|› d	ƒ‚|9t $|9¡ }=|j|8|=d}>||> ||> ||>   }}?}|| }@| %¡ }A||A|@  }Bt|B|	ƒ
rž|t |B¡ƒ}C|t |C¡ƒ}Dntj }D|d7 }|d7 }|D|k
rÈ|B}P nŽt |B|? |@¡}E|Edk r |B||>< |9|>  d|A 9  < nV|Edkr$|B||>< |9|>  |A9  < n2t#d|› d|› d|› d|@› d |B› d|› d!|D› ƒ‚	qêW q²W |||d"œ}F|B|C|D||FfS )#aÕ  
    Return a new live point proposed by "Hamiltonian" Slice Sampling
    using a series of random trajectories away from an existing live point.
    Each trajectory is based on the provided axes and samples are determined
    by moving forwards/backwards in time until the trajectory hits an edge
    and approximately reflecting off the boundaries.
    Once a series of reflections has been established, we propose a new live
    point by slice sampling across the entire path.

    Parameters
    ----------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the initial sample. **This is a copy of an existing live
        point.**

    loglstar : float
        Ln(likelihood) bound.

    axes : `~numpy.ndarray` with shape (ndim, ndim)
        Axes used to propose new slice directions.

    scale : float
        Value used to scale the provided axes.

    prior_transform : function
        Function transforming a sample from the a unit cube to the parameter
        space of interest according to the prior.

    loglikelihood : function
        Function returning ln(likelihood) given parameters as a 1-d `~numpy`
        array of length `ndim`.

    kwargs : dict
        A dictionary of additional method-specific parameters.

    Returns
    -------
    u : `~numpy.ndarray` with shape (npdim,)
        Position of the final proposed point within the unit cube.

    v : `~numpy.ndarray` with shape (ndim,)
        Position of the final proposed point in the target parameter space.

    logl : float
        Ln(likelihood) of the final proposed point.

    nc : int
        Number of function calls used to generate the sample.

    blob : dict
        Collection of ancillary quantities used to tune :data:`scale`.

    rB   Nr   rU   rV   ÚgradÚmax_moveéd   Úcompute_jacFg      Ð?)rb   g{®Gáz„?g      ð?)FFr   Tg»½×Ùß|Û=g»½×Ùß|ë=rC   g®Gáz®ï¿)Zdtypegñhãˆµøä>zRHamiltonian slice sampling appears to be stuck! Some useful output quantities:
u: z

 u_left: z

u_right: z
 loglstar: Ú.)ÚpzRSlice sampler has failed to find a valid point. Some useful output quantities:
u: z	
 u_hat: z	
u_prop: z
logl_prop: )ÚnmoveÚnreflectrL   )&r   r   r   r   r   r   r   r   r   r(   r)   r*   rX   rZ   rc   r   rE   r   r4   ÚarrayÚcopyÚuniformÚappendr   r   r?   ÚpopÚisfiniteÚfloatÚchoicer3   ÚvalÚ	enumerateÚzipÚanyrI   Úsumr2   )Gr   r   r   r   r   r   r   r   r!   rB   r,   rU   re   rf   rh   Újitterr   rk   rl   rL   r\   Znodes_lZnodes_mZnodes_rrd   r^   ZvelZu_lZu_rÚreverseZreflectr.   Zu_outZu_inZv_rrO   Zlogl_outÚhÚiZu_r_lZu_r_rZv_r_rZlogl_r_rZv_r_lZlogl_r_lZjacZvel_refZdotprodZv_lrN   Zu_l_lZu_l_rZv_l_rZlogl_l_rZv_l_lZlogl_l_lZNchordsZaxlenÚnlÚnrZ
axlen_initZaxprobr]   Zu_mZu_hatZrpropr/   r0   r1   Úsr   r   r   r   r   ¸  sö   9














&













&




"


:)NNNN)Ú__doc__Úcollectionsr   rG   Únumpyr   r   Úutilsr   r   r   Zboundingr   Ú__all__r   r	   r
   r    r+   r>   rS   r   r   r   r   r   r   r   Ú<module>	   s0   
A;y   
)% mg