
    _Mh                       d dl Z d dlmZmZ d dl mZ d dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZ d dlmZ  e            Z d Z!g dZ"dZ#dZ$ G d de          Z% G d de%          Z& G d de&          Z' G d de&          Z( G d de          Z) G d de)          Z* G d d          Z+d Z,d  Z-d! Z.d" Z/d# Z0dd$Z1d% Z2d& Z3d' Z4d( Z5d)d*d+Z6d, Z7d- Z8 G d. d/e          Z9i d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQi dRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsi dtdudvdwdxdydzd{d|d}d~dddddddddddddddddddddddi dddddddddddddddddddddddddddddddddddddddZ:d Z;d Z<d Z=d Z> G d de9          Z? G d de?          Z@ej         ej        fdĄZA G dń de?          ZB G dǄ de?          ZCdɄ ZD G dʄ de          ZE G d̄ de?          ZF G d΄ de?          ZGdЄ ZHdф ZId҄ ZJdS )    N)ABCabstractmethod)cached_property)inf)
_lazywhere
_rng_spawn)ClassDocNumpyDocString)specialstats)tanhsinh)_bracket_root_bracket_minimum)_chandrupatla_chandrupatla_minimize)_ProbabilityDistribution)qmcc                 6    t          |           t          u p| d u S N)typeobjectxs    h/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/stats/_distribution_infrastructure.py_isnullr      s    77f)T	)    )make_distributionMixtureorder_statistictruncateabsexplogskip_allno_cachec                       e Zd ZdZej        dej         dej        dej         diZed             Z	ed             Z
ed             Zed	             Zd
S )_Domaina   Representation of the applicable domain of a parameter or variable.

    A `_Domain` object is responsible for storing information about the
    domain of a parameter or variable, determining whether a value is within
    the domain (`contains`), and providing a text/mathematical representation
    of itself (`__str__`). Because the domain of a parameter/variable can have
    a complicated relationship with other parameters and variables of a
    distribution, `_Domain` itself does not try to represent all possibilities;
    in fact, it has no implementation and is meant for subclassing.

    Attributes
    ----------
    symbols : dict
        A map from special numerical values to symbols for use in `__str__`

    Methods
    -------
    contains(x)
        Determine whether the argument is contained within the domain (True)
        or not (False). Used for input validation.
    get_numerical_endpoints()
        Gets the numerical values of the domain endpoints, which may have been
        defined symbolically.
    __str__()
        Returns a text representation of the domain (e.g. ``[0, b)``).
        Used for generating documentation.

    z\inftyz-\inftyz\piz-\pic                     t                      r   NotImplementedErrorselfr   s     r   containsz_Domain.contains       !###r   c                     t                      r   r)   )r,   ns     r   drawz_Domain.draw   r.   r   c                     t                      r   r)   r+   s     r   get_numerical_endpointsz_Domain.get_numerical_endpoints   r.   r   c                     t                      r   r)   r,   s    r   __str__z_Domain.__str__   r.   r   N)__name__
__module____qualname____doc__npr   pisymbolsr   r-   r1   r3   r6    r   r   r'   r'      s         8 vy26':rufrufgVG$ $ ^$ $ $ ^$ $ $ ^$ $ $ ^$ $ $r   r'   c                   B     e Zd ZdZe efdf fd	Zd Zd ZddZ xZ	S )	_SimpleDomainaG   Representation of a simply-connected domain defined by two endpoints.

    Each endpoint may be a finite scalar, positive or negative infinity, or
    be given by a single parameter. The domain may include the endpoints or
    not.

    This class still does not provide an implementation of the __str__ method,
    so it is meant for subclassing (e.g. a subclass for domains on the real
    line).

    Attributes
    ----------
    symbols : dict
        Inherited. A map from special values to symbols for use in `__str__`.
    endpoints : 2-tuple of float(s) and/or str(s)
        A tuple with two values. Each may be either a float (the numerical
        value of the endpoints of the domain) or a string (the name of the
        parameters that will define the endpoint).
    inclusive : 2-tuple of bools
        A tuple with two boolean values; each indicates whether the
        corresponding endpoint is included within the domain or not.

    Methods
    -------
    define_parameters(*parameters)
        Records any parameters used to define the endpoints of the domain
    get_numerical_endpoints(parameter_values)
        Gets the numerical values of the domain endpoints, which may have been
        defined symbolically.
    contains(item, parameter_values)
        Determines whether the argument is contained within the domain

    )FFc                     t                      j                                        | _        |\  }}t          j        |          d         t          j        |          d         f| _        || _        d S Nr>   )superr=   copyr;   asarray	endpoints	inclusive)r,   rF   rG   ab	__class__s        r   __init__z_SimpleDomain.__init__  sV    ww++--1Ar*BJqMM",=="r   c                 R    d |D             }| j                             |           dS )a%   Records any parameters used to define the endpoints of the domain.

        Adds the keyword name of each parameter and its text representation
        to the  `symbols` attribute as key:value pairs.
        For instance, a parameter may be passed into to a distribution's
        initializer using the keyword `log_a`, and the corresponding
        string representation may be '\log(a)'. To form the text
        representation of the domain for use in documentation, the
        _Domain object needs to map from the keyword name used in the code
        to the string representation.

        Returns None, but updates the `symbols` attribute.

        Parameters
        ----------
        *parameters : _Parameter objects
            Parameters that may define the endpoints of the domain.

        c                 (    i | ]}|j         |j        S r>   )namesymbol.0params     r   
<dictcomp>z3_SimpleDomain.define_parameters.<locals>.<dictcomp>'  s    HHHEuz5<HHHr   N)r=   update)r,   
parametersnew_symbolss      r   define_parametersz_SimpleDomain.define_parameters  s4    ( IHZHHHK(((((r   c                    | j         \  }}	 t          j        |                    ||                    }t          j        |                    ||                    }n-# t          $ r }d| j         d}t	          |          |d}~ww xY w||fS )a   Get the numerical values of the domain endpoints.

        Domain endpoints may be defined symbolically. This returns numerical
        values of the endpoints given numerical values for any variables.

        Parameters
        ----------
        parameter_values : dict
            A dictionary that maps between string variable names and numerical
            values of parameters, which may define the endpoints.

        Returns
        -------
        a, b : ndarray
            Numerical values of the endpoints

        zThe endpoints of the distribution are defined by parameters, but their values were not provided. When using a private method of zA, pass all required distribution parameters as keyword arguments.N)rF   r;   rE   get	TypeErrorrJ   )r,   parameter_valuesrH   rI   emessages         r   r3   z%_SimpleDomain.get_numerical_endpoints*  s    & ~1
		,
+//15566A
+//15566AA 	, 	, 	,$48N$ $ $G
 G$$!+	, !ts   AA 
B'BBNc                     |pi }|                      |          \  }}| j        \  }}|r||k    n||k    }|r||k    n||k     }||z  S )a  Determine whether the argument is contained within the domain.

        Parameters
        ----------
        item : ndarray
            The argument
        parameter_values : dict
            A dictionary that maps between string variable names and numerical
            values of parameters, which may define the endpoints.

        Returns
        -------
        out : bool
            True if `item` is within the domain; False otherwise.

        )r3   rG   )	r,   itemr[   rH   rI   left_inclusiveright_inclusivein_leftin_rights	            r   r-   z_SimpleDomain.containsO  sl    " ,1r ++,<==1*..'-;$!))4!8 /=4199TAX!!r   r   )
r7   r8   r9   r:   r   rK   rW   r3   r-   __classcell__rJ   s   @r   r@   r@      s           B $'$ # # # # # #) ) ).# # #J "  "  "  "  "  "  "  "r   r@   c                        e Zd ZdZd ZddZdS )_RealDomaina   Represents a simply-connected subset of the real line; i.e., an interval

    Completes the implementation of the `_SimpleDomain` class for simple
    domains on the real line.

    Methods
    -------
    define_parameters(*parameters)
        (Inherited) Records any parameters used to define the endpoints of the
        domain.
    get_numerical_endpoints(parameter_values)
        (Inherited) Gets the numerical values of the domain endpoints, which
        may have been defined symbolically.
    contains(item, parameter_values)
        (Inherited) Determines whether the argument is contained within the
        domain
    __str__()
        Returns a string representation of the domain, e.g. "[a, b)".
    draw(size, rng, proportions, parameter_values)
        Draws random values based on the domain. Proportions of values within
        the domain, on the endpoints of the domain, outside the domain,
        and having value NaN are specified by `proportions`.

    c                     | j         \  }}| j        \  }}|rdnd}| j                            ||           }|rdnd}| j                            ||           }| | d| | S )N[(]), )rF   rG   r=   rY   )r,   rH   rI   r`   ra   leftrights          r   r6   z_RealDomain.__str__  s    ~1*..'$-ss#LQ1''&/CLQ1'''''Q''''r   Nc                    t           j                            |          }|                                |                                }}t          j        |          t          j        |          z  }	d||	<   d||	<   |f|z   }
|dk    r|                    |||
          }n|dk    r<|
}t          j        |          }|                    |          dk     }	|||	<   |||	 <   n|dk    rX||                    |
          z
  }||                    |
          z   }|                    |          dk     }	||	         ||	<   n%|dk    rt          j        |
t           j                  }|S )	a   Draw random values from the domain.

        Parameters
        ----------
        n : int
            The number of values to be drawn from the domain.
        type_ : str
            A string indicating whether the values are

            - strictly within the domain ('in'),
            - at one of the two endpoints ('on'),
            - strictly outside the domain ('out'), or
            - NaN ('nan').
        min, max : ndarray
            The endpoints of the domain.
        squeezed_based_shape : tuple of ints
            See _RealParameter.draw.
        rng : np.Generator
            The Generator used for drawing random values.

        r      in)sizeon      ?outnan)	r;   randomdefault_rngrD   isnanuniformonesfullrw   )r,   r0   type_minmaxsqueezed_base_shaperngmin_nnmax_nnishapez
z_on_shapezrs                 r   r1   z_RealDomain.draw  s_   , i##C(( SXXZZHVrx///q	q	**D==FF77AAd]]J
##A


""S(AAaDAqbEEe^^%000A#++5+111B


""S(Aa5AaDDe^^rv&&Ar   r   )r7   r8   r9   r:   r6   r1   r>   r   r   rg   rg   r  sA         2	( 	( 	(4 4 4 4 4 4r   rg   c                       e Zd ZdZd ZdS )_IntegerDomainz Representation of a domain of consecutive integers.

    Completes the implementation of the `_SimpleDomain` class for domains
    composed of consecutive integer values.

    To be completed when needed.
    c                     t           r   r)   r5   s    r   rK   z_IntegerDomain.__init__  s    !!r   N)r7   r8   r9   r:   rK   r>   r   r   r   r     s-         " " " " "r   r   c                   P    e Zd ZdZddddZd Zd
ddddddZed	             ZdS )
_Parametera   Representation of a distribution parameter or variable.

    A `_Parameter` object is responsible for storing information about a
    parameter or variable, providing input validation/standardization of
    values passed for that parameter, providing a text/mathematical
    representation of the parameter for the documentation (`__str__`), and
    drawing random values of itself for testing and benchmarking. It does
    not provide a complete implementation of this functionality and is meant
    for subclassing.

    Attributes
    ----------
    name : str
        The keyword used to pass numerical values of the parameter into the
        initializer of the distribution
    symbol : str
        The text representation of the variable in the documentation. May
        include LaTeX.
    domain : _Domain
        The domain of the parameter for which the distribution is valid.
    typical : 2-tuple of floats or strings (consider making a _Domain)
        Defines the endpoints of a typical range of values of the parameter.
        Used for sampling.

    Methods
    -------
    __str__():
        Returns a string description of the variable for use in documentation,
        including the keyword used to represent it in code, the symbol used to
        represent it mathemtatically, and a description of the valid domain.
    draw(size, *, rng, domain, proportions)
        Draws random values of the parameter. Proportions of values within
        the valid domain, on the endpoints of the domain, outside the domain,
        and having value NaN are specified by `proportions`.
    validate(x):
        Validates and standardizes the argument for use as numerical values
        of the parameter.

   N)rO   typicalc                    || _         |p|| _        || _        |$t          |t                    st          |          }|p|| _        d S r   )rN   rO   domain
isinstancer'   rg   r   )r,   rN   r   rO   r   s        r   rK   z_Parameter.__init__  sM    	nz'7'C'C!'**G(&r   c                 R    d| j          d| j         dt          | j                   dS )zA String representation of the parameter for use in documentation.`z` for :math:`z \in )rN   rO   strr   r5   s    r   r6   z_Parameter.__str__
  s1    Q49QQ4;QQc$+>N>NQQQQr   r   )r   regionproportionsr[   c                   |pi }| j         }|dn|}|t          j        |          z  }|                    |          \  }}	t          j        ||	          \  }}	|j        }
t          j        ||
          }t          j        |          }t          j        |
          }|rt          ||z            nd}t          j	        
                    |          }|                    ||          \  }}}}dt          |          t          |
          z
  z  |
z   }t          j        t          j        |          dk              d         }t          t!          t          |                              }t          j        |          |         }t          j        |                                          }t          j        |	                                          }|j        }|dk    r| j        }|                    |          \  }}	t          j        ||	          \  }}	t          j        |                                          }t          j        |	                                          }|                    |d||||          }n|                    |d||||          }|                    |d	||||          }|                    |d
||||          }|                    |d||||          } t          j        |||| fd          }!|                    |!d          }!t          j        |!t          |          |z             }!t          j        |!||          }!|!S )aS   Draw random values of the parameter for use in testing.

        Parameters
        ----------
        size : tuple of ints
            The shape of the array of valid values to be drawn.
        rng : np.Generator
            The Generator used for drawing random values.
        region : str
            The region of the `_Parameter` from which to draw. Default is
            "domain" (the *full* domain); alternative is "typical". An
            enhancement would give a way to interpolate between the two.
        proportions : tuple of numbers
            A tuple of four non-negative numbers that indicate the expected
            relative proportion of elements that:

            - are strictly within the domain,
            - are at one of the two endpoints,
            - are strictly outside the domain, and
            - are NaN,

            respectively. Default is (1, 0, 0, 0). The number of elements in
            each category is drawn from the multinomial distribution with
            `np.prod(size)` as the number of trials and `proportions` as the
            event probabilities. The values in `proportions` are automatically
            normalized to sum to 1.
        parameter_values : dict
            Map between the names of parameters (that define the endpoints of
            `typical`) and numerical values (arrays).

        N)rq   r   r   r   r   rq   rq   r   rr   r   rt   rv   rw   axis)r   r;   sumr3   broadcast_arraysr   broadcast_shapesprodintrx   ry   multinomiallenwhererE   tuplerangesqueezer   r1   concatenatepermutedreshapemoveaxis)"r,   rs   r   r   r   r[   r   pvalsrH   rI   
base_shapeextended_shape
n_extendedn_baser0   n_inn_onn_outn_nanbase_shape_paddedbase_singletonsnew_base_singletonsshape_expansionr   r   r   r   min_heremax_herez_inz_onz_outz_nanr   s"                                     r   r1   z_Parameter.draw  s   B ,1r&1&9ll{bf[111--.>??1"1a((1W
,T:>>W^,,
$$(29C
V#$$$i##C((#&??1e#<#< dE5 "3~#6#6Z#HI)*(2:.?#@#@!#CDDQG#E#o*>*>$?$?@@*^44_E j%%j%%!iYlG223CDDDAq&q!,,DAqz!))++..Hz!))++..H<<dHh@S$'   ) )DD ;;tT35Hc;RRD{{4sC1D#{NNE5#s4GSQQ{{5%c3FC{PPND$u5A>>>LLL##Jq%003FFGGK.@@r   c                     t                      r   r)   )r,   arrs     r   validatez_Parameter.validatey  r.   r   r   )	r7   r8   r9   r:   rK   r6   r1   r   r   r>   r   r   r   r     s        & &N 04T ) ) ) ) )R R RiT("i i i i iV $ $ ^$ $ $r   r   c                       e Zd ZdZd ZdS )_RealParameterz Represents a real-valued parameter.

    Implements the remaining methods of _Parameter for real parameters.
    All attributes are inherited.

    c                    t          j        |          }d}|j        t           j        k    s|j        t           j        k    rn|j        t           j        k    s|j        t           j        k    r!t          j        |t           j                  }nt          j        |j        t           j                  rn_t          j        |j        t           j	                  r!t          j        |t           j                  }nd| j
         d}t          |          | j                            ||          }|||z  n|}|d         |j        |fS )a\   Input validation/standardization of numerical values of a parameter.

        Checks whether elements of the argument `arr` are reals, ensuring that
        the dtype reflects this. Also produces a logical array that indicates
        which elements meet the requirements.

        Parameters
        ----------
        arr : ndarray
            The argument array to be validated and standardized.
        parameter_values : dict
            Map of parameter names to parameter value arrays.

        Returns
        -------
        arr : ndarray
            The argument array that has been validated and standardized
            (converted to an appropriate dtype, if necessary).
        dtype : NumPy dtype
            The appropriate floating point dtype of the parameter.
        valid : boolean ndarray
            Logical array indicating which elements are valid (True) and
            which are not (False). The arrays of all distribution parameters
            will be broadcasted, and elements for which any parameter value
            does not meet the requirements will be replaced with NaN.

        NdtypezParameter `z` must be of real dtype.r>   )r;   rE   r   float64float32int32int64
issubdtypefloatingintegerrN   rZ   r   r-   )r,   r   r[   valid_dtyper]   valids         r   r   z_RealParameter.validate  s   8 joo 9
""ci2:&=&=Y"(""ci28&;&;*S
333CC]39bk22 	%]39bj11 	%*S
333CCGDIGGGGG$$$$$S*:;;'2'>##E2w	5((r   N)r7   r8   r9   r:   r   r>   r   r   r   r   ~  s-         0) 0) 0) 0) 0)r   r   c                   >    e Zd ZdZd Zd Zd Zd Zd Zd Z	dd
Z
dS )_Parameterizationa   Represents a parameterization of a distribution.

    Distributions can have multiple parameterizations. A `_Parameterization`
    object is responsible for recording the parameters used by the
    parameterization, checking whether keyword arguments passed to the
    distribution match the parameterization, and performing input validation
    of the numerical values of these parameters.

    Attributes
    ----------
    parameters : dict
        String names (of keyword arguments) and the corresponding _Parameters.

    Methods
    -------
    __len__()
        Returns the number of parameters in the parameterization.
    __str__()
        Returns a string representation of the parameterization.
    copy
        Returns a copy of the parameterization. This is needed for transformed
        distributions that add parameters to the parameterization.
    matches(parameters)
        Checks whether the keyword arguments match the parameterization.
    validation(parameter_values)
        Input validation / standardization of parameterization. Validates the
        numerical values of all parameters.
    draw(sizes, rng, proportions)
        Draw random values of all parameters of the parameterization for use
        in testing.
    c                 (    d |D             | _         d S )Nc                     i | ]
}|j         |S r>   )rN   rP   s     r   rS   z._Parameterization.__init__.<locals>.<dictcomp>  s    EEE5:uEEEr   )rU   r,   rU   s     r   rK   z_Parameterization.__init__  s    EE*EEEr   c                 *    t          | j                  S r   )r   rU   r5   s    r   __len__z_Parameterization.__len__  s    4?###r   c                 B    t          | j                                         S r   )r   rU   valuesr5   s    r   rD   z_Parameterization.copy  s     $/"8"8":":;;r   c                 V    |t          | j                                                  k    S )a   Checks whether the keyword arguments match the parameterization.

        Parameters
        ----------
        parameters : set
            Set of names of parameters passed into the distribution as keyword
            arguments.

        Returns
        -------
        out : bool
            True if the keyword arguments names match the names of the
            parameters of this parameterization.
        )setrU   keysr   s     r   matchesz_Parameterization.matches  s%     S!5!5!7!78888r   c                 T   d}t                      }|                                D ]K\  }}| j        |         }|                    ||          \  }}}|                    |           ||z  }|||<   Lt          |          dk    r|j        nt          j        t          |           }||fS )aB   Input validation / standardization of parameterization.

        Parameters
        ----------
        parameter_values : dict
            The keyword arguments passed as parameter values to the
            distribution.

        Returns
        -------
        all_valid : ndarray
            Logical array indicating the elements of the broadcasted arrays
            for which all parameter values are valid.
        dtype : dtype
            The common dtype of the parameter arrays. This will determine
            the dtype of the output of distribution methods.
        Trq   )
r   itemsrU   r   addr   r   r;   result_typelist)	r,   r[   	all_validdtypesrN   r   	parameterr   r   s	            r   
validationz_Parameterization.validation  s    $ 	)//11 	) 	)ID#-I ) 2 238H I ICJJu!E)I%(T"" [[!^^		f1N%r   c                 r    d | j                                         D             }d                    |          S )z8Returns a string representation of the parameterization.c                 2    g | ]\  }}t          |          S r>   r   )rQ   rN   rR   s      r   
<listcomp>z-_Parameterization.__str__.<locals>.<listcomp>  s"    JJJ;4CJJJJJr   rm   )rU   r   join)r,   messagess     r   r6   z_Parameterization.__str__  s5    JJ$/2G2G2I2IJJJyy"""r   Nr   c                 *   i }t          |          rt          j        |d                   s|gt          | j                  z  }t	          || j                                                  D ]'\  }}|                    |||||          ||j        <   (|S )a  Draw random values of all parameters for use in testing.

        Parameters
        ----------
        sizes : iterable of shape tuples
            The size of the array to be generated for each parameter in the
            parameterization. Note that the order of sizes is arbitary; the
            size of the array generated for a specific parameter is not
            controlled individually as written.
        rng : NumPy Generator
            The generator used to draw random values.
        proportions : tuple
            A tuple of four non-negative numbers that indicate the expected
            relative proportion of elements that are within the parameter's
            domain, are on the boundary of the parameter's domain, are outside
            the parameter's domain, and have value NaN. For more information,
            see the `draw` method of the _Parameter subclasses.
        domain : str
            The domain of the `_Parameter` from which to draw. Default is
            "domain" (the *full* domain); alternative is "typical".

        Returns
        -------
        parameter_values : dict (string: array)
            A dictionary of parameter name/value pairs.
        r   )r   r   r[   r   )r   r;   iterablerU   zipr   r1   rN   )r,   sizesr   r   r   r[   rs   rR   s           r   r1   z_Parameterization.draw  s    < 5zz 	1U1X!6!6 	1GC000Eudo&<&<&>&>?? 	 	KD%+0::#;!1 ,6 , ,UZ((  r   )NNNr   )r7   r8   r9   r:   rK   r   rD   r   r   r6   r1   r>   r   r   r   r     s         >F F F$ $ $< < <9 9 9"     <# # #
*  *  *  *  *  * r   r   c                     ddt           j         dft           j         dfdt           t           fdt           dfdt           fdddddhh d	d
dhddht          j                    fd            }|S )Nr   rq   r   )icdficcdfilogcdfilogccdf)r   r   )rq   r   )logpdfpdf_logcdf1	_logccdf1_cdf1_ccdf1r   r   >   r   r   r   r   r   r   r   r   c                 d   | j         t          k    r  | |g|R i |S  j        }t          j        |          }| j        }| j        }|j        |k    r0t          j        |j        |          }t          j        ||          }|j	        |k    sf	 t          j
        |j	        |          }t          j        ||          }n5# t          $ r(}d| j        j         d| d}t          |          |d }~ww xY w                    ||                                           \  }	}
