
    _Mhg                    X   d dl Z d dlZd dlmZmZmZ d dlmZ d dl	m
Z
mZ d dlZd dlmZmZmZmZ d dlmZmZmZ d dlmZmZmZmZ dd	lmZ dd
lmZmZ ddl m!Z! g dZ"d Z#dBdZ$dCdZ%d Z&d Z'd Z(d Z)e
 G d d                      Z* ed          ddddd dddddd
d            Z+d Z,e
 G d d                       Z- ed!d"          ddddd d#d$            Z.e
 G d% d&                      Z/d' Z0d( Z1d)dd*ddd+d,Z2e
 G d- d.                      Z3d/ Z4d0 Z5d1 Z6	 dBd2Z7	 dBd3Z8	 dBd4Z9d5 Z: ed          d6ddddd dd7d8            Z;e
 G d9 d:                      Z<e
 G d; d<e<                      Z=d=Z>e
 G d> d?e<                      Z?e
 G d@ dAe<                      Z@dS )D    N)combinationspermutationsproduct)Sequence)	dataclassfield)check_random_state_rename_parameterrng_integers_transition_to_rng)array_namespaceis_numpyxp_moveaxis_to_end)ndtrndtricomb	factorial   )ConfidenceInterval)_broadcast_concatenate_broadcast_arrays)DegenerateDataWarning)	bootstrapmonte_carlo_testpermutation_testc                      dd fd
}|S )zVectorize an n-sample statisticr   axisc                       fd|D             }t          j        |          d d         t          |           }t          j        | d          }fd}t          j        |d|          d         S )Nc                 *    g | ]}|j                  S  shape).0sampler   s     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/stats/_resampling.py
<listcomp>z9_vectorize_statistic.<locals>.stat_nd.<locals>.<listcomp>   s     999&6<%999    r   c                 8    t          j        |           } | S N)npsplit)zdatasplit_indices	statistics     r&   stat_1dz6_vectorize_statistic.<locals>.stat_nd.<locals>.stat_1d"   s!    8A}--D9d##r(   r!   )r,   cumsumr   moveaxisapply_along_axis)r   r/   lengthsr.   r2   r0   r1   s   `    @r&   stat_ndz%_vectorize_statistic.<locals>.stat_nd   s    9999D999	'**3B3/"4..
 K4##	$ 	$ 	$ 	$ 	$ 	$ "7Aq11"55r(   r!   )r1   r7   s   ` r&   _vectorize_statisticr8      s2      6 6 6 6 6 6 6 Nr(   c              #     K   | j         d         }|p|}t          d||          D ]}t          |||z
            }t          j        ||ft
                    }t          j        |dd|||z   f         d           t          j        |          }t          j        |||f          }||         	                    ||dz
  f          }| d|f         }|V  dS )z=Jackknife resample the sample. Only one-sample stats for now.r)   r   dtypeNFr   .)
r#   rangeminr,   onesboolfill_diagonalarangebroadcast_toreshape)	r%   batchnbatch_nominalkbatch_actualji	resampless	            r&   _jackknife_resamplerL   *   s      RAJQM1a''  =!A#.. G\1%T222
111a,../777IaLLOAa011aDLL,!,--36N	 r(   c                 \    | j         d         }t          |d|||f          }| d|f         }|S )zBootstrap resample the sample.r)   r   .)r#   r   )r%   n_resamplesrngrE   rJ   rK   s         r&   _bootstrap_resamplerP   >   s;    RA 	S!Qa 011AsAvIr(   c                     | j         |         }| |k                         |          | |k                        |          z   d|z  z  S )zVectorized, simplified `scipy.stats.percentileofscore`.
    Uses logic of the 'mean' value of percentileofscore's kind parameter.

    Unlike `stats.percentileofscore`, the percentile returned is a fraction
    in [0, 1].
    r      )r#   sum)ascorer   Bs       r&   _percentile_of_scorerW   I   sJ     	
AYOOO&&!u*)9)9t)9)D)DDQOOr(   c                    | j         dd         }t          j        ||          }t          j        |t          j                  }t          j        |          D ]n\  }}t          j        |          r5d}t          j        t          |          d           t          j
        ||<   N| |         }t          j        ||          ||<   o|d         S )z9`np.percentile` with different percentile for each slice.Nr)   r:   zThe BCa confidence interval cannot be calculated. This problem is known to occur when the distribution is degenerate or the statistic is np.min.   
stacklevelr!   )r#   r,   rB   
zeros_likefloat64ndenumerateisnanwarningswarnr   nan
percentile)theta_hat_balphar#   percentilesindicesalpha_imsgtheta_hat_b_is           r&   _percentile_along_axisrk   T   s    
 crc"EOE5))E-RZ888KN511 I I8G 	I= 
 M/44CCCC#%6K  '0M#%=#H#HK  r?r(   c           	      ^   t          j         || d|i          d         }t          ||d          }t          |          }g }	t	          |           D ]l\  }
}d | D             }g }t          ||          D ]3}|||
<   t          |d          }|                     ||ddi           4|	                    |           md |	D             }	d |	D             }d |	D             }d	 t          ||	|          D             }d
 t          ||          D             }d t          ||          D             }dt          |          z  t          |          dz  z  }t          |          }| }||z   }t          ||d||z  z
  z  z             }||z   }t          ||d||z  z
  z  z             }|||fS )z(Bias-corrected and accelerated interval.r   ).Nr)   r   c                 8    g | ]}t          j        |d           S ))r,   expand_dimsr$   r%   s     r&   r'   z!_bca_interval.<locals>.<listcomp>}   s$    AAA&2>&"--AAAr(   c                 :    g | ]}t          j        |d           S )r)   r   )r,   concatenater$   theta_hat_is     r&   r'   z!_bca_interval.<locals>.<listcomp>   s7     5 5 5# N;R888 5 5 5r(   c                 (    g | ]}|j         d          S r)   r"   rs   s     r&   r'   z!_bca_interval.<locals>.<listcomp>   s     
A
A
A[;R 
A
A
Ar(   c                 <    g | ]}|                     d d          S )r)   T)r   keepdims)meanrs   s     r&   r'   z!_bca_interval.<locals>.<listcomp>   s;     8 8 8& #''R$'?? 8 8 8r(   c                 ,    g | ]\  }}}|d z
  ||z
  z  S r   r!   )r$   theta_hat_dotrt   rE   s       r&   r'   z!_bca_interval.<locals>.<listcomp>   s<     8 8 8-{A U}{23 8 8 8r(   c                 R    g | ]$\  }}|d z                       d          |d z  z  %S )rY   r)   r   rS   r$   U_irE   s      r&   r'   z!_bca_interval.<locals>.<listcomp>   7    DDD63S!VLLbL!!!Q$&DDDr(   c                 R    g | ]$\  }}|d z                       d          |d z  z  %S )rR   r)   r   r~   r   s      r&   r'   z!_bca_interval.<locals>.<listcomp>   r   r(   gUUUUUU?g      ?r   )r,   asarrayrW   r   	enumeraterL   r   appendziprS   r   )r/   r1   r   re   rd   rD   	theta_hatrc   z0_hattheta_hat_jirI   r%   samplesrt   jackknife_samplebroadcastedn_jtheta_hat_j_dotU_jinumsdensa_hatz_alphaz_1alphanum1alpha_1num2alpha_2s                               r&   _bca_intervalr   l   s>   
 
99d66677	BI%k92FFFJ:F Lt__ ) )	6 BADAAA 3FE B B 	A 	A)GAJ+G"===Kyy+?B??@@@@K((((5 5'35 5 5L B
AL
A
A
AC8 8*68 8 8O8 8?L#668 8 8D EDSs^^DDDDDDSs^^DDDD#d))Oc$ii#..E EllGxHGD6D!eDj.1122GHD6D!eDj.1122GGU""r(   c                 h   |dvrt          d          |dt          j        |          j        v }|st	          |          }t          |          }||k    rt          d          d}	 t          |           }n# t          $ r t          d          w xY w|dk    rt          d          d	}d
 | D             } t                      }| D ]M}t          |j
                  }|                    |           |                    t          |                     Nt          |          dk    rt          j        |t           d           g }| D ]M}|j
        |         dk    rt          d          t#          j        ||d          }|                    |           N|dvrt          d          |r^|d         j
        d         }|dd         D ]$}|j
        d         |k    rd}t          |          %d||fd}t#          j        |          g}t+          |          }|                                }h d}||vrt          d|           t          |          }||k    s|dk     rt          d          ||}n*t          |          }||k    s|dk    rt          d          h d}|	                                }	|	|vrt          d|           d}|
t/          |
d          st          |          d}|
r|
j        j        s|dk    rt          |          t5          |          }||||||||||	|
|fS )z5Input validation and standardization for `bootstrap`.   FNT0`vectorized` must be `True`, `False`, or `None`.Nr   `axis` must be an integer.r   z%`data` must be a sequence of samples.z(`data` must contain at least one sample.ai  Ignoring the dimension specified by `axis`, arrays in `data` do not have the same shape. Beginning in SciPy 1.16.0, `bootstrap` will explicitly broadcast elements of `data` to the same shape (ignoring `axis`) before performing the calculation. To avoid this warning in the meantime, ensure that all samples have the same shape (except potentially along `axis`).c                 6    g | ]}t          j        |          S r!   )r,   
atleast_1drp   s     r&   r'   z!_bootstrap_iv.<locals>.<listcomp>   s"    555fBM&!!555r(   r   rY   rZ   Ieach sample in `data` must contain two or more observations along `axis`.r)   >   FTz#`paired` must be `True` or `False`.zIWhen `paired is True`, all samples must have the same length along `axis`c                 0      fd|D             } ||d|iS )Nc                 $    g | ]}|d f         S ).r!   )r$   r%   rJ   s     r&   r'   z4_bootstrap_iv.<locals>.statistic.<locals>.<listcomp>   s!    666vF36N666r(   r   r!   )rJ   r   r/   unpaired_statistics   `   r&   r1   z _bootstrap_iv.<locals>.statistic   s2    6666666D%%t7$777r(   >   lessgreater	two-sidedz`alternative` must be one of z-`n_resamples` must be a non-negative integer.+`batch` must be a positive integer or None.>   bcabasicrc   z`method` must be in z?`bootstrap_result` must have attribute `bootstrap_distribution'bootstrap_distributionzXEither `bootstrap_result.bootstrap_distribution.size` or `n_resamples` must be positive.)
ValueErrorinspect	signature
parametersr8   intlen	TypeErrorsetlistr#   popaddtupler`   ra   FutureWarningr,   r4   r   rA   floatlowerhasattrr   sizer	   )r/   r1   
vectorizedpairedr   confidence_levelalternativerN   rD   methodbootstrap_resultrO   axis_int	n_samplesmessagereduced_shapesr%   reduced_shapedata_ivrE   confidence_level_floatalternativesn_resamples_intbatch_ivmethodss                            r&   _bootstrap_ivr      s   
 ,,,KLLLw0;;FF
 4(33	4yyHx5666IBII		 B B B@AAAB A~~CDDD,G 65555DUUN 1 1V\**$5//0000
>ag};;;;G  <!Q&& : ; ; ;VXr22v]"">??? !AJR abbk 	* 	*F|B1$$6 ))) % !w9 	8 	8 	8 	8 9Q<<."#344##%%K333L,&&GGGHHH+&&Oo%%1)<)<HIII}u::HAJKKK,,,G\\^^FW999:::OG$,.FGG 	%!!!1G "49"1$$!!!
S
!
!CY
FH"K($c+ +s   )A9 9Bc                   L    e Zd ZU dZeed<   ej        ed<   eej        z  ed<   dS )BootstrapResulta  Result object returned by `scipy.stats.bootstrap`.

    Attributes
    ----------
    confidence_interval : ConfidenceInterval
        The bootstrap confidence interval as an instance of
        `collections.namedtuple` with attributes `low` and `high`.
    bootstrap_distribution : ndarray
        The bootstrap distribution, that is, the value of `statistic` for
        each resample. The last dimension corresponds with the resamples
        (e.g. ``res.bootstrap_distribution.shape[-1] == n_resamples``).
    standard_error : float or ndarray
        The bootstrap standard error, that is, the sample standard
        deviation of the bootstrap distribution.

    confidence_intervalr   standard_errorN)	__name__
