o
    * i$                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZmZ d dlmZ er>d d	lmZmZ G d
d dejZdS )    )annotationsN)Sequence)TYPE_CHECKING)
check_type)Variable)Gammadistribution)in_dynamic_mode)Tensordtypec                      s   e Zd ZU dZded< ded< ded< ded< ded< 		d"d# fddZd$ddZed%ddZed%ddZ	g fd&ddZ
d%ddZd'ddZd'd d!Z  ZS )(StudentTa1  
    The StudentT distribution with parameters: `df`, `loc`, `scale`.

    In probability theory and statistics, the StudentT distribution is one of the basic continuous probability distributions
    defined on the real number set.

    The probability density function (pdf) is

    .. math::

        pdf(x; \nu, \mu, \sigma) = \frac{\Gamma[(\nu+1)/2]}{\sigma\sqrt{\nu\pi}\Gamma(\nu/2)[1+(\frac{x-\mu}{\sigma})^2/\nu]^{(1+\nu)/2}}

    In the above equation:

    * :math:`df = \nu`: is the degree of freedom.
    * :math:`loc = \mu`: is the center parameter.
    * :math:`scale = \sigma`: is the scale parameter.
    * :math:`\Gamma(\cdot)`: is the gamma function.

    Args:
        df (float|Tensor): The degree of freedom of the distribution, which should be non-negative. If the input data type is float,
            the data type of `df` will be converted to a 1-D Tensor with paddle global default dtype. Supported dtype: float32, float64.
        loc (float|Tensor): The center of the distribution. If the input data type is float, the data type of `loc` will be converted to a
            1-D Tensor with paddle global default dtype. Supported dtype: float32, float64.
        scale (float|Tensor): The scale of the distribution, which should be non-negative. If the input data type is float, the data type
            of `scale` will be converted to a 1-D Tensor with paddle global default dtype. Supported dtype: float32, float64.
        name(str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import StudentT
            >>> paddle.set_device('cpu')
            >>> paddle.seed(100)
            >>> dist = StudentT(df=10.0, loc=0.0, scale=1.0)
            >>> dist.sample([3])
            Tensor(shape=[3, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[-2.07709980],
             [ 0.27981189],
             [ 0.00881413]])

            >>> dist2 = StudentT(df=paddle.to_tensor([10.0, 5.0]), loc=paddle.to_tensor([0.0, 0.0]), scale=paddle.to_tensor([1.0, 2.0]))
            >>> value_tensor = paddle.to_tensor([0.8], dtype="float32")
            >>> lp = dist2.log_prob(value_tensor)
            >>> print(lp)
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [-1.28509235, -1.75626254])

            >>> p = dist2.prob(value_tensor)
            >>> print(p)
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.27662504, 0.17268908])

            >>> entropy = dist2.entropy()
            >>> print(entropy)
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [1.52126312, 2.32064891])

    r
   dflocscalestrnamer   Nfloat | Tensor
str | NonereturnNonec                   s   t  s't|dtttjjfd t|dtttjjfd t|dtttjjfd |d ur-|nd| _| |||\| _	| _
| _| | j	sGtd| | jsQtd| j	j}t | td| j	 t| j	d| _d S )Nr   r   r   r   z<Every element of input parameter `df` should be nonnegative.z?Every element of input parameter `scale` should be nonnegative.      ?)r	   r   floatr   paddleZpirValuer   Z_broadcast_allr   r   r   _check_nonnegative
ValueErrorshapesuper__init__r   	full_like_chi2)selfr   r   r   r   Zbatch_shape	__class__ i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/student_t.pyr   b   sR   

 zStudentT.__init__valueboolc                 C  s   |dk  S )zCheck the non-negative constraint for input parameters

        Args:
            value (Tensor)

        Returns:
            bool: pass or not.
        g        )allr!   r&   r$   r$   r%   r      s   	zStudentT._check_nonnegativec                 C  s&   t | jdk| jt j| jtddS )zYMean of StudentT distribution.

        Returns:
            Tensor: mean value.
              ?nanZ
fill_value)r   wherer   r   r   r   )r!   r$   r$   r%   mean   s
   zStudentT.meanc                 C  s~   | j   }| j dk}t|| jd| |d  tj|tdd}| j dk	| j dk}t|tj|tdd|}|S )zaVariance of StudentT distribution.

        Returns:
            Tensor: variance value.
               @   r+   r,   r*   inf)
r   clonedetachr   r-   r   powr   r   logical_and)r!   varZvar_conditionZinf_conditionr$   r$   r%   variance   s   
zStudentT.variancer   Sequence[int]c                 C  sX   t |ts	td| |}tj|d}| j|}|t|| j	  }| j
| j|  S )a/  Generate StudentT samples of the specified shape. The final shape would be ``shape+batch_shape`` .

        Args:
            shape (Sequence[int], optional): Prepended shape of the generated samples.

        Returns:
            Tensor: Sampled data with shape `sample_shape` + `batch_shape`.
        z%sample shape must be Sequence object.)r   )
isinstancer   	TypeErrorZ_extend_shaper   normalr    sampleZrsqrtr   r   r   )r!   r   Zoutput_shapezZchi2xr$   r$   r%   r<      s   
	
zStudentT.samplec                 C  s|   t d| j td t d| jd   }| j d| jd  t d| jd  t d| j    d| j   | S )a  Shannon entropy in nats.

        The entropy is

        .. math::

            H = \log(\frac{\Gamma(\nu/2)\Gamma(1/2) \sigma \sqrt{\nu}}{\Gamma[(1+\nu)/2]}) + \frac{(1+\nu)}{2} \cdot \{\psi[(1+\nu)/2] - \psi(\nu/2)\}

        In the above equation:

        * :math:`\nu`: is the degree of freedom.
        * :math:`\Gamma()`: is the gamma function.
        * :math:`\psi()`: is the digamma function.

        Returns:
            Tensor: Shannon entropy of StudentT distribution. The data type is the same as `df`.
        r      )r   lgammar   mathr   logZdigamma)r!   Zlbetar$   r$   r%   entropy   s(   zStudentT.entropyc                 C  s   |  | j|}|| j | j }| j d| j   dttj  td| j  td| jd   }d| jd  t	|d | j  | S )zLog probability density function.

        Args:
          value (Tensor): The input tensor.

        Returns:
          Tensor: log probability density. The data type is the same as `df`.
        r   r*   g      r/   )
Z_check_values_dtype_in_probsr   r   r   rB   rA   pir   r@   log1p)r!   r&   yZr$   r$   r%   log_prob   s   	&zStudentT.log_probc                 C  s   t | |S )zProbability density function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: probability density. The data type is the same as `df`.
        )r   exprH   r)   r$   r$   r%   prob  s   	zStudentT.prob)N)
r   r   r   r   r   r   r   r   r   r   )r&   r
   r   r'   )r   r
   )r   r8   r   r
   )r&   r
   r   r
   )__name__
__module____qualname____doc____annotations__r   r   propertyr.   r7   r<   rC   rH   rJ   __classcell__r$   r$   r"   r%   r      s$   
 =
7

#r   )
__future__r   rA   collections.abcr   typingr   r   Zpaddle.base.data_feederr   Zpaddle.base.frameworkr   Zpaddle.distributionr   r   Zpaddle.frameworkr	   r
   r   Distributionr   r$   r$   r$   r%   <module>   s   