def check_divergences(samples, quiet=False, return_diagnostics=False):
    """Check transitions that ended with a divergence.

    Parameters
    ----------
    samples : ArviZ InferenceData instance
        Contains samples to be checked. Must contain both `posterior`
        and `samples_stats`.
    quiet : bool, default False
        If True, do not print diagnostic result to the screen.
    return_diagnostics : bool, default False
        If True, return both a Boolean about whether the diagnostic
        passed and the number of samples where the tree depth was too
        deep. Otherwise, only return Boolean if the test passed.

    Returns
    -------
    passed : bool
        Return True if tree depth test passed. Return False otherwise.
    n_divergent : int, optional
        Number of divergent samples.
    """
    n_divergent = samples.sample_stats['diverging'].values.sum()
    n_total = samples.sample_stats.dims['chain'] * samples.sample_stats.dims[
        'draw']
    if not quiet:
        msg = '{} of {} ({}%) iterations ended with a divergence.'.format(
            n_divergent, n_total, 100 * n_divergent / n_total)
        print(msg)
    pass_check = n_divergent == 0
    if not pass_check and not quiet:
        print('  Try running with larger adapt_delta to remove divergences.')
    if return_diagnostics:
        return pass_check, n_divergent
    return pass_check
