ó
    Eñi"  ã                   ó\   • S SK r S SK Jr  S SKJrJr  S SKJr  S SKJr  S/r	 " S S\5      r
g)é    N)ÚTensor)ÚCategoricalÚconstraints)ÚMixtureSameFamilyConstraint)ÚDistributionÚMixtureSameFamilyc            	       óZ  ^ • \ rS rSr% Sr0 r\\\R                  4   \
S'   Sr SS\S\S\S-  S	S4U 4S
 jjjrSU 4S jjr\R"                  S 5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       rS rS r\R8                  " 5       4S jrS rS rS r Sr!U =r"$ )r   é   a;  
The `MixtureSameFamily` distribution implements a (batch of) mixture
distribution where all component are from different parameterizations of
the same distribution type. It is parameterized by a `Categorical`
"selecting distribution" (over `k` component) and a component
distribution, i.e., a `Distribution` with a rightmost batch shape
(equal to `[k]`) which indexes each (batch of) component.

Examples::

    >>> # xdoctest: +SKIP("undefined vars")
    >>> # Construct Gaussian Mixture Model in 1D consisting of 5 equally
    >>> # weighted normal distributions
    >>> mix = D.Categorical(torch.ones(5,))
    >>> comp = D.Normal(torch.randn(5,), torch.rand(5,))
    >>> gmm = MixtureSameFamily(mix, comp)

    >>> # Construct Gaussian Mixture Model in 2D consisting of 5 equally
    >>> # weighted bivariate normal distributions
    >>> mix = D.Categorical(torch.ones(5,))
    >>> comp = D.Independent(D.Normal(
    ...          torch.randn(5,2), torch.rand(5,2)), 1)
    >>> gmm = MixtureSameFamily(mix, comp)

    >>> # Construct a batch of 3 Gaussian Mixture Models in 2D each
    >>> # consisting of 5 random weighted bivariate normal distributions
    >>> mix = D.Categorical(torch.rand(3,5))
    >>> comp = D.Independent(D.Normal(
    ...         torch.randn(3,5,2), torch.rand(3,5,2)), 1)
    >>> gmm = MixtureSameFamily(mix, comp)

Args:
    mixture_distribution: `torch.distributions.Categorical`-like
        instance. Manages the probability of selecting component.
        The number of categories must match the rightmost batch
        dimension of the `component_distribution`. Must have either
        scalar `batch_shape` or `batch_shape` matching
        `component_distribution.batch_shape[:-1]`
    component_distribution: `torch.distributions.Distribution`-like
        instance. Right-most batch dimension indexes component.
