
    sg                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZmZ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 d dlm Z   G d de      Z! G d dee      Z"y)    )Sum)Basic)Lambda)Dummy)Integral)NamedArgsMixinrandom_symbols_symbol_converterPSpaceRandomSymbol	is_randomDistribution)ContinuousDistributionSingleContinuousPSpace)DiscreteDistributionSingleDiscretePSpace)SingleFiniteDistributionSingleFinitePSpace)ContinuousDistributionHandmade)DiscreteDistributionHandmade)FiniteDistributionHandmadec                       e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        ZddZd ZdddZdddZddZdddZdddZy)CompoundPSpacez
    A temporary Probability Space for the Compound Distribution. After
    Marginalization, this returns the corresponding Probability Space of the
    parent distribution.
    c                 *   t        |      }t        |t              rt        ||      S t        |t              rt        ||      S t        |t              rt        ||      S t        |t              st        d|z        t        j                  | ||      S )Nz2%s should be an isinstance of CompoundDistribution)r
   
isinstancer   r   r   r   r   r   CompoundDistribution
ValueErrorr   __new__)clssdistributions      J/var/www/html/venv/lib/python3.12/site-packages/sympy/stats/compound_rv.pyr   zCompoundPSpace.__new__   s    a l$:;)!\::l$89'<88l$<=%a66,(<= /0<> ? ?}}S!\22    c                 .    t        | j                  |       S N)r   symbolselfs    r"   valuezCompoundPSpace.value$   s    DKK..r#   c                      | j                   d   S Nr   argsr'   s    r"   r&   zCompoundPSpace.symbol(       yy|r#   c                 .    | j                   j                  S r%   )r!   is_Continuousr'   s    r"   r0   zCompoundPSpace.is_Continuous,   s      ...r#   c                 .    | j                   j                  S r%   )r!   	is_Finiter'   s    r"   r2   zCompoundPSpace.is_Finite0   s      ***r#   c                 .    | j                   j                  S r%   )r!   is_Discreter'   s    r"   r4   zCompoundPSpace.is_Discrete4   s      ,,,r#   c                      | j                   d   S )N   r,   r'   s    r"   r!   zCompoundPSpace.distribution8   r.   r#   c                 L    | j                   j                  | j                        S r%   )r!   pdfr&   r'   s    r"   r8   zCompoundPSpace.pdf<   s      $$T[[11r#   c                 .    | j                   j                  S r%   )r!   setr'   s    r"   r:   zCompoundPSpace.set@   s      $$$r#   c                 6    | j                         j                  S r%   )_get_newpspacedomainr'   s    r"   r=   zCompoundPSpace.domainD   s    ""$+++r#   c                 
   t        d      }| j                  j                  d   }t        || j                  j	                  ||            }| j                  | j                  ||      }||S dt        |      z  }t        |      )Nxr   3Compound Distribution for %s is not implemented yet)	r   r!   r-   r   r8   _transform_pspacer&   strNotImplementedError)r(   evaluater?   parent_distfunc
new_pspacemessages          r"   r<   zCompoundPSpace._get_newpspaceH   s~    #J'',,Q/a**..q(;<++DKKdK
!H3{K[[!'**r#   c                 X   t        | ||            }|j                  }t        |t              rt	        |t        ||            S t        |t              rt        |t        ||            S t        |t              r+|D ci c]  }| ||       }}t        |t        |            S yc c}w )z
        This function returns the new pspace of the distribution using handmade
        Distributions and their corresponding pspace.
        N)r   r:   r   r   r   r   r   r   r   r   r   r   )r(   symdistr8   _setkdenss          r"   rA   z CompoundPSpace._transform_pspaceR   s    
 S#c(#xxd23)#/McSW/XYY23'-I#t-TUU67'+,!As1vI,D,%c+Ed+KLL 8,s   ?B'T)compound_evaluatec                    | j                  |      }|j                  | j                  |j                  i      } |j                  |fi |S r%   )r<   subsr)   compute_densityr(   exprrO   kwargsrG   s        r"   rR   zCompoundPSpace.compute_densitya   sH    (():;
yy$**j&6&678)z))$9&99r#   c                    | j                  |      }|j                  | j                  |j                  i      } |j                  |fi |S r%   )r<   rQ   r)   compute_cdfrS   s        r"   rW   zCompoundPSpace.compute_cdff   sH    (():;
yy$**j&6&678%z%%d5f55r#   Nc                 6   | j                  |      }|j                  | j                  |j                  i      }|r'|j                  | j                  |j                  i      }t        |t              r |j
                  ||fi |S  |j
                  |||fi |S r%   )r<   rQ   r)   r   r   compute_expectation)r(   rT   rvsrD   rU   rG   s         r"   rY   z"CompoundPSpace.compute_expectationk   s    ((2
yy$**j&6&678((DJJ
(8(89:Cj"451:11$FvFF-z--dCLVLLr#   c                    | j                  |      }|j                  | j                  |j                  i      }|j                  |      S r%   )r<   rQ   r)   probabilityr(   	conditionrO   rU   rG   s        r"   r\   zCompoundPSpace.probabilityt   sA    (():;
NNDJJ
0@0@#AB	%%i00r#   c                    | j                  |      }|j                  | j                  |j                  i      }|j                  |      S r%   )r<   rQ   r)   conditional_spacer]   s        r"   r`   z CompoundPSpace.conditional_spacey   sA    (():;
NNDJJ
0@0@#AB	++I66r#   F)NF)__name__
__module____qualname____doc__r   propertyr)   r&   r0   r2   r4   r!   r8   r:   r=   r<   rA   rR   rW   rY   r\   r`    r#   r"   r   r      s    3 / /   / / + + - -   2 2 % % , ,+M :> :
 6: 6
M ;? 1
 AE 7r#   r   c                   t    e Zd ZdZd Zed        Zed        Zed        Zed        Z	ddZ
d Zed	        Zy
)r   a  
    Class for Compound Distributions.

    Parameters
    ==========

    dist : Distribution
        Distribution must contain a random parameter

    Examples
    ========

    >>> from sympy.stats.compound_rv import CompoundDistribution
    >>> from sympy.stats.crv_types import NormalDistribution
    >>> from sympy.stats import Normal
    >>> from sympy.abc import x
    >>> X = Normal('X', 2, 4)
    >>> N = NormalDistribution(X, 4)
    >>> C = CompoundDistribution(N)
    >>> C.set
    Interval(-oo, oo)
    >>> C.pdf(x, evaluate=True).simplify()
    exp(-x**2/64 + x/16 - 1/16)/(8*sqrt(pi))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Compound_probability_distribution

    c                     t        |t        t        t        f      sdt	        |      z  }t        |      | j                  |      s|S t        j                  | |      S )Nr@   )	r   r   r   r   rB   rC   _compound_checkr   r   )r   rK   rH   s      r"   r   zCompoundDistribution.__new__   sZ    $!7(*>!@ AKcRViWG%g..""4(K}}S$''r#   c                 4    | j                   d   j                  S r+   )r-   r:   r'   s    r"   r:   zCompoundDistribution.set   s    yy|r#   c                 <    t        | j                  d   t              S r+   )r   r-   r   r'   s    r"   r0   z"CompoundDistribution.is_Continuous   s    $))A,(>??r#   c                 <    t        | j                  d   t              S r+   )r   r-   r   r'   s    r"   r2   zCompoundDistribution.is_Finite   s    $))A,(@AAr#   c                 <    t        | j                  d   t              S r+   )r   r-   r   r'   s    r"   r4   z CompoundDistribution.is_Discrete   s    $))A,(<==r#   c                 d   | j                   d   }|j                   D cg c]  }t        |      s| }}t        |t              r t	        ddd      }|j                  |      }nt	        d      }|j                  |      }|D ]  }| j                  |||      }  t        ||      |      S c c}w )Nr   yTF)integernegative)	r-   r   r   r   r   pmfr8   _marginaliser   )r(   r?   rD   rK   rvrandomsrp   rT   s           r"   r8   zCompoundDistribution.pdf   s    yy| $		;"Yr]2;;d45c4%8A88A;Dc
A88A;D 	9B$$T2x8D	9vaq!! <s
   B-B-c                 :   t        |j                  j                  t              r&|j                  j                  j	                  |      }n%|j                  j                  j                  |      }|j                  j                  j                  }|j                  j                  s|j                  j                  r't        ||z  ||j                  |j                  f      }n&t        ||z  ||j                  |j                  f      }|r|j                         S |S r%   )r   pspacer!   r   rs   r8   r=   r:   r4   r2   r   _inf_supr   doit)r(   rT   ru   rD   rv_densrv_doms         r"   rt   z!CompoundDistribution._marginalise   s    bii,,.FGii,,004Gii,,004G!!%%99  BII$7$7tG|b&++KK&! "D DL2v{{KK+! "D99;r#   c                 |    g }|j                   D ]  }|j                  t        |              t        |      dk(  ryy)zN
        Checks if the given distribution contains random parameters.
        r   FT)r-   extendr	   len)r(   rK   rv   args       r"   rj   z$CompoundDistribution._compound_check   s?    
 99 	0CNN>#./	0w<1r#   Nra   )rb   rc   rd   re   r   rf   r:   r0   r2   r4   r8   rt   classmethodrj   rg   r#   r"   r   r      s~    >(     @ @ B B > >"  	 	r#   r   N)#sympy.concrete.summationsr   sympy.core.basicr   sympy.core.functionr   sympy.core.symbolr   sympy.integrals.integralsr   sympy.stats.rvr   r	   r
   r   r   r   r   sympy.stats.crvr   r   sympy.stats.drvr   r   sympy.stats.frvr   r   sympy.stats.crv_typesr   sympy.stats.drv_typesr   sympy.stats.frv_typesr   r   r   rg   r#   r"   <module>r      sW    ) " & # .G G G J F H @ > <l7V l7^`< `r#   