__module____qualname____doc__r   __annotations__r,   ndarrayr   r!   r(   r&   r   r     sK            ,+++J&&&BJ&&&&&&r(   r   random_state'  Fgffffff?r   BCa)
rN   rD   r   r   r   r   r   r   r   rO   c       
            t          | |||||||||	|
|          }|\  } }}}}}}}}}	}
}|
g n|
j        g}|p|pd}t          d||          D ]^}t          |||z
            }g }| D ])}t	          |||          }|                    |           *|                     ||ddi           _t          j        |d          }|dk    rd|z
  d	z  nd|z
  }|	d
k    r%t          | |d|||          dd	         }t          }n
|d|z
  f}d } |||d         dz            } |||d         dz            }|	dk    r || ddi}d	|z  |z
  d	|z  |z
  }}|dk    r!t          j
        |t          j                   }n%|dk    rt          j
        |t          j                  }t          t          ||          |t          j        |dd                    S )a9  
    Compute a two-sided bootstrap confidence interval of a statistic.

    When `method` is ``'percentile'`` and `alternative` is ``'two-sided'``,
    a bootstrap confidence interval is computed according to the following
    procedure.

    1. Resample the data: for each sample in `data` and for each of
       `n_resamples`, take a random sample of the original sample
       (with replacement) of the same size as the original sample.

    2. Compute the bootstrap distribution of the statistic: for each set of
       resamples, compute the test statistic.

    3. Determine the confidence interval: find the interval of the bootstrap
       distribution that is

       - symmetric about the median and
       - contains `confidence_level` of the resampled statistic values.

    While the ``'percentile'`` method is the most intuitive, it is rarely
    used in practice. Two more common methods are available, ``'basic'``
    ('reverse percentile') and ``'BCa'`` ('bias-corrected and accelerated');
    they differ in how step 3 is performed.

    If the samples in `data` are  taken at random from their respective
    distributions :math:`n` times, the confidence interval returned by
    `bootstrap` will contain the true value of the statistic for those
    distributions approximately `confidence_level`:math:`\, \times \, n` times.

    Parameters
    ----------
    data : sequence of array-like
         Each element of `data` is a sample containing scalar observations from an
         underlying distribution. Elements of `data` must be broadcastable to the
         same shape (with the possible exception of the dimension specified by `axis`).

         .. versionchanged:: 1.14.0
             `bootstrap` will now emit a ``FutureWarning`` if the shapes of the
             elements of `data` are not the same (with the exception of the dimension
             specified by `axis`).
             Beginning in SciPy 1.16.0, `bootstrap` will explicitly broadcast the
             elements to the same shape (except along `axis`) before performing
             the calculation.

    statistic : callable
        Statistic for which the confidence interval is to be calculated.
        `statistic` must be a callable that accepts ``len(data)`` samples
        as separate arguments and returns the resulting statistic.
        If `vectorized` is set ``True``,
        `statistic` must also accept a keyword argument `axis` and be
        vectorized to compute the statistic along the provided `axis`.
    n_resamples : int, default: ``9999``
        The number of resamples performed to form the bootstrap distribution
        of the statistic.
    batch : int, optional
        The number of resamples to process in each vectorized call to
        `statistic`. Memory usage is O( `batch` * ``n`` ), where ``n`` is the
        sample size. Default is ``None``, in which case ``batch = n_resamples``
        (or ``batch = max(n_resamples, n)`` for ``method='BCa'``).
    vectorized : bool, optional
        If `vectorized` is set ``False``, `statistic` will not be passed
        keyword argument `axis` and is expected to calculate the statistic
        only for 1D samples. If ``True``, `statistic` will be passed keyword
        argument `axis` and is expected to calculate the statistic along `axis`
        when passed an ND sample array. If ``None`` (default), `vectorized`
        will be set ``True`` if ``axis`` is a parameter of `statistic`. Use of
        a vectorized statistic typically reduces computation time.
    paired : bool, default: ``False``
        Whether the statistic treats corresponding elements of the samples
        in `data` as paired. If True, `bootstrap` resamples an array of
        *indices* and uses the same indices for all arrays in `data`; otherwise,
        `bootstrap` independently resamples the elements of each array.
    axis : int, default: ``0``
        The axis of the samples in `data` along which the `statistic` is
        calculated.
    confidence_level : float, default: ``0.95``
        The confidence level of the confidence interval.
    alternative : {'two-sided', 'less', 'greater'}, default: ``'two-sided'``
        Choose ``'two-sided'`` (default) for a two-sided confidence interval,
        ``'less'`` for a one-sided confidence interval with the lower bound
        at ``-np.inf``, and ``'greater'`` for a one-sided confidence interval
        with the upper bound at ``np.inf``. The other bound of the one-sided
        confidence intervals is the same as that of a two-sided confidence
        interval with `confidence_level` twice as far from 1.0; e.g. the upper
        bound of a 95% ``'less'``  confidence interval is the same as the upper
        bound of a 90% ``'two-sided'`` confidence interval.
    method : {'percentile', 'basic', 'bca'}, default: ``'BCa'``
        Whether to return the 'percentile' bootstrap confidence interval
        (``'percentile'``), the 'basic' (AKA 'reverse') bootstrap confidence
        interval (``'basic'``), or the bias-corrected and accelerated bootstrap
        confidence interval (``'BCa'``).
    bootstrap_result : BootstrapResult, optional
        Provide the result object returned by a previous call to `bootstrap`
        to include the previous bootstrap distribution in the new bootstrap
        distribution. This can be used, for example, to change
        `confidence_level`, change `method`, or see the effect of performing
        additional resampling without repeating computations.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator state. When `rng` is None, a new
        `numpy.random.Generator` is created using entropy from the
        operating system. Types other than `numpy.random.Generator` are
        passed to `numpy.random.default_rng` to instantiate a ``Generator``.

    Returns
    -------
    res : BootstrapResult
        An object with attributes:

        confidence_interval : ConfidenceInterval
            The bootstrap confidence interval as an instance of
            `collections.namedtuple` with attributes `low` and `high`.
        bootstrap_distribution : ndarray
            The bootstrap distribution, that is, the value of `statistic` for
            each resample. The last dimension corresponds with the resamples
            (e.g. ``res.bootstrap_distribution.shape[-1] == n_resamples``).
        standard_error : float or ndarray
            The bootstrap standard error, that is, the sample standard
            deviation of the bootstrap distribution.

    Warns
    -----
    `~scipy.stats.DegenerateDataWarning`
        Generated when ``method='BCa'`` and the bootstrap distribution is
        degenerate (e.g. all elements are identical).

    Notes
    -----
    Elements of the confidence interval may be NaN for ``method='BCa'`` if
    the bootstrap distribution is degenerate (e.g. all elements are identical).
    In this case, consider using another `method` or inspecting `data` for
    indications that other analysis may be more appropriate (e.g. all
    observations are identical).

    References
    ----------
    .. [1] B. Efron and R. J. Tibshirani, An Introduction to the Bootstrap,
       Chapman & Hall/CRC, Boca Raton, FL, USA (1993)
    .. [2] Nathaniel E. Helwig, "Bootstrap Confidence Intervals",
       http://users.stat.umn.edu/~helwig/notes/bootci-Notes.pdf
    .. [3] Bootstrapping (statistics), Wikipedia,
       https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29

    Examples
    --------
    Suppose we have sampled data from an unknown distribution.

    >>> import numpy as np
    >>> rng = np.random.default_rng()
    >>> from scipy.stats import norm
    >>> dist = norm(loc=2, scale=4)  # our "unknown" distribution
    >>> data = dist.rvs(size=100, random_state=rng)

    We are interested in the standard deviation of the distribution.

    >>> std_true = dist.std()      # the true value of the statistic
    >>> print(std_true)
    4.0
    >>> std_sample = np.std(data)  # the sample statistic
    >>> print(std_sample)
    3.9460644295563863

    The bootstrap is used to approximate the variability we would expect if we
    were to repeatedly sample from the unknown distribution and calculate the
    statistic of the sample each time. It does this by repeatedly resampling
    values *from the original sample* with replacement and calculating the
    statistic of each resample. This results in a "bootstrap distribution" of
    the statistic.

    >>> import matplotlib.pyplot as plt
    >>> from scipy.stats import bootstrap
    >>> data = (data,)  # samples must be in a sequence
    >>> res = bootstrap(data, np.std, confidence_level=0.9, rng=rng)
    >>> fig, ax = plt.subplots()
    >>> ax.hist(res.bootstrap_distribution, bins=25)
    >>> ax.set_title('Bootstrap Distribution')
    >>> ax.set_xlabel('statistic value')
    >>> ax.set_ylabel('frequency')
    >>> plt.show()

    The standard error quantifies this variability. It is calculated as the
    standard deviation of the bootstrap distribution.

    >>> res.standard_error
    0.24427002125829136
    >>> res.standard_error == np.std(res.bootstrap_distribution, ddof=1)
    True

    The bootstrap distribution of the statistic is often approximately normal
    with scale equal to the standard error.

    >>> x = np.linspace(3, 5)
    >>> pdf = norm.pdf(x, loc=std_sample, scale=res.standard_error)
    >>> fig, ax = plt.subplots()
    >>> ax.hist(res.bootstrap_distribution, bins=25, density=True)
    >>> ax.plot(x, pdf)
    >>> ax.set_title('Normal Approximation of the Bootstrap Distribution')
    >>> ax.set_xlabel('statistic value')
    >>> ax.set_ylabel('pdf')
    >>> plt.show()

    This suggests that we could construct a 90% confidence interval on the
    statistic based on quantiles of this normal distribution.

    >>> norm.interval(0.9, loc=std_sample, scale=res.standard_error)
    (3.5442759991341726, 4.3478528599786)

    Due to central limit theorem, this normal approximation is accurate for a
    variety of statistics and distributions underlying the samples; however,
    the approximation is not reliable in all cases. Because `bootstrap` is
    designed to work with arbitrary underlying distributions and statistics,
    it uses more advanced techniques to generate an accurate confidence
    interval.

    >>> print(res.confidence_interval)
    ConfidenceInterval(low=3.57655333533867, high=4.382043696342881)

    If we sample from the original distribution 100 times and form a bootstrap
    confidence interval for each sample, the confidence interval
    contains the true value of the statistic approximately 90% of the time.

    >>> n_trials = 100
    >>> ci_contains_true_std = 0
    >>> for i in range(n_trials):
    ...    data = (dist.rvs(size=100, random_state=rng),)
    ...    res = bootstrap(data, np.std, confidence_level=0.9,
    ...                    n_resamples=999, rng=rng)
    ...    ci = res.confidence_interval
    ...    if ci[0] < std_true < ci[1]:
    ...        ci_contains_true_std += 1
    >>> print(ci_contains_true_std)
    88

    Rather than writing a loop, we can also determine the confidence intervals
    for all 100 samples at once.

    >>> data = (dist.rvs(size=(n_trials, 100), random_state=rng),)
    >>> res = bootstrap(data, np.std, axis=-1, confidence_level=0.9,
    ...                 n_resamples=999, rng=rng)
    >>> ci_l, ci_u = res.confidence_interval

    Here, `ci_l` and `ci_u` contain the confidence interval for each of the
    ``n_trials = 100`` samples.

    >>> print(ci_l[:5])
    [3.86401283 3.33304394 3.52474647 3.54160981 3.80569252]
    >>> print(ci_u[:5])
    [4.80217409 4.18143252 4.39734707 4.37549713 4.72843584]

    And again, approximately 90% contain the true value, ``std_true = 4``.

    >>> print(np.sum((ci_l < std_true) & (std_true < ci_u)))
    93

    `bootstrap` can also be used to estimate confidence intervals of
    multi-sample statistics. For example, to get a confidence interval
    for the difference between means, we write a function that accepts
    two sample arguments and returns only the statistic. The use of the
    ``axis`` argument ensures that all mean calculations are perform in
    a single vectorized call, which is faster than looping over pairs
    of resamples in Python.

    >>> def my_statistic(sample1, sample2, axis=-1):
    ...     mean1 = np.mean(sample1, axis=axis)
    ...     mean2 = np.mean(sample2, axis=axis)
    ...     return mean1 - mean2

    Here, we use the 'percentile' method with the default 95% confidence level.

    >>> sample1 = norm.rvs(scale=1, size=100, random_state=rng)
    >>> sample2 = norm.rvs(scale=2, size=100, random_state=rng)
    >>> data = (sample1, sample2)
    >>> res = bootstrap(data, my_statistic, method='basic', rng=rng)
    >>> print(my_statistic(sample1, sample2))
    0.16661030792089523
    >>> print(res.confidence_interval)
    ConfidenceInterval(low=-0.29087973240818693, high=0.6371338699912273)

    The bootstrap estimate of the standard error is also available.

    >>> print(res.standard_error)
    0.238323948262459

    Paired-sample statistics work, too. For example, consider the Pearson
    correlation coefficient.

    >>> from scipy.stats import pearsonr
    >>> n = 100
    >>> x = np.linspace(0, 10, n)
    >>> y = x + rng.uniform(size=n)
    >>> print(pearsonr(x, y)[0])  # element 0 is the statistic
    0.9954306665125647

    We wrap `pearsonr` so that it returns only the statistic, ensuring
    that we use the `axis` argument because it is available.

    >>> def my_statistic(x, y, axis=-1):
    ...     return pearsonr(x, y, axis=axis)[0]

    We call `bootstrap` using ``paired=True``.

    >>> res = bootstrap((x, y), my_statistic, paired=True, rng=rng)
    >>> print(res.confidence_interval)
    ConfidenceInterval(low=0.9941504301315878, high=0.996377412215445)

    The result object can be passed back into `bootstrap` to perform additional
    resampling:

    >>> len(res.bootstrap_distribution)
    9999
    >>> res = bootstrap((x, y), my_statistic, paired=True,
    ...                 n_resamples=1000, rng=rng,
    ...                 bootstrap_result=res)
    >>> len(res.bootstrap_distribution)
    10999

    or to change the confidence interval options:

    >>> res2 = bootstrap((x, y), my_statistic, paired=True,
    ...                  n_resamples=0, rng=rng, bootstrap_result=res,
    ...                  method='percentile', confidence_level=0.9)
    >>> np.testing.assert_equal(res2.bootstrap_distribution,
    ...                         res.bootstrap_distribution)
    >>> res.confidence_interval
    ConfidenceInterval(low=0.9941574828235082, high=0.9963781698210212)

    without repeating computation of the original bootstrap distribution.

    Nr   r   )rN   rO   r   r)   r   r   rR   r   )r   re   rd   rD   c                 0    t          j        | |d          S )Nr)   )rT   qr   )r,   rc   )rT   r   s     r&   percentile_funz!bootstrap.<locals>.percentile_fun  s    =13333r(   d   r   r   r   )ddofr   )r   r   r   )r   r   r<   r=   rP   r   r,   rr   r   rk   	full_likeinfr   r   std)r/   r1   rN   rD   r   r   r   r   r   r   r   rO   argsrd   rF   rG   rH   resampled_datar%   resamplere   intervalr   ci_lci_ur   s                             r&   r   r   %  s   ^
 y*fd);U!138 8D
 