| j        j        j        \  }}|"v r|r||	k     n||	k    }|"v r|r||
k    n||
k    }||z  }|j	        dk    r|nt          j        |          }d}|!v r2||	k    }||
k    }||z  }|j	        dk    r|nt          j        |          }|r&t          j        ||d          }t          j        ||<   t          j          | |g|R i |          }d}|j        |k    rt          j        || j                  }d}|j	        |k    r t          j        || j                  }|p|p|}|rt          j        ||d          }|r9#                    |t          j        t          j        f          \  }}|||<   |||<   |r|                                 \  }}|j	        |k    rRt          j        t          j        ||          d	          }t          j        t          j        ||          d	          }|                    d
          r||         n||         }|                    d
          r||         n||         }|||<   |||<   |v rt          j        |dd          }n!|v r|j        }t          j        |d d          }|d         S )Nr   zThe argument provided to `.zJ` cannot be be broadcast to the same shape as the distribution parameters.r>   FTr   rD   rD   ccdf              ?)validation_policy	_SKIP_ALLr7   r;   rE   _dtype_shaper   r   r   r   broadcast_to
ValueErrorrJ   rY   support	_variabler   rG   anyarrayrw   endswithclipreal)$r,   r   argskwargsmethod_namer   r   r\   r]   lowhighleft_inc	right_incmask_low	mask_highmask_invalidany_invalidany_endpointmask_low_endpointmask_high_endpointmask_endpointresres_needs_copyreplace_lowreplace_highrH   rI   replace_low_endpointreplace_high_endpointr  clip_logrF   freplace_exactreplace_strictreplacementss$                                r   filteredz"_set_invalid_nan.<locals>.filtered\  sf   !Y..1T1.t...v...jJqMM 7eN17E22E
1E***A w%	1+AGU;;OAu-- 1 1 1<1H < <#< < <  !))q01 MM+t||~~>>	T #n3=) +~ = =( =AGGc 	"-"?"?I"?QXXt) 	 9,'3'9R'?'?||F<00 	
 -''!"c"#t).1CCM-:-@B-F-FMM!#!6!6 
  	%%d333A fAlOj44T444V4455 9N5$+66E!N9/#t{33C+J{JlN 	8(3e$777C  	*  rvrv.>?? &K'CM)C	N  	<<<>>DAqw%HR_Q66TBBBHR_Q66TBBB )4(<(<V(D(D *#$$() ! *5)=)=f)E)E +$%%)* " &:C!"&;C"# $'#r2&&CCH$$(C'#tR((C2ws   /B? ?
C1	#C,,C1)r;   r   	functoolswraps)r&  r*  r  r%  rF   r'  r(  r)  s   ` @@@@@@r   _set_invalid_nanr-  B  s       & fWaLwlD DI #tcTl6"%q	SD	#v7 7L X&N<<<MXDK(H_Qj j j j j j j j j j jX Or   c                 F     t          j                    fd            }|S )Nc                    | j         t          k    r | g|R i |S  | g|R i |}|t          | j                  t	          j        |          }d}|j        }|| j        k    rt	          j        || j                  }d}|j	        | j
        k    r#t	          j        || j
                  }|p| j        }|r|                    |d          }| j        rt          j        || j        <   |d         S )NFTr   r>   )r  r  r*   _not_implementedr;   rE   r   r  r   r   r  r  _any_invalidastyperw   _invalid)r,   r  r  r  
needs_copyr   r&  s         r   r*  z+_set_invalid_nan_property.<locals>.filtered  s   !Y..1T+D+++F+++a&t&&&v&&;%d&;<<<joo
	DKN5$+66EJ9##/#t{33C#8t'8J 	5**5t*44C 	( "$C2wr   r+  r,  )r&  r*  s   ` r   _set_invalid_nan_propertyr6    s:     _Q    > Or   c                 L     t          j                   d d fd
            }|S )Nmethodc                   j         }|p| j                            |d           }t          |          rna|1|dk    rdn|}|                    d|          }t          | |          }n. | g|R d|i|}|dk    r| j        t          k    r
|| j        |<   	  ||i |S # t          $ r}t          | j
                  |d }~ww xY w)Nzlog/explogexpdispatchr9  _sample_dispatch)r7   _method_cacherY   callablereplacegetattrcache_policy	_NO_CACHEKeyErrorr*   r0  )r,   r9  r  r  	func_namer  r\   r&  s          r   wrappedz_dispatch.<locals>.wrapped
  s   J	B4-11)TBBF 		7!'9!4!4XX&F#++J??KT;//FFQt<d<<<6<V<<F...43D	3Q3Q06"9-	D64*6*** 	D 	D 	D%d&;<<!C	Ds   B   
C*B??Cr5  r&  rF  s   ` r   	_dispatchrH    sN    $ _Q$( D D D D D D D& Nr   c                 F     t          j                    fd            }|S )Nc                    j         }|                                 \  }}t          j        ||||          \  }}}}t          j        |j        |j        | j                  }|                    |d          |                    |d          }}||k     }	||	         ||	         c||	<   ||	<   ||k     }
||
         ||
<   ||k     }
||
         ||
<   ||k    }
||
         ||
<   ||k    }
||
         ||
<    | ||g|R i |}|dv rt          j        |dd          }nt          j        |d d          }t          j	        |          }|dk    r||	xx         dz  cc<   n|dk    r!||	xx         d	z  cc<   ||	xx         d
z  cc<   n|dk    rIt          j
        |	          rt          j	        |dz             n|}||	         t          j        dz  z   ||	<   n0t          t          j        d          ||	                   j        ||	<   |d         S )NTr   >   _cdf2_ccdf2r  r  rK        rL  g       @_logcdf2                              ?   r>   )r7   r	  r;   r   r   r   r  r2  r  rE   r  r<   _logexpxmexpyr#   r  )r,   r   yr  r  rE  r  r  r   i_swapr   r  r&  s               r   rF  z'_cdf2_input_validation.<locals>.wrapped3  s5   J	LLNN	T-aC>>1c4qw==xxDx))188E8+E+E1
 Q y!F)&	1V9G1v!G1v!HAw!HAw!aa,T,,,V,, +++'#r2&&CC'#tR((C jooKKK3KKKK(""KKK2KKKKKK2KKKK*$$*,&..A"*S2X&&&cCf+b0CKK (q		3v;??DCK2wr   r5  rG  s   ` r   _cdf2_input_validationrV  !  s;    $ _Q- - - - -^ Nr   c                     t          j        | j        t           j                  rt          j        | j                  S t          j        |           S r   )r;   r   r   inexactfinfoiinfor   s    r   _fiinfor[  f  s:    	}QWbj)) x   x{{r   c                      |pg }|pi }t          |                                          t          |           fd}t          |          t          |                                          z   }||fS )Nc                 r     | g|d          R i t          t          |d                              S r   )dictr   )r   r  r&  n_argsnamess     r   rF  z_kwargs2args.<locals>.wrapped{  sE    qFT'6']FFFd3ud677m+D+D&E&EFFFr   )r   r   r   r   )r&  r  r  rF  r_  r`  s   `   @@r   _kwargs2argsra  m  s     :2D\rFEYYFG G G G G G G ::V]]__---DD=r   c                 J    d }d }t          | dk     | f||          d         S )a  Compute the log of the complement of the exponential.

    This function is equivalent to::

        log1mexp(x) = np.log(1-np.exp(x))

    but avoids loss of precision when ``np.exp(x)`` is nearly 0 or 1.

    Parameters
    ----------
    x : array_like
        Input array.

    Returns
    -------
    y : ndarray
        An array of the same shape as `x`.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.stats._distribution_infrastructure import _log1mexp
    >>> x = 1e-300  # log of a number very close to 1
    >>> _log1mexp(x)  # log of the complement of a number very close to 1
    -690.7755278982137
    >>> # np.log1p(-np.exp(x))  # -inf; emits warning

    c                 P    t          j        t          j        |                      S r   )r;   log1pr"   r   s    r   f1z_log1mexp.<locals>.f1  s    x
###r   c           	          t          j        d          5  t          j        t          j        t	          j        | dz                                  cd d d            S # 1 swxY w Y   d S )NignoredividerP  )r;   errstater  r#   r   expm1r   s    r   f2z_log1mexp.<locals>.f2  s    [))) 	; 	;7267=R#8#8"899::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   <AA#&A#rN  )r&  rl  r>   )r   )r   re  rl  s      r   	_log1mexprm    sD    :$ $ $; ; ;
 a"fqdbR00044r   c                    t          j        t          j        |                    }t          j        |          rGt          j        |                                          }t          j        |j                  j        ||<   t          j	        | |          \  } }t          j        t          j        | |t           j        dz  z   gd                    }| |k    }t           j         ||<   |S )z Compute the log of the difference of the exponentials of two arguments.

    Avoids over/underflow, but does not prevent loss of precision otherwise.
    rQ  r   r   )r;   isneginfr  r  rE   rD   rY  r   r   r   r   	logsumexpr<   r   )r   rT  r   r  s       r   rS  rS    s     	BGAJJA	vayy %Jqvvxx  x  $!q!$$DAq
*W&1RU2X:Q???
@
@C	
aAfWCFJr   c                    t          j        | d          }t          j        |          }t          j        |           t          j        |          z  }| |         ||<   ||         ||<   t          j        |           t          j        |           z  }| |         ||<   | |         dz   ||<   t          j        |          t          j        |            z  }||         dz
  ||<   ||         ||<   ||fS )NrM  rq   )r;   	full_like	ones_likeisfinite)xminxmaxrH   rI   r   s        r   _guess_bracketrw    s    
T4  A
TA
DBK---A7AaD7AaD
DR[....A7AaD7Q;AaD
DR[....A7Q;AaD7AaDa4Kr   c                 ^   | j         }t          j        |           } t          j        |                               | j                  }t          j        |           }|}t          j        |dz            dk     }||         t          j        dz  z   ||<   |	                    |          d         S )a  Standardizes the (complex) logarithm of a real number.

    The logarithm of a real number may be represented by a complex number with
    imaginary part that is a multiple of pi*1j. Even multiples correspond with
    a positive real and odd multiples correspond with a negative real.

    Given a logarithm of a real number `x`, this function returns an equivalent
    representation in a standard form: the log of a positive real has imaginary
    part `0` and the log of a negative real has imaginary part `pi`.

    rQ  ru   r>   )
r   r;   
atleast_1dr  r2  r   imagr"   r<   r   )r   r   r  complexrT  negatives         r   _log_real_standardizer}    s     GE
aA71::QW%%DgajjGAvgbj!!C'HH+
*AhK99UBr   Tinclude_examplesc                   t          t          j                  }|                    d           |s|                    d           t	          |           }t	          t
                    }|D ]v}|dv r||         ||<   |dv r|dk    r)||                             t          |                      F|dk    rt          |           g||<   `||xx         ||         z  cc<   wt          |          S )NindexExamples>   Methods
Attributes>   SummaryzExtended Summary)
r   r
   sectionsremover	   ContinuousDistributionappend_generate_domain_support_generate_exampler   )dist_familyr  fieldsdocsuperdocfields         r   _combine_docsr    s	   ())F
MM' "j!!!
;

C.//H 
* 
*---!%CJJk!!(((J6{CCDDDDj  +K889CJJJJJ(5/)JJJJs88Or   c                    t          | j                  }d| j        j         d}|dk    rd}na|dk    rdt	          | j        d                    d}n<dd	d
dd|         }d | j        D             }d                    |          }d| d| d}d                    d |                    d          D             dd                    }||z   S )Nz
for :math:`x` in z.
r   z@
        This class accepts no distribution parameters.
        rq   z:
        This class accepts one parameterization:
        z
.
        twothreefourfive)rR           c                 2    g | ]}d t          |           S )z- r   )rQ   ps     r   r   z,_generate_domain_support.<locals>.<listcomp>  s0     = = =q]#a&&]] = = =r   
z
        This class accepts z parameterizations:

        	
        c                 6    g | ]}|                                 S r>   lstriprQ   lines     r   r   z,_generate_domain_support.<locals>.<listcomp>       GGG4GGGr   )r   _parameterizationsr
  r   r   r   split)r  n_parameterizationsr   r	  numberparameterizationss         r   r  r    s/   k<==D;#8#?DDDFa 
	!	!	[+A.	/	/  
 w6f==!= =(;= = = II&788"  
  
 iiGG7==3F3FGGGKLLGGr   c                    |                      d          }dg|z  }t          j                            d          }d}|                     |||          t          j                            d          }| j        }|rt          j        |         j                  }fd|D             }d t          ||          D             }| dd	
                    |           d
}	d	
                    d j        D                       }
 | di t          t          ||                    }n| d}	}d}t          |                    |          d          }t          |                    d|z            d          }d| d|	 d|                                 d}|r0|d|
 dt!          |j                                                   dz  }|d| d| d|                    |           d| d|                                |                                |                                f d|                                |                                f d|                                |                                f dt5          |                    d                     dz  }d 
                    d! |                    d           D             d"d                    }|S )#Nr   r>   l   j? )r   i_parameterizationl   l c                 L    g | ] }t          t          |          d           !S rR  )roundrA  )rQ   rN   dists     r   r   z%_generate_example.<locals>.<listcomp>&  s9     - - -dE'$"5"5q99 - - -r   c                 "    g | ]\  }}| d | S =r>   )rQ   rN   values      r   r   z%_generate_example.<locals>.<listcomp>(  s6     ? ? ?[T5$(((( ? ? ?r   rj   rm   rl   c                     g | ]}d | S )zX.r>   rP   s     r   r   z%_generate_example.<locals>.<listcomp>+  s    KKKUKKKr   z()g{Gz?rR  a  
    To use the distribution class, it must be instantiated using keyword
    parameters corresponding with one of the accepted parameterizations.

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>> from scipy.stats import z
    >>> X = a  

    For convenience, the ``plot`` method can be used to visualize the density
    and other functions of the distribution.

    >>> X.plot()
    >>> plt.show()

    The support of the underlying distribution is available using the ``support``
    method.

    >>> X.support()
    z
    z
        The numerical values of parameters associated with all parameterizations
        are available as attributes.

        >>> r  ze
    To evaluate the probability density function of the underlying distribution
    at argument ``x=z``:

    >>> x = z
    >>> X.pdf(x)
    a  

    The cumulative distribution function, its complement, and the logarithm
    of these functions are evaluated similarly.

    >>> np.allclose(np.exp(X.logccdf(x)), 1 - X.cdf(x))
    True

    The inverse of these functions with respect to the argument ``x`` is also
    available.

    >>> logp = np.log(1 - X.ccdf(x))
    >>> np.allclose(X.ilogcdf(logp), x)
    True

    Note that distribution functions and their logarithms also have two-argument
    versions for working with the probability mass between two arguments. The
    result tends to be more accurate than the naive implementation because it avoids
    subtractive cancellation.

    >>> y = z
    >>> np.allclose(X.ccdf(x, y), 1 - (X.cdf(y) - X.cdf(x)))
    True

    There are methods for computing measures of central tendency,
    dispersion, higher moments, and entropy.

    >>> X.mean(), X.median(), X.mode()
    z2
    >>> X.variance(), X.standard_deviation()
    z(
    >>> X.skewness(), X.kurtosis()
    aR  
    >>> np.allclose(X.moment(order=6, kind='standardized'),
    ...             X.moment(order=6, kind='central') / X.variance()**3)
    True
    >>> np.allclose(np.exp(X.logentropy()), X.entropy())
    True

    Pseudo-random samples can be drawn from
    the underlying distribution using ``sample``.

    >>> X.sample(shape=(4,))
    )r  r   z  # may vary
    r  c                 6    g | ]}|                                 S r>   r  r  s     r   r   z%_generate_example.<locals>.<listcomp>  r  r   rq   )_num_parametersr;   rx   ry   _drawr7   r   r  rU   r   r   _parametersr^  r  r   r	  r   r   r   meanmedianmodevariancestandard_deviationskewnesskurtosisreprsampler  )r  n_parametersshapesr   r   rN   parameter_namesr[   name_valuesinstantiation
attributesXr  r   rT  exampler  s                   @r   r  r    sp   ..q11LTL F
)


0
0C	AVCCD
)



+
+CD t6q9DEE- - - -+- - -? ??,<==? ? ?;;$))K"8"8;;;YYKK$:JKKKLL
KGG$s?4DEEFFGGAaffQiiAaffQUmmQA "&   ( YY[[)  G.    	 
 
q}##%%	&	&   	  22 2 	2 2 UU1XX2 24 52 2D VVXXqxxzz16688$E2 2H ZZ\\1''))*I2 2L ZZ\\1::<< M2 2b 
!(((

c2 2 2 2Gj iiGG7==3F3FGGGKLLGNr   c                      e Zd ZdZdZg ZeddddZdddZd Z	d	 Z
d
 Zd Zd Zd Zd Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Z d! Z!d" Z"d# Z#d$ Z$e%	 	 dd%            Z&e%d&             Z'e%dd(            Z(	 	 dd*Z)ddd+d,Z*d- Z+d. Z,d/ Z-e.dd0d1            Z/e0dd2            Z1d3 Z2d4 Z3d5 Z4d6 Z5e.dd0d7            Z6e0dd8            Z7d9 Z8d: Z9d; Z:e.dd0d<            Z;e0dd=            Z<d> Z=d? Z>e.dd0d@            Z?e0ddA            Z@dB ZAdC ZBdd0dDZCdd0dEZDdd0dFZEdd0dGZFddHdIdJZGeHdd0dK            ZIe0dd0dL            ZJdM ZKdN ZLeHdd0dO            ZMe0dd0dP            ZNdQ ZOdR ZPddd0dSZQeRdT             ZSe0dd0dU            ZTdV ZUdW ZVdX ZWdY ZXdZ ZYeHdd0d[            ZZe0dd0d\            Z[d] Z\d^ Z]d_ Z^d` Z_da Z`ddd0dbZaeRdc             Zbe0dd0dd            Zcde Zddf Zedg Zfdh Zgdi ZheHdj             Zie0dd0dk            Zjdl Zkdm Zldn Zmdo Zndp Zoddd0dqZpeRdr             Zqe0dd0ds            Zrdt Zsdu ZteHdd0dv            Zue0ddw            Zvdx Zwdy Zxdz Zyd{ Zzd| Z{ddd0d}Z|eRd~             Z}e0dd0d            Z~d Zd ZeHd             Ze0dd            Zd Zd Zd Zd Zd ZeHdd0d            Ze0dd            Zd Zd Zd ZeHdd0d            Ze0dd            Zd Zd Zd Zd ZeHdd0d            Ze0dd            Zd Zd Zd ZeHdd0d            Ze0dd            Zd Zd Zd Zd ZdddddZe0d             Zd Zd Zd Zed             Zed             Zed             Zd Ze.ddd0d            Zddd0dZd Zd Zd Zd Zddd0dZd Zd Zd Zd Zd Zddd0dZd Zd Zd Zd Zd Zd Zd Zddd)ddZd ZddddŜdƄZdS )r  a2	   Class that represents a continuous statistical distribution.

    Parameters
    ----------
    tol : positive float, optional
        The desired relative tolerance of calculations. Left unspecified,
        calculations may be faster; when provided, calculations may be
        more likely to meet the desired accuracy.
    validation_policy : {None, "skip_all"}
        Specifies the level of input validation to perform. Left unspecified,
        input validation is performed to ensure appropriate behavior in edge
        case (e.g. parameters out of domain, argument outside of distribution
        support, etc.) and improve consistency of output dtype, shape, etc.
        Pass ``'skip_all'`` to avoid the computational overhead of these
        checks when rough edges are acceptable.
    cache_policy : {None, "no_cache"}
        Specifies the extent to which intermediate results are cached. Left
        unspecified, intermediate results of some calculations (e.g. distribution
        support, moments, etc.) are cached to improve performance of future
        calculations. Pass ``'no_cache'`` to reduce memory reserved by the class
        instance.

    Attributes
    ----------
    All parameters are available as attributes.

    Methods
    -------
    support

    plot

    sample

    moment

    mean
    median
    mode

    variance
    standard_deviation

    skewness
    kurtosis

    pdf
    logpdf

    cdf
    icdf
    ccdf
    iccdf

    logcdf
    ilogcdf
    logccdf
    ilogccdf

    entropy
    logentropy

    See Also
    --------
    :ref:`rv_infrastructure` : Tutorial

    Notes
    -----
    The following abbreviations are used throughout the documentation.

    - PDF: probability density function
    - CDF: cumulative distribution function
    - CCDF: complementary CDF
    - entropy: differential entropy
    - log-*F*: logarithm of *F* (e.g. log-CDF)
    - inverse *F*: inverse function of *F* (e.g. inverse CDF)

    The API documentation is written to describe the API, not to serve as
    a statistical reference. Effort is made to be correct at the level
    required to use the functionality, not to be mathematically rigorous.
    For example, continuity and differentiability may be implicitly assumed.
    For precise mathematical definitions, consult your preferred mathematical
    text.

    rq   N)tolr  rB  c                    || _         || _        || _        d| j        j         d| _        i | _        d t          |                                          D             } | j	        di | d S )Nr   z` does not provide an accurate implementation of the required method. Consider leaving `method` and `tol` unspecified to use another implementation.c                     i | ]
\  }}|||S r   r>   )rQ   keyvals      r   rS   z3ContinuousDistribution.__init__.<locals>.<dictcomp>  s-     E E E8347O 34COOr   r>   )
r  r  rB  rJ   r7   r0  _original_parameterssortedr   _update_parameters)r,   r  r  rB  rU   s        r   rK   zContinuousDistribution.__init__  s    !2(L' L L L 	
 %'!
E EZ--//00E E E
--*-----r   r  c          	         | j                                         x}} |j        d	i | d}t          j        d          | _        d| _        t                      | _        d| _	        d| _
        t          j        | _        |p| j        t          k    r | j        d	i |}nt!          | j                  s2|r/d| j        j         dt)          |           d}t+          |          n|                     |          }|                     |          \  }}}}	|                     ||          \  }}
}} | j        d	i |}|
| _        || _        || _        || _
        |	| _	        || _        |                                  || _        || _        || _         | j                                        D ]>}t;          | j        |          rt=          | j        |t?          |fd                     ?dS )
a   Update the numerical values of distribution parameters.

        Parameters
        ----------
        **params : array_like
            Desired numerical values of the distribution parameters. Any or all
            of the parameters initially used to instantiate the distribution
            may be modified. Parameters used in alternative parameterizations
            are not accepted.

        validation_policy : str
            To be documented. See Question 3 at the top.
        NFr   rq   The `zB` distribution family does not accept parameters, but parameters `z` were provided.c                 L    | j         |                                         d         S rB   )r  rD   )self_name_s     r   <lambda>z;ContinuousDistribution._update_parameters.<locals>.<lambda>E  s$    383DU3K3P3P3R3RSU3V r   r>   ) r  rD   rT   r;   rE   r3  r1  r   r  _ndim_sizer   r  r  r  _process_parametersr   r  rJ   r7   r   r  _identify_parameterization
_broadcast	_validatereset_cacher  _parameterizationr   hasattrsetattrproperty)r,   r  paramsrU   original_parametersparameterizationr]   r   rs   ndiminvalidr  r   rN   s                 r   r  z)ContinuousDistribution._update_parameters  sM    ,0+D+I+I+K+KK
(
##F###
5))!gg

