a
    ߙfbeI                     @   s   d Z ddlZddlZddlmZ ddlmZ g dZdddZ	G d	d
 d
Z
G dd de
ZG dd de
ZG dd de
ZdS )ug  
Bayesian Blocks for Time Series Analysis
========================================

Dynamic programming algorithm for solving a piecewise-constant model for
various datasets. This is based on the algorithm presented in Scargle
et al 2013 [1]_. This code was ported from the astroML project [2]_.

Applications include:

- finding an optimal histogram with adaptive bin widths
- finding optimal segmentation of time series data
- detecting inflection points in the rate of event data

The primary interface to these routines is the :func:`bayesian_blocks`
function. This module provides fitness functions suitable for three types
of data:

- Irregularly-spaced event data via the :class:`Events` class
- Regularly-spaced event data via the :class:`RegularEvents` class
- Irregularly-spaced point measurements via the :class:`PointMeasures` class

For more fine-tuned control over the fitness functions used, it is possible
to define custom :class:`FitnessFunc` classes directly and use them with
the :func:`bayesian_blocks` routine.

One common application of the Bayesian Blocks algorithm is the determination
of optimal adaptive-width histogram bins. This uses the same fitness function
as for irregularly-spaced time series events. The easiest interface for
creating Bayesian Blocks histograms is the :func:`astropy.stats.histogram`
function.

References
----------
.. [1] https://ui.adsabs.harvard.edu/abs/2013ApJ...764..167S
.. [2] https://www.astroml.org/ https://github.com//astroML/astroML/
.. [3] Bellman, R.E., Dreyfus, S.E., 1962. Applied Dynamic
   Programming. Princeton University Press, Princeton.
   https://press.princeton.edu/books/hardcover/9780691651873/applied-dynamic-programming
.. [4] Bellman, R., Roth, R., 1969. Curve fitting by segmented
   straight lines. J. Amer. Statist. Assoc. 64, 1079–1084.
   https://www.tandfonline.com/doi/abs/10.1080/01621459.1969.10501038
    N)	signature)AstropyUserWarning)FitnessFuncEventsRegularEventsPointMeasuresbayesian_blockseventsc                 K   sd   t ttd}|||}t|tu r>t|tr>|f i |}nt|trN|}ntd|	| ||S )u  Compute optimal segmentation of data with Scargle's Bayesian Blocks

    This is a flexible implementation of the Bayesian Blocks algorithm
    described in Scargle 2013 [1]_.

    Parameters
    ----------
    t : array-like
        data times (one dimensional, length N)
    x : array-like, optional
        data values
    sigma : array-like or float, optional
        data errors
    fitness : str or object
        the fitness function to use for the model.
        If a string, the following options are supported:

        - 'events' : binned or unbinned event data.  Arguments are ``gamma``,
          which gives the slope of the prior on the number of bins, or
          ``ncp_prior``, which is :math:`-\ln({\tt gamma})`.
        - 'regular_events' : non-overlapping events measured at multiples of a
          fundamental tick rate, ``dt``, which must be specified as an
          additional argument.  Extra arguments are ``p0``, which gives the
          false alarm probability to compute the prior, or ``gamma``, which
          gives the slope of the prior on the number of bins, or ``ncp_prior``,
          which is :math:`-\ln({\tt gamma})`.
        - 'measures' : fitness for a measured sequence with Gaussian errors.
          Extra arguments are ``p0``, which gives the false alarm probability
          to compute the prior, or ``gamma``, which gives the slope of the
          prior on the number of bins, or ``ncp_prior``, which is
          :math:`-\ln({\tt gamma})`.

        In all three cases, if more than one of ``p0``, ``gamma``, and
        ``ncp_prior`` is chosen, ``ncp_prior`` takes precedence over ``gamma``
        which takes precedence over ``p0``.

        Alternatively, the fitness parameter can be an instance of
        :class:`FitnessFunc` or a subclass thereof.

    **kwargs :
        any additional keyword arguments will be passed to the specified
        :class:`FitnessFunc` derived class.

    Returns
    -------
    edges : ndarray
        array containing the (N+1) edges defining the N bins

    Examples
    --------

    .. testsetup::

        >>> np.random.seed(12345)

    Event data:

    >>> t = np.random.normal(size=100)
    >>> edges = bayesian_blocks(t, fitness='events', p0=0.01)

    Event data with repeats:

    >>> t = np.random.normal(size=100)
    >>> t[80:] = t[:20]
    >>> edges = bayesian_blocks(t, fitness='events', p0=0.01)

    Regular event data:

    >>> dt = 0.05
    >>> t = dt * np.arange(1000)
    >>> x = np.zeros(len(t))
    >>> x[np.random.randint(0, len(t), len(t) // 10)] = 1
    >>> edges = bayesian_blocks(t, x, fitness='regular_events', dt=dt)

    Measured point data with errors:

    >>> t = 100 * np.random.random(100)
    >>> x = np.exp(-0.5 * (t - 50) ** 2)
    >>> sigma = 0.1
    >>> x_obs = np.random.normal(x, sigma)
    >>> edges = bayesian_blocks(t, x_obs, sigma, fitness='measures')

    References
    ----------
    .. [1] Scargle, J et al. (2013)
       https://ui.adsabs.harvard.edu/abs/2013ApJ...764..167S

    .. [2] Bellman, R.E., Dreyfus, S.E., 1962. Applied Dynamic
       Programming. Princeton University Press, Princeton.
       https://press.princeton.edu/books/hardcover/9780691651873/applied-dynamic-programming

    .. [3] Bellman, R., Roth, R., 1969. Curve fitting by segmented
       straight lines. J. Amer. Statist. Assoc. 64, 1079–1084.
       https://www.tandfonline.com/doi/abs/10.1080/01621459.1969.10501038

    See Also
    --------
    astropy.stats.histogram : compute a histogram using bayesian blocks
    )r	   Zregular_eventsZmeasuresz fitness parameter not understood)