T9j&$0@+uf.>)122)@A  -[-AM1k=11 
@ 
@=+a-88 	, 	,F*6|/24 4 4H!!(++++ 	99n>2>>????.2666K *5)C)Ca""A%%'' 
 yr-8G G GGIrK/!E'>	4 	4 	4 >+x{377D>+x{377DIt-"--	y[4'9t);df|D26'**			!	!|D"&))/A$/M/M2=*,&12*N*N*NP P P Pr(   c           
      D   t          |          }||k    rt          d          |dvrt          d          t          |t                    s|f}| f} |D ] }	t	          |	          st          d          !d}
	 t          |            n"# t
          $ r}t          |
          |d}~ww xY wt          |          t          |           k    st          |
          t	          |          st          d          |G	 t          j        |          j	        }n(# t          $ r}d|d	}
t          |
          |d}~ww xY wd
|v }t          |  }|s9t          |          rt          |          }nd|j         d}
t          |
          |}t          | ||          } g }| D ]L}|j        dk    r|                    |d          n|}t#          |||          }|                    |           Mt          |          }||k    s|dk    rt          d          ||}n*t          |          }||k    s|dk    rt          d          h d}|                                }||vrt          d|           t)          |d|j                  } |j        g ||R  }||||||||||f
S )(Input validation for `monte_carlo_test`.r   r   r   0`rvs` must be callable or sequence of callables.z:If `rvs` is a sequence, `len(rvs)` must equal `len(data)`.Nz`statistic` must be callable.z"Signature inspection of statistic=z% failed; pass `vectorize` explicitly.r   zV`statistic` must be vectorized (i.e. support an `axis` argument) when `data` contains z arrays.)xpr   r{   )`n_resamples` must be a positive integer.r   >   r   r   r   `alternative` must be in float16)r   r   
isinstancer   callabler   r   r   r   r   r   r   r8   r   r   ndimrB   r   r   r   getattrfloat32result_type)r/   rvsr1   r   rN   rD   r   r   r   rvs_ir   er   r   statistic_vectorizedr   r%   r   r   r   	min_floatr;   s                         r&   _monte_carlo_test_ivr     sl    4yyHx5666,,,KLLLc8$$ fw P P 	PNOOO	P
 KG)D				 ) ) )!!q()s88s4yy  !!!I 97888	-))44?II 	- 	- 	-6) 6 6 6GW%%1,	- y(
	$	B )B<< 	&#7	#B#B  O9;O O OGW%%%(T4B///DG  28+2B2B...#FH<<<v+&&Oo%%A)=)=DEEE}u::HAJKKK333L##%%K,&&C\CCDDD Irz22IBN/G/Y///ES.
Ok8UB8 8s0   9B	 	
B(B##B(;D 
D:D55D:c                   \    e Zd ZU dZeej        z  ed<   eej        z  ed<   ej        ed<   dS )MonteCarloTestResultat  Result object returned by `scipy.stats.monte_carlo_test`.

    Attributes
    ----------
    statistic : float or ndarray
        The observed test statistic of the sample.
    pvalue : float or ndarray
        The p-value for the given alternative.
    null_distribution : ndarray
        The values of the test statistic generated under the null
        hypothesis.
    r1   pvaluenull_distributionNr   r   r   r   r   r,   r   r   r!   r(   r&   r   r     R           rz!!!!BJz!!!!!r(   r   r%   r/   )r   rN   rD   r   r   c          
      b   t          | ||||||          }|\
  } }}}}}}                     || ddi          }	|	j        dk    r|	d         n|	}	d | D             }
|p}g }t          d|          D ]L}t	          ||z
            fdt          ||
          D             }|                     ||ddi           M                    |          }                    |dgdg|	j        z  z             }	                    |	j
        d          sdn!                    |	j
                  j        d	z  }                    ||	z            fd