j7!7IEE11??J??JJT,-- $	  *A4>#: A A":A A A !)))	*0  $>>zJJ,0OOJ,G,G)JtT/<< 4Je11??J??J#DM +DDKDJDJDK%!1$7!$))++ 	Y 	YDt~t,, DND(t 4W 4W 4W +X +X Y Y Y Y		Y 	Yr   c                 Z    i | _         i | _        i | _        d| _        i | _        d| _        dS )z Clear all cached values.

        To improve the speed of some calculations, the distribution's support
        and moments are cached.

        This function is called automatically whenever the distribution
        parameters are updated.

        N)_moment_raw_cache_moment_central_cache_moment_standardized_cache_support_cacher>  _constant_cacher5   s    r   r  z"ContinuousDistribution.reset_cacheH  s:     "$%'"*,'"#r   c                     t          |          }| j        D ]}|                    |          r nK|sd| j        j         d}n(|                     |          }d| d| j        j         d}t          |          |S )Nr  zB` distribution family requires parameters, but none were provided.zThe provided parameters `z4` do not match a supported parameterization of the `z` distribution family.)r   r  r   rJ   r7   _get_parameter_strr  )r,   rU   parameter_names_setr  r]   r  s         r   r  z1ContinuousDistribution._identify_parameterization]  s     "*oo $ 7 	& 	&''(;<<  ' P'4>#: ' ' ' #'"9"9*"E"EO O O#~6O O O W%%%r   c                 @   d |                                 D             }t          d |D                       }t          |          dk    r'||d         j        |d         j        |d         j        fS 	 t          j        | }nJ# t          $ r=}| 	                    |          }d| d| j
        j         d}t          |          |d }~ww xY wt          t          |                                |                    |d         j        |d         j        |d         j        fS )Nc                 6    g | ]}t          j        |          S r>   r;   rE   rQ   r   s     r   r   z5ContinuousDistribution._broadcast.<locals>.<listcomp>  s8     @ @ @' *Y// @ @ @r   c              3   $   K   | ]}|j         V  d S r   r  r  s     r   	<genexpr>z4ContinuousDistribution._broadcast.<locals>.<genexpr>  s$      OO9yOOOOOOr   rq   r   zThe parameters `z` provided to the `z<` distribution family cannot be broadcast to the same shape.)r   r   r   r   rs   r  r;   r   r  r  rJ   r7   r^  r   r   )r,   rU   parameter_valsparameter_shapesr\   r  r]   s          r   r  z!ContinuousDistribution._broadcast|  sd   @ @+5+<+<+>+>@ @ @OOOOOOO  A%%q 1 7"1%*N1,=,BD D	-0.ANN 	- 	- 	-"55jAAO@/ @ @>2@ @ @G W%%1,	- S**N;;<<q!'q!&q!&( 	(s   3B 
C	8CC	c                     |                     |          \  }}| }|j        dk    r|nt          j        |          }|r7|D ]4}t          j        ||                   ||<   t          j        ||         |<   5||||fS rB   )r   r   r;   r  rD   rw   )r,   r  rU   r   r   r  r  parameter_names           r   r  z ContinuousDistribution._validate  s     (22:>>u&!("!4!4gg"&// 	=", = =-/W~..0 .0
>*68f
>*7337K66r   c                     |S )a/   Process and cache distribution parameters for reuse.

        This is intended to be overridden by subclasses. It allows distribution
        authors to pre-process parameters for re-use. For instance, when a user
        parameterizes a LogUniform distribution with `a` and `b`, it makes
        sense to calculate `log(a)` and `log(b)` because these values will be
        used in almost all distribution methods. The dictionary returned by
        this method is passed to all private methods that calculate functions
        of the distribution.
        r>   r,   r  s     r   r  z*ContinuousDistribution._process_parameters  s	     r   c                 X    dd                     |                                           dS )N{rm   })r   r   r   s     r   r  z)ContinuousDistribution._get_parameter_str  s)    4DIIjoo//004444r   c                     | j                                         | _         t          t          | j                             D ])}| j         |                                         | j         |<   *d S r   )r  rD   r   r   )r,   r   s     r   _copy_parameterizationz-ContinuousDistribution._copy_parameterization  sm    "&"9">">"@"@s423344 	K 	KA)-)@)C)H)H)J)JD#A&&	K 	Kr   c                     | j         S )zpositive float:
        The desired relative tolerance of calculations. Left unspecified,
        calculations may be faster; when provided, calculations may be
        more likely to meet the desired accuracy.
        )_tolr5   s    r   r  zContinuousDistribution.tol  s     yr   c                     t          |          r	|| _        d S t          j        |          }|j        dk    s*|dk    r$t          j        |j        t          j                  sd| j        j	         d}t          |          |d         | _        d S )Nr>   r   zAttribute `tol` of `z)` must be a positive float, if specified.)r   r  r;   rE   r   r   r   r   rJ   r7   r  )r,   r  r]   s      r   r  zContinuousDistribution.tol  s    3<< 	DIFjooIOO377M#)R[99 $+<dn.E < < <GW%%%G			r   c                     | j         S )ai  {None, "no_cache"}:
        Specifies the extent to which intermediate results are cached. Left
        unspecified, intermediate results of some calculations (e.g. distribution
        support, moments, etc.) are cached to improve performance of future
        calculations. Pass ``'no_cache'`` to reduce memory reserved by the class
        instance.
        )_cache_policyr5   s    r   rB  z#ContinuousDistribution.cache_policy  s     !!r   c                     |!t          |                                          nd }d dh}||vr"d| j        j         d| d}t	          |          || _        d S )Nr%   zAttribute `cache_policy` of `` must be one of , if specified.)r   lowerrJ   r7   r  r  )r,   rB  cache_policiesr]   s       r   rB  z#ContinuousDistribution.cache_policy  s    4@4Ls<((..000RV