r   r   r   gettype
issubclassr   
isinstance
ValueErrorfit)txsigmafitnesskwargsZFITNESS_DICTZfitfunc r   <lib/python3.9/site-packages/astropy/stats/bayesian_blocks.pyr   ;   s    e
r   c                   @   sR   e Zd ZdZdddZdddZdd	 Zd
d Zedd Z	dd Z
dddZdS )r   a2  Base class for bayesian blocks fitness functions

    Derived classes should overload the following method:

    ``fitness(self, **kwargs)``:
      Compute the fitness given a set of named arguments.
      Arguments accepted by fitness must be among ``[T_k, N_k, a_k, b_k, c_k]``
      (See [1]_ for details on the meaning of these parameters).

    Additionally, other methods may be overloaded as well:

    ``__init__(self, **kwargs)``:
      Initialize the fitness function with any parameters beyond the normal
      ``p0`` and ``gamma``.

    ``validate_input(self, t, x, sigma)``:
      Enable specific checks of the input data (``t``, ``x``, ``sigma``)
      to be performed prior to the fit.

    ``compute_ncp_prior(self, N)``: If ``ncp_prior`` is not defined explicitly,
      this function is called in order to define it before fitting. This may be
      calculated from ``gamma``, ``p0``, or whatever method you choose.

    ``p0_prior(self, N)``:
      Specify the form of the prior given the false-alarm probability ``p0``
      (See [1]_ for details).

    For examples of implemented fitness functions, see :class:`Events`,
    :class:`RegularEvents`, and :class:`PointMeasures`.

    References
    ----------
    .. [1] Scargle, J et al. (2013)
       https://ui.adsabs.harvard.edu/abs/2013ApJ...764..167S
    皙?Nc                 C   s   || _ || _|| _d S N)p0gamma	ncp_priorselfr   r   r   r   r   r   __init__   s    zFitnessFunc.__init__c                 C   s*  t j|td}t |}|jdkr*tdt j|ddd\}}}|du r|durZtdnd}t|t|krzt |}n