fdfd}|d} ||         ||	          }                    |dd          }t          |	||          S )a  Perform a Monte Carlo hypothesis test.

    `data` contains a sample or a sequence of one or more samples. `rvs`
    specifies the distribution(s) of the sample(s) in `data` under the null
    hypothesis. The value of `statistic` for the given `data` is compared
    against a Monte Carlo null distribution: the value of the statistic for
    each of `n_resamples` sets of samples generated using `rvs`. This gives
    the p-value, the probability of observing such an extreme value of the
    test statistic under the null hypothesis.

    Parameters
    ----------
    data : array-like or sequence of array-like
        An array or sequence of arrays of observations.
    rvs : callable or tuple of callables
        A callable or sequence of callables that generates random variates
        under the null hypothesis. Each element of `rvs` must be a callable
        that accepts keyword argument ``size`` (e.g. ``rvs(size=(m, n))``) and
        returns an N-d array sample of that shape. If `rvs` is a sequence, the
        number of callables in `rvs` must match the number of samples in
        `data`, i.e. ``len(rvs) == len(data)``. If `rvs` is a single callable,
        `data` is treated as a single sample.
    statistic : callable
        Statistic for which the p-value of the hypothesis test is to be
        calculated. `statistic` must be a callable that accepts a sample
        (e.g. ``statistic(sample)``) or ``len(rvs)`` separate samples (e.g.
        ``statistic(samples1, sample2)`` if `rvs` contains two callables and
        `data` contains two samples) and returns the resulting statistic.
        If `vectorized` is set ``True``, `statistic` must also accept a keyword
        argument `axis` and be vectorized to compute the statistic along the
        provided `axis` of the samples in `data`.
    vectorized : bool, optional
        If `vectorized` is set ``False``, `statistic` will not be passed
        keyword argument `axis` and is expected to calculate the statistic
        only for 1D samples. If ``True``, `statistic` will be passed keyword
        argument `axis` and is expected to calculate the statistic along `axis`
        when passed ND sample arrays. If ``None`` (default), `vectorized`
        will be set ``True`` if ``axis`` is a parameter of `statistic`. Use of
        a vectorized statistic typically reduces computation time.
    n_resamples : int, default: 9999
        Number of samples drawn from each of the callables of `rvs`.
        Equivalently, the number statistic values under the null hypothesis
        used as the Monte Carlo null distribution.
    batch : int, optional
        The number of Monte Carlo samples to process in each call to
        `statistic`. Memory usage is O( `batch` * ``sample.size[axis]`` ). Default
        is ``None``, in which case `batch` equals `n_resamples`.
    alternative : {'two-sided', 'less', 'greater'}
        The alternative hypothesis for which the p-value is calculated.
        For each alternative, the p-value is defined as follows.

        - ``'greater'`` : the percentage of the null distribution that is
          greater than or equal to the observed value of the test statistic.
        - ``'less'`` : the percentage of the null distribution that is
          less than or equal to the observed value of the test statistic.
        - ``'two-sided'`` : twice the smaller of the p-values above.

    axis : int, default: 0
        The axis of `data` (or each sample within `data`) over which to
        calculate the statistic.

    Returns
    -------
    res : MonteCarloTestResult
        An object with attributes:

        statistic : float or ndarray
            The test statistic of the observed `data`.
        pvalue : float or ndarray
            The p-value for the given alternative.
        null_distribution : ndarray
            The values of the test statistic generated under the null
            hypothesis.

    .. warning::
        The p-value is calculated by counting the elements of the null
        distribution that are as extreme or more extreme than the observed
        value of the statistic. Due to the use of finite precision arithmetic,
        some statistic functions return numerically distinct values when the
        theoretical values would be exactly equal. In some cases, this could
        lead to a large error in the calculated p-value. `monte_carlo_test`
        guards against this by considering elements in the null distribution
        that are "close" (within a relative tolerance of 100 times the
        floating point epsilon of inexact dtypes) to the observed
        value of the test statistic as equal to the observed value of the
        test statistic. However, the user is advised to inspect the null
        distribution to assess whether this method of comparison is
        appropriate, and if not, calculate the p-value manually.

    References
    ----------

    .. [1] B. Phipson and G. K. Smyth. "Permutation P-values Should Never Be
       Zero: Calculating Exact P-values When Permutations Are Randomly Drawn."
       Statistical Applications in Genetics and Molecular Biology 9.1 (2010).

    Examples
    --------

    Suppose we wish to test whether a small sample has been drawn from a normal
    distribution. We decide that we will use the skew of the sample as a
    test statistic, and we will consider a p-value of 0.05 to be statistically
    significant.

    >>> import numpy as np
    >>> from scipy import stats
    >>> def statistic(x, axis):
    ...     return stats.skew(x, axis)

    After collecting our data, we calculate the observed value of the test
    statistic.

    >>> rng = np.random.default_rng()
    >>> x = stats.skewnorm.rvs(a=1, size=50, random_state=rng)
    >>> statistic(x, axis=0)
    0.12457412450240658

    To determine the probability of observing such an extreme value of the
    skewness by chance if the sample were drawn from the normal distribution,
    we can perform a Monte Carlo hypothesis test. The test will draw many
    samples at random from their normal distribution, calculate the skewness
    of each sample, and compare our original skewness against this
    distribution to determine an approximate p-value.

    >>> from scipy.stats import monte_carlo_test
    >>> # because our statistic is vectorized, we pass `vectorized=True`
    >>> rvs = lambda size: stats.norm.rvs(size=size, random_state=rng)
    >>> res = monte_carlo_test(x, rvs, statistic, vectorized=True)
    >>> print(res.statistic)
    0.12457412450240658
    >>> print(res.pvalue)
    0.7012

    The probability of obtaining a test statistic less than or equal to the
    observed value under the null hypothesis is ~70%. This is greater than
    our chosen threshold of 5%, so we cannot consider this to be significant
    evidence against the null hypothesis.

    Note that this p-value essentially matches that of
    `scipy.stats.skewtest`, which relies on an asymptotic distribution of a
    test statistic based on the sample skewness.

    >>> stats.skewtest(x).pvalue
    0.6892046027110614

    This asymptotic approximation is not valid for small sample sizes, but
    `monte_carlo_test` can be used with samples of any size.

    >>> x = stats.skewnorm.rvs(a=1, size=7, random_state=rng)
    >>> # stats.skewtest(x) would produce an error due to small sample
    >>> res = monte_carlo_test(x, rvs, statistic, vectorized=True)

    The Monte Carlo distribution of the test statistic is provided for
    further investigation.

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> ax.hist(res.null_distribution, bins=50)
    >>> ax.set_title("Monte Carlo distribution of test statistic")
    >>> ax.set_xlabel("Value of Statistic")
    >>> ax.set_ylabel("Frequency")
    >>> plt.show()

    r   r)   r   r!   c                 (    g | ]}|j         d          S rv   r"   rp   s     r&   r'   z$monte_carlo_test.<locals>.<listcomp>  s    :::6fl2&:::r(   c                 2    g | ]\  }} ||f           S )r   r!   )r$   r   n_observations_irH   s      r&   r'   z$monte_carlo_test.<locals>.<listcomp>  sG     N N N0 0 U/? @AAA N N Nr(   r   zreal floatingr   c                     | |z   k    }                     |          }                    |d          dz   dz   z  }|S Nr:   r   )r   r;         ?r   rS   r   observedcmpspvaluesr;   gammarN   r   s       r&   r   zmonte_carlo_test.<locals>.less  R     Hu$44zz$ez,,66$Qe644r9kB>NOr(   c                     | |z
  k    }                     |          }                    |d          dz   dz   z  }|S r	  r  r  s       r&   r   z!monte_carlo_test.<locals>.greater  r  r(   c                 j     | |          } | |          }                     ||          dz  }|S NrR   )minimum)r   r  pvalues_lesspvalues_greaterr  r   r   r   s        r&   	two_sidedz#monte_carlo_test.<locals>.two_sided  sD    t-x88!'"3X>>**\?;;a?r(   r   r   r   g        r
  )r   r   r   r<   r=   r   r   concatrC   isdtyper;   finfoepsabsclipr   )r/   r   r1   r   rN   rD   r   r   r   r  n_observationsrF   r   rG   rK   r  r  comparer  rH   r;   r  r   r   r   s       `              @@@@@@r&   r   r     s   P  c9j +UKG GD -1*T3	:{
Kub zz))T33344H'}11x||xH::T:::N([M1k=11 A A=+/::N N N N47^4L4LN N N	  I!?B!?!?@@@@		"344

#4rdaS=N6NOO ZZBB 2AA(.))-c1 FF3>""E                     !%' 'G #gk"#4h??Ggggr2&&G'3DEEEr(   c                   H    e Zd ZU dZeej        z  ed<   eej        z  ed<   dS )PowerResultzResult object returned by `scipy.stats.power`.

    Attributes
    ----------
    power : float or ndarray
        The estimated power.
    pvalues : float or ndarray
        The simulated p-values.
    powerr  Nr  r!   r(   r&   r#  r#    sD           2:RZr(   r#  c                     	 t          t          j        |           j                                                  }n# t
          $ r dh}Y nw xY w|| dd}|S )z?Wrap callable to accept arbitrary kwargs and ignore unused onesr   )keysfunc                 T      fd|                                 D             } ||i |S )Nc                 $    i | ]\  }}|v 	||S r!   r!   )r$   keyvalr&  s      r&   
<dictcomp>z7_wrap_kwargs.<locals>.wrapped_rvs_i.<locals>.<dictcomp>  s/     " " "xsCD[[ s [[r(   )items)r&  r'  r   
all_kwargskwargss   `    r&   wrapped_rvs_iz#_wrap_kwargs.<locals>.wrapped_rvs_i  sK    " " " ":+;+;+=+= " " "sD#F###r(   )r   r   r   r   r&  r   )r'  r&  r0  s      r&   _wrap_kwargsr1    s~    7$S))499;;<<   x
 #'C $ $ $ $ $ s   8; A
Ac                    |dvrt          d          t          | t                    s| f} |f}| D ] }t          |          st	          d          !t          |           t          |          k    sd}	t          |	          t          j        |          d         }t          j        |j	        t          j
                  r0t          j        |          dk     st          j        |          dk    rt          d          |t                      n|}t          |t                    st	          d
          |                                }
|                                }d | D             }t          j        t          j        g ||
R            }|j        }|j        dk    r|t          j        d	d	f         }n"|                    |d         df          j        }|d	d	d	t          |           f         |d	d	t          |           d	f         }
}|                    t.                    }t          |          st	          d          |dt1          j        |          j        v }|st7          |          }n|}t9          |          }t/          |          }||k    s|dk    rt          d          ||}n*t/          |          }||k    s|dk    rt          d          ||||||||
||dd	         f
S )r   r   r   r   zDIf `rvs` is a sequence, `len(rvs)` must equal `len(n_observations)`.r!   r   r   z3`significance` must contain floats between 0 and 1.Nz;`kwargs` must be a dictionary that maps keywords to arrays.c                 ,    g | ]}t          |          S r!   )r1  )r$   r   s     r&   r'   z_power_iv.<locals>.<listcomp>.  s     8885<&&888r(   r)   z`test` must be callable.r   r   r   )r   r   r   r   r   r   r,   r   
issubdtyper;   floatingr=   maxdictvaluesr&  broadcast_arraysr#   r   newaxisrC   Tastyper   r   r   r   r8   r1  )r   testr   significancer   rN   rD   r/  r   r   valsr&  wrapped_rvstmpr#   nobstest_vectorizedr   r   s                      r&   	_power_ivrD    s    ,,,KLLLc8$$ +f(* P P 	PNOOO	P s88s>****7!!!:l++B/LM,,bk:: Pvl##a''26,+?+?!+C+CNOOO~TVVV6Ffd## WUVVV==??D;;==D 98C888K *R(@.@4@@@
A
ACIE
x1}}"*aaa- kk58R.))+QQQ	S	\"C3s8899$5$D;;sDD>> 42333w066AA
 .t44"?33O+&&Oo%%A)=)=DEEE}u::HAJKKK$jXtT59> >r(   g{Gz?i'  )r>  r   rN   rD   r/  c          
      x   t          || ||||||          }|\
  }} }	}}}}}
}}|p|}g }t          |	|
          D ]\  }}t          t          ||                    g }t          d||          D ]d}t	          |||z
            fdt          ||          D             } | |i ddi}t          |d|          }|                    |           et          j        |d          }|                    |           ||j	        dd         z  }t          j        |d          }|
                    |dz             }|j        dk    rDt          t          |j        |j        |j        z                       }t          j        ||          }t          j        ||k     d          }t          ||	          S )