+~--It~7N I I)7I I IGW%%%)r   c                     | j         S )a  {None, "skip_all"}:
        Specifies the level of input validation to perform. Left unspecified,
        input validation is performed to ensure appropriate behavior in edge
        case (e.g. parameters out of domain, argument outside of distribution
        support, etc.) and improve consistency of output dtype, shape, etc.
        Use ``'skip_all'`` to avoid the computational overhead of these
        checks when rough edges are acceptable.
        )_validation_policyr5   s    r   r  z(ContinuousDistribution.validation_policy  s     &&r   c                     |!t          |                                          nd }d dh}||vr"d| j        j         d| d}t	          |          || _        d S )Nr$   z"Attribute `validation_policy` of `r
  r  )r   r  rJ   r7   r  r  )r,   r  iv_policiesr]   s       r   r  z(ContinuousDistribution.validation_policy  s     !2 = !!23399;;;CG 	Z(K//FDN<S F F)4F F FGW%%%"3r   c                 h   | j         j        }t          | j                                                  }g }t          j        d          5  d |D             }ddd           n# 1 swxY w Y   d                    |           }|                    |           | dd                    |           dS )z Returns a string representation of the distribution.

        Includes the name of the distribution family, the names of the
        parameters and the `repr` of each of their values.


        
   	thresholdc                 <    g | ]\  }}| d t          |           S r  )r  rQ   rO   r  s      r   r   z3ContinuousDistribution.__repr__.<locals>.<listcomp>  s0    XXXMFE77$u++77XXXr   Nrm   rj   rl   	rJ   r7   r   r  r   r;   printoptionsr   r  r,   
class_namerU   infostr_parameterss        r   __repr__zContinuousDistribution.__repr__  s    ^,
$399;;<<
_r*** 	Y 	YXXZXXXN	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y IIn557N###11tyy1111   
A##A'*A'c                 h   | j         j        }t          | j                                                  }g }t          j        d          5  d |D             }d d d            n# 1 swxY w Y   d                    |           }|                    |           | dd                    |           dS )Nr  r  c                 <    g | ]\  }}| d t          |           S r  r   r  s      r   r   z2ContinuousDistribution.__str__.<locals>.<listcomp>&  s0    WWW=6566#e**66WWWr   rm   rj   rl   r  r  s        r   r6   zContinuousDistribution.__str__!  s    ^,
$399;;<<
_r*** 	X 	XWWJWWWN	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X IIn557N###11tyy1111r  c                 $    t          | |          S N)locShiftedScaledDistributionr,   r$  s     r   __add__zContinuousDistribution.__add__+  s    (37777r   c                 &    t          | |           S r#  r%  r'  s     r   __sub__zContinuousDistribution.__sub__.  s    (C48888r   c                 $    t          | |          S )Nscaler%  r,   r-  s     r   __mul__zContinuousDistribution.__mul__1  s    (U;;;;r   c                 *    t          | d|z            S )Nrq   r,  r%  r.  s     r   __truediv__z"ContinuousDistribution.__truediv__4  s    (QuW====r   c                    t          j                  rdk    st                    k    rd}t          |          t          j        d          5  dt          |            dt                     }dt          |            dt                     }d d d            n# 1 swxY w Y   dz  dk    rt          |           n| }t          fd||fd	fd
          }t          |fi |ddiS )Nr   zrRaising a random variable to the power of an argument is only implemented when the argument is a positive integer.r  r  rj   z)**rR  c                     | z  S r   r>   uothers    r   r  z0ContinuousDistribution.__pow__.<locals>.<lambda>D  s    E r   c                 b    t          j        |           t          j        |           dz  z  z  S Nrq   )r;   signr!   r4  s    r   r  z0ContinuousDistribution.__pow__.<locals>.<lambda>F  s%    bfQii!e).D!D r   c                 J    dz  t          j        |           dz  dz
  z  z  S r8  )r;   r!   r4  s    r   r  z0ContinuousDistribution.__pow__.<locals>.<lambda>G  s%    !E'BF1II%!,D"D r   grepr_patternstr_patternhdh
increasingT)
r;   isscalarr   r*   r  r  r   r!   r^   MonotonicTransformedDistribution)r,   r6  r]   r=  r>  r  funcss    `     r   __pow__zContinuousDistribution.__pow__7  so   {5!! 	/UaZZ5CJJ3F3FNG%g... _r*** 	9 	9;tDzz;;d5kk;;L8c$ii88CJJ88K	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 aCIIIT))))!,DDDDDDDDF F F
 0LLULLtLLLLs   AB&&B*-B*c                 ,    |                      |          S r   )r(  r,   r6  s     r   __radd__zContinuousDistribution.__radd__K      ||E"""r   c                 P    |                                                      |          S r   )__neg__r(  rG  s     r   __rsub__zContinuousDistribution.__rsub__N  s    ||~~%%e,,,r   c                 ,    |                      |          S r   )r/  rG  s     r   __rmul__zContinuousDistribution.__rmul__Q  rI  r   c           
         |                                  \  }}t          j        d          5  t          d t	          |           dt	          |            dt          |           dt          |            dd d           }d d d            n# 1 swxY w Y   t          j        |d	k              st          j        |d	k              rt          | fi |d
di}nd}t          |          t          j        |dk              r|S ||z  S )Nr  r  c                     d| z  S r8  r>   r5  s    r   r  z5ContinuousDistribution.__rtruediv__.<locals>.<lambda>W  
    QU r   z/(rl   c                     d| z  S r8  r>   rQ  s    r   r  z5ContinuousDistribution.__rtruediv__.<locals>.<lambda>Z  rR  r   c                     d| dz  z  S )Nrq   rR  r>   rQ  s    r   r  z5ContinuousDistribution.__rtruediv__.<locals>.<lambda>Z  s    Q!V r   r;  r   rA  FzjDivision by a random variable is only implemented when the support is either non-negative or non-positive.rq   )	r	  r;   r  r^  r  r   allrC  r*   )r,   r6  rH   rI   rD  rv   r]   s          r   __rtruediv__z#ContinuousDistribution.__rtruediv__T  sx   ||~~1_r*** 	E 	E??)-e&E&ET

&E&E&E(+E

%B%Bc$ii%B%B%B*?/C/CE E EE	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E
 6!q&>> 	/RVAF^^ 	/24SS5SSUSSSCCRG%g...6%1* 	J;s   ABBBc                    t          j        d          5  t          fdfdfdt                     dt          |            dt	                     dt	          |            d          }d d d            n# 1 swxY w Y   t          j                  rd	k    sd
k    rd}t          |          d
k    rt          | fi |ddiS t          | fi |ddiS )Nr  r  c                     | z  S r   r>   r4  s    r   r  z1ContinuousDistribution.__rpow__.<locals>.<lambda>h  s    UAX r   c                 V    t          j        |           t          j                  z  S r   r;   r#   r4  s    r   r  z1ContinuousDistribution.__rpow__.<locals>.<lambda>i  s    RVAYY%> r   c                 \    dt          j        | t          j                  z            z  S r8  )r;   r!   r#   r4  s    r   r  z1ContinuousDistribution.__rpow__.<locals>.<lambda>j  s#    a"&RVE]]1B*C*C&C r   z**(rl   )r<  r?  r@  r=  r>  r   rq   z~Raising an argument to the power of a random variable is only implemented when the argument is a positive scalar other than 1.rA  TF)r;   r  r^  r  r   rB  r*   rC  )r,   r6  rD  r]   s    `  r   __rpow__zContinuousDistribution.__rpow__f  s}   _r*** 	F 	F---->>>>CCCC)-e&F&Fd&F&F&F(+E

%C%Cs4yy%C%C%C	F F FE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F {5!! 	/UaZZ5A::G &g...1993DSSESSdSSSS3DTTETTeTTTTs   ABBBc                     | dz  S )NrN  r>   r5   s    r   rK  zContinuousDistribution.__neg__y  s    byr   c                      t          |           S r   FoldedDistributionr5   s    r   __abs__zContinuousDistribution.__abs__|  s    !$'''r   c                    | j         t          k    r|S t          j        || j                  d         }d| j        j         d}	 t          |                                          }n"# t          $ r}t          |          |d }~ww xY w|dk     s||k    rt          |          d| j        j         dt          |           d}|                                |vrt          |          |S )	Nr   r>   zArgument `order` of `z,.moment` must be a finite, positive integer.r   zArgument `kind` of `z.moment` must be one of r   )r  r  r;   rE   r  rJ   r7   r  r_   	Exceptionr  r   r  )r,   orderkindkindsr]   	order_intr\   s          r   _validate_order_kindz+ContinuousDistribution._validate_order_kind  s   
 !Y..L
5444R894>+B 9 9 9	-ejjll++II  	- 	- 	-W%%1,	- a<<9--W%%%3$.*A 3 3%(ZZ3 3 3::<<u$$W%%%s   !A' '
B1BBc                     t          j        |          }|j        | j        k    r|                    | j                  }|d         S rB   )r;   rE   r   r  r2  r+   s     r   _preserve_typez%ContinuousDistribution._preserve_type  s:    JqMM7dk!!%%Aur   c                 ^   t           j                            |          }t          | j                  dk    r
 |             S |=|                                 }|                    dt          d|dz
            d          }| j        |         }|                    |||d          } | di |S )	z Draw a specific (fully-defined) distribution from the family.

        See _Parameterization.draw for documentation details.
        r   Nrq   T)endpointr   )r   r   r>   )	r;   rx   ry   r   r  _num_parameterizationsintegersr   r1   )clsr   r   r  r   r0   r  rU   s           r   r  zContinuousDistribution._draw  s     i##C((s%&&!++355L%**,,A!$aQA!N!N12DE%**5#;2; + = =
s  Z   r   c                 *    t          | j                  S r   )r   r  )ro  s    r   rm  z-ContinuousDistribution._num_parameterizations  s     3)***r   r   c                 b    |                                  sdnt          | j        |                   S Nr   )rm  r   r  )ro  r  s     r   r  z&ContinuousDistribution._num_parameters  s7     3355 E/0BCDD	Fr   Fc                 x   | | j         di |n|\  }}t          j        ||          \  }}|j        s t          j        |j        | j                  S |g n|}|i n|}t          |||          \  }}t          j        | }t          | j	                  rd n| j	        }	t          ||||||	          }
|
j        S )Nr   r  r  )r  r#   rtolr>   )_supportr;   r   rs   emptyr   r  ra  r   r  	_tanhsinhintegral)r,   	integrandlimitsr  r  r#   rH   rI   r&  ru  r  s              r   _quadraturez"ContinuousDistribution._quadrature  s     +1.}t}&&v&&&f1"1a((1v 	88AG4;7777\rrt~6ytFCCC4"D)tx((6ttdh1ad$??? |r   )boundsr  c                   | | j         di |n|\  }}|i n|}t          j        |||          \  }}}|j        s t          j        |j        | j                  S fd}t          ||g|          \  }}	|j        }
t          j        ||          \  }}t          ||          \  }}|
                    |
          }|
                    |
          }|
                    |
          }|
                    |
          }t          ||||||	          }t          | j                  rd n| j        }t          ||j        |j        |	|          j        S )Nr   c                      | fi ||z
  S r   r>   )r   _pr  r&  s      r   rl  z1ContinuousDistribution._solve_bounded.<locals>.f2  s    1Q>>&>>B&&r   rt  xl0xr0ru  rv  r  )rH   rI   r  xrtolr>   )rv  r;   r   rs   rw  r   r  ra  ry  rw  r   r   r   r  r   xlxrr   )r,   r&  r  r}  r  ru  rv  rl  f3r  r   r  r  r  r  s    `             r   _solve_boundedz%ContinuousDistribution._solve_bounded  ss    17]T],,V,,,F
d~6+AtT::4v 	88AG4;7777	' 	' 	' 	' 	'  !V<<<D 
]4..
d!$--S||E""||E""kk%  kk%  BCSt$TRRR  ))7txR36SV$eLLLNNr   c                 b    t          | j        |d           }t          t          |d           }||uS r   )rA  rJ   r  )r,   r  r9  super_methods       r   
_overridesz!ContinuousDistribution._overrides  s4     d;;5{DII\))r   c                 B   | j         | j         S  | j        di | j        \  }}|j        | j        k    rt          j        || j                  }|j        | j        k    rt          j        || j                  }| j        rt          j        |          	                                t          j        |          	                                }}t
          j
        t
          j
        c|| j        <   || j        <   |d         |d         }}||f}| j        t          k    r|| _         |S rB   )r  rv  r  r   r  r;   r  r1  rE   rD   rw   r3  rB  rC  )r,   rH   rI   r	  s       r   r	  zContinuousDistribution.supportI  s   
 *&&t}00t/0017dk!!4;//A7dk!!4;//A 	 :a==%%''A););)=)=qA13.Adma.R5!B%qAa&	))")Dr   c                    | j         j                            |          \  }}t          |          rrt	          |                                          }|d         j        }|j        |k    rt          j        ||          n|}|j        |k    rt          j        ||          n|}| 	                    |          | 	                    |          fS rr  )
r
  r   r3   r   r   r   r   r;   r  rj  )r,   r  rH   rI   valsr   s         r   rv  zContinuousDistribution._supportc  s    ~$<<VDD1v;; 	E((DGME-.W-=-=5)))1A-.W-=-=5)))1A""1%%t':':1'='===r   r8  c                0     | j         dd|i| j        dz   S )Nr9  rP  r>   )_logentropy_dispatchr  r,   r9  s     r   
logentropyz!ContinuousDistribution.logentropyn  s(    (t(KKK$:JKKbPPr   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )N_logentropy_formula_entropy_formula)r  r  _logentropy_logexp_safe_logentropy_quadraturer,   r9  r  s      r   r  z+ContinuousDistribution._logentropy_dispatchr  sM    ??011 	1-FF__/00 	11FF0Fr   c                 *    t          | j                  r   r*   r0  r  s     r   r  z*ContinuousDistribution._logentropy_formula|      !$"7888r   c                 d    t          j         | j        di |dz             }t          |          S )NrP  r>   )r;   r#   _entropy_dispatchr}  )r,   r  r  s      r   _logentropy_logexpz)ContinuousDistribution._logentropy_logexp  s8    f+T+55f55b899$S)))r   c                     | j         di |}t          j        |j                  t          j                  rDfd|                                D             }t          j        |          } | j        di ||<   |d         S )Nc                 (    i | ]\  }}||         S r>   r>   rQ   r  r  masks      r   rS   zBContinuousDistribution._logentropy_logexp_safe.<locals>.<dictcomp>  s#    HHHXS#3s4yHHHr   r>   )r  r;   isinfr  r  r   rE   r  )r,   r  rv   params_maskr  s       @r   r  z.ContinuousDistribution._logentropy_logexp_safe  s    %d%////x!!6$<< 	CHHHHHHHK*S//C33BBkBBCI2wr   c                 |      fd}                      ||d          }t          |t          j        dz  z             S )Nc                 T     j         | fi |}|t          j        d|z             z   S NrP  )_logpdf_dispatchr;   r#   )r   r  r   r,   s      r   logintegrandzCContinuousDistribution._logentropy_quadrature.<locals>.logintegrand  s6    *T*17777FBF2f9----r   T)r  r#   rQ  )r|  r}  r;   r<   )r,   r  r  r  s   `   r   r  z-ContinuousDistribution._logentropy_quadrature  sP    	. 	. 	. 	. 	. |FEE$S258^444r   c                *     | j         dd|i| j        S Nr9  r>   )r  r  r  s     r   entropyzContinuousDistribution.entropy  s#    %t%HHVHt7GHHHr   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  )r  r  _entropy_logexp_entropy_quadraturer  s      r   r  z(ContinuousDistribution._entropy_dispatch  sM    ??-.. 	.*FF__233 	.)FF-Fr   c                 *    t          | j                  r   r  r  s     r   r  z'ContinuousDistribution._entropy_formula  r  r   c           	      d    t          j        t          j         | j        di |                    S rB   )r;   r  r"   r  r  s     r   r  z&ContinuousDistribution._entropy_logexp  s0    wrv7d7AA&AABBCCCr   c                 >      fd}                      ||           S )Nc                 F     j         | fi |} j        | fi |}||z  S r   )_pdf_dispatchr  )r   r  r   r   r,   s       r   rz  z=ContinuousDistribution._entropy_quadrature.<locals>.integrand  sA    $$$Q11&11C*T*17777FC<r   r  r|  )r,   r  rz  s   `  r   r  z*ContinuousDistribution._entropy_quadrature  s:    	  	  	  	  	    6 ::::r   c                *     | j         dd|i| j        S r  )_median_dispatchr  r  s     r   r  zContinuousDistribution.median  s#    $t$GGFGd6FGGGr   c                 N    |                      d          r| j        }n| j        }|S )N_median_formula)r  r  _median_icdfr  s      r   r  z'ContinuousDistribution._median_dispatch  s.    ??,-- 	')FF&Fr   c                 *    t          | j                  r   r  r  s     r   r  z&ContinuousDistribution._median_formula  r  r   c                      | j         di |S )Nru   )ru   _icdf_dispatchr  s     r   r  z#ContinuousDistribution._median_icdf  s    "t"11&111r   c                *     | j         dd|i| j        S r  )_mode_dispatchr  r  s     r   r  zContinuousDistribution.mode  s#    "t"EE&ED4DEEEr   c                 N    |                      d          r| j        }n| j        }|S )N_mode_formula)r  r  _mode_optimizationr  s      r   r  z%ContinuousDistribution._mode_dispatch  s/     ???++ 	-'FF,Fr   c                 *    t          | j                  r   r  r  s     r   r  z$ContinuousDistribution._mode_formula  r  r   c                      j         s t          j         j         j                  S   j        di |\  }}  j        di |}t           fdd|          \  }}t          |||||          }t          ||j
        |j        |j        |          }t          j        |j                  }	|j        dk    }
|
|j        |j        k    z  }|
|j        |j        k     z  }||         |	|<   ||         |	|<   |	d         S )Nr   c                 "     j         | fi | S r   r  )r   r  r,   s     r   r  z;ContinuousDistribution._mode_optimization.<locals>.<lambda>  s     4FD4Fq4S4SF4S4S3S r   r>   rt  )ru  rv  r  )r  rN  )r  r;   rw  r  r  rv  r  ra  r   r   r  xmr  rE   r   statusflfmfr)r,   r  rH   rI   mr&  r  res_br  r  mode_at_boundarymode_at_leftmode_at_rights   `            r   r  z)ContinuousDistribution._mode_optimization  s"   z 	<8DKt{;;;;t}&&v&&1!D!++F++SSSS$&v7 7 74 AAADAAA$Q%(EH4PPPz#%   <2-'58ux+?@(EHux,?@|_\.]Bxr   c                2    |                      dd|          S )Nrq   rawre  r9  momentr  s     r   r  zContinuousDistribution.mean  s    {{15{888r   c                2    |                      dd|          S )NrR  centralr  r  r  s     r   r  zContinuousDistribution.variance  s    {{19V{<<<r   c                R    t          j        |                     |                    S Nr8  )r;   sqrtr  r  s     r   r  z)ContinuousDistribution.standard_deviation  s     wt}}F}33444r   c                2    |                      dd|          S )Nr  standardizedr  r  r  s     r   r  zContinuousDistribution.skewness  s    {{1>&{AAAr   
non-excess)r9  
conventionc                    ddh}d| j         j         d| d}|                                }||vrt          |          |                     dd|          }|dk    r|d	z
  n|S )
Nr  excesszParameter `convention` of `z.kurtosis` must be one of r   r  r  r  r  )rJ   r7   r  r  r  )r,   r9  r  conventionsr]   ks         r   r  zContinuousDistribution.kurtosis  s    #X.41H 4 4%04 4 4%%''
[((W%%%KKvK>>"h..q1uuA5r   c               ,     | j         |fd|i| j        S Nr9  )r  r  r,   r   r9  s      r   r   zContinuousDistribution.logpdf	  %    $t$QJJvJ9IJJJr   c                v    |                      d          r| j        }nt          | j                  r| j        }|S )N_logpdf_formula)r  r  r   r  _logpdf_logexpr,   r   r9  r  s       r   r  z'ContinuousDistribution._logpdf_dispatch!	  s@    ??,-- 	))FFTX 	)(Fr   c                 *    t          | j                  r   r  r,   r   r  s      r   r  z&ContinuousDistribution._logpdf_formula)	  r  r   c                 B    t          j         | j        |fi |          S r   )r;   r#   r  r  s      r   r  z%ContinuousDistribution._logpdf_logexp,	  (    v(d(55f55666r   c               ,     | j         |fd|i| j        S r  )r  r  r  s      r   r   zContinuousDistribution.pdf/	  %    !t!!GGFGd6FGGGr   c                N    |                      d          r| j        }n| j        }|S )N_pdf_formula)r  r  _pdf_logexpr  s       r   r  z$ContinuousDistribution._pdf_dispatch3	  s-    ??>** 	&&FF%Fr   c                 *    t          | j                  r   r  r  s      r   r  z#ContinuousDistribution._pdf_formula;	  r  r   c                 B    t          j         | j        |fi |          S r   )r;   r"   r  r  s      r   r  z"ContinuousDistribution._pdf_logexp>	  (    v+d+A8888999r   c               d    ||                      ||          S |                     |||          S r  )r   rO  r,   r   rT  r9  s       r   logcdfzContinuousDistribution.logcdfC	  s5    9==6=222==Af=555r   c                     | j         ||fd|i| j        }t          j        |j        t          j                  s|dz   n|S )Nr9  rP  )_logcdf2_dispatchr  r;   r   r   complexfloating)r,   r   rT  r9  rv   s        r   rO  zContinuousDistribution._logcdf2I	  sK    $d$QMM&MD<LMM!#sy":L!M!MVbSVVr   c                   |                      d          r| j        }nk|                      d          s|                      d          r| j        }n9|                      d          s|                      d          r| j        }n| j        }|S )N_logcdf2_formula_logcdf_formula_logccdf_formula_cdf_formula_ccdf_formula)r  r   _logcdf2_subtraction_logcdf2_logexp_safe_logcdf2_quadraturer,   r   rT  r9  r  s        r   r  z(ContinuousDistribution._logcdf2_dispatchN	  s     ??-.. 		.*FFoo/00 	.!344	..FFoon-- 	.11	..FF-Fr   c                 *    t          | j                  r   r  r,   r   rT  r  s       r   r   z'ContinuousDistribution._logcdf2_formula^	  r  r   c                 J   ||k    }t          j        ||          t          j        ||          }} | j        |fi |} | j        |fi |} | j        |fi |} | j        |fi |}|dk     |dk     z  }	|dk     |dk     z  }
|	|
z   }t          ||          }t          ||          |
         ||
<   t          j        ||          |         }t          |          ||<   ||xx         t           j        dz  z  cc<   t          j	        |          r|d         n|j
        d         S )NrN  rQ  r>   )r;   minimummaximum_logcdf_dispatch_logccdf_dispatchrS  	logaddexprm  r<   r  r  )r,   r   rT  r  	flip_signlogcdf_xlogcdf_y	logccdf_x	logccdf_y	case_left
case_rightcase_centrallog_masslog_tails                 r   r  z+ContinuousDistribution._logcdf2_subtractiona	  s[   E	z!QAq!1!11(4(55f55(4(55f55*D*17777	*D*17777	]x"}5	"nR8
"Z/0 844,Y	BB:N<)44\B!*8!4!4rurz)!vi00Gx||hmB6GGr   c                      | j         ||fi |}t          j        ||k              r|dz   n|}t          j        |          S r  )_cdf2_dispatchr;   r  r#   )r,   r   rT  r  express        r   _logcdf2_logexpz&ContinuousDistribution._logcdf2_logexpr	  sJ    $$Q44V44 "q1u9"6vf~~r   c                 0    | j         ||fi |}t          j        |j                  t          j                  rRfd|                                D             }t          j        |          } | j        |         |         fi ||<   |d         S )Nc                 X    i | ]&\  }}|t          j        |j                           'S r>   r;   r  r   r  s      r   rS   z?ContinuousDistribution._logcdf2_logexp_safe.<locals>.<dictcomp>{	  B     ; ; ;'sC TZ @ @ F ; ; ;r   r>   )r  r;   r  r  r  r   rE   r  )r,   r   rT  r  rv   r  r  s         @r   r  z+ContinuousDistribution._logcdf2_logexp_safew	  s    "d"1a22622x!!6$<< 	R; ; ; ;+1<<>>; ; ;K*S//C004!D'QQ[QQCI2wr   c                 F    |                      | j        ||fd|          }|S )NT)r{  r#   r  )r|  r  )r,   r   rT  r  logress        r   r  z*ContinuousDistribution._logcdf2_quadrature	  s1    !!$"7A&*6 " ; ;r   c                ,     | j         |fd|i| j        S r  )r  r  r  s      r   r   zContinuousDistribution._logcdf1	  r  r   c                    |                      d          r| j        }nA|                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  r  )r  r  _logcdf_complement_logcdf_logexp_safe_logcdf_quadraturer  s       r   r  z'ContinuousDistribution._logcdf_dispatch	  sk    ??,-- 	-)FF__/00 	-,FF__^,, 	--FF,Fr   c                 *    t          | j                  r   r  r  s      r   r  z&ContinuousDistribution._logcdf_formula	  r  r   c                 8    t           | j        |fi |          S r   )rm  r  r  s      r   r'  z)ContinuousDistribution._logcdf_complement	  s&    //<<V<<===r   c                 B    t          j         | j        |fi |          S r   )r;   r#   _cdf_dispatchr  s      r   _logcdf_logexpz%ContinuousDistribution._logcdf_logexp	  r  r   c                     | j         |fi |}t          j        |          t          j                  rKfd|                                D             }t          j        |          } | j        |         fi ||<   |d         S )Nc                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   z>ContinuousDistribution._logcdf_logexp_safe.<locals>.<dictcomp>	  sB     ; ; ;'sC rsDJ??E ; ; ;r   r>   )r.  r;   r  r  r   rE   r)  r,   r   r  rv   r  r  s        @r   r(  z*ContinuousDistribution._logcdf_logexp_safe	  s    !d!!..v..x}}6$<< 	H; ; ; ;+1<<>>; ; ;K*S//C//$GG;GGCI2wr   c                 b     | j         di |\  }}|                     | j        ||f|d          S NT)r{  r  r#   r>   rv  r|  r  r,   r   r  rH   _s        r   r)  z)ContinuousDistribution._logcdf_quadrature	  J    t}&&v&&1 5q!f'-4   9 9 	9r   c               d    ||                      ||          S |                     |||          S r  )r   rK  r  s       r   cdfzContinuousDistribution.cdf	  s5    9::a:///::a6:222r   c                .     | j         ||fd|i| j        S r  )r  r  r  s       r   rK  zContinuousDistribution._cdf2	  s'    "t"1aKKK$:JKKKr   c                   |                      d          r| j        }nk|                      d          s|                      d          r| j        }n9|                      d          s|                      d          r| j        }n| j        }|S )N_cdf2_formular  r  r  r  )r  r<  _cdf2_logexp_cdf2_subtraction_safe_cdf2_quadraturer  s        r   r  z%ContinuousDistribution._cdf2_dispatch	  s     ???++ 	+'FFoo/00 	+!344	+&FF__^,, 	+0P0P 	+0FF*Fr   c                 *    t          | j                  r   r  r
  s       r   r<  z$ContinuousDistribution._cdf2_formula	  r  r   c           	      h    t          j        t          j         | j        ||fi |                    S r   )r;   r  r"   r  r
  s       r   r=  z#ContinuousDistribution._cdf2_logexp	  s4    wrv4d4QDDVDDEEFFFr   c                      | j         |fi |} | j         |fi |} | j        |fi |} | j        |fi |}|dk     |dk     z  }t          j        |||z
  ||z
            S Nru   )r-  _ccdf_dispatchr;   r   )	r,   r   rT  r  cdf_xcdf_yccdf_xccdf_yr   s	            r   _cdf2_subtractionz(ContinuousDistribution._cdf2_subtraction	  s     #"1////""1////$$Q11&11$$Q11&11c\fsl+x6&=%+666r   c                     | j         |fi |} | j         |fi |} | j        |fi |} | j        |fi |}|dk     |dk     z  }t          j        |||z
  ||z
            }	t          j        | j                  j        }
t          | j                  s| j        nt          j	        |
          }t          j
        ||          }t          j
        ||          }t          j        t          j        |||                    }t          j        ||	z            |k     t          j                  rRfd|                                D             }t          j        |	          }	 | j        |         |         g|R  |	<   |	d         S )Nru   c                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   zAContinuousDistribution._cdf2_subtraction_safe.<locals>.<dictcomp>	  r"  r   r>   )r-  rD  r;   r   rY  r  epsr   r  r  r  spacingr!   r  r   rE   r?  )r,   r   rT  r  rE  rF  rG  rH  r   rv   rL  r  cdf_maxccdf_maxrM  r  r  s                   @r   r>  z-ContinuousDistribution._cdf2_subtraction_safe	  s   ""1////""1////$$Q11&11$$Q11&11c\fsl+hq&-u55ht{##'%dh//AdhhRWS\\*UE**:ff--*RXa7;;<<vcCi  7*6$<< 	N; ; ; ;+1<<>>; ; ;K*S//C--agqwMMMMCI2wr   c                 @    |                      | j        ||f|          S )Nr{  r  )r|  r  r
  s       r   r?  z'ContinuousDistribution._cdf2_quadrature	  s$     2Aq6&QQQr   c                ,     | j         |fd|i| j        S r  )r-  r  r  s      r   r   zContinuousDistribution._cdf1	  r  r   c                    |                      d          r| j        }nA|                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  r  )r  r  _cdf_logexp_cdf_complement_safe_cdf_quadraturer  s       r   r-  z$ContinuousDistribution._cdf_dispatch	  sj    ??>** 	*&FF__.// 	*%FF___-- 	*.FF)Fr   c                 *    t          | j                  r   r  r  s      r   r  z#ContinuousDistribution._cdf_formula
  r  r   c                 B    t          j         | j        |fi |          S r   )r;   r"   r  r  s      r   rT  z"ContinuousDistribution._cdf_logexp
  r  r   c                 $    d | j         |fi |z
  S r8  )rD  r  s      r   _cdf_complementz&ContinuousDistribution._cdf_complement
  s#    &4&q33F3333r   c                     | j         |fi |}d|z
  }t          j        | j                  j        }t          | j                  s| j        nt          j        |          }||z  t          j        |          k     t          j	                  rKfd|
                                D             }t          j        |          } | j        |         g|R  |<   |d         S )Nrq   c                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   z?ContinuousDistribution._cdf_complement_safe.<locals>.<dictcomp>
  r"  r   r>   )rD  r;   rY  r  rL  r   r  r  rM  r  r   rE   rV  )	r,   r   r  r   rv   rL  r  r  r  s	           @r   rU  z+ContinuousDistribution._cdf_complement_safe

  s    "t"1////$hht{##'%dh//AdhhRWS\\Sy2:d+++6$<< 	D; ; ; ;+1<<>>; ; ;K*S//C,,QtWC{CCCCI2wr   c                 `     | j         di |\  }}|                     | j        ||f|          S NrQ  r>   rv  r|  r  r5  s        r   rV  z&ContinuousDistribution._cdf_quadrature
  H    t}&&v&&1 2Aq6'-   / / 	/r   c               d    ||                      ||          S |                     |||          S r  )r   	_logccdf2r  s       r   logccdfzContinuousDistribution.logccdf
  s5    9>>!F>333>>!Qv>666r   c                .     | j         ||fd|i| j        S r  )_logccdf2_dispatchr  r  s       r   rb  z ContinuousDistribution._logccdf2"
  s'    &t&q!OOFOd>NOOOr   c                N    |                      d          r| j        }n| j        }|S )N_logccdf2_formula)r  rg  _logccdf2_additionr  s        r   re  z)ContinuousDistribution._logccdf2_dispatch&
  s0     ??.// 	-+FF,Fr   c                 *    t          | j                  r   r  r
  s       r   rg  z(ContinuousDistribution._logccdf2_formula0
  r  r   c                 j     | j         |fi |} | j        |fi |}t          j        ||gd          S )Nr   r   )r  r  r   rp  )r,   r   rT  r  r  r  s         r   rh  z)ContinuousDistribution._logccdf2_addition3
  sQ    (4(55f55*D*17777	 (I!6Q????r   c                ,     | j         |fd|i| j        S r  )r  r  r  s      r   r   z ContinuousDistribution._logccdf18
  s%    %t%aKKK$:JKKKr   c                     |                      d          r| j        }nA|                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  r  )r  r  _logccdf_complement_logccdf_logexp_safe_logccdf_quadraturer  s       r   r  z(ContinuousDistribution._logccdf_dispatch<
  sk    ??-.. 	.*FF__.// 	.-FF___-- 	..FF-Fr   c                 *    t          | j                  r   r  r  s      r   r  z'ContinuousDistribution._logccdf_formulaH
  r  r   c                 8    t           | j        |fi |          S r   )rm  r  r  s      r   rm  z*ContinuousDistribution._logccdf_complementK
  s&    ..q;;F;;<<<r   c                 B    t          j         | j        |fi |          S r   )r;   r#   rD  r  s      r   _logccdf_logexpz&ContinuousDistribution._logccdf_logexpN
  s(    v)d)!66v66777r   c                     | j         |fi |}t          j        |          t          j                  rKfd|                                D             }t          j        |          } | j        |         fi ||<   |d         S )Nc                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   z?ContinuousDistribution._logccdf_logexp_safe.<locals>.<dictcomp>U
  r"  r   r>   )rs  r;   r  r  r   rE   ro  r1  s        @r   rn  z+ContinuousDistribution._logccdf_logexp_safeQ
  s    "d"1////x}}6$<< 	I; ; ; ;+1<<>>; ; ;K*S//C004HHKHHCI2wr   c                 b     | j         di |\  }}|                     | j        ||f|d          S r3  r4  r,   r   r  r6  rI   s        r   ro  z*ContinuousDistribution._logccdf_quadrature[
  r7  r   c               d    ||                      ||          S |                     |||          S r  )r   rL  r  s       r   r   zContinuousDistribution.ccdf`
  s5    9;;q;000;;q!F;333r   c                .     | j         ||fd|i| j        S r  )_ccdf2_dispatchr  r  s       r   rL  zContinuousDistribution._ccdf2f
  s'    #t#AqLLL4;KLLLr   c                N    |                      d          r| j        }n| j        }|S )N_ccdf2_formula)r  r|  _ccdf2_additionr  s        r   rz  z&ContinuousDistribution._ccdf2_dispatchj
  s.    ??+,, 	*(FF)Fr   c                 *    t          | j                  r   r  r
  s       r   r|  z%ContinuousDistribution._ccdf2_formular
  r  r   c                 D     | j         |fi |} | j        |fi |}||z   S r   )r-  rD  )r,   r   rT  r  rE  rH  s         r   r}  z&ContinuousDistribution._ccdf2_additionu
  s@    ""1////$$Q11&11v~r   c                ,     | j         |fd|i| j        S r  )rD  r  r  s      r   r   zContinuousDistribution._ccdf1{
  %    "t"1HHVHt7GHHHr   c                     |                      d          r| j        }nA|                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  r  )r  r  _ccdf_logexp_ccdf_complement_safe_ccdf_quadraturer  s       r   rD  z%ContinuousDistribution._ccdf_dispatch
  sj    ???++ 	+'FF__/00 	+&FF__^,, 	+/FF*Fr   c                 *    t          | j                  r   r  r  s      r   r  z$ContinuousDistribution._ccdf_formula
  r  r   c                 B    t          j         | j        |fi |          S r   )r;   r"   r  r  s      r   r  z#ContinuousDistribution._ccdf_logexp
  s(    v,d,Q99&99:::r   c                 $    d | j         |fi |z
  S r8  )r-  r  s      r   _ccdf_complementz'ContinuousDistribution._ccdf_complement
  s#    %4%a2262222r   c                     | j         |fi |}d|z
  }t          j        | j                  j        }t          | j                  s| j        nt          j        |          }||z  t          j        |          k     t          j	                  rKfd|
                                D             }t          j        |          } | j        |         fi ||<   |d         S )Nrq   c                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   z@ContinuousDistribution._ccdf_complement_safe.<locals>.<dictcomp>
  r"  r   r>   )r-  r;   rY  r  rL  r   r  r  rM  r  r   rE   r  )	r,   r   r  r9  rv   rL  r  r  r  s	           @r   r  z,ContinuousDistribution._ccdf_complement_safe
  s     d --f--#ght{##'%dh//AdhhRWS\\Sy2:c??*6$<< 	F; ; ; ;+1<<>>; ; ;K*S//C--agEEEECI2wr   c                 `     | j         di |\  }}|                     | j        ||f|          S r^  r_  rw  s        r   r  z'ContinuousDistribution._ccdf_quadrature
  r`  r   c               ,     | j         |fd|i| j        S r  )_ilogcdf_dispatchr  r,   logpr9  s      r   r   zContinuousDistribution.ilogcdf
  s%    %t%dNN6NT=MNNNr   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )N_ilogcdf_formula_ilogccdf_formula)r  r  _ilogcdf_complement_ilogcdf_inversionr  s       r   r  z(ContinuousDistribution._ilogcdf_dispatch
  sM    ??-.. 	-*FF__011 	--FF,Fr   c                 *    t          | j                  r   r  r  s      r   r  z'ContinuousDistribution._ilogcdf_formula
  r  r   c                 8     | j         t          |          fi |S r   )_ilogccdf_dispatchrm  r  s      r   r  z*ContinuousDistribution._ilogcdf_complement
  s#    &t&y||>>v>>>r   c                 <    |                      | j        ||          S Nr  )r  r  r  s      r   r  z)ContinuousDistribution._ilogcdf_inversion
  s     ""4#8!F"KKKr   c               ,     | j         |fd|i| j        S r  )r  r  r,   r  r9  s      r   r   zContinuousDistribution.icdf
  r  r   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )N_icdf_formula_iccdf_formula)r  r  _icdf_complement_safe_icdf_inversionr  s       r   r  z%ContinuousDistribution._icdf_dispatch
  sL    ???++ 	*'FF__-.. 	*/FF)Fr   c                 *    t          | j                  r   r  r  s      r   r  z$ContinuousDistribution._icdf_formula
  r  r   c                 $     | j         d|z
  fi |S r8  )_iccdf_dispatchr  s      r   _icdf_complementz'ContinuousDistribution._icdf_complement
  s!    #t#AE44V444r   c                     | j         |fi |}t          j        | j                  j        }t          | j                  s| j        nt          j        |          }||z  t          j        d|z
            k     t          j	                  rKfd|
                                D             }t          j        |          } | j        |         g|R  |<   |d         S )Nrq   c                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   z@ContinuousDistribution._icdf_complement_safe.<locals>.<dictcomp>
  r"  r   r>   )r  r;   rY  r  rL  r   r  r  rM  r  r   rE   r  r,   r   r  rv   rL  r  r  r  s          @r   r  z,ContinuousDistribution._icdf_complement_safe
  s    #d#A0000ht{##'%dh//AdhhRWS\\QwAE***6$<< 	D; ; ; ;+1<<>>; ; ;K*S//C,,QtWC{CCCCI2wr   c                 <    |                      | j        ||          S r  )r  r-  r  s      r   r  z&ContinuousDistribution._icdf_inversion
  s     ""4#5q"HHHr   c               ,     | j         |fd|i| j        S r  )r  r  r  s      r   r   zContinuousDistribution.ilogccdf
  s%    &t&tOOFOd>NOOOr   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  )r  r  _ilogccdf_complement_ilogccdf_inversionr  s       r   r  z)ContinuousDistribution._ilogccdf_dispatch
  sM    ??.// 	.+FF__/00 	..FF-Fr   c                 *    t          | j                  r   r  r  s      r   r  z(ContinuousDistribution._ilogccdf_formula
  r  r   c                 8     | j         t          |          fi |S r   )r  rm  r  s      r   r  z+ContinuousDistribution._ilogccdf_complement
  s#    %t%ill==f===r   c                 <    |                      | j        ||          S r  )r  r  r  s      r   r  z*ContinuousDistribution._ilogccdf_inversion
  s     ""4#91V"LLLr   c               ,     | j         |fd|i| j        S r  )r  r  r  s      r   r   zContinuousDistribution.iccdf
  s%    #t#AIIfI8HIIIr   c                     |                      d          r| j        }n$|                      d          r| j        }n| j        }|S )Nr  r  )r  r  _iccdf_complement_safe_iccdf_inversionr  s       r   r  z&ContinuousDistribution._iccdf_dispatch
  sL    ??+,, 	+(FF___-- 	+0FF*Fr   c                 *    t          | j                  r   r  r  s      r   r  z%ContinuousDistribution._iccdf_formula  r  r   c                 $     | j         d|z
  fi |S r8  r  r  s      r   _iccdf_complementz(ContinuousDistribution._iccdf_complement
  s!    "t"1q533F333r   c                     | j         |fi |}t          j        | j                  j        }t          | j                  s| j        nt          j        |          }||z  t          j        d|z
            k     t          j	                  rKfd|
                                D             }t          j        |          } | j        |         g|R  |<   |d         S )Nrq   c                 X    i | ]&\  }}|t          j        |j                           'S r>   r!  r  s      r   rS   zAContinuousDistribution._iccdf_complement_safe.<locals>.<dictcomp>  r"  r   r>   )r  r;   rY  r  rL  r   r  r  rM  r  r   rE   r  r  s          @r   r  z-ContinuousDistribution._iccdf_complement_safe  s    $d$Q11&11ht{##'%dh//AdhhRWS\\QwAE***6$<< 	E; ; ; ;+1<<>>; ; ;K*S//C--agDDDDCI2wr   c                 <    |                      | j        ||          S r  )r  rD  r  s      r   r  z'ContinuousDistribution._iccdf_inversion  s     ""4#6&"IIIr   r>   r9  r   c                >   t          j        |          s|fnt          |          }|| j        z   }t	          |t
          j                  st           j                            |          n|} | j	        ||f||d| j
        }|                    | j        d          S )Nr  Fr   )r;   r   r   r  r   r   	QMCEnginerx   ry   r=  r  r2  r  )r,   r   r9  r   sample_shape
full_shaper  s          r   r  zContinuousDistribution.sample:  s     (*{5'9'9KxxuU||!DK/
0:30N0NWbi##C(((TW#d#L* AV(+A A/3/?A A zz$+Ez222r   c                    |                      d          r"t          |t          j                  s| j        }n| j        }|S )N_sample_formula)r  r   r   r  r  _sample_inverse_transform)r,   r  r  r9  r   r  s         r   r=  z'ContinuousDistribution._sample_dispatchE  sC     ??,-- 	4jcm6T6T 	4)FF3Fr   c                *    t          | j                  r   r  )r,   r  r  r   r  s        r   r  z&ContinuousDistribution._sample_formulaN  r  r   c                    t          |t          j                  r | j        ||fd|i|}n|                    || j                  } | j        |fi |S )Nqrng)rs   r   )r   r   r  _qmc_uniformrx   r  r  )r,   r  r  r   r  r{   s         r   r  z0ContinuousDistribution._sample_inverse_transformQ  sl    c3=)) 	E'd'jUUsUfUUGGjjjjDDG"t"755f555r   c                l   |r|d         nd}t          j        |r
|dd          n|          }t          |j        |          }|j        }t          d|j        |j                  }	t          |t          j
                  r
|j        |	d<   g }
|D ]U} |dd|i|	}|                    |          }|                    |r|nd          d         }|
                    |           V|
r(t          j        t          j        |
          dd          nt          j        g           }|                    |          S )Nr   rq   )dscrambleoptimizationbitsseedr>   rN  )mathr   r   r   rJ   r^  r  _optimizationr   r   Sobolr  rx   r   r  r;   r   stackrE   )r,   r  r  r  r  n_low_discrepancyn_independentrngs
qrng_classr  uniformsr   r{   s                r   r  z#ContinuousDistribution._qmc_uniformX  sG   
 0<BLOO	L"P*QRR..jQQ
 $(M22^
DM@RSSSdCI&& 	'!YF6N  	% 	%C:1131&11Dkk"344Goo<&O&7&7RPPQSTGOOG$$$$ =EX"+bhx00"a888"*UW..z***r   c                 
    h dS )N>   cacheformulageneral	normalize	transform
quadraturer>   r5   s    r   _moment_methodsz&ContinuousDistribution._moment_methods  s    6 6 6 	6r   c                 6    |                                  d         S rr  
_constantsr5   s    r   _zerozContinuousDistribution._zero        ##r   c                 6    |                                  d         S r8  r  r5   s    r   _onezContinuousDistribution._one  r  r   c                 ~    | j         | j         S |                     ddg          }| j        t          k    r|| _         |S Nr   rq   )r  rj  rB  rC  )r,   	constantss     r   r  z!ContinuousDistribution._constants  sG    +''''A//		))#,D r   r  c                    | j         | j        | j        d}|                     |||          }||         } |||          S )Nr  r  r  r8  )_moment_raw_moment_central_moment_standardizedrh  r,   rd  re  r9  rf  moment_kinds         r   r  zContinuousDistribution.moment  sW    ( 0!%!:< < ))%u==Dk{50000r   c                D    || j         n|h} | j        |fd|i| j        S )z)Raw distribution moment about the origin.Nmethods)r  _moment_raw_dispatchr  r,   rd  r9  r  s       r   r  z"ContinuousDistribution._moment_raw  s:    
 +1.$&&vh(t(TTT4CSTTTr   c                p   d }d|v r| j                             |d           }|d|v r | j        |fi |}|d|v r|dk    r | j        |fi |}|d|v r | j        |fi |}|d|v r | j        |fd| j        i|}|d|v r | j        |fd| j        i|}|| j        t          k    r
|| j         |<   |S )	Nr  r  r  rq   r  r  centerquadrature_icdf)
r  rY   _moment_raw_formula_moment_raw_transform_moment_raw_general_moment_integrate_pdfr  _moment_integrate_icdfrB  rC  r,   rd  r  r  r  s        r   r  z+ContinuousDistribution._moment_raw_dispatch  s3   g+//t<<F>i722-T-e>>v>>F>kW44/T/@@@@F>i722-T-e>>v>>F>lg55/T/SSdjSFSSF>/7::0T0TTtzTVTTF$"3y"@"@,2D"5)r   c                     d S r   r>   r,   rd  r  s      r   r  z*ContinuousDistribution._moment_raw_formula      tr   c                    g }t          t          |          dz             D ]0}h d} | j        d||d|}| d S |                    |           1h d} | j        | j        fd|i|}|d S |                     |||| j                  }	|	S )Nrq   >   r  r  r  r  rd  r  >   r  r  r  r  r>   )r   r   _moment_central_dispatchr  r  r  _moment_transform_centerr  )
r,   rd  r  central_momentsr   r  moment_imean_methodsr  r  s
             r   r  z,ContinuousDistribution._moment_raw_transform  s    s5zzA~&& 	- 	-ABBBG4t4X1gXXQWXXHtt""8,,,, :99(t(SSLSFSS<4..uotTZXXr   c                      |dk    r| j         nd S rr  )r  r  s      r   r  z*ContinuousDistribution._moment_raw_general  s     "QJJtyyD0r   c                D    || j         n|h} | j        |fd|i| j        S )z#Distribution moment about the mean.Nr  )r  r  r  r  s       r   r  z&ContinuousDistribution._moment_central  s8    *0.$&&vh,t,UXXGXtGWXXXr   c                   d }d|v r| j                             |d           }|d|v r | j        |fi |}|d|v r | j        |fi |}|d|v r|dk    r | j        |fi |}|d|v r | j        |fi |}|0d|v r, | j        | j        fi |d| j        i} | j	        |fd	|i|}|0d
|v r, | j        | j        fi |d| j        i} | j
        |fd	|i|}|| j        t          k    r
|| j         |<   |S )Nr  r  r  r  rR  r  r  r  r  r  )r  rY   _moment_central_formula_moment_central_transform_moment_central_normalize_moment_central_generalr  r  r  r  r  rB  rC  )r,   rd  r  r  r  r  s         r   r  z/ContinuousDistribution._moment_central_dispatch  s   g/33E4@@F>i7221T1%BB6BBF>kW443T3EDDVDDF>kW443T3EDDVDDF>i7221T1%BB6BBF>lg55,4,TY K K& K K595IK K KD/T/MMdMfMMF>/7::,4,TY K K& K K595IK K KD0T0NNtNvNNF$"3y"@"@06D&u-r   c                     d S r   r>   r  s      r   r	  z.ContinuousDistribution._moment_central_formula  r  r   c                    g }t          t          |          dz             D ]0}h d} | j        d||d|}| d S |                    |           1| j        } | j        | j        fd|i|}|                     ||| j        |          }	|	S )Nrq   >   r  r  r  r   r  r>   )r   r   r  r  r  r  r  r  )
r,   rd  r  raw_momentsr   r  r  r  r  r  s
             r   r
  z0ContinuousDistribution._moment_central_transform  s    s5zzA~&& 	) 	)A555G0t0Tq'TTVTTHttx((((+(t(SSLSFSS..uk4:tTTr   c                 t    h d} | j         |fi |d|i}|d S  | j        dd| j        i|}|||dz  z  z  S )N>   r  r  r  r  rR  r  )_moment_standardized_dispatchr  r  )r,   rd  r  r  standard_momentvars         r   r  z0ContinuousDistribution._moment_central_normalize/  s    111<$<U N Nf N NELN N N"4+d+VVt7KVvVVsU1W~--r   c                 L    | j         | j        d}|                    |d           S )Nr   r  r  rY   )r,   rd  r  general_central_momentss       r   r  z.ContinuousDistribution._moment_central_general8  s*    &*iDJ"?"?&**5$777r   c                D    || j         n|h} | j        |fd|i| j        S )z!Standardized distribution moment.Nr  )r  r  r  r  s       r   r  z+ContinuousDistribution._moment_standardized<  sK    *0.$&&vh1t1% F F F484DF F 	Fr   c                $   d }d|v r| j                             |d           }|d|v r | j        |fi |}|d|v r | j        |dfi |}|d|v r | j        |fi |}|d|v r | j        |dfi |}|| j        t          k    r
|| j         |<   |S )Nr  r  r  Fr  T)r  rY   _moment_standardized_formula_moment_standardized_normalize_moment_standardized_generalrB  rC  r  s        r   r  z4ContinuousDistribution._moment_standardized_dispatchB  s    g488EEF>i7226T6uGGGGF>kW448T8PPPPF>i7226T6uGGGGF>kW448T8OOOOF$"3y"@"@5;D+E2r   c                     d S r   r>   r  s      r   r  z3ContinuousDistribution._moment_standardized_formulaY  r  r   c                 ~    |rdhnh d} | j         |fi |d|i}|d S  | j         dd| j        i|}|||dz  z  z  S )Nr  >   r  r  r  r  rR  r  )r  r  )r,   rd  use_quadraturer  r  central_momentr  s          r   r  z5ContinuousDistribution._moment_standardized_normalize\  s    %3 :L>>999 	66u H H H H?FH H H!4+d+ 6 6t7K 6.46 6cE!Gn,,r   c                 X    | j         | j        | j         d}|                    |d           S )N)r   rq   rR  r  )r,   rd  r  general_standard_momentss       r   r  z3ContinuousDistribution._moment_standardized_generalg  s.    '+yTZDI#N#N '++E4888r   c                 B      fd}                      |||f|          S )Nc                 6     j         | fi |}|| |z
  |z  z  S r   r  )r   rd  r  r  r   r,   s        r   rz  z?ContinuousDistribution._moment_integrate_pdf.<locals>.integrandl  s1    $$$Q11&11C&5(((r   )r  r  r  r,   rd  r  r  rz  s   `    r   r  z,ContinuousDistribution._moment_integrate_pdfk  s=    	) 	) 	) 	) 	) 	OOOr   c                 D      fd}                      |d||f|          S )Nc                 0     j         | fi |} | |z
  |z  S r   r  )r   rd  r  r  r,   s       r   rz  z@ContinuousDistribution._moment_integrate_icdf.<locals>.integrandr  s,    ##A0000AfHu$$r   )r  r  )r{  r  r  r  r$  s   `    r   r  z-ContinuousDistribution._moment_integrate_icdfq  sK    	% 	% 	% 	% 	% 	(&+V_V   E E 	Er   c                    t          j        ||g|R  ^}}}|}t          j        |dz                                 dgdg|j        z  z             }|                     |          }t          j        ||          }t          j        d          5  t          j	        ||z  ||z
  ||z
  z  z  d          }d d d            n# 1 swxY w Y   |S )Nrq   rN  rg  )r  r   r   )
r;   r   aranger   r  rj  r   binomrj  r   )	r,   rd  	moment_asrH   rI   r0   r   
n_choose_imoment_bs	            r   r  z/ContinuousDistribution._moment_transform_centerx  s!   .q!@i@@@1yIacNN""B4AF
?33""]1a((
[*** 	I 	Ivj2AaC1Q3<?aHHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   &CCC)	logcenterr  c                    ||du r% | j         | j        t          j         fi | j        }nd }||n|} | j         ||fi | j        }|r | j         d|fi | j        }|||dz  z  z
  }|S )NTrR  )_logmoment_quadr  r;   r   r  )r,   rd  r-  r  logmeanr  logvars          r   
_logmomentz!ContinuousDistribution._logmoment  s     4 4*d*49rvgRRAQRRGGG(0GGi	"d"5)HHt7GHH 	+)T)!WII8HIIF%'**C
r   c                 D      fd}                      |||f|d          S )Nc                 v     j         | fi |}||t          t          j        | dz             |          z  z   S r  )r  rS  r;   r#   )r   rd  r-  r  r   r,   s        r   r  z<ContinuousDistribution._logmoment_quad.<locals>.logintegrand  sC    *T*17777FEM"&R..)$L$LLLLr   T)r  r  r#   r  )r,   rd  r-  r  r  s   `    r   r/  z&ContinuousDistribution._logmoment_quad  sN    	M 	M 	M 	M 	M E93E'-4   9 9 	9r   r   r   )r9  gMb@?gCl?)taxc          	          h d}h d}|                     |          }|                     ddh          } j        }	||}}
|d         t          j        |dd                   }}|	r|ddt          j        f         n|}d j        j         d	| }||vrt          |          d
 j        j         d	| }|
|vrt          |          d j        j         d	| }||vrt          |          d j        j         d} j        rt          |          d}|	dk    rt          |          	 ddl	m
} n2# t          $ r%}d j        j         d}t          |          |d}~ww xY w||                                n|}||v r|n t           d|z             |          }d j        j         dd|z    d| d| d	}t          j        t          j        |                    st          |          t          j        ddd          }|	r|ddt          j        f         n|}|d         |d         |d         z
  |z  z   }|
|v r|n t           |
          |          }||v r|n t           |          |          }|                    ||           |                    d|
 d           |                    d| d           |                    t+                                t-           j                  rg } j        j        t5                    } fd|D             }t7          | D ]F}fdt7          ||          D             }|                    d                    |                     G|                    |           |S )a1	  Plot a function of the distribution.

        Convenience function for quick visualization of the distribution
        underlying the random variable.

        Parameters
        ----------
        x, y : str, optional
            String indicating the quantities to be used as the abscissa and
            ordinate (horizontal and vertical coordinates), respectively.
            Defaults are ``'x'`` (the domain of the random variable) and
            ``'pdf'`` (the probability density function). Valid values are:
            'x', 'pdf', 'cdf', 'ccdf', 'icdf', 'iccdf', 'logpdf', 'logcdf',
            'logccdf', 'ilogcdf', 'ilogccdf'.
        t : 3-tuple of (str, float, float), optional
            Tuple indicating the limits within which the quantities are plotted.
            Default is ``('cdf', 0.001, 0.999)`` indicating that the central
            99.9% of the distribution is to be shown. Valid values are:
            'x', 'cdf', 'ccdf', 'icdf', 'iccdf', 'logcdf', 'logccdf',
            'ilogcdf', 'ilogccdf'.
        ax : `matplotlib.axes`, optional
            Axes on which to generate the plot. If not provided, use the
            current axes.

        Returns
        -------
        ax : `matplotlib.axes`
            Axes on which the plot was generated.
            The plot can be customized by manipulating this object.

        Examples
        --------
        Instantiate a distribution with the desired parameters:

        >>> import numpy as np
        >>> import matplotlib.pyplot as plt
        >>> from scipy import stats
        >>> X = stats.Normal(mu=1., sigma=2.)

        Plot the PDF over the central 99.9% of the distribution.
        Compare against a histogram of a random sample.

        >>> ax = X.plot()
        >>> sample = X.sample(10000)
        >>> ax.hist(sample, density=True, bins=50, alpha=0.5)
        >>> plt.show()

        Plot ``logpdf(x)`` as a function of ``x`` in the left tail,
        where the log of the CDF is between -10 and ``np.log(0.5)``.

        >>> X.plot('x', 'logpdf', t=('logcdf', -10, np.log(0.5)))
        >>> plt.show()

        Plot the PDF of the normal distribution as a function of the
        CDF for various values of the scale parameter.

        >>> X = stats.Normal(mu=0., sigma=[0.5, 1., 2])
        >>> X.plot('cdf', 'pdf')
        >>> plt.show()

        >   r   r   r   r   r   >   r9  r   r  rc  r   r   r   rq   NzArgument `t` of `z.plot` "must be one of zArgument `x` of `zArgument `y` of `r   z.plot` was called on a random variable with at least one invalid shape parameters. When a parameter is invalid, no plot can be shown.z^To use `plot`, distribution parameters must be scalars or arrays with one or fewer dimensions.z'`matplotlib` must be installed to use `z.plot`.r   z/.plot` received invalid input for `t`: calling rj   z) produced r   i,  $c                 N    g | ]!}t          j        j        |                   "S r>   )r;   ry  r  )rQ   pnamer,   s     r   r   z/ContinuousDistribution.plot.<locals>.<listcomp>9  s;     6 6 6 % M$*:5*ABB 6 6 6r   c                 >    g | ]\  }}d |         j          d|dS )r8  z$ = z.4g)rO   )rQ   rN   r  rU   s      r   r   z/ContinuousDistribution.plot.<locals>.<listcomp><  sO     N N N#,4  J:d#3#:IIIII N N Nr   rm   )unionr  r;   rE   newaxisrJ   r7   r  r1  matplotlib.pyplotpyplotModuleNotFoundErrorgcarA  rU  rt  linspaceplot
set_xlabel
set_ylabel	set_titler   r   r  r  rU   r   r   r  r   legend)r,   r   rT  r5  r6  t_is_quantilet_is_probabilityvalid_tvalid_xyr  x_namey_namet_nametlimr]   pltexcqlimgridqlabelparam_namesparam_arrays
param_valsassignmentsrU   s   `                        @r   rC  zContinuousDistribution.plot  s   X FEE???%%&677MM5("344zAtRZ!""..&*4tAAArzM""/t~'> / /%,/ /  W%%%0t~'> 0 0%-0 0!!W%%%0t~'> 0 0%-0 0!!W%%%Ct~. C C C  	&W%%%E!88W%%%	8+++++++" 	8 	8 	8<>2< < <G%g..C7	8 *SWWYYY" ..tt4MGD#f*4M4Md4S4SDt~. D D!&jD D+/D D<@D D Dvbk$''(( 	&W%%% {1a%%&*4tAAArzM""GtAwa(D00 =((AA.CgdF.C.CA.F.F=((AA.CgdF.C.CA.F.F 	1
m&mmm$$$
m&mmm$$$
SYY t   
	E/:Jz**K6 6 6 6)46 6 6L!<0 5 5
N N N N03K0L0LN N NTYY{334444IIe	s   -D4 4
E#> EE#)NNNN)r   )NNNFr   r>   rq   r  r   )r   r   )r7   r8   r9   r:   __array_priority__r  _nullrK   r  r  r  r  r  r  r  r  r  r  setterrB  r  r  r6   r(  r*  r/  r1  rE  rH  rL  rN  rV  r\  rK  ra  rh  rj  classmethodr  rm  r  r|  r  r  r	  rv  r6  r  rH  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r-  r   r  r  r  r   r  r  r  r  rV  rO  r  r   r  r  r  r  r   r  r  r'  r.  r(  r)  r9  rK  r  r<  r=  rI  r>  r?  r   r-  r  rT  rZ  rU  rV  rc  rb  re  rg  rh  r   r  r  rm  rs  rn  ro  r   rL  rz  r|  r}  r   rD  r  r  r  r  r  r   r  r  r  r  r   r  r  r  r  r  r   r  r  r  r  r   r  r  r  r  r  r  r=  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  r  r  r  r  r  r  r2  r/  rC  r>   r   r   r  r    sh       T Tj  $t$ . . . . .& 7; JY JY JY JY JYX$ $ $*     >!( !( !(F7 7 7&  5 5 5K K K   X 	Z  Z " " X" * * * 	' 	' X	' 4 4 42 2 2"2 2 28 8 89 9 9< < <> > >M M M(# # #- - -# # #  $U U U&  ( ( (  8   <@! ! ! [!$ + + [+ F F F [F 8<%*   ( .2$ O O O O OF* * *T  4	> 	> 	> #' Q Q Q Q Q    Y9 9 9* * *  5 5 5  $ I I I I I    Y9 9 9D D D; ; ; # H H H H H    Y9 9 92 2 2 ! F F F F F    Y9 9 9  & " 9 9 9 9 9 "& = = = = = ,0 5 5 5 5 5 "& B B B B B "&, 6 6 6 6 6Z %) K K K K K ,0     Y9 9 97 7 7 "& H H H H H )-     Y9 9 9: : :
6T 6 6 6 6 6 W W W 04     Y9 9 9H H H"  
    
 $( K K K K K ,0 	 	 	 	 Y	9 9 9> > >7 7 7  9 9 9
3$ 3 3 3 3 3 L L L -1     Y9 9 9G G G	7 	7 	7  .R R R H H H )- 	 	 	 	 Y	9 9 9: : :4 4 4  / / /
7d 7 7 7 7 7 P P P 15     Y9 9 9@ @ @
 %) L L L L L 	 	 	 Y	9 9 9= = =8 8 8  9 9 9
44 4 4 4 4 4 M M M .2     Y9 9 9   I I I 	 	 	 Y	9 9 9; ; ;3 3 3  / / / )- O O O O O    Y9 9 9? ? ?L L L #' I I I I I    Y9 9 95 5 5
 
 
I I I *. P P P P P    Y9 9 9> > >M M M $( J J J J J    Y9 9 94 4 4
 
 
J J JB	34 	3 	3 	3 	3 	3   Y9 9 96 6 6+ + +@ 6 6 _6 $ $ X$ $ $ X$	 	 	 1D 1 1 1 1 1UT U U U U U  4    &1 1 1
Y Y Y Y Y Y
  B     . . .8 8 8Fd F F F F F  .  	- 	- 	-9 9 9P P PE E E  t%     9 9 9$_(?D _ _ _ _ _ _ _r   r  argusARGUS	betaprime	BetaPrimechi2
ChiSquaredcrystalballCrystalBalldgammaDoubleGammadweibullDoubleWeibullexponExponential	exponnormExponentiallyModifiedNormal	exponweibExponentialWeibullexponpowExponentialPowerfatiguelifeFatigueLife
foldcauchyFoldedCauchyfoldnormFoldedNormalgenlogisticGeneralizedLogisticgennormGeneralizedNormal	genparetoGeneralizedParetogenexponGeneralizedExponential
genextremeGeneralizedExtremeValue
gausshyperGaussHypergeometricgengammaGeneralizedGammagenhalflogisticGeneralizedHalfLogisticgeninvgaussGeneralizedInverseGaussiangumbel_rGumbelgumbel_lReflectedGumbel
halfcauchy
HalfCauchyhalflogisticHalfLogistichalfnorm
HalfNormalhalfgennormHalfGeneralizedNormal	hypsecantHyperbolicSecantinvgammaInverseGammmainvgaussInverseGaussian
invweibullInverseWeibull	irwinhall	IrwinHall	jf_skew_tJonesFaddySkewT	johnsonsb	JohnsonSB	johnsonsu	JohnsonSUksone
KSOneSidedkstwo
KSTwoSided	kstwobignKSTwoSidedAsymptoticlaplace_asymmetricLaplaceAsymmetriclevy_lLevyLeftlevy_stable
LevyStableloggammaExpGamma
loglaplace
LogLaplacelognorm	LogNormal
loguniform
LogUniformncx2NoncentralChiSquarednctNoncentralTnormNormalnorminvgaussNormalInverseGaussianpowerlawPowerLaw	powernormPowerNormalrdistRrel_breitwignerRelativisticBreitWignerrecipinvgaussReciprocalInverseGaussian
reciprocalsemicircularSemiCircular
skewcauchy
SkewCauchyskewnorm
SkewNormalstudentized_rangeStudentizedRanger5  StudentT	trapezoidTrapezoidaltriang
Triangular
truncexponTruncatedExponential	truncnormTruncatedNormaltruncparetoTruncatedParetotruncweibull_minTruncatedWeibulltukeylambdaTukeyLambdaVonMisesLineWeibullReflectedWeibullWrappedCauchyLine)vonmises_lineweibull_minweibull_max
wrapcauchyc                      t           j        t           j        hv rt          d j         d          t           t           j                  sd}t          |          g g }t           d j	         j
        f          }                                 D ]b}t          |j        |j                  }t          |j        |          }                    |           |                    |j                   ct          |d          }t          d|d	
          t"                               j         j                                                   G fddt(                    } fd}	d1dd fd}
 fd} fd} fd} fd}ddddddddddd 
}d!d"d#hi}|                                D ]r\  }} j        |v r|| j                 v rt           j        |d          }t          t           j        |d          }||urt/          ||t           |                     s fd$} |d%          r|j        j        }|	|_         |d&          r||_         |d'          r|
|_         |d(          r ||_         |d&          s||_        ||_        t?          |d)*                                           }d+ j         d,d- d.d/| g}d0!                    |          |_"        |S )2a  Generate a `ContinuousDistribution` from an instance of `rv_continuous`

    The returned value is a `ContinuousDistribution` subclass. Like any subclass
    of `ContinuousDistribution`, it must be instantiated (i.e. by passing all shape
    parameters as keyword arguments) before use. Once instantiated, the resulting
    object will have the same interface as any other instance of
    `ContinuousDistribution`; e.g., `scipy.stats.Normal`.

    .. note::

        `make_distribution` does not work perfectly with all instances of
        `rv_continuous`. Known failures include `levy_stable` and `vonmises`,
        and some methods of some distributions will not support array shape
        parameters.

    Parameters
    ----------
    dist : `rv_continuous`
        Instance of `rv_continuous`.

    Returns
    -------
    CustomDistribution : `ContinuousDistribution`
        A subclass of `ContinuousDistribution` corresponding with `dist`. The
        initializer requires all shape parameters to be passed as keyword arguments
        (using the same names as the instance of `rv_continuous`).

    Notes
    -----
    The documentation of `ContinuousDistribution` is not rendered. See below for
    an example of how to instantiate the class (i.e. pass all shape parameters of
    `dist` to the initializer as keyword arguments). Documentation of all methods
    is identical to that of `scipy.stats.Normal`. Use ``help`` on the returned
    class or its methods for more information.

    Examples
    --------
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>> LogU = stats.make_distribution(stats.loguniform)
    >>> X = LogU(a=1.0, b=3.0)
    >>> np.isclose((X + 0.25).median(), stats.loguniform.ppf(0.5, 1, 3, loc=0.25))
    np.True_
    >>> X.plot()
    >>> sample = X.sample(10000, rng=np.random.default_rng())
    >>> plt.hist(sample, density=True, bins=30)
    >>> plt.legend(('pdf', 'histogram'))
    >>> plt.show()

    r   z` is not supported.z4The argument must be an instance of `rv_continuous`.rv  rF   rG   )r   TTr   )rN  rq   r   r   c                   F     e Zd Zr e gng ZZ fdZ fdZ xZS )-make_distribution.<locals>.CustomDistributionc                 p    t                                                      }|                    d          S NCustomDistribution)rC   r  r@  r,   srJ   repr_strs     r   r  z6make_distribution.<locals>.CustomDistribution.__repr__  s-      ""A9918<<<r   c                 p    t                                                      }|                    d          S r  )rC   r6   r@  r  s     r   r6   z5make_distribution.<locals>.CustomDistribution.__str__  s+    !!A9918<<<r   )	r7   r8   r9   r   r  r
  r  r6   rd   )rJ   _x_paramrU   r  s   @r   r  r    s        BL &00*=>>#% 			= 	= 	= 	= 	= 	=	= 	= 	= 	= 	= 	= 	= 	= 	= 	=r   r  c                      j         di | \  }}t          j        |          d         t          j        |          d         fS rB   )_get_supportr;   rE   )r[   rH   rI   r  s      r   r3   z2make_distribution.<locals>.get_numerical_endpoints	  sE     t 44#3441z!}}R "*Q--"333r   r>   Nr   c                $     j         d||d|S )N)rs   random_stater>   )_rvs)r,   r6  r  r   r  r  s        r   r  z*make_distribution.<locals>._sample_formula  s"    tyEjsEEfEEEr   c                 :     j         t          |          fi |S r   )_munpr   r,   rd  r  r  s      r   r  z.make_distribution.<locals>._moment_raw_formula  s#    tz#e**/////r   c                 :    |dk    rd S  j         di |d         S )Nrq   r   r>   _statsr  s      r   _moment_raw_formula_1z0make_distribution.<locals>._moment_raw_formula_1  .    A::4t{$$V$$Q''r   c                 :    |dk    rd S  j         di |d         S )NrR  rq   r>   r  r  s      r   r	  z2make_distribution.<locals>._moment_central_formula  r  r   c                     |dk    r/j         rd|d<    j        di |t          |dz
                     S |dk    r8j         rd|d<    j        di |t          |dz
                     }||n|dz   S d S )Nr  r  momentsrq   r  r  r>   )_stats_has_momentsr  r   )r,   rd  r  r  r  s       r   _moment_standard_formulaz3make_distribution.<locals>._moment_standard_formula  s    A::& ($'y!4;((((UQY88aZZ& ($'y!%%f%%c%!)nn5A	11q1u,4r   r  r  r  r  r  r  r  r  r  r  )
_logpdf_pdf_logcdf_cdf_logsf_sf_ppf_isf_entropy_medianr  r  r  c                 f    t          j        | d           t          t          j        | d           uS r   )rA  rJ   r   rv_continuous)r  r  s    r   r  z%make_distribution.<locals>._overridesB  s2    T::u2KFFG 	Hr   r  r  r   r  Fr~  z#This class represents `scipy.stats.z,` as a subclass of `ContinuousDistribution`.z(The `repr`/`str` of class instances is `z`.z'The PDF of the distribution is defined r  rZ  )#r   r  vonmisesr*   rN   r   r  r  rA  rH   rI   _shape_inforg   rF   rG   r   r  _distribution_namesrY   
capitalizer  r   rJ   r  r
  r   r3   r  r  r  r	  r  r  r   r:   )r  r]   r`  r	  
shape_infor   rR   
_x_supportr  r3   r  r  r  r	  r  r  skip_override
old_method
new_methodr9  r  r  support_etcdocsr  rU   r  s   `                       @@@r   r   r     s3   h !5>222!"Ddi"D"D"DEEEdE/00 "H!!!JEdJ(899G&&(( & &
z';'1';= = =zv>>>%   Z_%%%%w,GGGJc*gFFFH"&&ty$)2F2F2H2HIIH= = = = = = = = =3 = = = 4 4 4 4 4Ft F F F F F F F0 0 0 0 0( ( ( ( (
( ( ( ( (
     ,%+%+%&'-+	- 	-G $eV_5M")--// O O
J9%%*di8P*P*PT::u2JEE%%&
GD*4M4MNNNH H H H H z.!! A#-4)@&z' E1D.z& =-<*z( Q:R7z'"" 	Q5J29P6 2UKKKRRTTK	$di 	$ 	$ 	$?8????+??	D #'))D//r   c                       fd}|S )Nc                (   j         }t          | j        |          }	|                     |||          }
|                     |||          } |	|
|g|R i |} |	||
g|R i |}t	          j        t	          j        |||                    d         S rB   )r7   rA  _dist
_transformr;   real_if_closer   )r,   r   rT  r$  r-  r9  r  r  r_   r&  xtytfxyfyxfuncs                 r   rF  z8_shift_scale_distribution_function_2arg.<locals>.wrappedo  s    }DJ%% __QU++__QU++aB((((((aB((((((sC 8 899"==r   r>   )r.  rF  s   ` r   '_shift_scale_distribution_function_2argr/  n  s#    > > > > > Nr   c                 $     ddddd fd}|S )Nr  rD  r  r-  )r  r-  r  rD  c                   j         }t          | j        |          }t          | j        |                   }	|                     |||          }
 ||
g|R i |} |	|
g|R i |}t	          j        |||          d         S rB   )r7   rA  r'  r(  r;   r   )r,   r   r$  r-  r9  r  r  r_   r&  cfr*  fxcfxcitemr.  s                r   rF  z3_shift_scale_distribution_function.<locals>.wrapped  s    }DJ%%TZt-- __QU++Qr#D###F##b%d%%%f%%xb#&&r**r   r>   r.  rF  r5  s   ` @r   "_shift_scale_distribution_functionr7  ~  sA    !4."4.0 0E+ + + + + + Nr   c                 $     ddddd fd}|S )Nr  r  r  r  )r  r  r  r  c                .   j         }t          | j        |          }t          | j        |                   }	|                      ||g|R i |||          }
|                      |	|g|R i |||          }t	          j        ||
|          d         S rB   )r7   rA  r'  _itransformr;   r   )r,   r  r$  r-  r9  r  r  r_   r&  r2  r3  r4  r5  r.  s               r   rF  z._shift_scale_inverse_function.<locals>.wrapped  s    }DJ%%TZt-- qq4T444V44c5AArr!5d555f55sEBBxb#&&r**r   r>   r6  s   ` @r   _shift_scale_inverse_functionr;    sA    "60#6 02 2E
+ 
+ 
+ 
+ 
+ 
+ Nr   c                   T     e Zd Z fdZ fdZ fdZdd fd
Zd Zd Zd	 Z	 xZ
S )
TransformedDistributionc                   |                                   |j        | _        || _        |j        r|j        j        }t          |          }| j        s&| j                            t                                 | j        D ]X}|	                    |j                  r"d| d| j
        j         d}t          |          |j                            |           Y t                      j        |i | d S )Nz!One or more of the parameters of z% has the same name as a parameter of z;. Name collisions create ambiguities and are not supported.)r  r
  r'  r  rU   r   r  r  r   intersectionrJ   r7   r  rT   rC   rK   )	r,   r  r  r  dist_parameters
set_paramsr  r]   rJ   s	           r   rK   z TransformedDistribution.__init__  s   ##%%%
 	D1<O_--J* D'../@/B/BCCC$($; D D **+;+FGG . K1  K  K"&."9 K  K  KG %W--- +22?CCCC$)&)))))r   c                 z    | j                             |          p t                                          |          S r   )r'  r  rC   )r,   r  rJ   s     r   r  z"TransformedDistribution._overrides  s5    
%%k22 377%%k22	4r   c                 z    | j                                          t                                                       d S r   )r'  r  rC   )r,   rJ   s    r   r  z#TransformedDistribution.reset_cache  s3    
   r   Nr  c                    i }|                     | j        j                   |                     |            t                      j        dd|i| d S )Nr  r>   )rT   r'  r  rC   r  )r,   r  r  rU   rJ   s       r   r  z*TransformedDistribution._update_parameters  s`    

 	$*9:::&!!!""UU5FU*UUUUUr   c                 &     | j         j        di |S rB   )r'  r  r  s     r   r  z+TransformedDistribution._process_parameters  s    -tz-77777r   c                     t                      r   r)   r5   s    r   r  z TransformedDistribution.__repr__      !###r   c                     t                      r   r)   r5   s    r   r6   zTransformedDistribution.__str__  rG  r   )r7   r8   r9   rK   r  r  r  r  r  r6   rd   re   s   @r   r=  r=    s        * * * * *(4 4 4 4 4     7; 	V 	V 	V 	V 	V 	V 	V8 8 8$ $ $$ $ $ $ $ $ $r   r=  c                   L    e Zd ZdZ ee dfd          Z edded          Z edefd	          Z	 edd
e	d          Z
 eee
           ee           ee
          gZej         ej        d fd
ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )TruncatedDistributionzTruncated distribution.ub)TFr  lbb_l)皙?g?rO   r   r   )FTb_u)g?g?rL  rK  c               D     t                      j        |g|R ||d|S )NrQ  rC   rK   )r,   r  rL  rK  r  r  rJ   s         r   rK   zTruncatedDistribution.__init__  s1    uwwADAARBAA&AAAr   Nc           	      l   ||n%t          j        |t           j                   d         }||n$t          j        |t           j                  d         } | j        j        di |} | j        d||d|\  }} | j        j        ||fi |}|                    t          |||||                     |S )Nr>   rQ  )rL  rK  _a_blogmass)	r;   rr  r   r'  r  rv  r  rT   r^  )r,   rL  rK  r  rU   rH   rI   rW  s           r   r  z)TruncatedDistribution._process_parameters  s    >RRr|B'@'@'D>RRr|B'?'?'C3TZ3==f==
t}8r88Z881.$*.q!BBzBB$"aIIIJJJr   c                      | j         j        di |\  }}t          j        ||          t          j        ||          fS rB   )r'  rv  r;   r  r  )r,   rL  rK  r  rH   rI   s         r   rv  zTruncatedDistribution._support  sC    "tz",,V,,1z!R  "*Q"3"333r   c                     dS NFr>   r,   r  s     r   r  z TruncatedDistribution._overrides  s    ur   c                8     | j         j        |g|R i |}	|	|z
  S r   )r'  r  )
r,   r   rL  rK  rU  rV  rW  r  r  r   s
             r   r  z&TruncatedDistribution._logpdf_dispatch  s1    ,,Q@@@@@@r   c                :     | j         j        ||g|R i |}	|	|z
  S r   r'  r  )
r,   r   rL  rK  rU  rV  rW  r  r  r  s
             r   r  z&TruncatedDistribution._logcdf_dispatch  s3    --b!EdEEEfEEr   c                :     | j         j        ||g|R i |}	|	|z
  S r   r^  )
r,   r   rL  rK  rU  rV  rW  r  r  rc  s
             r   r  z'TruncatedDistribution._logccdf_dispatch  s3    .$*.q"FtFFFvFF  r   c                :     | j         j        ||g|R i |	}
|
|z
  S r   r^  )r,   r   rT  rL  rK  rU  rV  rW  r  r  logcdf2s              r   r  z'TruncatedDistribution._logcdf2_dispatch  s3    .$*.q!EdEEEfEE  r   c                     | j         j        |g|R i |}	t          j        |	||z             }
 | j         j        |
g|R i |S r   )r'  r  r;   r  r  )r,   r  rL  rK  rU  rV  rW  r  r  log_Falogp_adjusteds              r   r  z'TruncatedDistribution._ilogcdf_dispatch  s`    ,,RA$AAA&AAVTG^<<+tz+MKDKKKFKKKr   c                     | j         j        |g|R i |}	t          j        |	||z             }
 | j         j        |
g|R i |S r   )r'  r  r;   r  r  )r,   r  rL  rK  rU  rV  rW  r  r  log_cFbrd  s              r   r  z(TruncatedDistribution._ilogccdf_dispatch  s`    .$*.rCDCCCFCCWdWn==,tz,]LTLLLVLLLr   c                     | j         j        |g|R i |}	|	|t          j        |          z  z   }
 | j         j        |
g|R i |S r   )r'  r-  r;   r"   r  )r,   r  rL  rK  rU  rV  rW  r  r  Fa
p_adjusteds              r   r  z$TruncatedDistribution._icdf_dispatch  sb    %TZ%b:4:::6::!BF7OO++
(tz(EdEEEfEEEr   c                     | j         j        |g|R i |}	|	|t          j        |          z  z   }
 | j         j        |
g|R i |S r   )r'  rD  r;   r"   r  )r,   r  rL  rK  rU  rV  rW  r  r  cFbri  s              r   r  z%TruncatedDistribution._iccdf_dispatch  sb    'dj'<T<<<V<<1RVG__,,
)tz)*FtFFFvFFFr   c           	          t          j        d          5  dt          | j                   dt          | j                   dt          | j                   dcd d d            S # 1 swxY w Y   d S Nr  r  z	truncate(z, lb=z, ub=rl   )r;   r  r  r'  rL  rK  r5   s    r   r  zTruncatedDistribution.__repr__$  s    _r*** 	@ 	@?TZ 0 0 ? ?tw--? ?.247mm? ? ?	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@   AA%%A),A)c           	          t          j        d          5  dt          | j                   dt          | j                   dt          | j                   dcd d d            S # 1 swxY w Y   d S rm  )r;   r  r   r'  rL  rK  r5   s    r   r6   zTruncatedDistribution.__str__)  s    _r*** 	> 	>=DJ = =dg,,= =-0\\= = =	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>rn  NN)r7   r8   r9   r:   rg   r   
_lb_domainr   	_lb_param
_ub_domain	_ub_paramr   r  r;   rK   r  rv  r  r  r  r  r  r  r  r  r  r  r6   rd   re   s   @r   rJ  rJ    s       !! d|}MMMJtF'1:G G GI c{mLLLJtF)3ZI I II ,+IyAA++I66++I668 )+w26 B B B B B B B   4 4 4            
! ! !! ! !L L L
M M M
F F F
G G G
@ @ @
> > > > > > >r   rJ  c                 &    t          | ||          S )a  Truncate the support of a random variable.

    Given a random variable `X`, `truncate` returns a random variable with
    support truncated to the interval between `lb` and `ub`. The underlying
    probability density function is normalized accordingly.

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable to be truncated.
    lb, ub : float array-like
        The lower and upper truncation points, respectively. Must be
        broadcastable with one another and the shape of `X`.

    Returns
    -------
    X : `ContinuousDistribution`
        The truncated random variable.

    References
    ----------
    .. [1] "Truncated Distribution". *Wikipedia*.
           https://en.wikipedia.org/wiki/Truncated_distribution

    Examples
    --------
    Compare against `scipy.stats.truncnorm`, which truncates a standard normal,
    *then* shifts and scales it.

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>> loc, scale, lb, ub = 1, 2, -2, 2
    >>> X = stats.truncnorm(lb, ub, loc, scale)
    >>> Y = scale * stats.truncate(stats.Normal(), lb, ub) + loc
    >>> x = np.linspace(-3, 5, 300)
    >>> plt.plot(x, X.pdf(x), '-', label='X')
    >>> plt.plot(x, Y.pdf(x), '--', label='Y')
    >>> plt.xlabel('x')
    >>> plt.ylabel('PDF')
    >>> plt.title('Truncated, then Shifted/Scaled Normal')
    >>> plt.legend()
    >>> plt.show()

    However, suppose we wish to shift and scale a normal random variable,
    then truncate its support to given values. This is straightforward with
    `truncate`.

    >>> Z = stats.truncate(scale * stats.Normal() + loc, lb, ub)
    >>> Z.plot()
    >>> plt.show()

    Furthermore, `truncate` can be applied to any random variable:

    >>> Rayleigh = stats.make_distribution(stats.rayleigh)
    >>> W = stats.truncate(Rayleigh(), lb=0, ub=3)
    >>> W.plot()
    >>> plt.show()

    rQ  )rJ  )r  rL  rK  s      r   r    r    /  s    z !rb1111r   c                      e Zd ZdZ ee efd          Z edded          Z ee efd          Z	 edd	e	d
          Z
 eee
           ee           ee
          gZd-dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zeddd            Zeddd            Zeddd            Zeddd            Zeddd            Zeddd            Z eddd            Z!eddd             Z"e#ddd!            Z$e#ddd"            Z%e#ddd#            Z&e#ddd$            Z'd% Z(d& Z)d' Z*d( Z+d) Z,d* Z-d+ Z.d, Z/dS ).r&  z8Distribution with a standard shift/scale transformation.r  r  r$  z\murq   rR  rO  r-  z\sigma)rN  r  Nc                     ||nt          j        |          d         }||nt          j        |          d         }|dk    } | j        j        di |}|                    t          |||                     |S )Nr>   r   r$  r-  r9  )r;   
zeros_likers  r'  r  rT   r^  )r,   r$  r-  r  r9  rU   s         r   r  z-ShiftedScaledDistribution._process_parameters~  s    _cc"-*>*>r*B*S0A0A"0Eqy3TZ3==f==
$3e$???@@@r   c                     ||z
  |z  S r   r>   r,   r   r$  r-  r  s        r   r(  z$ShiftedScaledDistribution._transform  s    Cr   c                     ||z  |z   S r   r>   r|  s        r   r:  z%ShiftedScaledDistribution._itransform  s    5y3r   c                      | j         j        di |\  }}|                     |||          |                     |||          }}t          j        |||          d         t          j        |||          d         fS rB   )r'  rv  r:  r;   r   )r,   r$  r-  r9  r  rH   rI   s          r   rv  z"ShiftedScaledDistribution._support  s~    "tz",,V,,13..0@0@C0O0O1xa##B'$1)=)=b)AAAr   c                    t          j        d          5  t          | j                   dt          | j                   }| j        j        s'| j        dk     r|dt          | j                    z  }n`t          j        | j        dk              s)t          j        | j        j	        | j        j	                  s|dt          | j                   z  }d d d            n# 1 swxY w Y   |S Nr  r  *r   z - z + )
r;   r  r  r-  r'  r$  r  r  can_castr   r,   results     r   r  z"ShiftedScaledDistribution.__repr__  s   _r*** 	1 	1dj))>>D,<,<>>F8= 1TX\\1dhY111&Q'' 1TX^TZ5EFF1 0TX000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1    B?C!!C%(C%c                    t          j        d          5  t          | j                   dt          | j                   }| j        j        s'| j        dk     r|dt          | j                    z  }n`t          j        | j        dk              s)t          j        | j        j	        | j        j	                  s|dt          | j                   z  }d d d            n# 1 swxY w Y   |S r  )
r;   r  r   r-  r'  r$  r  r  r  r   r  s     r   r6   z!ShiftedScaledDistribution.__str__  s   _r*** 	0 	0TZ<<3tz??<<F8= 0TX\\0TXI000&Q'' 0TX^TZ5EFF0 /DH///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 r  c                t     | j         j        |i |t          j        t          j        |                    z   S r   )r'  r  r;   r#   r!   )r,   r$  r-  r9  r  r  s         r   r  z+ShiftedScaledDistribution._entropy_dispatch  s8    ,
,d=f==&''( 	)r   c                     | j         j        |i |}t          j        t          j        t          j        |                    dz             }t          j        t          j        ||          d          S )NrP  r   r   )r'  r  r;   r#   r!   r   rp  r   )r,   r$  r-  r9  r  r  lH0llss           r   r  z.ShiftedScaledDistribution._logentropy_dispatch  sd    -dj-t>v>>fRVBF5MM**2-.. !4S#!>!>QGGGGr   c                X     | j         j        dd|i|}|                     |||          S r  )r'  r  r:  r,   r9  r$  r-  r9  r  r  s          r   r  z*ShiftedScaledDistribution._median_dispatch  s9    )dj)BBB6BBS%000r   c                X     | j         j        dd|i|}|                     |||          S r  )r'  r  r:  r  s          r   r  z(ShiftedScaledDistribution._mode_dispatch  s9    'dj'@@v@@@S%000r   c                    |                      |||          } | j        j        |g|R i |}|t          j        t          j        |                    z
  S r   )r(  r'  r  r;   r#   r!   )r,   r   r$  r-  r9  r  r  r   s           r   r  z*ShiftedScaledDistribution._logpdf_dispatch  sX    OOAsE**,,Q@@@@@@rve}}----r   c                    |                      |||          } | j        j        |g|R i |}|t          j        |          z  S r   )r(  r'  r  r;   r!   )r,   r   r$  r-  r9  r  r  r   s           r   r  z'ShiftedScaledDistribution._pdf_dispatch  sN    OOAsE**&dj&q:4:::6::RVE]]""r   r8  c                    d S r   r>   r  s       r   r  z*ShiftedScaledDistribution._logcdf_dispatch      r   c                    d S r   r>   r  s       r   r-  z'ShiftedScaledDistribution._cdf_dispatch  r  r   c                    d S r   r>   r  s       r   r  z+ShiftedScaledDistribution._logccdf_dispatch  r  r   c                    d S r   r>   r  s       r   rD  z(ShiftedScaledDistribution._ccdf_dispatch  r  r   c                    d S r   r>   r  s        r   r  z+ShiftedScaledDistribution._logcdf2_dispatch  r  r   c                    d S r   r>   r  s        r   r  z(ShiftedScaledDistribution._cdf2_dispatch  r  r   c                    d S r   r>   r  s        r   re  z,ShiftedScaledDistribution._logccdf2_dispatch  r  r   c                    d S r   r>   r  s        r   rz  z)ShiftedScaledDistribution._ccdf2_dispatch  r  r   c                    d S r   r>   r  s       r   r  z+ShiftedScaledDistribution._ilogcdf_dispatch  r  r   c                    d S r   r>   r  s       r   r  z(ShiftedScaledDistribution._icdf_dispatch  r  r   c                    d S r   r>   r  s       r   r  z,ShiftedScaledDistribution._ilogccdf_dispatch  r  r   c                    d S r   r>   r  s       r   r  z)ShiftedScaledDistribution._iccdf_dispatch  r  r   c                h     | j         j        |fd|i|}|d n|t          j        |          |z  z  S Nr  )r'  r  r;   r9  r,   rd  r$  r-  r9  r  r  r  s           r   r  z7ShiftedScaledDistribution._moment_standardized_dispatch  sP    7tz7. .".&,. .{ttbgenne.C(CCr   c                D     | j         j        |fd|i|}|d n|||z  z  S r  )r'  r  r  s           r   r  z2ShiftedScaledDistribution._moment_central_dispatch	  sG    2tz2. .".&,. .{tteUl(::r   c                   g }|}t          t          |          dz             D ]H}	|	|k     r| j        n|} | j        j        |	fd|i|}
|
 d S |
||	z  z  }|                    |           I|                     |||| j                  S )Nrq   r  )r   r   r  r'  r  r  r  r  )r,   rd  r$  r-  r9  r  r  r  methods_highest_orderr   r  r  s               r   r  z.ShiftedScaledDistribution._moment_raw_dispatch  s     's5zzA~&& 	) 	)A/05yyt++1 1$*1!OOWOOOC{ttUAX~Hx((((,,;TZ1 1 	1r   c                T     | j         j        ||f||d|}	 | j        |	f|||d|S )Nr  ry  )r'  r=  r:  )
r,   r  r  r   r$  r-  r9  r9  r  rvss
             r   r=  z*ShiftedScaledDistribution._sample_dispatch  s]    )dj)*H-3H H@FH HtOEOOOOOr   c                 J    t          | j        | j        |z   | j                  S N)r$  r-  r&  r'  r$  r-  r'  s     r   r(  z!ShiftedScaledDistribution.__add__%  *    (C/3z; ; ; 	;r   c                 J    t          | j        | j        |z
  | j                  S r  r  r'  s     r   r*  z!ShiftedScaledDistribution.__sub__)  r  r   c                 P    t          | j        | j        |z  | j        |z            S r  r  r.  s     r   r/  z!ShiftedScaledDistribution.__mul__-  5    (-1X-=/3zE/AC C C 	Cr   c                 P    t          | j        | j        |z  | j        |z            S r  r  r.  s     r   r1  z%ShiftedScaledDistribution.__truediv__2  r  r   rp  )0r7   r8   r9   r:   rg   r   _loc_domainr   
_loc_param_scale_domain_scale_paramr   r  r  r(  r:  rv  r  r6   r  r  r  r  r  r  r7  r  r-  r  rD  r/  r  r  re  rz  r;  r  r  r  r  r  r  r  r=  r(  r*  r/  r1  r>   r   r   r&  r&  o  s       BB+#s|LLLKf'2FD D DJ  KC4+NNNM!>'))6	K K KL ,+JEE++J77++L99;       B B B
 
 

 
 
2) ) )H H H
1 1 11 1 1. . .
# # # (,0     (' ()-     (' (-1     (' (*.     (' -04     -, --1     -, -15     -, -.2     -, #-1     #" #*.     #" #.2     #" #+/     #"D D D; ; ;1 1 1 P P P; ; ;; ; ;C C C
C C C C Cr   r&  c                   $    e Zd ZdZ edd          Z eded          Z edej	        fd          Z
 ed	e
d
          Ze                    e            eee          gZ fdZd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )OrderStatisticDistributiona  Probability distribution of an order statistic

    An instance of this class represents a random variable that follows the
    distribution underlying the :math:`r^{\text{th}}` order statistic of a
    sample of :math:`n` observations of a random variable :math:`X`.

    Parameters
    ----------
    dist : `ContinuousDistribution`
        The random variable :math:`X`
    n : array_like
        The (integer) sample size :math:`n`
    r : array_like
        The (integer) rank of the order statistic :math:`r`


    Notes
    -----
    If we make :math:`n` observations of a continuous random variable
    :math:`X` and sort them in increasing order
    :math:`X_{(1)}, \dots, X_{(r)}, \dots, X_{(n)}`,
    :math:`X_{(r)}` is known as the :math:`r^{\text{th}}` order statistic.

    If the PDF, CDF, and CCDF underlying math:`X` are denoted :math:`f`,
    :math:`F`, and :math:`F'`, respectively, then the PDF underlying
    math:`X_{(r)}` is given by:

    .. math::

        f_r(x) = \frac{n!}{(r-1)! (n-r)!} f(x) F(x)^{r-1} F'(x)^{n - r}

    The CDF and other methods of the distribution underlying :math:`X_{(r)}`
    are calculated using the fact that :math:`X = F^{-1}(U)`, where :math:`U` is
    a standard uniform random variable, and that the order statistics of
    observations of `U` follow a beta distribution, :math:`B(r, n - r + 1)`.

    References
    ----------
    .. [1] Order statistic. *Wikipedia*. https://en.wikipedia.org/wiki/Order_statistic

    Examples
    --------
    Suppose we are interested in order statistics of samples of size five drawn
    from the standard normal distribution. Plot the PDF underlying the fourth
    order statistic and compare with a normalized histogram from simulation.

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>> from scipy.stats._distribution_infrastructure import OrderStatisticDistribution
    >>>
    >>> X = stats.Normal()
    >>> data = X.sample(shape=(10000, 5))
    >>> ranks = np.sort(data, axis=1)
    >>> Y = OrderStatisticDistribution(X, r=4, n=5)
    >>>
    >>> ax = plt.gca()
    >>> Y.plot(ax=ax)
    >>> ax.hist(ranks[:, 3], density=True, bins=30)
    >>> plt.show()

    )rq   r0   r  r  rrw  r  rq   r0   )rq   r  c               H     t                      j        |g|R ||d| d S )Nr  r0   rS  )r,   r  r  r0   r  r  rJ   s         r   rK   z#OrderStatisticDistribution.__init__  s7    999Q99&99999r   c                &     | j         j        |i |S r   )r'  rv  )r,   r  r0   r  r  s        r   rv  z#OrderStatisticDistribution._support  s    "tz"D3F333r   Nc                 r     | j         j        di |}|                    t          ||                     |S )Nr  r>   )r'  r  rT   r^  )r,   r  r0   r  rU   s        r   r  z.OrderStatisticDistribution._process_parameters  s@    3TZ3==f==
$a...)))r   c                 
    |dv S )N>   r  r  r  r  r  r  r>   r[  s     r   r  z%OrderStatisticDistribution._overrides  s     B B 	Br   c                    t          j        |||z
  dz             } | j        j        |fi |} | j        j        |j        fi |} | j        j        |j        fi |}t          j        |dz
  dk    t          j	        |          z  d|dz
  |z            }	t          j        ||z
  dk    t          j	        |          z  d||z
  |z            }
||	z   |
z   |z
  S )Nrq   r   )
r   betalnr'  r  r  r  r  r;   r   ro  )r,   r   r  r0   r  
log_factorlog_fXlog_FXlog_cFX
rm1_log_FXnmr_log_cFXs              r   r  z*OrderStatisticDistribution._logpdf_formula  s    ^Aq1uqy11
,,Q99&99 -,QV>>v>>.$*.qv@@@@ Xq1uzR[-@-@@!ac6\RR
hA
bk'.B.BBA!W}UU
"[0:==r   c                     t          j        |||z
  dz             } | j        j        |fi |} | j        j        |fi |} | j        j        |fi |}|||dz
  z  z  |||z
  z  z  |z  S r8  )r   betar'  r  r-  rD  )	r,   r   r  r0   r  factorfXFXcFXs	            r   r  z'OrderStatisticDistribution._pdf_formula  s    aQ++%TZ%a22622%TZ%a22622'dj'44V44B1I~ac
*V33r   c                 `     | j         j        |fi |}t          j        |||z
  dz   |          S r8  )r'  r-  r   betaincr,   r   r  r0   r  x_s         r   r  z'OrderStatisticDistribution._cdf_formula  s:    %TZ%a22622q!A#a%,,,r   c                 `     | j         j        |fi |}t          j        |||z
  dz   |          S r8  )r'  r-  r   betainccr  s         r   r  z(OrderStatisticDistribution._ccdf_formula  s;    %TZ%a226221Q3q5"---r   c                 `    t          j        |||z
  dz   |          } | j        j        |fi |S r8  )r   
betaincinvr'  r  r,   r  r  r0   r  p_s         r   r  z(OrderStatisticDistribution._icdf_formula  s;    1Q3q5!,,(tz(66v666r   c                 `    t          j        |||z
  dz   |          } | j        j        |fi |S r8  )r   betainccinvr'  r  r  s         r   r  z)OrderStatisticDistribution._iccdf_formula  s;     AaCE1--(tz(66v666r   c           	          t          j        d          5  dt          | j                   dt          | j                   dt          | j                   dcd d d            S # 1 swxY w Y   d S Nr  r  zorder_statistic(z, r=z, n=rl   )r;   r  r  r'  r  r0   r5   s    r   r  z#OrderStatisticDistribution.__repr__  s    _r*** 	* 	*)tDJ'7'7 ) )T$&\\ ) )df) ) )	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*rn  c           	          t          j        d          5  dt          | j                   dt          | j                   dt          | j                   dcd d d            S # 1 swxY w Y   d S r  )r;   r  r   r'  r  r0   r5   s    r   r6   z"OrderStatisticDistribution.__str__  s    _r*** 	) 	)(s4: ( (CKK ( (TV( ( (	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)rn  rp  )r7   r8   r9   r:   rg   	_r_domainr   _r_paramr;   r   	_n_domain_n_paramrW   r   r  rK   rv  r  r  r  r  r  r  r  r  r  r6   rd   re   s   @r   r  r  8  sv       = =B h,GGGI~c)VDDDHq"&k\JJJI~c)VDDDH)))++Hh??@: : : : :4 4 4   
B B B
> > >4 4 4- - -. . .7 7 77 7 7* * *
) ) ) ) ) ) )r   r  c               \   t          j        |          t          j        |          }}t          j        |t          j        |          k    |dk     z            s1t          j        |t          j        |          k    |dk     z            rd}t	          |          t          | ||          S )av	  Probability distribution of an order statistic

    Returns a random variable that follows the distribution underlying the
    :math:`r^{\text{th}}` order statistic of a sample of :math:`n`
    observations of a random variable :math:`X`.

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X`
    r : array_like
        The (positive integer) rank of the order statistic :math:`r`
    n : array_like
        The (positive integer) sample size :math:`n`

    Returns
    -------
    Y : `ContinuousDistribution`
        A random variable that follows the distribution of the prescribed
        order statistic.

    Notes
    -----
    If we make :math:`n` observations of a continuous random variable
    :math:`X` and sort them in increasing order
    :math:`X_{(1)}, \dots, X_{(r)}, \dots, X_{(n)}`,
    :math:`X_{(r)}` is known as the :math:`r^{\text{th}}` order statistic.

    If the PDF, CDF, and CCDF underlying math:`X` are denoted :math:`f`,
    :math:`F`, and :math:`F'`, respectively, then the PDF underlying
    math:`X_{(r)}` is given by:

    .. math::

        f_r(x) = \frac{n!}{(r-1)! (n-r)!} f(x) F(x)^{r-1} F'(x)^{n - r}

    The CDF and other methods of the distribution underlying :math:`X_{(r)}`
    are calculated using the fact that :math:`X = F^{-1}(U)`, where :math:`U` is
    a standard uniform random variable, and that the order statistics of
    observations of `U` follow a beta distribution, :math:`B(r, n - r + 1)`.

    References
    ----------
    .. [1] Order statistic. *Wikipedia*. https://en.wikipedia.org/wiki/Order_statistic

    Examples
    --------
    Suppose we are interested in order statistics of samples of size five drawn
    from the standard normal distribution. Plot the PDF underlying each
    order statistic and compare with a normalized histogram from simulation.

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>>
    >>> X = stats.Normal()
    >>> data = X.sample(shape=(10000, 5))
    >>> sorted = np.sort(data, axis=1)
    >>> Y = stats.order_statistic(X, r=[1, 2, 3, 4, 5], n=5)
    >>>
    >>> ax = plt.gca()
    >>> colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
    >>> for i in range(5):
    ...     y = sorted[:, i]
    ...     ax.hist(y, density=True, bins=30, alpha=0.1, color=colors[i])
    >>> Y.plot(ax=ax)
    >>> plt.show()

    r   z0`r` and `n` must contain only positive integers.r  )r;   rE   r  floorr  r  )r  r  r0   r]   s       r   r   r     s    L :a=="*Q--qA	vqBHQKKAE*++ "rvqBHQKK7GAPQE6R/S/S "D!!!%a12222r   c                      e Zd ZdZd ZdddZed             Zed             Zd Z	d	 Z
d
 Zd Zd ZdddZdddZdddZdddZdddZdddZdddZdddZdddZd-dddZd Zd Zd ZdddZdddZd.dddZd.ddd Zd.ddd!Zd.ddd"Z d# Z!ddd$Z"ddd%Z#ddd&Z$ddd'Z%d/ddd)d*Z&d+ Z'd, Z(dS )0r   a%  Representation of a mixture distribution.

    A mixture distribution is the distribution of a random variable
    defined in the following way: first, a random variable is selected
    from `components` according to the probabilities given by `weights`, then
    the selected random variable is realized.

    Parameters
    ----------
    components : sequence of `ContinuousDistribution`
        The underlying instances of `ContinuousDistribution`.
        All must have scalar shape parameters (if any); e.g., the `pdf` evaluated
        at a scalar argument must return a scalar.
    weights : sequence of floats, optional
        The corresponding probabilities of selecting each random variable.
        Must be non-negative and sum to one. The default behavior is to weight
        all components equally.

    Attributes
    ----------
    components : sequence of `ContinuousDistribution`
        The underlying instances of `ContinuousDistribution`.
    weights : ndarray
        The corresponding probabilities of selecting each random variable.

    Methods
    -------
    support

    sample

    moment

    mean
    median
    mode

    variance
    standard_deviation

    skewness
    kurtosis

    pdf
    logpdf

    cdf
    icdf
    ccdf
    iccdf

    logcdf
    ilogcdf
    logccdf
    ilogccdf

    entropy

    Notes
    -----
    The following abbreviations are used throughout the documentation.

    - PDF: probability density function
    - CDF: cumulative distribution function
    - CCDF: complementary CDF
    - entropy: differential entropy
    - log-*F*: logarithm of *F* (e.g. log-CDF)
    - inverse *F*: inverse function of *F* (e.g. inverse CDF)

    References
    ----------
    .. [1] Mixture distribution, *Wikipedia*,
           https://en.wikipedia.org/wiki/Mixture_distribution

    c                    t          |          dk    rd}t          |          |D ]D}t          |t                    sd}t          |          |j        dk    sd}t          |          E|||fS t          j        |          }|j        t          |          fk    rd}t          |          t          j        |j	        t
          j
                  sd}t          |          t          j        t          j        |          d          sd	}t          |          t          j        |dk              sd
}t          |          ||fS )Nr   z7`components` must contain at least one random variable.zMEach element of `components` must be an instance of `ContinuousDistribution`.r>   z5All elements of `components` must have scalar shapes.z5`components` and `weights` must have the same length.z)`weights` must have floating point dtype.r  z`weights` must sum to 1.0.z#All `weights` must be non-negative.)r   r  r   r  r  r;   rE   r   r   r   rX  iscloser   rU  )r,   
componentsweightsr]   r  s        r   _input_validationzMixture._input_validationa  s\   z??aPGW%%% 		* 		*C c#9:: *7 ))):##Q ))) $ ?w&&*W%%=S__...MGW%%%}W]BJ77 	&AGW%%%z"&//3// 	&2GW%%%vgl## 	&;GW%%%7""r   N)r  c                2   |                      ||          \  }}t          |          }t          j        d |D              }t          j        d |D              | _        ||c| _        | _        |t          j        |d|z  |          n|| _	        d | _
        d S )Nc              3   $   K   | ]}|j         V  d S r   )r  rQ   r  s     r   r  z#Mixture.__init__.<locals>.<genexpr>  s$       B B B B B B B Br   c              3   $   K   | ]}|j         V  d S r   )r  r  s     r   r  z#Mixture.__init__.<locals>.<genexpr>  s$      +M+M3CJ+M+M+M+M+M+Mr   rq   r   )r  r   r;   r   r   r  r  _componentsr}   _weightsr  )r,   r  r  r0   r   s        r   rK   zMixture.__init__  s    "44ZII
G
OO B Bz B B BC)+M+M*+M+M+MN(-z%T%8?1Q3e4444W!%r   c                 *    t          | j                  S r   )r   r  r5   s    r   r  zMixture.components  s    D$%%%r   c                 4    | j                                         S r   )r  rD   r5   s    r   r  zMixture.weights  s    }!!###r   c                     d |D             }t          j        | j        gd |D             R  }t          j        | j        gd |D             R  }t          j        |||          S )Nc                 6    g | ]}t          j        |          S r>   r  rQ   args     r   r   z!Mixture._full.<locals>.<listcomp>  s     000C
3000r   c              3   $   K   | ]}|j         V  d S r   r   r  s     r   r  z Mixture._full.<locals>.<genexpr>  s$      -H-HCci-H-H-H-H-H-Hr   c              3   $   K   | ]}|j         V  d S r   r  r  s     r   r  z Mixture._full.<locals>.<genexpr>  s$      2M2M392M2M2M2M2M2Mr   r   )r;   r   r  r   r  r}   )r,   r  r  r   r   s        r   _fullzMixture._full  s}    004000t{I-H-H4-H-H-HIII#DKN2M2M2M2M2MNNNwuc////r   c                      | j         dg|R  }t          | j        | j                  D ]\  }}| t	          ||          | |z  z  }|d         S Nr   r>   )r  r   r  r  rA  )r,   funr  rv   r  weights         r   _sumzMixture._sum  si    dj"T"""t/?? 	5 	5KC$73$$d+f44CC2wr   c           	           | j         t          j         g|R  }t          | j        t          j        | j                            D ]0\  }}t          j        | t          ||          | |z   |           1|d         S )N)rv   r>   )	r  r;   r   r   r  r#   r  r  rA  )r,   r  r  rv   r  
log_weights         r   _logsumzMixture._logsum  s    dj"&(4((("4#3RVDM5J5JKK 	N 	NOCL/gc3//6CMMMMM2wr   c                 P   |                      t          j                  }|                      t          j                   }| j        D ]\}t          j        ||                                d                   }t          j        ||                                d                   }]||fS r  )r  r;   r   r  r  r	  r  )r,   rH   rI   r  s       r   r	  zMixture.support  s    JJrvJJw# 	0 	0C
1ckkmmA.//A
1ckkmmA.//AA!tr   c                 (    |t          d          d S )Nz/`method` not implemented for this distribution.r)   r  s     r   _raise_if_methodzMixture._raise_if_method  s    %&WXXX r   r8  c                                           |            fd}t          |g                                 R ddij        }t	          |t
          j        dz  z             S )Nc                                          | j                  t          j                             | j                  dz             z   S r  )r   r  r;   r#   r   r,   s    r   log_integrandz)Mixture.logentropy.<locals>.log_integrand  s;    
 ;;qv&&AF0C0Cb0H)I)IIIr   r#   TrQ  )r  rx  r	  ry  r}  r;   r<   )r,   r9  r  r  s   `   r   r  zMixture.logentropy  sw    f%%%	J 	J 	J 	J 	J AAAADAAJ$S258^444r   c                |                           |           t           fdg                                 R  j        S )Nc                 \                         |                                |           z  S r   )r   r   r  s    r   r  z!Mixture.entropy.<locals>.<lambda>  s"    DHHQKK<$++a..#@ r   )r  rx  r	  ry  r  s   ` r   r  zMixture.entropy  sM    f%%%@@@@ *,,..* * **2	3r   c                                           |                                            \  }} fd}t          |d||          }t          ||j        |j        |j                  }|j        S )Nc                 0                         |            S r   )r   r  s    r   r&  zMixture.mode.<locals>.f  s    $((1++%r   r  )ru  rv  )r  r	  r   r   r  r  r  r   )r,   r9  rH   rI   r&  r  s   `     r   r  zMixture.mode  sm    f%%%||~~1%%%%%q"11555$Q??ur   c                V    |                      |           |                     d          S rC  )r  r   r  s     r   r  zMixture.median  s&    f%%%yy~~r   c                V    |                      |           |                     d          S )Nr  r  r  r  s     r   r  zMixture.mean  s(    f%%%yy   r   c                V    |                      |           |                     d          S )NrR  )r  r  r  s     r   r  zMixture.variance  s*    f%%%##A&&&r   c                Z    |                      |           |                                 dz  S rC  )r  r  r  s     r   r  zMixture.standard_deviation  s)    f%%%}}##r   c                V    |                      |           |                     d          S )Nr  r  r  r  s     r   r  zMixture.skewness  *    f%%%((+++r   c                V    |                      |           |                     d          S )Nr  r
  r  s     r   r  zMixture.kurtosis  r  r   rq   r  c                    |                      |           | j        | j        | j        d}t                              | |||          }||         } ||          S )Nr  )r  r  r  r  r  rh  r  s         r   r  zMixture.moment  sh    f%%%( 0!%!:< < ';;D%uUUDk{5!!!r   c                     |                      d          }t          | j        | j                  D ]"\  }}||                    |d          |z  z  }#|d         S )Nr   r  re  r>   )r  r   r  r  r  )r,   rd  rv   r  r  s        r   r  zMixture._moment_raw  s]    jjmmt/?? 	: 	:KC3::e%:00699CC2wr   c                 h   t          |          }|                     d          }t          | j        | j                  D ]k\  }fdt          |dz             D             }                                |                                 }}                    ||||          }|||z  z  }l|d         S )Nr   c                 >    g | ]}                     |d           S )r  r  r  )rQ   rd  r  s     r   r   z+Mixture._moment_central.<locals>.<listcomp>  s9     8 8 8" E	:: 8 8 8r   rq   r>   )r   r  r   r  r  r   r  r  )	r,   rd  rv   r  r*  rH   rI   r  r  s	           @r   r  zMixture._moment_central  s    E

jjmmt/?? 	# 	#KC8 8 8 8&+EAI&6&68 8 8I88::tyy{{qA11%AqIIF6F?"CC2wr   c                 \    |                      |          |                                 |z  z  S r   )r  r  )r,   rd  s     r   r  zMixture._moment_standardized  s,    ##E**T-D-D-F-F-MMMr   c               X    |                      |           |                     d|          S )Nr   r  r  s      r   r   zMixture.pdf  s*    f%%%yy"""r   c               X    |                      |           |                     d|          S )Nr   r  r  r  s      r   r   zMixture.logpdf  *    f%%%||Ha(((r   c               Z    |                      |           ||fn||f} | j        dg|R  S Nr9  r  r,   r   rT  r9  r  s        r   r9  zMixture.cdf  sC    f%%%yttq!fty&&&&&r   c               Z    |                      |           ||fn||f} | j        dg|R  S Nr  r  r  s        r   r  zMixture.logcdf  sC    f%%%yttq!ft|H,t,,,,r   c               Z    |                      |           ||fn||f} | j        dg|R  S Nr   r  r  s        r   r   zMixture.ccdf  sC    f%%%yttq!fty'$''''r   c               Z    |                      |           ||fn||f} | j        dg|R  S Nrc  r  r  s        r   rc  zMixture.logccdf  sC    f%%%yttq!ft|I-----r   c                     |                                  \  }}t          |           fd}t          ||          \  }}t          ||||||f          }t	          ||j        |j        |f          j        S )Nc                       |           |z
  S r   r>   )r   r  r  s     r   r  z!Mixture._invert.<locals>.<lambda>#  s    Q! r   r  )rH   rI   r  )r	  rA  rw  r   r   r  r  r   )	r,   r  r  ru  rv  r&  r  r  r  s	    `       r   _invertzMixture._invert   s    \\^^
