a
    <b                     @   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r0t||S t|trDt||S t|tsZt	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   g/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/stats/compound_rv.pyr      s    






zCompoundPSpace.__new__c                 C   s   t | j| S N)r   symbolselfr   r   r    value$   s    zCompoundPSpace.valuec                 C   s
   | j d S Nr   argsr#   r   r   r    r"   (   s    zCompoundPSpace.symbolc                 C   s   | j jS r!   )r   is_Continuousr#   r   r   r    r)   ,   s    zCompoundPSpace.is_Continuousc                 C   s   | j jS r!   )r   	is_Finiter#   r   r   r    r*   0   s    zCompoundPSpace.is_Finitec                 C   s   | j jS r!   )r   is_Discreter#   r   r   r    r+   4   s    zCompoundPSpace.is_Discretec                 C   s
   | j d S )N   r'   r#   r   r   r    r   8   s    zCompoundPSpace.distributionc                 C   s   | j | jS r!   )r   pdfr"   r#   r   r   r    r-   <   s    zCompoundPSpace.pdfc                 C   s   | j jS r!   )r   setr#   r   r   r    r.   @   s    zCompoundPSpace.setc                 C   s
   |   jS r!   )_get_newpspacedomainr#   r   r   r    r0   D   s    zCompoundPSpace.domainFc                 C   s\   t d}| jjd }t|| j||}| | j||}|d urD|S dt| }t|d S )Nxr   2Compound Distribution for %s is not implemeted yet)	r   r   r(   r   r-   _transform_pspacer"   strNotImplementedError)r$   evaluater1   Zparent_distfunc
new_pspacemessager   r   r    r/   H   s    zCompoundPSpace._get_newpspacec                    sv   t | | |j}t|tr.t|t |S t|trHt|t |S t|t	rr 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r-   r   r    
<dictcomp>^       z4CompoundPSpace._transform_pspace.<locals>.<dictcomp>N)r   r.   r   r   r   r   r   r   r   r   r   r   )r$   symdistr-   Z_setZdensr   r<   r    r3   R   s    


z CompoundPSpace._transform_pspaceT)compound_evaluatec                K   s.   |  |}|| j|ji}|j|fi |S r!   )r/   subsr%   compute_densityr$   exprrA   kwargsr8   r   r   r    rC   a   s    
zCompoundPSpace.compute_densityc                K   s.   |  |}|| j|ji}|j|fi |S r!   )r/   rB   r%   compute_cdfrD   r   r   r    rG   f   s    
zCompoundPSpace.compute_cdfNc                 K   sf   |  |}|| j|ji}|r2|| j|ji}t|trP|j||fi |S |j|||fi |S r!   )r/   rB   r%   r   r   compute_expectation)r$   rE   Zrvsr6   rF   r8   r   r   r    rH   k   s    

z"CompoundPSpace.compute_expectationc                K   s&   |  |}|| j|ji}||S r!   )r/   rB   r%   probabilityr$   	conditionrA   rF   r8   r   r   r    rI   t   s    
zCompoundPSpace.probabilityc                K   s&   |  |}|| j|ji}||S r!   )r/   rB   r%   conditional_spacerJ   r   r   r    rL   y   s    
z CompoundPSpace.conditional_space)F)NF)__name__
__module____qualname____doc__r   propertyr%   r"   r)   r*   r+   r   r-   r.   r0   r/   r3   rC   rG   rH   rI   rL   r   r   r   r    r      s6   











	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|| |s2|S t| |S )Nr2   )	r   r   r   r   r4   r5   _compound_checkr   r   )r   r@   r9   r   r   r    r      s    
zCompoundDistribution.__new__c                 C   s   | j d jS r&   )r(   r.   r#   r   r   r    r.      s    zCompoundDistribution.setc                 C   s   t | jd tS r&   )r   r(   r   r#   r   r   r    r)      s    z"CompoundDistribution.is_Continuousc                 C   s   t | jd tS r&   )r   r(   r   r#   r   r   r    r*      s    zCompoundDistribution.is_Finitec                 C   s   t | jd tS r&   )r   r(   r   r#   r   r   r    r+      s    z CompoundDistribution.is_DiscreteFc                 C   sv   | j d }dd |j D }t|tr>tdddd}||}ntd}||}|D ]}| |||}qTt|||S )Nr   c                 S   s   g | ]}t |r|qS r   )r   )r:   rvr   r   r    
<listcomp>   r>   z,CompoundDistribution.pdf.<locals>.<listcomp>yTF)integernegative)r(   r   r   r   pmfr-   _marginaliser   )r$   r1   r6   r@   randomsrU   rE   rS   r   r   r    r-      s    


zCompoundDistribution.pdfc                 C   s   t |jjtr|jj|}n|jj|}|jjj}|jjsF|jj	r`t
|| ||j|jf}nt|| ||j|jf}|r| S |S r!   )r   Zpspacer   r   rX   r-   r0   r.   r+   r*   r   _infZ_supr   Zdoit)r$   rE   rS   r6   Zrv_densZrv_domr   r   r    rY      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$   r@   rZ   argr   r   r    rR      s    
z$CompoundDistribution._compound_checkN)F)rM   rN   rO   rP   r   rQ   r.   r)   r*   r+   r-   rY   classmethodrR   r   r   r   r    r      s   	




r   N)#Zsympy.concrete.summationsr   Zsympy.core.basicr   Zsympy.core.functionr   Zsympy.core.symbolr   Zsympy.integrals.integralsr   Zsympy.stats.rvr   r   r	   r
   r   r   r   Zsympy.stats.crvr   r   Zsympy.stats.drvr   r   Zsympy.stats.frvr   r   Zsympy.stats.crv_typesr   Zsympy.stats.drv_typesr   Zsympy.stats.frv_typesr   r   r   r   r   r   r    <module>   s   $o