U
    d»{j3?  ã                   @   s  d Z ddlmZ ddlZddlmZ dd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zd5dd„Zd6dd„Zd7dd„Zd8dd„Zd9dd„Zd:dd„Zd;dd„Zd<d d!„Zed=d"d#„ƒZd>d$d%„Zed?d&d'„ƒZd@d)d*„ZdAd+d,„ZdBd-d.„Zeeeeed/œZdCd1d2„Zeeeeed/œZdDd3d4„ZdS )Ezê
Utilities for computing periodogram statistics.

This is an internal module; users should access this functionality via the
``false_alarm_probability`` and ``false_alarm_level`` methods of the
``astropy.timeseries.LombScargle`` API.
é    ©ÚwrapsN)Úunitsc                 C   s$   |d k	r| |d    ¡ S |   ¡ S d S ©Né   )Úsum©ÚvalÚdy© r   úv/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/timeseries/periodograms/lombscargle/_statistics.pyÚ_weighted_sum   s    r   c                 C   s0   |d kr|   ¡ S t| |ƒtt | j¡|ƒ S d S ©N)Zmeanr   ÚnpZonesÚshaper   r   r   r   Ú_weighted_mean   s    r   c                 C   s   t | d |ƒt | |ƒd  S r   )r   r   r   r   r   Ú_weighted_var   s    r   c                 C   s<   ddl m} t d|  ¡t || d ƒ|| d d ƒ ¡ S )Nr   )Úgammalnr   é   )Zscipy.specialr   r   ÚsqrtÚexp)ÚNr   r   r   r   Ú_gamma"   s    r   c                    s   t ˆ ƒ‡ fdd„ƒ}|S )Nc                    s0   t  | ¡} t  ‡ ‡‡fdd„| jD ƒ¡ | j¡S )Nc                    s   g | ]}ˆ|fˆ žˆŽ‘qS r   r   )Ú.0Úxi)ÚargsÚfuncÚkwargsr   r   Ú
<listcomp>-   s     z>vectorize_first_argument.<locals>.new_func.<locals>.<listcomp>)r   ÚasarrayÚarrayZflatZreshaper   )Úxr   r   ©r   )r   r   r   Únew_func*   s    
z*vectorize_first_argument.<locals>.new_funcr   )r   r#   r   r"   r   Úvectorize_first_argument)   s    r$   r   é   c                 C   s¸   t  | ¡} || dkrtdƒ‚|| }|dkr:t  |  ¡S |dkr^d| d|  d| d   S |dkr‚d| d|  d| d   S |d	kr¤d| t  d| |  ¡ S td
|› d�ƒ‚dS )aÂ  Probability density function for Lomb-Scargle periodogram

    Compute the expected probability density function of the periodogram
    for the null hypothesis - i.e. data consisting of Gaussian noise.

    Parameters
    ----------
    z : array-like
        The periodogram value.
    N : int
        The number of data points from which the periodogram was computed.
    normalization : {'standard', 'model', 'log', 'psd'}
        The periodogram normalization.
    dH, dK : int, optional
        The number of parameters in the null hypothesis and the model.

    Returns
    -------
    pdf : np.ndarray
        The expected probability density function.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r   úDegrees of freedom != 2ÚpsdÚstandardç      à?r   Úmodelç      à¿Úlogúnormalization='ú' is not recognizedN©r   r   ÚNotImplementedErrorr   Ú
ValueError©Úzr   ÚnormalizationÚdHÚdKÚNkr   r   r   Ú
pdf_single2   s     
r8   c                 C   s˜   t  | ¡} || dkrtdƒ‚|| }|dkr:t  |  ¡S |dkrRd|  d|  S |dkrjd|  d|  S |d	kr„t  d| |  ¡S td
|› d�ƒ‚dS )a%  Single-frequency false alarm probability for the Lomb-Scargle periodogram

    This is equal to 1 - cdf, where cdf is the cumulative distribution.
    The single-frequency false alarm probability should not be confused with
    the false alarm probability for the largest peak.

    Parameters
    ----------
    z : array-like
        The periodogram value.
    N : int
        The number of data points from which the periodogram was computed.
    normalization : {'standard', 'model', 'log', 'psd'}
        The periodogram normalization.
    dH, dK : int, optional
        The number of parameters in the null hypothesis and the model.

    Returns
    -------
    false_alarm_probability : np.ndarray
        The single-frequency false alarm probability.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r   r&   r'   r(   r   r)   r*   r+   r,   r-   r.   Nr/   r2   r   r   r   Ú
fap_singlec   s    !
r9   c              
   C   sà   t  | ¡} || dkrtdƒ‚|| }t jdd��¤ |dkrTt  | ¡ W  5 Q R £ S |dkrxd| d|   W  5 Q R £ S |dkrœd	| d