a#  Simulate the power of a hypothesis test under an alternative hypothesis.

    Parameters
    ----------
    test : callable
        Hypothesis test for which the power is to be simulated.
        `test` must be a callable that accepts a sample (e.g. ``test(sample)``)
        or ``len(rvs)`` separate samples (e.g. ``test(samples1, sample2)`` if
        `rvs` contains two callables and `n_observations` contains two values)
        and returns the p-value of the test.
        If `vectorized` is set to ``True``, `test` must also accept a keyword
        argument `axis` and be vectorized to perform the test along the
        provided `axis` of the samples.
        Any callable from `scipy.stats` with an `axis` argument that returns an
        object with a `pvalue` attribute is also acceptable.
    rvs : callable or tuple of callables
        A callable or sequence of callables that generate(s) random variates
        under the alternative hypothesis. Each element of `rvs` must accept
        keyword argument ``size`` (e.g. ``rvs(size=(m, n))``) and return an
        N-d array of that shape. If `rvs` is a sequence, the number of callables
        in `rvs` must match the number of elements of `n_observations`, i.e.
        ``len(rvs) == len(n_observations)``. If `rvs` is a single callable,
        `n_observations` is treated as a single element.
    n_observations : tuple of ints or tuple of integer arrays
        If a sequence of ints, each is the sizes of a sample to be passed to `test`.
        If a sequence of integer arrays, the power is simulated for each
        set of corresponding sample sizes. See Examples.
    significance : float or array_like of floats, default: 0.01
        The threshold for significance; i.e., the p-value below which the
        hypothesis test results will be considered as evidence against the null
        hypothesis. Equivalently, the acceptable rate of Type I error under
        the null hypothesis. If an array, the power is simulated for each
        significance threshold.
    kwargs : dict, optional
        Keyword arguments to be passed to `rvs` and/or `test` callables.
        Introspection is used to determine which keyword arguments may be
        passed to each callable.
        The value corresponding with each keyword must be an array.
        Arrays must be broadcastable with one another and with each array in
        `n_observations`. The power is simulated for each set of corresponding
        sample sizes and arguments. See Examples.
    vectorized : bool, optional
        If `vectorized` is set to ``False``, `test` will not be passed keyword
        argument `axis` and is expected to perform the test only for 1D samples.
        If ``True``, `test` will be passed keyword argument `axis` and is
        expected to perform the test along `axis` when passed N-D sample arrays.
        If ``None`` (default), `vectorized` will be set ``True`` if ``axis`` is
        a parameter of `test`. Use of a vectorized test typically reduces
        computation time.
    n_resamples : int, default: 10000
        Number of samples drawn from each of the callables of `rvs`.
        Equivalently, the number tests performed under the alternative
        hypothesis to approximate the power.
    batch : int, optional
        The number of samples to process in each call to `test`. Memory usage is
        proportional to the product of `batch` and the largest sample size. Default
        is ``None``, in which case `batch` equals `n_resamples`.

    Returns
    -------
    res : PowerResult
        An object with attributes:

        power : float or ndarray
            The estimated power against the alternative.
        pvalues : ndarray
            The p-values observed under the alternative hypothesis.

    Notes
    -----
    The power is simulated as follows:

    - Draw many random samples (or sets of samples), each of the size(s)
      specified by `n_observations`, under the alternative specified by
      `rvs`.
    - For each sample (or set of samples), compute the p-value according to
      `test`. These p-values are recorded in the ``pvalues`` attribute of
      the result object.
    - Compute the proportion of p-values that are less than the `significance`
      level. This is the power recorded in the ``power`` attribute of the
      result object.

    Suppose that `significance` is an array with shape ``shape1``, the elements
    of `kwargs` and `n_observations` are mutually broadcastable to shape ``shape2``,
    and `test` returns an array of p-values of shape ``shape3``. Then the result
    object ``power`` attribute will be of shape ``shape1 + shape2 + shape3``, and
    the ``pvalues`` attribute will be of shape ``shape2 + shape3 + (n_resamples,)``.

    Examples
    --------
    Suppose we wish to simulate the power of the independent sample t-test
    under the following conditions:

    - The first sample has 10 observations drawn from a normal distribution
      with mean 0.
    - The second sample has 12 observations drawn from a normal distribution
      with mean 1.0.
    - The threshold on p-values for significance is 0.05.

    >>> import numpy as np
    >>> from scipy import stats
    >>> rng = np.random.default_rng(2549598345528)
    >>>
    >>> test = stats.ttest_ind
    >>> n_observations = (10, 12)
    >>> rvs1 = rng.normal
    >>> rvs2 = lambda size: rng.normal(loc=1, size=size)
    >>> rvs = (rvs1, rvs2)
    >>> res = stats.power(test, rvs, n_observations, significance=0.05)
    >>> res.power
    0.6116

    With samples of size 10 and 12, respectively, the power of the t-test
    with a significance threshold of 0.05 is approximately 60% under the chosen
    alternative. We can investigate the effect of sample size on the power
    by passing sample size arrays.

    >>> import matplotlib.pyplot as plt
    >>> nobs_x = np.arange(5, 21)
    >>> nobs_y = nobs_x
    >>> n_observations = (nobs_x, nobs_y)
    >>> res = stats.power(test, rvs, n_observations, significance=0.05)
    >>> ax = plt.subplot()
    >>> ax.plot(nobs_x, res.power)
    >>> ax.set_xlabel('Sample Size')
    >>> ax.set_ylabel('Simulated Power')
    >>> ax.set_title('Simulated Power of `ttest_ind` with Equal Sample Sizes')
    >>> plt.show()

    Alternatively, we can investigate the impact that effect size has on the power.
    In this case, the effect size is the location of the distribution underlying
    the second sample.

    >>> n_observations = (10, 12)
    >>> loc = np.linspace(0, 1, 20)
    >>> rvs2 = lambda size, loc: rng.normal(loc=loc, size=size)
    >>> rvs = (rvs1, rvs2)
    >>> res = stats.power(test, rvs, n_observations, significance=0.05,
    ...                   kwargs={'loc': loc})
    >>> ax = plt.subplot()
    >>> ax.plot(loc, res.power)
    >>> ax.set_xlabel('Effect Size')
    >>> ax.set_ylabel('Simulated Power')
    >>> ax.set_title('Simulated Power of `ttest_ind`, Varying Effect Size')
    >>> plt.show()

    We can also use `power` to estimate the Type I error rate (also referred to by the
    ambiguous term "size") of a test and assess whether it matches the nominal level.
    For example, the null hypothesis of `jarque_bera` is that the sample was drawn from
    a distribution with the same skewness and kurtosis as the normal distribution. To
    estimate the Type I error rate, we can consider the null hypothesis to be a true
    *alternative* hypothesis and calculate the power.

    >>> test = stats.jarque_bera
    >>> n_observations = 10
    >>> rvs = rng.normal
    >>> significance = np.linspace(0.0001, 0.1, 1000)
    >>> res = stats.power(test, rvs, n_observations, significance=significance)
    >>> size = res.power

    As shown below, the Type I error rate of the test is far below the nominal level
    for such a small sample, as mentioned in its documentation.

    >>> ax = plt.subplot()
    >>> ax.plot(significance, size)
    >>> ax.plot([0, 0.1], [0, 0.1], '--')
    >>> ax.set_xlabel('nominal significance level')
    >>> ax.set_ylabel('estimated test size (Type I error rate)')
    >>> ax.set_title('Estimated test size vs nominal significance level')
    >>> ax.set_aspect('equal', 'box')
    >>> ax.legend(('`ttest_1samp`', 'ideal test'))
    >>> plt.show()

    As one might expect from such a conservative test, the power is quite low with
    respect to some alternatives. For example, the power of the test under the
    alternative that the sample was drawn from the Laplace distribution may not
    be much greater than the Type I error rate.

    >>> rvs = rng.laplace
    >>> significance = np.linspace(0.0001, 0.1, 1000)
    >>> res = stats.power(test, rvs, n_observations, significance=0.05)
    >>> print(res.power)
    0.0587

    This is not a mistake in SciPy's implementation; it is simply due to the fact
    that the null distribution of the test statistic is derived under the assumption
    that the sample size is large (i.e. approaches infinity), and this asymptotic
    approximation is not accurate for small samples. In such cases, resampling
    and Monte Carlo methods (e.g. `permutation_test`, `goodness_of_fit`,
    `monte_carlo_test`) may be more appropriate.

    r   c                 .    g | ]\  }} |dd |fiS )r   r!   r!   )r$   rvs_jnobs_ijrH   kwargs_is      r&   r'   zpower.<locals>.<listcomp>%  sM     A A A+UG HH\7$;HxHH A A Ar(   r   r)   r   r   Nrv   )r$  r  )rD  r   r7  r<   r=   r   r   r,   rr   r#   rC   r   r   ro   ry   r#  )r=  r   r   r>  r   rN   rD   r/  rA  rB  r   kwdsr#   rF   r  nobs_iargs_i	pvalues_irG   rK   respnewdimspowersrH   rI  s                           @@r&   r$  r$  W  s   D C~|UF< <C :=8S$leT4([MGdD// " "D&))**	q+}55 	  	 A}kAo>>LA A A A A/23/?/?A A AI$	7X77B777CXs++AQN92666	y!!!!	Y_SbS!!EnW1---Gooeem,,G1l/@Q1QRRSS~lG<<WW|+"555FVW5555r(   c                   \    e Zd ZU dZeej        z  ed<   eej        z  ed<   ej        ed<   dS )PermutationTestResultar  Result object returned by `scipy.stats.permutation_test`.

    Attributes
    ----------
    statistic : float or ndarray
        The observed test statistic of the data.
    pvalue : float or ndarray
        The p-value for the given alternative.
    null_distribution : ndarray
        The values of the test statistic generated under the null
        hypothesis.
    r1   r   r   Nr  r!   r(   r&   rS  rS  ;  r  r(   rS  c              #     K   d fdt          t          t          j        |                               } || dd                   D ]<}t          j        d |D                                           t                    }|V  =dS )zu
    Generate all partitions of indices of groups of given sizes, concatenated

    `ns` is an iterable of ints.
    c              3   d   K   t          | |          D ]}t          |          }| |z
  }||gV  d S r+   )r   r   )r.   rE   cx0x1s        r&   all_partitionsz4_all_partitions_concatenated.<locals>.all_partitionsT  sL      a## 	 	AQBRBr(NNNN	 	r(   c              3      K   t          |          dk    r| gV  d S  | |d                   D ].} |d         |dd                    D ]}|dd         |z   V  /d S )Nr   r   )r   )r.   nsrV  drY  all_partitions_ns       r&   r]  z6_all_partitions_concatenated.<locals>.all_partitions_nZ  s      r77a<<#IIIF2a5)) 	! 	!A%%adBqrrF33 ! !!fqj    !	! 	!r(   Nc                 ,    g | ]}t          |          S r!   )r   )r$   	partitions     r&   r'   z0_all_partitions_concatenated.<locals>.<listcomp>d  s.     ; ; ; ) !OO ; ; ;r(   )r   r<   r,   rS   rr   r<  r   )r[  r.   partitioningxrY  r]  s       @@r&   _all_partitions_concatenatedrb  N  s        ! ! ! ! ! ! 	E"&**A((BqqqE22  N ; ;-9; ; ; < <<BF3KK 	
 r(   c              #     K   t          |           }|dk    rt          d          d t          t          |          |          D             }|r/|V  d t          t          |          |          D             }|-dS dS )z<A generator that yields batches of elements from an iterabler   z`batch` must be positive.c                     g | ]\  }}|S r!   r!   r$   rJ   items      r&   r'   z$_batch_generator.<locals>.<listcomp>n  s    999'!T999r(   c                     g | ]\  }}|S r!   r!   re  s      r&   r'   z$_batch_generator.<locals>.<listcomp>q  s    ===gaT===r(   N)iterr   r   r<   )iterablerD   iteratorr.   s       r&   _batch_generatorrk  i  s      H~~Hzz455599Sux88999A
 >==U5\\8!<!<===  > > > > >r(   c                      t                     t          d          r
 fd}n	 fd} |            S )Npermutedc               3      K   t          j                  } t          j        | df          } t          d          D ]8}t	          |z
            }                    | d          }|d |         V  9d S )Nr   r   r)   r   )r,   rA   tiler<   r=   rm  )	rg   rG   rH   permuted_indicesrD   n_obs_samplen_permutationsr   rO   s	       r&   batched_perm_generatorz:_pairings_permutations_gen.<locals>.batched_perm_generator|  s      i--Gggy!'<==G1ne44 6 6"5.*:;;#&<<b<#A#A &}}55555	6 6r(   c               3      K   t          d          D ]P} t          | z
            }|f}                    |          }t          j        |d          d |         V  Qd S )Nr   r  r)   r   )r<   r=   randomr,   argsort)	rG   rH   r   ra  rD   rq  rr  r   rO   s	       r&   rs  z:_pairings_permutations_gen.<locals>.batched_perm_generator  s      1ne44 < <"5.*:;;$i>JJDJ))j,,,]l];;;;;	< <r(   )r=   r   )rr  r   rq  rD   rO   rs  s   ````` r&   _pairings_permutations_genrw  t  s    
 ~&&EsJ <	6 	6 	6 	6 	6 	6 	6 	6 	6 	6	< 	< 	< 	< 	< 	< 	< 	< 	< "!###r(   c                    t          |           }d | D             }t          j        |          d         t          j        fdt	          |dz
  dd          D                       }||k    rd}|}t          |          }	nd}fdt	          |          D             }	|pt          |          }g }
t          j        | d	          } t          |	|
          D ]p}t          j	        |          }| d|f         }t          j
        |dd          }t          j        |dd         d	          }|
                     ||ddi           qt          j        |
d	          }
|
||fS )zC
    Calculate null distribution for independent sample tests.
    c                 (    g | ]}|j         d          S rv   r"   rp   s     r&   r'   z(_calculate_null_both.<locals>.<listcomp>  s    333Fv|B333r(   r)   c                 N    g | ]!}t          |         |d z
                     "S r{   )r   )r$   rJ   n_obs_ics     r&   r'   z(_calculate_null_both.<locals>.<listcomp>  s?     9 9 9 (1+x!}55 9 9 9r(   r   r   TFc              3   B   K   | ]}                               V  d S r+   )permutation)r$   rJ   n_obsrO   s     r&   	<genexpr>z'_calculate_null_both.<locals>.<genexpr>  sC       : : //%00 : : : : : :r(   r   rD   .rn   Nr   )r   r,   r3   prodr<   rb  r   rr   rk  arrayr4   r-   r   )r/   r1   rr  rD   rO   r   n_obs_in_max