Úarg_constraintsFNÚmixture_distributionÚcomponent_distributionÚvalidate_argsÚreturnc                 óè  >• Xl         X l        [        U R                   [        5      (       d  [	        S5      e[        U R                  [
        5      (       d  [	        S5      eU R                   R                  nU R                  R                  S S n[        [        U5      [        U5      5       H,  u  pgUS:w  d  M  US:w  d  M  Xg:w  d  M  [	        SU SU S35      e   U R                   R                  R                  S   nU R                  R                  S   n	Ub  U	b  X‰:w  a  [	        SU S	U	 S35      eX€l        U R                  R                  n
[        U
5      U l        [        TU ]A  UU
US
9  g )NzU The Mixture distribution needs to be an  instance of torch.distributions.CategoricalzUThe Component distribution need to be an instance of torch.distributions.Distributionéÿÿÿÿé   z$`mixture_distribution.batch_shape` (z>) is not compatible with `component_distribution.batch_shape`(Ú)z"`mixture_distribution component` (z;) does not equal `component_distribution.batch_shape[-1]` (©Úbatch_shapeÚevent_shaper   )Ú_mixture_distributionÚ_component_distributionÚ
isinstancer   Ú
ValueErrorr   r   ÚzipÚreversedÚlogitsÚshapeÚ_num_componentr   ÚlenÚ_event_ndimsÚsuperÚ__init__)Úselfr   r   r   ÚmdbsÚcdbsÚsize1Úsize2ÚkmÚkcr   Ú	__class__s              €Úd/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/distributions/mixture_same_family.pyr#   ÚMixtureSameFamily.__init__;   s‚  ø€ ð &:Ô"Ø'=Ô$ä˜$×4Ñ4´k×BÑBÜð?óð ô
 ˜$×6Ñ6¼×EÑEÜð?óð ð ×)Ñ)×5Ñ5ˆØ×+Ñ+×7Ñ7¸¸Ð<ˆÜ¤¨£´¸³Ö?‰LˆEØ˜�z˜e q�j¨U­^Ü Ø:¸4¸&ð A$à$( 6¨ð,óð ñ @ð ×'Ñ'×.Ñ.×4Ñ4°RÑ8ˆØ×)Ñ)×5Ñ5°bÑ9ˆØ‰>˜b™n°³ÜØ4°R°Dð 9à�D˜ðóð ð
 !Ôà×2Ñ2×>Ñ>ˆÜ Ó,ˆÔÜ‰ÑàØ#Ø'ð	 	ò 	
ó    c                 ó´  >• [         R                  " U5      nXR                  4-   nU R                  [        U5      nU R
                  R                  U5      Ul        U R                  R                  U5      Ul        U R                  Ul        U R                  Ul        UR
                  R                  n[        [        U]/  XSS9  U R                  Ul        U$ )NFr   )ÚtorchÚSizer   Ú_get_checked_instancer   r   Úexpandr   r!   r   r"   r#   Ú_validate_args)r$   r   Ú	_instanceÚbatch_shape_compÚnewr   r+   s         €r,   r3   ÚMixtureSameFamily.expando   sÉ   ø€ Ü—j’j Ó-ˆØ&×*=Ñ*=Ð)?Ñ?ÐØ×(Ñ(Ô):¸IÓFˆØ&*×&BÑ&B×&IÑ&IØó'
ˆÔ#ð %)×$>Ñ$>×$EÑ$EÀkÓ$RˆÔ!Ø!×0Ñ0ˆÔØ×,Ñ,ˆÔØ×1Ñ1×=Ñ=ˆÜÔ Ñ.Ø#ÈEð 	/ñ 	
ð "×0Ñ0ˆÔØˆ
r.   c                 ó@   • [        U R                  R                  5      $ ©N)r   r   Úsupport©r$   s    r,   r;   ÚMixtureSameFamily.support€   s   € ô +¨4×+GÑ+G×+OÑ+OÓPÐPr.   c                 ó   • U R                   $ r:   )r   r<   s    r,   r   Ú&MixtureSameFamily.mixture_distribution…   s   € à×)Ñ)Ð)r.   c                 ó   • U R                   $ r:   )r   r<   s    r,   r   Ú(MixtureSameFamily.component_distribution‰   s   € à×+Ñ+Ð+r.   c                 ó¼   • U R                  U R                  R                  5      n[        R                  " XR
                  R                  -  SU R                  -
  S9$ ©Nr   ©Údim)Ú_pad_mixture_dimensionsr   Úprobsr0   Úsumr   Úmeanr!   )r$   rG   s     r,   rI   ÚMixtureSameFamily.mean�   sN   € à×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜ�yŠyØ×/Ñ/×4Ñ4Ñ4¸"¸t×?PÑ?PÑ:Pñ
ð 	
r.   c                 óŠ  • U R                  U R                  R                  5      n[        R                  " XR
                  R                  -  SU R                  -
  S9n[        R                  " XR
                  R                  U R                  U R                  5      -
  R                  S5      -  SU R                  -
  S9nX#-   $ )Nr   rD   g       @)rF   r   rG   r0   rH   r   Úvariancer!   rI   Ú_padÚpow)r$   rG   Úmean_cond_varÚvar_cond_means       r,   rL   ÚMixtureSameFamily.variance”   s¦   € ð ×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜŸ	š	Ø×/Ñ/×8Ñ8Ñ8¸bÀ4×CTÑCTÑ>Tñ
ˆô Ÿ	š	Ø×0Ñ0×5Ñ5¸¿	¹	À$Ç)Á)Ó8LÑL×QÑQÐRUÓVÑVØ�T×&Ñ&Ñ&ñ
ˆð Ñ,Ð,r.   c                 ó´   • U R                  U5      nU R                  R                  U5      nU R                  R                  n[
        R                  " X#-  SS9$ rC   )rM   r   Úcdfr   rG   r0   rH   )r$   ÚxÚcdf_xÚmix_probs       r,   rS   ÚMixtureSameFamily.cdf¡   sJ   € Ø�I‰I�a‹LˆØ×+Ñ+×/Ñ/°Ó2ˆØ×,Ñ,×2Ñ2ˆä�yŠy˜Ñ)¨rÑ2Ð2r.   c                 ó  • U R                   (       a  U R                  U5        U R                  U5      nU R                  R	                  U5      n[
        R                  " U R                  R                  SS9n[
        R                  " X#-   SS9$ rC   )
r4   Ú_validate_samplerM   r   Úlog_probr0   Úlog_softmaxr   r   Ú	logsumexp)r$   rT   Ú
log_prob_xÚlog_mix_probs       r,   rZ   ÚMixtureSameFamily.log_prob¨   ss   € Ø××Ø×!Ñ! !Ô$Ø�I‰I�a‹LˆØ×0Ñ0×9Ñ9¸!Ó<ˆ
Ü×(Ò(Ø×%Ñ%×,Ñ,°"ñ
ˆô �Š˜zÑ8¸bÑAÐAr.   c           
      ó   • [         R                  " 5          [        U5      n[        U R                  5      nX#-   nU R                  nU R
                  R                  U5      nUR                  nU R                  R                  U5      nUR                  U[         R                  " S/[        U5      S-   -  5      -   5      n	U	R                  [         R                  " S/[        U5      -  5      [         R                  " S/5      -   U-   5      n	[         R                  " X„U	5      n
U
R                  U5      sS S S 5        $ ! , (       d  f       g = f)Nr   )r0   Úno_gradr    r   r   r   Úsampler   r   Úreshaper1   ÚrepeatÚgatherÚsqueeze)r$   Úsample_shapeÚ
sample_lenÚ	batch_lenÚ
gather_dimÚesÚ
mix_sampleÚ	mix_shapeÚcomp_samplesÚmix_sample_rÚsampless              r,   rb   ÚMixtureSameFamily.sample²   s
  € Ü�]Š]�_Ü˜\Ó*ˆJÜ˜D×,Ñ,Ó-ˆIØ#Ñ/ˆJØ×!Ñ!ˆBð ×2Ñ2×9Ñ9¸,ÓGˆJØ"×(Ñ(ˆIð  ×6Ñ6×=Ñ=¸lÓKˆLð &×-Ñ-ØœEŸJšJ¨ s¬c°"«g¸©kÑ':Ó;Ñ;óˆLð (×.Ñ.Ü—
’
˜A˜3¤ Y£Ñ/Ó0´5·:²:¸q¸c³?ÑBÀRÑGóˆLô —l’l <¸\ÓJˆGØ—?‘? :Ó.÷- �_�_ús   –DD?Ä?
Ec                 ó>   • UR                  SU R                  -
  5      $ )Nr   )Ú	unsqueezer!   )r$   rT   s     r,   rM   ÚMixtureSameFamily._padË   s   € Ø�{‰{˜2 × 1Ñ 1Ñ1Ó2Ð2r.   c                 óR  • [        U R                  5      n[        U R                  R                  5      nUS:X  a  SOX#-
  nUR                  nUR	                  US S [
        R                  " US/-  5      -   USS  -   [
        R                  " U R                  S/-  5      -   5      nU$ )Nr   r   r   )r    r   r   r   rc   r0   r1   r!   )r$   rT   Údist_batch_ndimsÚcat_batch_ndimsÚ	pad_ndimsÚxss         r,   rF   Ú)MixtureSameFamily._pad_mixture_dimensionsÎ   s§   € Ü˜t×/Ñ/Ó0ÐÜ˜d×7Ñ7×CÑCÓDˆØ(¨AÓ-‘AÐ3CÑ3Uˆ	Ø�W‰WˆØ�I‰IØˆs�ˆGÜ�jŠj˜ a S™Ó)ñ*à��ˆgñô �jŠj˜×*Ñ*¨a¨SÑ0Ó1ñ2ó
ˆð ˆr.   c                 óJ   • SU R                    SU R                   3nSU-   S-   $ )Nz
  z,
  zMixtureSameFamily(r   )r   r   )r$   Úargs_strings     r,   Ú__repr__ÚMixtureSameFamily.__repr__Û   s7   € à�4×,Ñ,Ð-¨U°4×3NÑ3NÐ2OÐPð 	ð )¨;Ñ6¸Ñ<Ð<r.   )r   r!   r   r   r:   )#Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   ÚdictÚstrr   Ú
ConstraintÚ__annotations__Úhas_rsampler   r   Úboolr#   r3   Údependent_propertyr;   Úpropertyr   r   r   rI   rL   rS   rZ   r0   r1   rb   rM   rF   r}   Ú__static_attributes__Ú__classcell__)r+   s   @r,   r   r      s   ø‡ ñ(ðT :<€O�T˜#˜{×5Ñ5Ð5Ñ6Ó;Ø€Kð &*ñ	2
à)ð2
ð !-ð2
ð ˜d‘{ð	2
ð
 
÷2
ð 2
÷hð" ×#Ñ#ñQó $ðQð ð* kó *ó ð*ð ð,¨ó ,ó ð,ð ð
�fó 
ó ð
ð ð
-˜&ó 
-ó ð
-ò3òBð #(§*¢*£,ô /ò23ò÷=ð =r.   )r0   r   Útorch.distributionsr   r   Útorch.distributions.constraintsr   Ú torch.distributions.distributionr   Ú__all__r   © r.   r,   Ú<module>r“      s.   ðó Ý ß 8Ý GÝ 9ð Ð
€ôR=˜õ R=r.   