ddC  ####!$--SA3CdQDQQQQ#&CF!>>>@@r   c               X    |                      |           |                     d|          S r  r  r"  r  s      r   r   zMixture.icdf(  s*    f%%%||E1%%%r   c               X    |                      |           |                     d|          S r  r$  r  s      r   r   zMixture.iccdf,  s*    f%%%||FA&&&r   c               X    |                      |           |                     d|          S r  r$  r  s      r   r   zMixture.ilogcdf0  r  r   c               X    |                      |           |                     d|          S r  r$  r  s      r   r   zMixture.ilogccdf4  s*    f%%%||Iq)))r   r>   )r   r9  c                   |                      |           t          j                                      t          j        t          j        |                    }                    || j                  }fdt          || j	                  D             }t          j
                            t          j        |                    |          }|d         S )Nc                 D    g | ]\  }}|                     |           S ))r   r   )r  )rQ   r0   r  r   s      r   r   z"Mixture.sample.<locals>.<listcomp>=  s-    RRRfaSZZaSZ))RRRr   r>   )r  r;   rx   ry   r   ry  r   r  r   r  r   r   r   )r,   r   r   r9  rs   nsr   s     `    r   r  zMixture.sample8  s    f%%%i##C((wr}U++,,__T4=11RRRRB@P8Q8QRRRJs||BN1$5$566>>ur   c                     d}|dz  }t          j        d          5  | j        D ]}|dt          |           dz  }|dz  }|dt          | j                   dz  }d d d            n# 1 swxY w Y   |d	z  }|S 
