
    zj$                        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ej                  ZdS )    )annotationsN)Sequence)TYPE_CHECKING)
check_type)Variable)Gammadistribution)in_dynamic_mode)Tensordtypec                       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ZddZedd            Zedd            Z	g fddZ
ddZddZddZ x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                
   t                      st          |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           ||nd| _        |                     |||          \  | _	        | _
        | _        |                     | j	                  st          d          |                     | j                  st          d          | j	        j        }t                                          |           t#          d| j	        z  t	          j        | 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   paddlepirValuer   _broadcast_allr   r   r   _check_nonnegative
ValueErrorshapesuper__init__r   	full_like_chi2)selfr   r   r   r   batch_shape	__class__s         m/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/student_t.pyr#   zStudentT.__init__b   s       	J$
 	 	 	 J$
 	 	 	 J$
 	 	 	 !,DD*	(,(;(;BU(K(K%4:&&tw// 	N   &&tz22 	Q   gm%%%3=&*:47C*H*HII


    valueboolc                2    |dk                                     S )zCheck the non-negative constraint for input parameters

        Args:
            value (Tensor)

        Returns:
            bool: pass or not.
        g        )allr&   r+   s     r)   r   zStudentT._check_nonnegative   s     !!###r*   c           
         t          j        | j        dk    | j        t          j        | j        t          d                              S )zYMean of StudentT distribution.

        Returns:
            Tensor: mean value.
              ?nan
fill_value)r   wherer   r   r$   r   )r&   s    r)   meanzStudentT.mean   sA     |GcMHTX%,,???
 
 	
r*   c           
        | j                                                                         }| j         dk    }t          j        || j                            d          |z  |dz
  z  t          j        |t          d                              }| j         dk    	                    | j         dk              }t          j        |t          j        |t          d                    |          }|S )zaVariance of StudentT distribution.

        Returns:
            Tensor: variance value.
               @   r2   r3   r1   inf)
r   clonedetachr   r5   r   powr$   r   logical_and)r&   varvar_conditioninf_conditions       r)   variancezStudentT.variance   s     gmmoo$$&&#lJNN1#sQw/SU5\\:::
 

 C44TWs]CCl6+CE%LLIII3
 
 
r*   r!   Sequence[int]c                4   t          |t                    st          d          |                     |          }t	          j        |          }| j                            |          }|t	          j        || j	        z            z  }| j
        | j        |z  z   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   	TypeError_extend_shaper   normalr%   samplersqrtr   r   r   )r&   r!   output_shapezchi2xs         r)   rI   zStudentT.sample   s     %** 	ECDDD))%00M---z  ''TDG^,,,x$*q.((r*   c                   t          j        d| j        z            t          j        d          z   t          j        d| j        dz   z            z
  }| j                                        d| j        dz   z  t          j        d| j        dz   z            t          j        d| j        z            z
  z  z   d| j                                        z  z   |z   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   logdigamma)r&   lbetas     r)   entropyzStudentT.entropy   s    & M#-((k#mC47Q;/001 	 JNNw{ sdgk233.tw//0 DGKKMM!" 
	
r*   c                   |                      | j        |          }|| j        z
  | j        z  }| j                                        d| j                                        z  z   dt          j        t
          j                  z  z   t          j        d| j        z            z   t          j        d| j        dz   z            z
  }d| j        dz   z  t          j	        |dz  | j        z            z  |z
  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   r1   g      r8   )
_check_values_dtype_in_probsr   r   r   rS   rR   pir   rQ   log1p)r&   r+   yZs       r)   log_probzStudentT.log_prob   s     11$'5AATX+JNNDGKKMM!"DHTW%%%& mC$'M**+ mC47S=122	3 	
 tw}%QVdg5E(F(FFJJr*   c                P    t          j        |                     |                    S )zProbability density function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: probability density. The data type is the same as `df`.
        )r   expr]   r/   s     r)   probzStudentT.prob  s      z$--..///r*   )N)
r   r   r   r   r   r   r   r   r   r   )r+   r   r   r,   )r   r   )r!   rC   r   r   )r+   r   r   r   )__name__
__module____qualname____doc____annotations__r#   r   propertyr6   rB   rI   rV   r]   r`   __classcell__)r(   s   @r)   r   r      s>        ; ;z JJJKKKMMMIIILLL  5J 5J 5J 5J 5J 5J 5Jn	$ 	$ 	$ 	$ 

 

 

 X

    X& -/ ) ) ) ) )$!
 !
 !
 !
FK K K K(	0 	0 	0 	0 	0 	0 	0 	0r*   r   )
__future__r   rR   collections.abcr   typingr   r   paddle.base.data_feederr   paddle.base.frameworkr   paddle.distributionr   r	   paddle.frameworkr
   r   r   Distributionr    r*   r)   <module>rq      s    # " " " " "  $ $ $ $ $ $              . . . . . . * * * * * * 3 3 3 3 3 3 3 3 , , , , , , %$$$$$$$$y0 y0 y0 y0 y0|( y0 y0 y0 y0 y0r*   