exact_testperm_generatorr   rg   
data_batchr~  r{  s       `        @@r&   _calculate_null_bothr    s   
 D		I 43d333Gy!!HRLEG 9 9 9 9#IaKB779 9 9 : :E 
5g>>

: : : : :#(#8#8: : : (S((E >$R(((D#N%@@@ B B(7## #w,'

 [R33
Xj(3B3-bAAA
  J!@R!@!@AAAA'8qAAAnj88r(   c                    t          |           }| d         j        d         t                    |z  }||k    rId}|}|pt          |          }t	          fdt          |          D              }t          ||          }	n#d}|pt          |          }||||f}
t          |
 }	g }|	D ]}t          j	        |          }t          j
        |dd          }dg|z  }t          |          D ]:}| |         d	||         f         ||<   t          j        ||         d
d          ||<   ;|                     ||ddi           t          j        |d          }|||fS )z<
    Calculate null distribution for association tests.
    r   r)   Tc              3   P   K   | ] }t          t                              V  !d S r+   )r   r<   )r$   rJ   rq  s     r&   r  z+_calculate_null_pairings.<locals>.<genexpr>  sG       #> #>'( $0l0C0C#D#D #> #> #> #> #> #>r(   r  Fr   N.rn   r   r   )r   r#   r   r   r   r<   rk  rw  r,   r  swapaxesr4   r   rr   )r/   r1   rr  rD   rO   r   r  r  r  rs  r   r   rg   r  rJ   rq  s                  @r&   _calculate_null_pairingsr    s   
 D		I 7=$Ll##Y.E 
,^,,  #> #> #> #>,1),<,<#> #> #> ?!1.!N!N!N
,^,, y,sB!;T!B) B B(7## +gq!,, VI%
y!! 	> 	>A GCO4JqMK
1r1==JqMM  J!@R!@!@AAAA'8qAAAnj88r(   c                     t          |           dk    r| d         | d          g} t          j        | dd          } fd}t          | ||||          S )z>
    Calculate null distribution for paired-sample tests.
    r   r   r)   c                 `    t          j        |dd          }dk    r