|   W  5 Q R £ S |dkrÂd
| t  | ¡ W  5 Q R £ S td|› d�ƒ‚W 5 Q R X dS )a*  Single-frequency inverse false alarm probability

    This function computes the periodogram value associated with the specified
    single-frequency false alarm probability. This should not be confused with
    the false alarm level of the largest peak.

    Parameters
    ----------
    fap : array-like
        The false alarm probability.
    N : int
        The number of data points from which the periodogram was computed.
    normalization : {'standard', 'model', 'log', 'psd'}
        The periodogram normalization.
    dH, dK : int, optional
        The number of parameters in the null hypothesis and the model.

    Returns
    -------
    z : np.ndarray
        The periodogram power corresponding to the single-peak false alarm
        probability.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r   r&   Úignore©Údivider'   r(   r   r*   éÿÿÿÿéþÿÿÿr,   r-   r.   N)r   r   r0   Úerrstater,   r1   )Úfapr   r4   r5   r6   r7   r   r   r   Úinv_fap_single•   s    "
rA   c                 C   s   dt | ||||d� S )aÀ  Cumulative distribution for the Lomb-Scargle periodogram

    Compute the expected cumulative distribution of the periodogram
    for the null hypothesis - i.e. data consisting of Gaussian noise.

    Parameters
    ----------
    z : array-like
        The periodogram value.
    N : int
        The number of data points from which the periodogram was computed.
    normalization : {'standard', 'model', 'log', 'psd'}
        The periodogram normalization.
    dH, dK : int, optional
        The number of parameters in the null hypothesis and the model.

    Returns
    -------
    cdf : np.ndarray
        The expected cumulative distribution function.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r   )r4   r5   r6   )r9   )r3   r   r4   r5   r6   r   r   r   Ú
cdf_singleÊ   s     rB   r(   c                 C   s0  t |ƒ}|| }	|| }
t||ƒ}t dtj | ¡}|| }t | ¡} |dkrj|t |  ¡ t | ¡ S |dkr¤t|	ƒ| d|  d|
d    t d|	 |  ¡ S |dkrÚt|
ƒ| d|  d|
   t d|
 |  ¡ S |dk�rt|
ƒ| t d|  |
d  ¡ t |
t d|  ¡ ¡ S t	d	|› �ƒ‚d
S )z=tau factor for estimating Davies bound (Baluev 2008, Table 1)é   r'   r(   r   r)   r*   r+   r,   znormalization=N)
Úlenr   r   r   Úpir   r   r   Úsinhr0   )ÚZÚfmaxÚtÚyr
   r4   r5   r6   r   ZNHZNKZDtZTeffÚWr   r   r   Ú
tau_daviesí   s.    

2.
ÿþýÿrL   c           
   
   C   sj   t |ƒ}t|ƒt|ƒ }|| }t| ||d�}	tjdd��& t |t |	 ¡ ¡ W  5 Q R £ S Q R X dS )zFFalse Alarm Probability based on estimated number of indep frequencies©r4   r:   r;   N)rD   ÚmaxÚminr9   r   r?   Úexpm1Úlog1p)
rG   rH   rI   rJ   r
   r4   r   ÚTÚN_effÚfap_sr   r   r   Ú	fap_naive  s    rU   c           
   	   C   sh   t  | ¡} t|ƒ}t|ƒt|ƒ }|| }t jdd��  t  t  d|  ¡| ¡ }	W 5 Q R X t|	||ƒS )z:Inverse FAP based on estimated number of indep frequenciesr:   r;   r   )	r   r   rD   rN   rO   r?   rP   r,   rA   )
r@   rH   rI   rJ   r
   r4   r   rR   rS   rT   r   r   r   Úinv_fap_naive  s    
$rV   c           	      C   s2   t |ƒ}t| ||d�}t| |||||d�}|| S )zRDavies upper-bound to the false alarm probability

    (Eqn 5 of Baluev 2008)
    rM   )rD   r9   rL   )	rG   rH   rI   rJ   r
   r4   r   rT   Útaur   r   r   Ú
