o
    oh                     @   s   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 deZ!G dd deeZ"dS )    )Sum)Basic)Lambda)Dummy)Integral)NamedArgsMixinrandom_symbols_symbol_converterPSpaceRandomSymbol	is_randomDistribution)ContinuousDistributionSingleContinuousPSpace)DiscreteDistributionSingleDiscretePSpace)SingleFiniteDistributionSingleFinitePSpace)ContinuousDistributionHandmade)DiscreteDistributionHandmade)FiniteDistributionHandmadec                   @   s   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zd(ddZdd ZddddZdddd Zd)d"d#Zddd$d%Zddd&d'Zd!S )*CompoundPSpacez
    A temporary Probability Space for the Compound Distribution. After
    Marginalization, this returns the corresponding Probability Space of the
    parent distribution.
    c                 C   sh   t |}t|trt||S t|trt||S t|tr"t||S t|ts-t	d| t
| ||S )Nz2%s should be an isinstance of CompoundDistribution)r	   
isinstancer   r   r   r   r   r   CompoundDistribution
ValueErrorr   __new__)clssdistribution r   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/stats/compound_rv.pyr      s   






zCompoundPSpace.__new__c                 C   s   t | j| S N)r   symbolselfr   r   r    value$      zCompoundPSpace.valuec                 C   
   | j d S Nr   argsr#   r   r   r    r"   (      
zCompoundPSpace.symbolc                 C      | j jS r!   )r   is_Continuousr#   r   r   r    r-   ,      zCompoundPSpace.is_Continuousc                 C   r,   r!   )r   	is_Finiter#   r   r   r    r/   0   r.   zCompoundPSpace.is_Finitec                 C   r,   r!   )r   is_Discreter#   r   r   r    r0   4   r.   zCompoundPSpace.is_Discretec                 C   r'   )N   r)   r#   r   r   r    r   8   r+   zCompoundPSpace.distributionc                 C   s   | j | jS r!   )r   pdfr"   r#   r   r   r    r2   <   s   zCompoundPSpace.pdfc                 C   r,   r!   )r   setr#   r   r   r    r3   @   r.   zCompoundPSpace.setc                 C   s
   |   jS r!   )_get_newpspacedomainr#   r   r   r    r5   D   r+   zCompoundPSpace.domainFc                 C   sX   t d}| jjd }t|| j||}| | j||}|d ur"|S dt| }t|)Nxr   3Compound Distribution for %s is not implemented yet)	r   r   r*   r   r2   _transform_pspacer"   strNotImplementedError)r$   evaluater6   parent_distfunc
new_pspacemessager   r   r    r4   H   s   zCompoundPSpace._get_newpspacec                    sv   t | | |j}t|trt|t |S t|tr$t|t |S t|t	r9 fdd|D }t
|t|S dS )z
        This function returns the new pspace of the distribution using handmade
        Distributions and their corresponding pspace.
        c                    s   i | ]}| |qS r   r   ).0kr2   r   r    
<dictcomp>^   s    z4CompoundPSpace._transform_pspace.<locals>.<dictcomp>N)r   r3   r   r   r   r   r   r   r   r   r   r   )r$   symdistr2   _setdensr   rB   r    r8   R   s   


z CompoundPSpace._transform_pspaceT)compound_evaluatec                K   .   |  |}|| j|ji}|j|fi |S r!   )r4   subsr%   compute_densityr$   exprrH   kwargsr>   r   r   r    rK   a      
zCompoundPSpace.compute_densityc                K   rI   r!   )r4   rJ   r%   compute_cdfrL   r   r   r    rP   f   rO   zCompoundPSpace.compute_cdfNc                 K   sf   |  |}|| j|ji}|r|| j|ji}t|tr(|j||fi |S |j|||fi |S r!   )r4   rJ   r%   r   r   compute_expectation)r$   rM   rvsr;   rN   r>   r   r   r    rQ   k   s   

z"CompoundPSpace.compute_expectationc                K   &   |  |}|| j|ji}||S r!   )r4   rJ   r%   probabilityr$   	conditionrH   rN   r>   r   r   r    rT   t      

zCompoundPSpace.probabilityc                K   rS   r!   )r4   rJ   r%   conditional_spacerU   r   r   r    rX   y   rW   z CompoundPSpace.conditional_spaceF)NF)__name__
__module____qualname____doc__r   propertyr%   r"   r-   r/   r0   r   r2   r3   r5   r4   r8   rK   rP   rQ   rT   rX   r   r   r   r    r      s8    











	r   c                   @   sf   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	dddZ
dd Zedd ZdS )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                 C   s>   t |tttfsdt| }t|| |s|S t| |S )Nr7   )	r   r   r   r   r9   r:   _compound_checkr   r   )r   rE   r?   r   r   r    r      s   
zCompoundDistribution.__new__c                 C   s   | j d jS r(   )r*   r3   r#   r   r   r    r3      r&   zCompoundDistribution.setc                 C      t | jd tS r(   )r   r*   r   r#   r   r   r    r-         z"CompoundDistribution.is_Continuousc                 C   r`   r(   )r   r*   r   r#   r   r   r    r/      ra   zCompoundDistribution.is_Finitec                 C   r`   r(   )r   r*   r   r#   r   r   r    r0      ra   z CompoundDistribution.is_DiscreteFc                 C   sv   | j d }dd |j D }t|trtdddd}||}n	td}||}|D ]	}| |||}q*t|||S )Nr   c                 S   s   g | ]}t |r|qS r   )r   )r@   rvr   r   r    
<listcomp>   s    z,CompoundDistribution.pdf.<locals>.<listcomp>yTF)integernegative)r*   r   r   r   pmfr2   _marginaliser   )r$   r6   r;   rE   randomsrd   rM   rb   r   r   r    r2      s   


zCompoundDistribution.pdfc                 C   s   t |jjtr|jj|}n|jj|}|jjj}|jjs#|jj	r0t
|| ||j|jf}nt|| ||j|jf}|rB| S |S r!   )r   pspacer   r   rg   r2   r5   r3   r0   r/   r   _inf_supr   doit)r$   rM   rb   r;   rv_densrv_domr   r   r    rh      s   
z!CompoundDistribution._marginalisec                 C   s2   g }|j D ]	}|t| qt|dkrdS dS )zN
        Checks if the given distribution contains random parameters.
        r   FT)r*   extendr   len)r$   rE   ri   argr   r   r    r_      s   
z$CompoundDistribution._compound_checkNrY   )rZ   r[   r\   r]   r   r^   r3   r-   r/   r0   r2   rh   classmethodr_   r   r   r   r    r      s    	




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   r   r   r   r    <module>   s    $o