|dd         } |d| iS )Nr   r)   r   r   )r,   r  )r   r/   r   r1   s     r&   statistic_wrappedz2_calculate_null_samples.<locals>.statistic_wrapped  sA    {4B''>>!9Dy$*T***r(   )r   r,   r  r  )r/   r1   rr  rD   rO   r  r   s    `    @r&   _calculate_null_samplesr    s    
 D		I
 A~~Q$q'" ;tQ##D+ + + + + + $D*;^$)30 0 0r(   c	           	      *   t          |          }	||	k    rt          d          h d}
|                                }||
vrt          d|
 d          |dvrt          d          |dt          j        |          j        v }|st          |          }d	}	 t          |           d
k     r|dk    rt          |          n# t          $ r t          |          w xY wt          | |          } g }| D ]a}t          j        |          }|j        |         dk    rt          d          t          j        ||	d          }|                    |           bt          j        |          st          |          nt          j        }||k    s|dk    rt          d          ||}n*t          |          }||k    s|dk    rt          d          h d}|                                }||vrt          d|           t#          |          }||||||||	|f	S )z(Input validation for `permutation_test`.r   >   r   pairingsindependentz`permutation_type` must be in .r   r   Nr   z6`data` must be a tuple containing at least two samplesrR   r  r   r   r)   r   r   r   >   r   r   r   r   )r   r   r   r   r   r   r8   r   r   r   r,   r   r#   r4   r   isinfr   r	   )r/   r1   permutation_typer   rN   rD   r   r   rO   r   permutation_typesr   r   r%   r   r   r   s                    r&   _permutation_test_ivr  #  s    4yyHx5666>>>'--//000N:KNNNOOO,,,KLLLw0;;FF
 4(33	FG!t99q==->>W%%% ! ! !   ! T4((DG  v&&<"" : ; ; ;VXr22v/1x/D/D #s;'''F o%%A)=)=DEEE}u::HAJKKK333L##%%K,&&C\CCDDD
S
!
!CY 0*ok8S2 2s   (C Cr  )r  r   rN   rD   r   r   rO   c                *   t          | |||||||	  	        }	|	\	  } }}}}}}} || ddi}
t          t          t          d}| |||f}||         } || \  }}|rdndt	          j        |
j        t          j                  sdn t	          j        |
j                  j	        dz  }t	          j
        ||
z            fdfdfd	}|d
} ||         ||
          }t	          j        |dd          }t          |
||          S )aQ  
    Performs a permutation test of a given statistic on provided data.

    For independent sample statistics, the null hypothesis is that the data are
    randomly sampled from the same distribution.
    For paired sample statistics, two null hypothesis can be tested:
    that the data are paired at random or that the data are assigned to samples
    at random.

    Parameters
    ----------
    data : iterable of array-like
        Contains the samples, each of which is an array of observations.
        Dimensions of sample arrays must be compatible for broadcasting except
        along `axis`.
    statistic : callable
        Statistic for which the p-value of the hypothesis test is to be
        calculated. `statistic` must be a callable that accepts samples
        as separate arguments (e.g. ``statistic(*data)``) and returns the
        resulting statistic.
        If `vectorized` is set ``True``, `statistic` must also accept a keyword
        argument `axis` and be vectorized to compute the statistic along the
        provided `axis` of the sample arrays.
    permutation_type : {'independent', 'samples', 'pairings'}, optional
        The type of permutations to be performed, in accordance with the
        null hypothesis. The first two permutation types are for paired sample
        statistics, in which all samples contain the same number of
        observations and observations with corresponding indices along `axis`
        are considered to be paired; the third is for independent sample
        statistics.

        - ``'samples'`` : observations are assigned to different samples
          but remain paired with the same observations from other samples.
          This permutation type is appropriate for paired sample hypothesis
          tests such as the Wilcoxon signed-rank test and the paired t-test.
        - ``'pairings'`` : observations are paired with different observations,
          but they remain within the same sample. This permutation type is
          appropriate for association/correlation tests with statistics such
          as Spearman's :math:`\rho`, Kendall's :math:`\tau`, and Pearson's
          :math:`r`.
        - ``'independent'`` (default) : observations are assigned to different
          samples. Samples may contain different numbers of observations. This
          permutation type is appropriate for independent sample hypothesis
          tests such as the Mann-Whitney :math:`U` test and the independent
          sample t-test.

          Please see the Notes section below for more detailed descriptions
          of the permutation types.

    vectorized : bool, optional
        If `vectorized` is set ``False``, `statistic` will not be passed
        keyword argument `axis` and is expected to calculate the statistic
        only for 1D samples. If ``True``, `statistic` will be passed keyword
        argument `axis` and is expected to calculate the statistic along `axis`
        when passed an ND sample array. If ``None`` (default), `vectorized`
        will be set ``True`` if ``axis`` is a parameter of `statistic`. Use
        of a vectorized statistic typically reduces computation time.
    n_resamples : int or np.inf, default: 9999
        Number of random permutations (resamples) used to approximate the null
        distribution. If greater than or equal to the number of distinct
        permutations, the exact null distribution will be computed.
        Note that the number of distinct permutations grows very rapidly with
        the sizes of samples, so exact tests are feasible only for very small
        data sets.
    batch : int, optional
        The number of permutations to process in each call to `statistic`.
        Memory usage is O( `batch` * ``n`` ), where ``n`` is the total size
        of all samples, regardless of the value of `vectorized`. Default is
        ``None``, in which case ``batch`` is the number of permutations.
    alternative : {'two-sided', 'less', 'greater'}, optional
        The alternative hypothesis for which the p-value is calculated.
        For each alternative, the p-value is defined for exact tests as
        follows.

        - ``'greater'`` : the percentage of the null distribution that is
          greater than or equal to the observed value of the test statistic.
        - ``'less'`` : the percentage of the null distribution that is
          less than or equal to the observed value of the test statistic.
        - ``'two-sided'`` (default) : twice the smaller of the p-values above.

        Note that p-values for randomized tests are calculated according to the
        conservative (over-estimated) approximation suggested in [2]_ and [3]_
        rather than the unbiased estimator suggested in [4]_. That is, when
        calculating the proportion of the randomized null distribution that is
        as extreme as the observed value of the test statistic, the values in
        the numerator and denominator are both increased by one. An
        interpretation of this adjustment is that the observed value of the
        test statistic is always included as an element of the randomized
        null distribution.
        The convention used for two-sided p-values is not universal;
        the observed test statistic and null distribution are returned in
        case a different definition is preferred.

    axis : int, default: 0
        The axis of the (broadcasted) samples over which to calculate the
        statistic. If samples have a different number of dimensions,
        singleton dimensions are prepended to samples with fewer dimensions
        before `axis` is considered.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator state. When `rng` is None, a new
        `numpy.random.Generator` is created using entropy from the
        operating system. Types other than `numpy.random.Generator` are
        passed to `numpy.random.default_rng` to instantiate a ``Generator``.

    Returns
    -------
    res : PermutationTestResult
        An object with attributes:

        statistic : float or ndarray
            The observed test statistic of the data.
        pvalue : float or ndarray
            The p-value for the given alternative.
        null_distribution : ndarray
            The values of the test statistic generated under the null
            hypothesis.

    Notes
    -----

    The three types of permutation tests supported by this function are
    described below.

    **Unpaired statistics** (``permutation_type='independent'``):

    The null hypothesis associated with this permutation type is that all
    observations are sampled from the same underlying distribution and that
    they have been assigned to one of the samples at random.

    Suppose ``data`` contains two samples; e.g. ``a, b = data``.
    When ``1 < n_resamples < binom(n, k)``, where

    * ``k`` is the number of observations in ``a``,
    * ``n`` is the total number of observations in ``a`` and ``b``, and
    * ``binom(n, k)`` is the binomial coefficient (``n`` choose ``k``),

    the data are pooled (concatenated), randomly assigned to either the first
    or second sample, and the statistic is calculated. This process is
    performed repeatedly, `permutation` times, generating a distribution of the
    statistic under the null hypothesis. The statistic of the original
    data is compared to this distribution to determine the p-value.

    When ``n_resamples >= binom(n, k)``, an exact test is performed: the data
    are *partitioned* between the samples in each distinct way exactly once,
    and the exact null distribution is formed.
    Note that for a given partitioning of the data between the samples,
    only one ordering/permutation of the data *within* each sample is
    considered. For statistics that do not depend on the order of the data
    within samples, this dramatically reduces computational cost without
    affecting the shape of the null distribution (because the frequency/count
    of each value is affected by the same factor).

    For ``a = [a1, a2, a3, a4]`` and ``b = [b1, b2, b3]``, an example of this
    permutation type is ``x = [b3, a1, a2, b2]`` and ``y = [a4, b1, a3]``.
    Because only one ordering/permutation of the data *within* each sample
    is considered in an exact test, a resampling like ``x = [b3, a1, b2, a2]``
    and ``y = [a4, a3, b1]`` would *not* be considered distinct from the
    example above.

    ``permutation_type='independent'`` does not support one-sample statistics,
    but it can be applied to statistics with more than two samples. In this
    case, if ``n`` is an array of the number of observations within each
    sample, the number of distinct partitions is::

        np.prod([binom(sum(n[i:]), sum(n[i+1:])) for i in range(len(n)-1)])

    **Paired statistics, permute pairings** (``permutation_type='pairings'``):

    The null hypothesis associated with this permutation type is that
    observations within each sample are drawn from the same underlying
    distribution and that pairings with elements of other samples are
    assigned at random.

    Suppose ``data`` contains only one sample; e.g. ``a, = data``, and we
    wish to consider all possible pairings of elements of ``a`` with elements
    of a second sample, ``b``. Let ``n`` be the number of observations in
    ``a``, which must also equal the number of observations in ``b``.

    When ``1 < n_resamples < factorial(n)``, the elements of ``a`` are
    randomly permuted. The user-supplied statistic accepts one data argument,
    say ``a_perm``, and calculates the statistic considering ``a_perm`` and
    ``b``. This process is performed repeatedly, `permutation` times,
    generating a distribution of the statistic under the null hypothesis.
    The statistic of the original data is compared to this distribution to
    determine the p-value.

    When ``n_resamples >= factorial(n)``, an exact test is performed:
    ``a`` is permuted in each distinct way exactly once. Therefore, the
    `statistic` is computed for each unique pairing of samples between ``a``
    and ``b`` exactly once.

    For ``a = [a1, a2, a3]`` and ``b = [b1, b2, b3]``, an example of this
    permutation type is ``a_perm = [a3, a1, a2]`` while ``b`` is left
    in its original order.

    ``permutation_type='pairings'`` supports ``data`` containing any number
    of samples, each of which must contain the same number of observations.
    All samples provided in ``data`` are permuted *independently*. Therefore,
    if ``m`` is the number of samples and ``n`` is the number of observations
    within each sample, then the number of permutations in an exact test is::

        factorial(n)**m

    Note that if a two-sample statistic, for example, does not inherently
    depend on the order in which observations are provided - only on the
    *pairings* of observations - then only one of the two samples should be
    provided in ``data``. This dramatically reduces computational cost without
    affecting the shape of the null distribution (because the frequency/count
    of each value is affected by the same factor).

    **Paired statistics, permute samples** (``permutation_type='samples'``):

    The null hypothesis associated with this permutation type is that
    observations within each pair are drawn from the same underlying
    distribution and that the sample to which they are assigned is random.

    Suppose ``data`` contains two samples; e.g. ``a, b = data``.
    Let ``n`` be the number of observations in ``a``, which must also equal
    the number of observations in ``b``.

    When ``1 < n_resamples < 2**n``, the elements of ``a`` are ``b`` are
    randomly swapped between samples (maintaining their pairings) and the
    statistic is calculated. This process is performed repeatedly,
    `permutation` times,  generating a distribution of the statistic under the
    null hypothesis. The statistic of the original data is compared to this
    distribution to determine the p-value.

    When ``n_resamples >= 2**n``, an exact test is performed: the observations
    are assigned to the two samples in each distinct way (while maintaining
    pairings) exactly once.

    For ``a = [a1, a2, a3]`` and ``b = [b1, b2, b3]``, an example of this
    permutation type is ``x = [b1, a2, b3]`` and ``y = [a1, b2, a3]``.

    ``permutation_type='samples'`` supports ``data`` containing any number
    of samples, each of which must contain the same number of observations.
    If ``data`` contains more than one sample, paired observations within
    ``data`` are exchanged between samples *independently*. Therefore, if ``m``
    is the number of samples and ``n`` is the number of observations within
    each sample, then the number of permutations in an exact test is::

        factorial(m)**n

    Several paired-sample statistical tests, such as the Wilcoxon signed rank
    test and paired-sample t-test, can be performed considering only the
    *difference* between two paired elements. Accordingly, if ``data`` contains
    only one sample, then the null distribution is formed by independently
    changing the *sign* of each observation.

    .. warning::
        The p-value is calculated by counting the elements of the null
        distribution that are as extreme or more extreme than the observed
        value of the statistic. Due to the use of finite precision arithmetic,
        some statistic functions return numerically distinct values when the
        theoretical values would be exactly equal. In some cases, this could
        lead to a large error in the calculated p-value. `permutation_test`
        guards against this by considering elements in the null distribution
        that are "close" (within a relative tolerance of 100 times the
        floating point epsilon of inexact dtypes) to the observed
        value of the test statistic as equal to the observed value of the
        test statistic. However, the user is advised to inspect the null
        distribution to assess whether this method of comparison is
        appropriate, and if not, calculate the p-value manually. See example
        below.

    References
    ----------

    .. [1] R. A. Fisher. The Design of Experiments, 6th Ed (1951).
    .. [2] B. Phipson and G. K. Smyth. "Permutation P-values Should Never Be
       Zero: Calculating Exact P-values When Permutations Are Randomly Drawn."
       Statistical Applications in Genetics and Molecular Biology 9.1 (2010).
    .. [3] M. D. Ernst. "Permutation Methods: A Basis for Exact Inference".
       Statistical Science (2004).
    .. [4] B. Efron and R. J. Tibshirani. An Introduction to the Bootstrap
       (1993).

    Examples
    --------

    Suppose we wish to test whether two samples are drawn from the same
    distribution. Assume that the underlying distributions are unknown to us,
    and that before observing the data, we hypothesized that the mean of the
    first sample would be less than that of the second sample. We decide that
    we will use the difference between the sample means as a test statistic,
    and we will consider a p-value of 0.05 to be statistically significant.

    For efficiency, we write the function defining the test statistic in a
    vectorized fashion: the samples ``x`` and ``y`` can be ND arrays, and the
    statistic will be calculated for each axis-slice along `axis`.

    >>> import numpy as np
    >>> def statistic(x, y, axis):
    ...     return np.mean(x, axis=axis) - np.mean(y, axis=axis)

    After collecting our data, we calculate the observed value of the test
    statistic.

    >>> from scipy.stats import norm
    >>> rng = np.random.default_rng()
    >>> x = norm.rvs(size=5, random_state=rng)
    >>> y = norm.rvs(size=6, loc = 3, random_state=rng)
    >>> statistic(x, y, 0)
    -3.5411688580987266

    Indeed, the test statistic is negative, suggesting that the true mean of
    the distribution underlying ``x`` is less than that of the distribution
    underlying ``y``. To determine the probability of this occurring by chance
    if the two samples were drawn from the same distribution, we perform
    a permutation test.

    >>> from scipy.stats import permutation_test
    >>> # because our statistic is vectorized, we pass `vectorized=True`
    >>> # `n_resamples=np.inf` indicates that an exact test is to be performed
    >>> res = permutation_test((x, y), statistic, vectorized=True,
    ...                        n_resamples=np.inf, alternative='less')
    >>> print(res.statistic)
    -3.5411688580987266
    >>> print(res.pvalue)
    0.004329004329004329

    The probability of obtaining a test statistic less than or equal to the
    observed value under the null hypothesis is 0.4329%. This is less than our
    chosen threshold of 5%, so we consider this to be significant evidence
    against the null hypothesis in favor of the alternative.

    Because the size of the samples above was small, `permutation_test` could
    perform an exact test. For larger samples, we resort to a randomized
    permutation test.

    >>> x = norm.rvs(size=100, random_state=rng)
    >>> y = norm.rvs(size=120, loc=0.2, random_state=rng)
    >>> res = permutation_test((x, y), statistic, n_resamples=9999,
    ...                        vectorized=True, alternative='less',
    ...                        rng=rng)
    >>> print(res.statistic)
    -0.4230459671240913
    >>> print(res.pvalue)
    0.0015

    The approximate probability of obtaining a test statistic less than or
    equal to the observed value under the null hypothesis is 0.0225%. This is
    again less than our chosen threshold of 5%, so again we have significant
    evidence to reject the null hypothesis in favor of the alternative.

    For large samples and number of permutations, the result is comparable to
    that of the corresponding asymptotic test, the independent sample t-test.

    >>> from scipy.stats import ttest_ind
    >>> res_asymptotic = ttest_ind(x, y, alternative='less')
    >>> print(res_asymptotic.pvalue)
    0.0014669545224902675

    The permutation distribution of the test statistic is provided for
    further investigation.

    >>> import matplotlib.pyplot as plt
    >>> plt.hist(res.null_distribution, bins=50)
    >>> plt.title("Permutation distribution of test statistic")
    >>> plt.xlabel("Value of Statistic")
    >>> plt.ylabel("Frequency")
    >>> plt.show()

    Inspection of the null distribution is essential if the statistic suffers
    from inaccuracy due to limited machine precision. Consider the following
    case:

    >>> from scipy.stats import pearsonr
    >>> x = [1, 2, 4, 3]
    >>> y = [2, 4, 6, 8]
    >>> def statistic(x, y, axis=-1):
    ...     return pearsonr(x, y, axis=axis).statistic
    >>> res = permutation_test((x, y), statistic, vectorized=True,
    ...                        permutation_type='pairings',
    ...                        alternative='greater')
    >>> r, pvalue, null = res.statistic, res.pvalue, res.null_distribution

    In this case, some elements of the null distribution differ from the
    observed value of the correlation coefficient ``r`` due to numerical noise.
    We manually inspect the elements of the null distribution that are nearly
    the same as the observed value of the test statistic.

    >>> r
    0.7999999999999999
    >>> unique = np.unique(null)
    >>> unique
    array([-1. , -1. , -0.8, -0.8, -0.8, -0.6, -0.4, -0.4, -0.2, -0.2, -0.2,
        0. ,  0.2,  0.2,  0.2,  0.4,  0.4,  0.6,  0.8,  0.8,  0.8,  1. ,
        1. ])  # may vary
    >>> unique[np.isclose(r, unique)].tolist()
    [0.7999999999999998, 0.7999999999999999, 0.8]  # may vary

    If `permutation_test` were to perform the comparison naively, the
    elements of the null distribution with value ``0.7999999999999998`` would
    not be considered as extreme or more extreme as the observed value of the
    statistic, so the calculated p-value would be too small.

    >>> incorrect_pvalue = np.count_nonzero(null >= r) / len(null)
    >>> incorrect_pvalue
    0.14583333333333334  # may vary

    Instead, `permutation_test` treats elements of the null distribution that
    are within ``max(1e-14, abs(r)*1e-14)`` of the observed value of the
    statistic ``r`` to be equal to ``r``.

    >>> correct_pvalue = np.count_nonzero(null >= r - 1e-14) / len(null)
    >>> correct_pvalue
    0.16666666666666666
    >>> res.pvalue == correct_pvalue
    True

    This method of comparison is expected to be accurate in most practical
    situations, but the user is advised to assess this by inspecting the
    elements of the null distribution that are close to the observed value
    of the statistic. Also, consider the use of statistics that can be
    calculated using exact arithmetic (e.g. integer statistics).

    r   r)   )r  r   r  r   r   r   c                 X    | |z   k    }|                     d          z   z   z  }|S Nr   r   r~   r   r  r  r  
adjustmentr  rN   s       r&   r   zpermutation_test.<locals>.less!  9     Hu$44888##j0[:5MNr(   c                 X    | |z
  k    }|                     d          z   z   z  }|S r  r~   r  s       r&   r   z!permutation_test.<locals>.greater&  r  r(   c                 h     | |          } | |          }t          j        ||          dz  }|S r  )r,   r  )r   r  r  r  r  r   r   s        r&   r  z#permutation_test.<locals>.two_sided+  sB    t-x88!'"3X>>*\?;;a?r(   r  )r  r  r  r  r,   r4  r;   inexactr  r  r  r  rS  )r/   r1   r  r   rN   rD   r   r   rO   r   r  null_calculatorsnull_calculator_argscalculate_nullr   r  r  r  r!  r  r  r  r   r   s       `               @@@@r&   r   r   a  s   L  i1A: +UK #% %D  $T9&