t 	|}|}n\t j|td}|j
dd	|jffvrtd
|t |7 }t|t|krtd|}|| }|du rd}n,t j|td}|j
dd	|jffvr td|||fS )a  Validate inputs to the model.

        Parameters
        ----------
        t : array-like
            times of observations
        x : array-like, optional
            values observed at each time
        sigma : float or array-like, optional
            errors in values x

        Returns
        -------
        t, x, sigma : array-like, float or None
            validated and perhaps modified versions of inputs
        Zdtype   z!t must be a one-dimensional arrayT)Zreturn_indexZreturn_inverseNz*If sigma is specified, x must be specifiedr   )r    zx does not match shape of tz6Repeated values in t not supported when x is specifiedz#sigma does not match the shape of x)npZasarrayfloatZarrayndimr   uniquelen	ones_likeZbincountshapesizeZ
zeros_like)r   r   r   r   Zunq_tZunq_indZunq_invr   r   r   validate_input   s:    



zFitnessFunc.validate_inputc                 K   s
   t  d S r   )NotImplementedError)r   r   r   r   r   r     s    zFitnessFunc.fitnessc                 C   s   dt d| j |d   S )aD  
        Empirical prior, parametrized by the false alarm probability ``p0``
        See  eq. 21 in Scargle (2013)

        Note that there was an error in this equation in the original Scargle
        paper (the "log" was missing). The following corrected form is taken
        from https://arxiv.org/abs/1304.2818
           gRaR@gd;O޿)r!   logr   r   Nr   r   r   p0_prior  s    	zFitnessFunc.p0_priorc                 C   s   t | jj S r   )r   r   
parameterskeys)r   r   r   r   _fitness_args*  s    zFitnessFunc._fitness_argsc                 C   s8   | j durt| j  S | jdur,| |S tddS )zj
        If ``ncp_prior`` is not explicitly defined, compute it from ``gamma``
        or ``p0``.
        NzL``ncp_prior`` cannot be computed as neither ``gamma`` nor ``p0`` is defined.)r   r!   r,   r   r/   r   r-   r   r   r   compute_ncp_prior.  s
    