Nz	Mixture(
z    [
r  r  z        z,
z    ],
z    weights=rl   )r;   r  r  r  r  r,   r  	components      r   r  zMixture.__repr__A  s    )_r*** 	= 	=!_ : :	9T)__9999j F<T$,%7%7<<<<F		= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=
 	#   AA**A.1A.c                     d}|dz  }t          j        d          5  | j        D ]}|dt          |           dz  }|dz  }|dt          | j                   dz  }d d d            n# 1 swxY w Y   |d	z  }|S r,  )r;   r  r  r   r  r-  s      r   r6   zMixture.__str__L  s    )_r*** 	< 	<!_ 9 9	8S^^8888j F;S%6%6;;;;F		< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<
 	#r/  r[  r   rZ  ))r7   r8   r9   r:   r  rK   r  r  r  r  r  r  r	  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r9  r  r   rc  r"  r   r   r   r   r  r  r6   r>   r   r   r   r     s       J J\$# $# $#L /3 & & & & & & & X& $ $ X$0 0 0      Y Y Y $( 
5 
5 
5 
5 
5 !% 3 3 3 3 3
 "       $      " ! ! ! ! ! "& ' ' ' ' ' ,0 $ $ $ $ $ "& , , , , , "& , , , , ,"D " " " " "  	 	 	N N N #' # # # # # &* ) ) ) ) )'$ ' ' ' ' '