K$y$(R((H$<#:';= = !)[!3(%&67N,- /{J !'aJ ]8>2:>> 2AA(.))-c1 F3>""E      
      
      !%' 'G #gk"#4h??Gggq!$$G 74EFFFr(   c                   0    e Zd ZU dZdZeed<   dZeed<   dS )ResamplingMethoda  Configuration information for a statistical resampling method.

    Instances of this class can be passed into the `method` parameter of some
    hypothesis test functions to perform a resampling or Monte Carlo version
    of the hypothesis test.

    Attributes
    ----------
    n_resamples : int
        The number of resamples to perform or Monte Carlo samples to draw.
    batch : int, optional
        The number of resamples to process in each vectorized call to
        the statistic. Batch sizes >>1 tend to be faster when the statistic
        is vectorized, but memory usage scales linearly with the batch size.
        Default is ``None``, which processes all resamples in a single batch.

    r   rN   NrD   )r   r   r   r   rN   r   r   rD   r!   r(   r&   r  r  ;  s>          " KE3r(   r  c                   >    e Zd ZU dZdZeed<   dZeed<   ddZd Z	dS )	MonteCarloMethoda  Configuration information for a Monte Carlo hypothesis test.

    Instances of this class can be passed into the `method` parameter of some
    hypothesis test functions to perform a Monte Carlo version of the
    hypothesis tests.

    Attributes
    ----------
    n_resamples : int, optional
        The number of Monte Carlo samples to draw. Default is 9999.
    batch : int, optional
        The number of Monte Carlo samples to process in each vectorized call to
        the statistic. Batch sizes >>1 tend to be faster when the statistic
        is vectorized, but memory usage scales linearly with the batch size.
        Default is ``None``, which processes all samples in a single batch.
    rvs : callable or tuple of callables, optional
        A callable or sequence of callables that generates random variates
        under the null hypothesis. Each element of `rvs` must be a callable
        that accepts keyword argument ``size`` (e.g. ``rvs(size=(m, n))``) and
        returns an N-d array sample of that shape. If `rvs` is a sequence, the
        number of callables in `rvs` must match the number of samples passed
        to the hypothesis test in which the `MonteCarloMethod` is used. Default
        is ``None``, in which case the hypothesis test function chooses values
        to match the standard version of the hypothesis test. For example,
        the null hypothesis of `scipy.stats.pearsonr` is typically that the
        samples are drawn from the standard normal distribution, so
        ``rvs = (rng.normal, rng.normal)`` where
        ``rng = np.random.default_rng()``.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator state. When `rng` is None, a new
        `numpy.random.Generator` is created using entropy from the
        operating system. Types other than `numpy.random.Generator` are
        passed to `numpy.random.default_rng` to instantiate a ``Generator``.

    Nr   rO   r   c                 h    ||d}t          |          || _        || _        || _        || _        d S )Nz.Use of `rvs` and `rng` are mutually exclusive.)r   rN   rD   r   rO   )selfrN   rD   r   rO   r   s         r&   __init__zMonteCarloMethod.__init__z  s?    O#/FGW%%%&
r(   c                 P    t          | j        | j        | j        | j                  S )N)rN   rD   r   rO   )r7  rN   rD   r   rO   r  s    r&   _asdictzMonteCarloMethod._asdict  s+     0
dh0 0 0 	0r(   )r   NNN)
r   r   r   r   r   objectr   rO   r  r  r!   r(   r&   r  r  R  sf         " "F CC   0 0 0 0 0r(   r  a  Use of attribute `random_state` is deprecated and replaced by `rng`. Support for `random_state` will be removed in SciPy 1.19.0. To silence this warning and ensure consistent behavior in SciPy 1.19.0, control the RNG using attribute `rng`. Values set using attribute `rng` will be validated by `np.random.default_rng`, so the behavior corresponding with a given value may change compared to use of `random_state`. For example, 1) `None` will result in unpredictable random numbers, 2) an integer will result in a different stream of random numbers, (with the same distribution), and 3) `np.random` or `RandomState` instances will result in an error. See the documentation of `default_rng` for more information.c                        e Zd ZU dZeed<    eddd          Zeed<   ed             Z	e	j
        d             Z	ed	             Zddd fdZd Z xZS )PermutationMethodaS	  Configuration information for a permutation hypothesis test.

    Instances of this class can be passed into the `method` parameter of some
    hypothesis test functions to perform a permutation version of the
    hypothesis tests.

    Attributes
    ----------
    n_resamples : int, optional
        The number of resamples to perform. Default is 9999.
    batch : int, optional
        The number of resamples to process in each vectorized call to
        the statistic. Batch sizes >>1 tend to be faster when the statistic
        is vectorized, but memory usage scales linearly with the batch size.
        Default is ``None``, which processes all resamples in a single batch.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator used to perform resampling.

        If `rng` is passed by keyword to the initializer or the `rng` attribute is used
        directly, types other than `numpy.random.Generator` are passed to
        `numpy.random.default_rng` to instantiate a ``Generator`` before use.
        If `rng` is already a ``Generator`` instance, then the provided instance is
        used. Specify `rng` for repeatable behavior.

        If this argument is passed by position, if `random_state` is passed by keyword
        into the initializer, or if the `random_state` attribute is used directly,
        legacy behavior for `random_state` applies:

        - If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState`
          singleton is used.
        - If `random_state` is an int, a new ``RandomState`` instance is used,
          seeded with `random_state`.
        - If `random_state` is already a ``Generator`` or ``RandomState`` instance then
          that instance is used.

        .. versionchanged:: 1.15.0

            As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
            transition from use of `numpy.random.RandomState` to
            `numpy.random.Generator`, this attribute name was changed from
            `random_state` to `rng`. For an interim period, both names will continue to
            work, although only one may be specified at a time. After the interim
            period, uses of `random_state` will emit warnings. The behavior of both
            `random_state` and `rng` are outlined above, but only `rng` should be used
            in new code.

    rO   FNinitreprdefault_rngc                     | j         S r+   _random_stater  s    r&   r   zPermutationMethod.random_state       !!r(   c                     || _         d S r+   r  r  r+  s     r&   r   zPermutationMethod.random_state       !r(   c                     | j         S r+   r  r  s    r&   rO   zPermutationMethod.rng  
    yr(   r   rO   c                j    || _         || _        t                                          ||           d S NrN   rD   )r  r  superr  )r  rN   rD   r   rO   	__class__s        r&   r  zPermutationMethod.__init__  s7     	)[>>>>>r(   c                     t          | j        | j                  }| j        
| j        |d<   | j        
| j        |d<   |S )Nr  rO   r   )r7  rN   rD   rO   r   r  r\  s     r&   r  zPermutationMethod._asdict  sG    T-TZ@@@8xAeH( $ 1Anr(   )r   NN)r   r   r   r   r  r   r   r  propertyr   setterrO   r  r  __classcell__r  s   @r&   r  r    s         . .^ 
KKK5e%>>>D&>>>" " X"
 ! ! !
   X?t ? ? ? ? ? ? ?      r(   r  c                        e Zd ZU dZeed<    eddd          Zeed<   dZe	ed<   e
d	             Zej        d
             Ze
d             Z	 	 ddd fdZd Z xZS )BootstrapMethoda9
  Configuration information for a bootstrap confidence interval.

    Instances of this class can be passed into the `method` parameter of some
    confidence interval methods to generate a bootstrap confidence interval.

    Attributes
    ----------
    n_resamples : int, optional
        The number of resamples to perform. Default is 9999.
    batch : int, optional
        The number of resamples to process in each vectorized call to
        the statistic. Batch sizes >>1 tend to be faster when the statistic
        is vectorized, but memory usage scales linearly with the batch size.
        Default is ``None``, which processes all resamples in a single batch.
    rng : `numpy.random.Generator`, optional
        Pseudorandom number generator used to perform resampling.

        If `rng` is passed by keyword to the initializer or the `rng` attribute is used
        directly, types other than `numpy.random.Generator` are passed to
        `numpy.random.default_rng` to instantiate a ``Generator``  before use.
        If `rng` is already a ``Generator`` instance, then the provided instance is
        used. Specify `rng` for repeatable behavior.

        If this argument is passed by position, if `random_state` is passed by keyword
        into the initializer, or if the `random_state` attribute is used directly,
        legacy behavior for `random_state` applies:

        - If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState`
          singleton is used.
        - If `random_state` is an int, a new ``RandomState`` instance is used,
          seeded with `random_state`.
        - If `random_state` is already a ``Generator`` or ``RandomState`` instance then
          that instance is used.

        .. versionchanged:: 1.15.0

            As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
            transition from use of `numpy.random.RandomState` to
            `numpy.random.Generator`, this attribute name was changed from
            `random_state` to `rng`. For an interim period, both names will continue to
            work, although only one may be specified at a time. After the interim
            period, uses of `random_state` will emit warnings. The behavior of both
            `random_state` and `rng` are outlined above, but only `rng` should be used
            in new code.

    method : {'BCa', 'percentile', 'basic'}
        Whether to use the 'percentile' bootstrap ('percentile'), the 'basic'
        (AKA 'reverse') bootstrap ('basic'), or the bias-corrected and
        accelerated bootstrap ('BCa', default).

    rO   FNr  r  r   r   c                     | j         S r+   r  r  s    r&   r   zBootstrapMethod.random_state'	  r  r(   c                     || _         d S r+   r  r  s     r&   r   zBootstrapMethod.random_state-	  r  r(   c                     | j         S r+   r  r  s    r&   rO   zBootstrapMethod.rng3	  r  r(   r   r  c                x    || _         || _        || _        t                                          ||           d S r  )r  r  r   r  r  )r  rN   rD   r   r   rO   r  s         r&   r  zBootstrapMethod.__init__7	  s>    
 	)[>>>>>r(   c                     t          | j        | j        | j                  }| j        
| j        |d<   | j        
| j        |d<   |S )N)rN   rD   r   rO   r   )r7  rN   rD   r   rO   r   r  s     r&   r  zBootstrapMethod._asdictA	  sR    T-TZ% % %8xAeH( $ 1Anr(   )r   NNr   )r   r   r   r   r  r   r   r  r   strr  r   r  rO   r  r  r  r  s   @r&   r  r    s         2 2f 
KKK5e%>>>D&>>>FC" " X"
 ! ! !
   X CG?&*? ? ? ? ? ? ?      r(   r  r+   )NN)Ar`   numpyr,   	itertoolsr   r   r   collections.abcr   dataclassesr   r   r   scipy._lib._utilr	   r
   r   r   scipy._lib._array_apir   r   r   scipy.specialr   r   r   r   _commonr   _axis_nan_policyr   r   _warnings_errorsr   __all__r8   rL   rP   rW   rk   r   r   r   r   r   r   r   r#  r1  rD  r$  rS  rb  rk  rw  r  r  r  r  r   r  r  _rs_deprecationr  r  r!   r(   r&   <module>r     sQ        9 9 9 9 9 9 9 9 9 9 $ $ $ $ $ $ ( ( ( ( ( ( ( ( 2 2 2 2 2 2 2 2 2 2 2 2 O O O O O O O O O O 6 6 6 6 6 6 6 6 6 6 6 6 ' ' ' ' ' ' G G G G G G G G 3 3 3 3 3 3
?
?
?  ,   (   P P P  00# 0# 0#fl+ l+ l+^ ' ' ' ' ' ' ' ', N##.2$e!d%edBP BP BP BP $#BPJO8 O8 O8d " " " " " " " "$ 8V$$9=!%T{ZF ZF ZF ZF %$ZFz                  "F> F> F>R 6:d4a6 a6 a6 a6 a6H " " " " " " " "$  6> > >$ $ $8 "99 99 99 99z "&69 69 69 69t !%0 0 0 0>;2 ;2 ;2| N##:G $$d!,1$VG VG VG VG $#VGr        , 40 40 40 40 40' 40 40 40n" R R R R R( R R Rj Z Z Z Z Z& Z Z Z Z Zr(   