fap_davies%  s    rX   c                    s`   ddl m} |||||f}tˆ f|žŽ }‡ fdd„}	|j|	||dd�}
|
jsZtdˆ › �ƒ‚|
jS )z!Inverse of the davies upper-boundr   ©Úoptimizec                    s   t | f|žŽ ˆ  S r   )rX   ©r3   r   ©Úpr   r   Ú<lambda>7  ó    z inv_fap_davies.<locals>.<lambda>Úlm©r   Úmethodú&inv_fap_baluev did not converge for p=©ZscipyrZ   rV   ÚrootÚsuccessr1   r!   ©r]   rH   rI   rJ   r
   r4   rZ   r   Zz0r   Úresr   r\   r   Úinv_fap_davies0  s    ri   c                 C   sB   t | t|ƒ|ƒ}t| |||||d�}t | ¡ |t | ¡  S )zTAlias-free approximation to false alarm probability

    (Eqn 6 of Baluev 2008)
    rM   )r9   rD   rL   r   rP   r   )rG   rH   rI   rJ   r
   r4   rT   rW   r   r   r   Ú
fap_baluev>  s    rj   c                    s`   ddl m} |||||f}tˆ f|žŽ }‡ fdd„}	|j|	||dd�}
|
jsZtdˆ › �ƒ‚|
jS )z.Inverse of the Baluev alias-free approximationr   rY   c                    s   t | f|žŽ ˆ  S r   )rj   r[   r\   r   r   r^   Q  r_   z inv_fap_baluev.<locals>.<lambda>r`   ra   rc   rd   rg   r   r\   r   Úinv_fap_baluevJ  s    rk   éè  c                 C   s˜   ddl m} tj |¡}g }	t|ƒD ]\}
| dt|ƒt|ƒ¡}|| || |dkrT|n|| |d�}|j|d�\}}|	 	| 
¡ ¡ q$t |	¡}	|	 ¡  |	S )z5Generate a sequence of bootstrap estimates of the maxr   )ÚLombScargler   NrM   )Zmaximum_frequency)Úcorerm   r   ÚrandomZdefault_rngÚrangeZintegersrD   Z	autopowerÚappendrN   ÚuZQuantityÚsort)rI   rJ   r
   rH   r4   Úrandom_seedZn_bootstraprm   ÚrngZ	power_maxÚ_ÚsZls_bootÚfreqÚpowerr   r   r   Ú_bootstrap_maxX  s        ÿ
rz   c           	      C   s,   t |||||||ƒ}dt || ¡t|ƒ  S )z1Bootstrap estimate of the false alarm probabilityr   )rz   r   ZsearchsortedrD   )	rG   rH   rI   rJ   r
   r4   Ún_bootstrapsrt   Úpmaxr   r   r   Úfap_bootstrapl  s    r}   c           	      C   sP   t  | ¡} t|||||||ƒ}|t  t  d|  t|ƒ ¡ t¡dt|ƒd ¡ S )z9Bootstrap estimate of the inverse false alarm probabilityr   r   )r   r   rz   ZclipÚfloorrD   ZastypeÚint)	r@   rH   rI   rJ   r
   r4   r{   rt   r|   r   r   r   Úinv_fap_bootstrapu  s    
r€   )ÚsingleZnaiveZdaviesÚbaluevZ	bootstrapr‚   c                 C   sT   |dkrt | t|ƒ|ƒS |tkr.td|› �ƒ‚t| }|p<i }|| |||||f|ŽS )a#  Compute the approximate false alarm probability for periodogram peaks Z

    This gives an estimate of the false alarm probability for the largest value
    in a periodogram, based on the null hypothesis of non-varying data with
    Gaussian noise. The true probability cannot be computed analytically, so
    each method available here is an approximation to the true value.

    Parameters
    ----------
    Z : array-like
        The periodogram value.
    fmax : float
        The maximum frequency of the periodogram.
    t, y, dy : array-like
        The data times, values, and errors.
    normalization : {'standard', 'model', 'log', 'psd'}, optional
        The periodogram normalization.
    method : {'baluev', 'davies', 'naive', 'bootstrap'}, optional
        The approximation method to use.
    method_kwds : dict, optional
        Additional method-specific keywords.

    Returns
    -------
    false_alarm_probability : np.ndarray
        The false alarm probability.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.

    See Also
    --------
    false_alarm_level : compute the periodogram level for a particular fap

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r�   úUnrecognized method: )r9   rD   ÚMETHODSr1   )rG   rH   rI   rJ   r
   r4   rb   Úmethod_kwdsr   r   r   Úfalse_alarm_probabilityˆ  s    +r†   c                 C   sT   |dkrt | t|ƒ|ƒS |tkr.td|› �ƒ‚t| }|p<i }|| |||||f|ŽS )a1  Compute the approximate periodogram level given a false alarm probability

    This gives an estimate of the periodogram level corresponding to a specified
    false alarm probability for the largest peak, assuming a null hypothesis
    of non-varying data with Gaussian noise. The true level cannot be computed
    analytically, so each method available here is an approximation to the true
    value.

    Parameters
    ----------
    p : array-like
        The false alarm probability (0 < p < 1).
    fmax : float
        The maximum frequency of the periodogram.
    t, y, dy : arrays
        The data times, values, and errors.
    normalization : {'standard', 'model', 'log', 'psd'}, optional
        The periodogram normalization.
    method : {'baluev', 'davies', 'naive', 'bootstrap'}, optional
        The approximation method to use.
    method_kwds : dict, optional
        Additional method-specific keywords.

    Returns
    -------
    z : np.ndarray
        The periodogram level.

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.

    See Also
    --------
    false_alarm_probability : compute the fap for a given periodogram level

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    r�   rƒ   )rA   rD   ÚINV_METHODSr1   )r]   rH   rI   rJ   r
   r4   rb   r…   r   r   r   Úfalse_alarm_levelÆ  s    ,rˆ   )r   r%   )r   r%   )r   r%   )r   r%   )r(   r   r%   )r(   )r(   )r(   )r(   )r(   )r(   )rl   )r(   rl   N)r(   rl   N)r(   r‚   N)r‚   N)Ú__doc__Ú	functoolsr   Únumpyr   Zastropyr   rr   r   r   r   r   r$   r8   r9   rA   rB   rL   rU   rV   rX   ri   rj   rk   rz   r}   r€   r„   r†   r‡   rˆ   r   r   r   r   Ú<module>   sb   	
1
2
5
#





     ÿ

     ÿ
û
     ÿ
6û
   ÿ