-T - - - - -
(4 ( ( ( ( (
.d . . . . .
A A A $( & & & & & %) ' ' ' ' ' '+ ) ) ) ) ) (, * * * * *d4     	 	 		 	 	 	 	r   r   c                        e Zd ZdZddddd fd
Zd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )rC  a  Distribution underlying a strictly monotonic function of a random variable

    Given a random variable :math:`X`; a strictly monotonic function
    :math:`g(u)`, its inverse :math:`h(u) = g^{-1}(u)`, and the derivative magnitude
    :math: `|h'(u)| = \left| \frac{dh(u)}{du} \right|`, define the distribution
    underlying the random variable :math:`Y = g(X)`.

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X`.
    g, h, dh : callable
        Elementwise functions representing the mathematical functions
        :math:`g(u)`, :math:`h(u)`, and :math:`|h'(u)|`
    logdh : callable, optional
        Elementwise function representing :math:`\log(h'(u))`.
        The default is ``lambda u: np.log(dh(u))``, but providing
        a custom implementation may avoid over/underflow.
    increasing : bool, optional
        Whether the function is strictly increasing (True, default)
        or strictly decreasing (False).
    repr_pattern : str, optional
        A string pattern for determining the __repr__. The __repr__
        for X will be substituted into the position where `***` appears.
        For example:
            ``"exp(***)"`` for the repr of an exponentially transformed
            distribution
        The default is ``f"{g.__name__}(***)"``.
    str_pattern : str, optional
        A string pattern for determining `__str__`. The `__str__`
        for X will be substituted into the position where `***` appears.
        For example:
            ``"exp(***)"`` for the repr of an exponentially transformed
            distribution
        The default is the value `repr_pattern` takes.
    NT)logdhrA  r=  r>  c                   t                      j        |g|	R i |
 || _        || _        | _        ||nfd| _        |r| j        j        | _        | j        j	        | _
        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        n| j        j	        | _        | j        j        | _
        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        || _        |p	|j         d| _        |p| j        | _        d S )Nc                 >    t          j         |                     S r   rZ  )r5  r@  s    r   r  z;MonotonicTransformedDistribution.__init__.<locals>.<lambda>  s    bfRRUUmm r   z(***))rC   rK   _g_h_dh_logdhr'  r-  _xdfrD  _cxdfr  _ixdfr  _icxdfr  _logxdfr  _logcxdfr  _ilogxdfr  	_ilogcxdf_increasingr7   _repr_pattern_str_pattern)r,   r  r<  r?  r@  r2  rA  r=  r>  r  r  rJ   s       `      r   rK   z)MonotonicTransformedDistribution.__init__~  s_    	,T,,,V,,, % 1uu3333 	 	:
0DI2DJ2DJ*4DK:6DL J8DM J8DM!Z:DNN
1DI1DJ3DJ*3DK:7DL J7DM J9DM!Z9DN%)A
-A-A-A'=4+=r   c                     t          j        d          5  | j                            dt	          | j                            cd d d            S # 1 swxY w Y   d S Nr  r  z***)r;   r  rB  r@  r  r'  r5   s    r   r  z)MonotonicTransformedDistribution.__repr__  s    _r*** 	G 	G%--eT$*5E5EFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G   -AAAc                     t          j        d          5  | j                            dt	          | j                            cd d d            S # 1 swxY w Y   d S rE  )r;   r  rC  r@  r   r'  r5   s    r   r6   z(MonotonicTransformedDistribution.__str__  s    _r*** 	E 	E$,,UC
OODD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	ErF  c                     dS rZ  r>   r[  s     r   r  z+MonotonicTransformedDistribution._overrides      ur   c                     | j         j        di |\  }}t          j        |dk    t          j        d|j                  |          }t          j        d          5  | j        r6|                     |          |                     |          fcd d d            S |                     |          |                     |          fcd d d            S # 1 swxY w Y   d S )Nr   z-0r   rg  rh  r>   )	r'  rv  r;   r   rE   r   rj  rA  r5  )r,   r  rH   rI   s       r   rv  z)MonotonicTransformedDistribution._support  s,   "tz",,V,,1HQT2:d!':::A>>[))) 	. 	. .wwqzz4771::-	. 	. 	. 	. 	. 	. 	. 	. wwqzz4771::-		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   1C)CCCc                      | j         j        |                     |          g|R i ||                     |          z   S r   )r'  r  r6  r8  r,   r   r  r  s       r   r  z1MonotonicTransformedDistribution._logpdf_dispatch  sA    *tz*4771::GGGGGG$++VW..XXr   c                      | j         j        |                     |          g|R i ||                     |          z  S r   )r'  r  r6  r7  rL  s       r   r  z.MonotonicTransformedDistribution._pdf_dispatch  sA    'tz'

DTDDDVDDtxxPQ{{RRr   c                 J     | j         |                     |          g|R i |S r   )r=  r6  rL  s       r   r  z1MonotonicTransformedDistribution._logcdf_dispatch  s.    t|DGGAJJ8888888r   c                 J     | j         |                     |          g|R i |S r   )r9  r6  rL  s       r   r-  z.MonotonicTransformedDistribution._cdf_dispatch  s.    ty5d555f555r   c                 J     | j         |                     |          g|R i |S r   )r>  r6  rL  s       r   r  z2MonotonicTransformedDistribution._logccdf_dispatch  s.    t}TWWQZZ9$999&999r   c                 J     | j         |                     |          g|R i |S r   )r:  r6  rL  s       r   rD  z/MonotonicTransformedDistribution._ccdf_dispatch  s.    tz$''!**6t666v666r   c                 J    |                       | j        |g|R i |          S r   )r5  r?  r,   r  r  r  s       r   r  z2MonotonicTransformedDistribution._ilogcdf_dispatch  s0    ww}t}Q888888999r   c                 J    |                       | j        |g|R i |          S r   )r5  r;  rS  s       r   r  z/MonotonicTransformedDistribution._icdf_dispatch  s0    wwztz!5d555f55666r   c                 J    |                       | j        |g|R i |          S r   )r5  r@  rS  s       r   r  z3MonotonicTransformedDistribution._ilogccdf_dispatch  s0    ww~t~a9$999&99:::r   c                 J    |                       | j        |g|R i |          S r   )r5  r<  rS  s       r   r  z0MonotonicTransformedDistribution._iccdf_dispatch  s0    ww{t{16t666v66777r   c                Z     | j         j        ||f||d|}|                     |          S Nr  )r'  r=  r5  r,   r  r  r9  r   r  r  s          r   r=  z1MonotonicTransformedDistribution._sample_dispatch  sI    )dj)*H-3H H@FH Hwws||r   )r7   r8   r9   r:   rK   r  r6   r  rv  r  r  r  r-  r  rD  r  r  r  r  r=  rd   re   s   @r   rC  rC  X  sG       # #J 59 t!> > > > > > >>G G GE E E  . . .Y Y YS S S9 9 96 6 6: : :7 7 7: : :7 7 7; ; ;8 8 8      r   rC  c                        e Zd ZdZ fdZd Zd ZdddZdddZddd	Z	ddd
Z
dddZdddZd Zd Zd Z xZS )r`  a  Distribution underlying the absolute value of a random variable

    Given a random variable :math:`X`; define the distribution
    underlying the random variable :math:`Y = |X|`.

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X`.

    Returns
    -------
    Y : `ContinuousDistribution`
        The random variable :math:`Y = |X|`

    c                     t                      j        |g|R i | d| j        j        j        d         f| j        j        _        d S )NTrq   )rC   rK   r
  r   rG   )r,   r  r  r  rJ   s       r   rK   zFoldedDistribution.__init__  sR    ,T,,,V,,,
 ,01F1PQR1S*T'''r   c                     dS rZ  r>   r[  s     r   r  zFoldedDistribution._overrides  rI  r   c                 <    | j         j        di |\  }}t          j        |          t          j        |          }}t          j        ||          t          j        ||          }}|dk     |dk    z  }t          j        |          }d||<   |d         |d         fS r  )r'  rv  r;   r!   r  r  rE   )r,   r  rH   rI   a_b_r   s          r   rv  zFoldedDistribution._support  s    "tz",,V,,1BF1IIBB##RZB%7%7BUq1uZ^^1"vr"v~r   Nr8  c                   t          j        |          } | j        j        |g|R d|i|} | j        j        | g|R d|i|}t          j        |          }t          j        |          } | j        j        di |\  }}t           j         || |k     <   t           j         |||k    <   t          j        ||g          }	t          j	        |	d          S )Nr9  r   r   r>   )
r;   r!   r'  r  rE   rv  r   r  r   rp  )
r,   r   r9  r  r  ro   rn   rH   rI   logpdfss
             r   r  z#FoldedDistribution._logpdf_dispatch  s    F1II+
+ANNNNVNvNN*tz*A2NNNNVNvNNz$
5!!"tz",,V,,1waR!Vwa!e(D%=)) q1111r   c                6   t          j        |          } | j        j        |g|R d|i|} | j        j        | g|R d|i|}t          j        |          }t          j        |          } | j        j        di |\  }}d|| |k     <   d|||k    <   ||z   S )Nr9  r   r>   )r;   r!   r'  r  rE   rv  )	r,   r   r9  r  r  ro   rn   rH   rI   s	            r   r  z FoldedDistribution._pdf_dispatch  s    F1II(
(KTKKK&KFKK'tz'KTKKK&KFKKz$
5!!"tz",,V,,1aR!Va!ee|r   c                    t          j        |          } | j        j        di |\  }}t          j        | |          }t          j        ||          } | j        j        ||g|R d|i|j        S r  )r;   r!   r'  rv  r  r  r  r  	r,   r   r9  r  r  rH   rI   r  r  s	            r   r  z#FoldedDistribution._logcdf_dispatch  s    F1II"tz",,V,,1ZAZ1+tz+BSTSSS&SFSSXXr   c                    t          j        |          } | j        j        di |\  }}t          j        | |          }t          j        ||          } | j        j        ||g|R i |S rB   )r;   r!   r'  rv  r  r  r  rd  s	            r   r-  z FoldedDistribution._cdf_dispatch  sx    F1II"tz",,V,,1ZAZ1(tz(RA$AAA&AAAr   c                    t          j        |          } | j        j        di |\  }}t          j        | |          }t          j        ||          } | j        j        ||g|R d|i|j        S r  )r;   r!   r'  rv  r  r  re  r  rd  s	            r   r  z$FoldedDistribution._logccdf_dispatch&  s    F1II"tz",,V,,1ZAZ1,tz,R 7d 7 7 76 7/57 77;	<r   c                    t          j        |          } | j        j        di |\  }}t          j        | |          }t          j        ||          } | j        j        ||g|R d|i|S r  )r;   r!   r'  rv  r  r  rz  rd  s	            r   rD  z!FoldedDistribution._ccdf_dispatch.  s}    F1II"tz",,V,,1ZAZ1)tz)"bQ4QQQQ&QQQr   c                X     | j         j        ||f||d|}t          j        |          S rX  )r'  r=  r;   r!   rY  s          r   r=  z#FoldedDistribution._sample_dispatch5  sG    )dj)*H-3H H@FH Hvc{{r   c                     t          j        d          5  dt          | j                   dcd d d            S # 1 swxY w Y   d S Nr  r  zabs(rl   )r;   r  r  r'  r5   s    r   r  zFoldedDistribution.__repr__;  s    _r*** 	. 	.-$tz**---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.   ;??c                     t          j        d          5  dt          | j                   dcd d d            S # 1 swxY w Y   d S rj  )r;   r  r   r'  r5   s    r   r6   zFoldedDistribution.__str__?  s    _r*** 	- 	-,#dj//,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-rk  )r7   r8   r9   r:   rK   r  rv  r  r  r  r-  r  rD  r=  r  r6   rd   re   s   @r   r`  r`    sU        &U U U U U     15 
2 
2 
2 
2 
2 .2 	 	 	 	 	 15 Y Y Y Y Y .2 B B B B B 26 < < < < < /3 R R R R R  . . .- - - - - - -r   r`  c                     t          |           S )aN  Absolute value of a random variable

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X`.

    Returns
    -------
    Y : `ContinuousDistribution`
        A random variable :math:`Y = |X|`.

    Examples
    --------
    Suppose we have a normally distributed random variable :math:`X`:

    >>> import numpy as np
    >>> from scipy import stats
    >>> X = stats.Normal()

    We wish to have a random variable :math:`Y` distributed according to
    the folded normal distribution; that is, a random variable :math:`|X|`.

    >>> Y = stats.abs(X)

    The PDF of the distribution in the left half plane is "folded" over to
    the right half plane. Because the normal PDF is symmetric, the resulting
    PDF is zero for negative arguments and doubled for positive arguments.

    >>> import matplotlib.pyplot as plt
    >>> x = np.linspace(0, 5, 300)
    >>> ax = plt.gca()
    >>> Y.plot(x='x', y='pdf', t=('x', -1, 5), ax=ax)
    >>> plt.plot(x, 2 * X.pdf(x), '--')
    >>> plt.legend(('PDF of `Y`', 'Doubled PDF of `X`'))
    >>> plt.show()

    r_  r  s    r   r!   r!   D  s    N a   r   c                V    t          | t          j        t          j        d d           S )a  Natural exponential of a random variable

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X`.

    Returns
    -------
    Y : `ContinuousDistribution`
        A random variable :math:`Y = \exp(X)`.

    Examples
    --------
    Suppose we have a normally distributed random variable :math:`X`:

    >>> import numpy as np
    >>> from scipy import stats
    >>> X = stats.Normal()

    We wish to have a lognormally distributed random variable :math:`Y`,
    a random variable whose natural logarithm is :math:`X`.
    If :math:`X` is to be the natural logarithm of :math:`Y`, then we
    must take :math:`Y` to be the natural exponential of :math:`X`.

    >>> Y = stats.exp(X)

    To demonstrate that ``X`` represents the logarithm of ``Y``,
    we plot a normalized histogram of the logarithm of observations of
    ``Y`` against the PDF underlying ``X``.

    >>> import matplotlib.pyplot as plt
    >>> rng = np.random.default_rng(435383595582522)
    >>> y = Y.sample(shape=10000, rng=rng)
    >>> ax = plt.gca()
    >>> ax.hist(np.log(y), bins=50, density=True)
    >>> X.plot(ax=ax)
    >>> plt.legend(('PDF of `X`', 'histogram of `log(y)`'))
    >>> plt.show()

    c                     d| z  S r8  r>   rQ  s    r   r  zexp.<locals>.<lambda>  s    PQTUPU r   c                 ,    t          j        |            S r   rZ  rQ  s    r   r  zexp.<locals>.<lambda>  s    RVAYYJ r   r<  r?  r@  r2  )rC  r;   r"   r#   rn  s    r   r"   r"   n  s4    T ,A26oo2F2FH H H Hr   c                    t          j        |                                 d         dk               rd}t          |          t	          | t           j        t           j        t           j        d           S )a!  Natural logarithm of a non-negative random variable

    Parameters
    ----------
    X : `ContinuousDistribution`
        The random variable :math:`X` with positive support.

    Returns
    -------
    Y : `ContinuousDistribution`
        A random variable :math:`Y = \exp(X)`.

    Examples
    --------
    Suppose we have a gamma distributed random variable :math:`X`:

    >>> import numpy as np
    >>> from scipy import stats
    >>> Gamma = stats.make_distribution(stats.gamma)
    >>> X = Gamma(a=1.0)

    We wish to have a exp-gamma distributed random variable :math:`Y`,
    a random variable whose natural exponential is :math:`X`.
    If :math:`X` is to be the natural exponential of :math:`Y`, then we
    must take :math:`Y` to be the natural logarithm of :math:`X`.

    >>> Y = stats.log(X)

    To demonstrate that ``X`` represents the exponential of ``Y``,
    we plot a normalized histogram of the exponential of observations of
    ``Y`` against the PDF underlying ``X``.

    >>> import matplotlib.pyplot as plt
    >>> rng = np.random.default_rng(435383595582522)
    >>> y = Y.sample(shape=10000, rng=rng)
    >>> ax = plt.gca()
    >>> ax.hist(np.exp(y), bins=50, density=True)
    >>> X.plot(ax=ax)
    >>> plt.legend(('PDF of `X`', 'histogram of `exp(y)`'))
    >>> plt.show()

    r   zXThe logarithm of a random variable is only implemented when the support is non-negative.c                     | S r   r>   rQ  s    r   r  zlog.<locals>.<lambda>  s    A r   rr  )r;   r  r	  r*   rC  r#   r"   )r  r]   s     r   r#   r#     sg    V 
vaiikk!nq !! +.!'***+A26bf2=+? ? ? ?r   rp  )Kr+  abcr   r   r   r  numpyr;   r   scipy._lib._utilr   r   scipy._lib._docscraper	   r
   scipyr   r   scipy.integrater   rx  scipy.optimize._bracketr   r   scipy.optimize._chandrupatlar   r   %scipy.stats._probability_distributionr   scipy.statsr   r   r]  r   __all__r  rC  r'   r@   rg   r   r   r   r   r-  r6  rH  rV  r[  ra  rm  rS  rw  r}  r  r  r  r  r  r   r/  r7  r;  r=  rJ  r    r&  r  r   r   rC  r`  r!   r"   r#   r>   r   r   <module>r     s	       # # # # # # # # % % % % % %            3 3 3 3 3 3 3 3 : : : : : : : :                 1 1 1 1 1 1 C C C C C C C C N N N N N N N N J J J J J J       	* * *, , , 		h-$ -$ -$ -$ -$c -$ -$ -$`D" D" D" D" D"G D" D" D"NY Y Y Y Y- Y Y Yx	" 	" 	" 	" 	"] 	" 	" 	"a$ a$ a$ a$ a$ a$ a$ a$H7) 7) 7) 7) 7)Z 7) 7) 7)tG  G  G  G  G  G  G  G TG G GT) ) )X& & &RB B BJ     ,&5 &5 &5R  &  &     , 48     ,  :p p pfs s s s s5 s s sh>IWII LI =	I
 mI I ]I .I %I "I =I .I I (I "I  $!I" (#I I$ +%I& ''I( ")I* 0+I, /-I. /I0 !1I2 ,3I4 N5I6 7I8 *9I: #;I< =I> !?I@ "AIB CID "EI I IF GIH IIJ \KIL \MIN 'OIP -QIR jSIT <UIV 
WIX ,YIZ {[I\ ,]I^ "_I` 
=aIb HcId +eIf 
gI I Ih iIj SkIl 0mIn 0oIp ,qIr NsIt ,uIv wIx +yIz {I| }I~ lI@ (AIB "CID $EIF *GIH =II IJ $%%QI I I Zp p p~     *  (/$ /$ /$ /$ /$4 /$ /$ /$dS> S> S> S> S>3 S> S> S>l F7rv =2 =2 =2 =2@FC FC FC FC FC 7 FC FC FCRJ) J) J) J) J)!8 J) J) J)ZJ3 J3 J3ZC C C C C& C C CL
} } } } }'> } } }@i- i- i- i- i-0 i- i- i-X'! '! '!T+H +H +H\0? 0? 0? 0? 0?r   