zFitnessFunc.compute_ncp_priorc                 C   s  |  |||\}}}d| jv r0t||d  }d| jv rF||d  }d| jv r`|| |d  }t|dd d|dd |dd   |dd g}|d | }t|}	tj|	td	}
tj|	td	}| j	du r| 
|	}n| j	}t|	D ]x}i }d
| jv r"|d|d  ||d   |d
< d| jv r\t|d|d  ddd ddd |d< d| jv rdt|d|d  ddd ddd  |d< d| jv rt|d|d  ddd ddd  |d< d| jv rdt|d|d  ddd ddd  |d< | jf i |}|| }|dd  |
d| 7  < t|}|||< || |
|< qtj|	td	}|	}|	}|dkr|d8 }|||< |dkrq||d  }q~|dkrd||< ||d }|| S )a  Fit the Bayesian Blocks model given the specified fitness function.

        Parameters
        ----------
        t : array-like
            data times (one dimensional, length N)
        x : array-like, optional
            data values
        sigma : array-like or float, optional
            data errors

        Returns
        -------
        edges : ndarray
            array containing the (M+1) edges defining the M optimal bins
        a_k   b_kZc_kNr    g      ?r   T_kN_kr   )r)   r2   r!   r&   Zconcatenater%   Zzerosr"   intr   r3   rangeZcumsumr   Zargmax)r   r   r   r   Zak_rawZbk_rawZck_rawZedgesZblock_lengthr.   ZbestZlastr   RkwdsZfit_vecZA_RZi_maxZchange_pointsZi_cpZindr   r   r   r   <  sb    




 .202



zFitnessFunc.fit)r   NN)NN)NN)__name__
__module____qualname____doc__r   r)   r   r/   propertyr2   r3   r   r   r   r   r   r      s   #

B
r   c                       s(   e Zd ZdZdd Z fddZ  ZS )r   af  Bayesian blocks fitness for binned or unbinned events

    Parameters
    ----------
    p0 : float, optional
        False alarm probability, used to compute the prior on
        :math:`N_{\rm blocks}` (see eq. 21 of Scargle 2013). For the Events
        type data, ``p0`` does not seem to be an accurate representation of the
        actual false alarm probability. If you are using this fitness function
        for a triggering type condition, it is recommended that you run
        statistical trials on signal-free noise to determine an appropriate
        value of ``gamma`` or ``ncp_prior`` to use for a desired false alarm
        rate.
    gamma : float, optional
        If specified, then use this gamma to compute the general prior form,
        :math:`p \sim {\tt gamma}^{N_{\rm blocks}}`.  If gamma is specified, p0
        is ignored.
    ncp_prior : float, optional
        If specified, use the value of ``ncp_prior`` to compute the prior as
        above, using the definition :math:`{\tt ncp\_prior} = -\ln({\tt
        gamma})`.
        If ``ncp_prior`` is specified, ``gamma`` and ``p0`` is ignored.
    c                 C   s   |t ||  S r   )r!   r,   )r   r9   r8   r   r   r   r     s    zEvents.fitnessc                    sB   t  |||\}}}|d ur8t|d dkr8td|||fS )Nr    r   z-x must be integer counts for fitness='events')superr)   r!   anyr   r   r   r   r   	__class__r   r   r)     s    zEvents.validate_input)r>   r?   r@   rA   r   r)   __classcell__r   r   rF   r   r     s   r   c                       s6   e Zd ZdZd
 fdd	Z fddZdd	 Z  ZS )r   a  Bayesian blocks fitness for regular events

    This is for data which has a fundamental "tick" length, so that all
    measured values are multiples of this tick length.  In each tick, there
    are either zero or one counts.

    Parameters
    ----------
    dt : float
        tick rate for data
    p0 : float, optional
        False alarm probability, used to compute the prior on :math:`N_{\rm
        blocks}` (see eq. 21 of Scargle 2013). If gamma is specified, p0 is
        ignored.
    ncp_prior : float, optional
        If specified, use the value of ``ncp_prior`` to compute the prior as
        above, using the definition :math:`{\tt ncp\_prior} = -\ln({\tt
        gamma})`.  If ``ncp_prior`` is specified, ``gamma`` and ``p0`` are
        ignored.
    r   Nc                    s   || _ t ||| d S r   )dtrC   r   )r   rI   r   r   r   rF   r   r   r     s    zRegularEvents.__init__c                    s>   t  |||\}}}t|dk|dkB s4td|||fS )Nr   r    z*Regular events must have only 0 and 1 in x)rC   r)   r!   allr   rE   rF   r   r   r)     s    zRegularEvents.validate_inputc                 C   st   || j  }|| }d}t|d| kr4tdt d| }d||dk< d||dk< |t| || t|  S )Ng:0yE>r    z3regular events: N/M > 1.  Is the time step correct?r   )rI   r!   rD   warningswarnr   r,   )r   r8   r9   ZM_kZN_over_MZepsZone_m_NMr   r   r   r     s    
zRegularEvents.fitness)r   NN)r>   r?   r@   rA   r   r)   r   rH   r   r   rF   r   r     s   r   c                       s6   e Zd ZdZd
 fdd	Zdd Z fdd	Z  ZS )r   a!  Bayesian blocks fitness for point measures

    Parameters
    ----------
    p0 : float, optional
        False alarm probability, used to compute the prior on :math:`N_{\rm
        blocks}` (see eq. 21 of Scargle 2013). If gamma is specified, p0 is
        ignored.
    ncp_prior : float, optional
        If specified, use the value of ``ncp_prior`` to compute the prior as
        above, using the definition :math:`{\tt ncp\_prior} = -\ln({\tt
        gamma})`.  If ``ncp_prior`` is specified, ``gamma`` and ``p0`` are
        ignored.
    r   Nc                    s   t  ||| d S r   )rC   r   r   rF   r   r   r     s    zPointMeasures.__init__c                 C   s   || d|  S )Nr+   r   )r   r4   r6   r   r   r   r     s    zPointMeasures.fitnessc                    s    |d u rt dt |||S )Nz&x must be specified for point measures)r   rC   r)   rE   rF   r   r   r)   
  s    zPointMeasures.validate_input)r   NN)r>   r?   r@   rA   r   r   r)   rH   r   r   rF   r   r     s   r   )NNr	   )rA   rK   Znumpyr!   inspectr   Zastropy.utils.exceptionsr   __all__r   r   r   r   r   r   r   r   r   <module>   s   +  
t r$0