a
    Oˆc�  ã                   @   s†   d Z ddlZddlmZ g d¢Zedƒddd„ƒZedƒdd
d„ƒZdd„ Zedƒddd„ƒZ	edƒddd„ƒZ
ed	ƒddd„ƒZdS )zS
Utilities for generating random numbers, random sequences, and
random selections.
é    N)Úpy_random_state)Úpowerlaw_sequenceÚzipf_rvÚcumulative_distributionÚdiscrete_sequenceÚrandom_weighted_sampleÚweighted_choiceé   ç       @c                    s   ‡ ‡fdd„t | ƒD ƒS )zK
    Return sample sequence of length n from a power law distribution.
    c                    s   g | ]}ˆ  ˆ d  ¡‘qS ©é   )Úparetovariate©Ú.0Úi©ÚexponentÚseed© úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/networkx/utils/random_sequence.pyÚ
<listcomp>   ó    z%powerlaw_sequence.<locals>.<listcomp>)Úrange)Únr   r   r   r   r   r      s    r   r   c           	      C   s’   |dk rt dƒ‚| dkr t dƒ‚| d }d| }d| ¡  }| ¡ }t||d|    ƒ}dd|  | }|| |d  |d  || kr0qŽq0|S )aw  Returns a random value chosen from the Zipf distribution.

    The return value is an integer drawn from the probability distribution

    .. math::

        p(x)=\frac{x^{-\alpha}}{\zeta(\alpha, x_{\min})},

    where $\zeta(\alpha, x_{\min})$ is the Hurwitz zeta function.

    Parameters
    ----------
    alpha : float
      Exponent value of the distribution
    xmin : int
      Minimum value
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    x : int
      Random value from Zipf distribution

    Raises
    ------
    ValueError:
      If xmin < 1 or
      If alpha <= 1

    Notes
    -----
    The rejection algorithm generates random values for a the power-law
    distribution in uniformly bounded expected time dependent on
    parameters.  See [1]_ for details on its operation.

    Examples
    --------
    >>> nx.utils.zipf_rv(alpha=2, xmin=3, seed=42)
    8

    References
    ----------
    .. [1] Luc Devroye, Non-Uniform Random Variate Generation,
       Springer-Verlag, New York, 1986.
    r   zxmin < 1za <= 1.0g      ð?r	   )Ú
ValueErrorÚrandomÚint)	ÚalphaZxminr   Za1ÚbÚuÚvÚxÚtr   r   r   r       s    1 r   c                 C   s@   dg}t | ƒ}tdt| ƒƒD ]}| || | | |  ¡ q|S )zFReturns normalized cumulative distribution from discrete distribution.g        r   )Úsumr   ÚlenÚappend)ÚdistributionÚcdfZpsumr   r   r   r   r   a   s
    r   é   c                    s`   ddl ‰ |dur|‰n|dur(t|ƒ‰n
t d¡‚‡fdd„t| ƒD ƒ}‡ ‡fdd„|D ƒ}|S )a#  
    Return sample sequence of length n from a given discrete distribution
    or discrete cumulative distribution.

    One of the following must be specified.

    distribution = histogram of values, will be normalized

    cdistribution = normalized discrete cumulative distribution

    r   Nz8discrete_sequence: distribution or cdistribution missingc                    s   g | ]}ˆ   ¡ ‘qS r   )r   r   )r   r   r   r   „   r   z%discrete_sequence.<locals>.<listcomp>c                    s   g | ]}ˆ   ˆ|¡d  ‘qS r   )Úbisect_left)r   Ús)Úbisectr'   r   r   r   ‡   r   )r+   r   ÚnxZNetworkXErrorr   )r   r&   Zcdistributionr   ZinputseqÚseqr   )r+   r'   r   r   r   k   s    
ÿr   c                 C   s@   |t | ƒkrtdƒ‚tƒ }t |ƒ|k r8| t| |ƒ¡ qt|ƒS )z€Returns k items without replacement from a weighted sample.

    The input is a dictionary of items with weights as values.
    zsample larger than population)r$   r   ÚsetÚaddr   Úlist)ÚmappingÚkr   Úsampler   r   r   r   ‹   s    r   c                 C   sB   |  ¡ t|  ¡ ƒ }|  ¡ D ] \}}||8 }|dk r|  S qdS )zuReturns a single element from a weighted sample.

    The input is a dictionary of items with weights as values.
    r   N)r   r#   ÚvaluesÚitems)r1   r   Zrndr2   Úwr   r   r   r   ™   s
    r   )r
   N)r   N)NNN)N)N)Ú__doc__Znetworkxr,   Znetworkx.utilsr   Ú__all__r   r   r   r   r   r   r   r   r   r   Ú<module>   s   @
