
    rh                         d dl Z d dlmZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZ dgZd Zg d	Zg d
Zg dZg dZeegZeegZddZej,                  j.                  d        Z G d de	      Zy)    N)Optional)Tensor)constraints)Distribution)broadcast_alllazy_propertyVonMisesc                 r    t        |      }|j                         }|r|j                         | |z  z   }|r|S N)listpop)ycoefresults      p/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributions/von_mises.py
_eval_polyr      s7    :DXXZF
a&j( M    )g      ?g$@g03@g,?N?g2t?gIx?gtHZr?)	 e3E?g-5?gՒ+Hub?gJNYgTPÂ?g'gZ?gUL+ߐg;^p?)      ?gY?g(z?g*O?gZ9?g.h?gӰ٩=5?)	r   g.kg?VmgtZOZ?g<Q g'8`?gP⥝gqJ:N?g;PJ4qc                 V   |dk(  s|dk(  sJ | dz  }||z  }t        |t        |         }|dk(  r| j                         |z  }|j                         }d| z  }| d| j                         z  z
  t        |t        |         j                         z   }t        j                  | dk  ||      }|S )zX
    Returns ``log(I_order(x))`` for ``x > 0``,
    where `order` is either 0 or 1.
    r      g      @r   )r   _COEF_SMALLabslog_COEF_LARGEtorchwhere)xorderr   smalllarger   s         r   _log_modified_bessel_fnr"   E   s    
 A:!## 	
DA	AAq+e,-Ez%IIKE 	qAaeeg
1k%.@ A E E GGE[[T5%0FMr   c                 6   t        j                  |j                  t         j                  | j                        }|j                         st        j                  d|j                  z   | j                  | j                        }|j                         \  }}}t        j                  t        j                  |z        }	d||	z  z   ||	z   z  }
|||
z
  z  }|d|z
  z  |z
  dkD  ||z  j                         dz   |z
  dk\  z  }|j                         r>t        j                  ||dz
  j                         |
j!                         z  |      }||z  }|j                         s|t        j                  z   | z   dt        j                  z  z  t        j                  z
  S )Ndtypedevice)   r      r   r   )r   zerosshapeboolr&   allrandr%   unbindcosmathpir   anyr   signacos)locconcentration
proposal_rr   doneuu1u2u3zfcaccepts                r   _rejection_samplerA   \   s<   ;;qwwejjDDhhjJJtagg~SYYszzJXXZ
BIIdggl#aJN3Z!^,A;#q(a"f\\^a-?!-Cq-HI::<FR#XOO$5$@!DA&=D hhj K#!dgg+.88r   c            	       r    e Zd ZdZej
                  ej                  dZej
                  ZdZ		 dde
de
dee   ddf fd	Zd
 Zede
fd       Zede
fd       Zede
fd       Z ej(                          ej*                         fd       Zd fd	Zede
fd       Zede
fd       Zede
fd       Z xZS )r	   aX  
    A circular von Mises distribution.

    This implementation uses polar coordinates. The ``loc`` and ``value`` args
    can be any real number (to facilitate unconstrained optimization), but are
    interpreted as angles modulo 2 pi.

    Example::
        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = VonMises(torch.tensor([1.0]), torch.tensor([1.0]))
        >>> m.sample()  # von Mises distributed with loc=1 and concentration=1
        tensor([1.9777])

    :param torch.Tensor loc: an angle in radians.
    :param torch.Tensor concentration: concentration parameter
    )r5   r6   FNr5   r6   validate_argsreturnc                     t        ||      \  | _        | _        | j                  j                  }t	        j
                         }t        |   |||       y r   )r   r5   r6   r*   r   Sizesuper__init__)selfr5   r6   rC   batch_shapeevent_shape	__class__s         r   rH   zVonMises.__init__   sD     (5S-'H$$$hhnnjjlk=Ar   c                     | j                   r| j                  |       | j                  t        j                  || j
                  z
        z  }|t        j                  dt        j                  z        z
  t        | j                  d      z
  }|S )Nr(   r   r   )
_validate_args_validate_sampler6   r   r/   r5   r0   r   r1   r"   )rI   valuelog_probs      r   rR   zVonMises.log_prob   sy    !!%(%%		%$((2B(CChhq477{#$%d&8&8BC 	
 r   c                 T    | j                   j                  t        j                        S r   )r5   tor   doublerI   s    r   _loczVonMises._loc   s    xx{{5<<((r   c                 T    | j                   j                  t        j                        S r   )r6   rT   r   rU   rV   s    r   _concentrationzVonMises._concentration   s    !!$$U\\22r   c                     | j                   }ddd|dz  z  z   j                         z   }|d|z  j                         z
  d|z  z  }d|dz  z   d|z  z  }d|z  |z   }t        j                  |dk  ||      S )Nr      r(   gh㈵>)rY   sqrtr   r   )rI   kappataurho_proposal_r_proposal_r_taylors         r   r`   zVonMises._proposal_r   s    ##1q5!8|#))++a#g^^%%!e)436za#g.Y.{{54<);[IIr   c                 B   | j                  |      }t        j                  || j                  j                  | j
                  j                        }t        | j                  | j                  | j                  |      j                  | j
                  j                        S )a  
        The sampling algorithm for the von Mises distribution is based on the
        following paper: D.J. Best and N.I. Fisher, "Efficient simulation of the
        von Mises distribution." Applied Statistics (1979): 152-157.

        Sampling is always done in double precision internally to avoid a hang
        in _rejection_sample() for small values of the concentration, which
        starts to happen for single precision around 1e-4 (see issue #88443).
        r$   )_extended_shaper   emptyrW   r%   r5   r&   rA   rY   r`   rT   )rI   sample_shaper*   r   s       r   samplezVonMises.sample   sn     $$\2KKTYY__TXX__M IIt**D,<,<a

"TXX^^
	r   c                    	 t         |   |      S # t        $ rh | j                  j	                  d      }| j
                  j                  |      }| j                  j                  |      } t        |       |||      cY S w xY w)NrO   )rC   )rG   expandNotImplementedError__dict__getr5   r6   type)rI   rJ   	_instancerC   r5   r6   rL   s         r   rh   zVonMises.expand   s{    	O7>+.." 	O MM--.>?M((//+.C ..55kBM4:c=NN		Os    A.BBc                     | j                   S )z8
        The provided mean is the circular one.
        r5   rV   s    r   meanzVonMises.mean   s    
 xxr   c                     | j                   S r   ro   rV   s    r   modezVonMises.mode   s    xxr   c                     dt        | j                  d      t        | j                  d      z
  j                         z
  S )z<
        The provided variance is the circular one.
        r   rN   r   )r"   r6   exprV   s    r   variancezVonMises.variance   s>     '(:(:!D)$*<*<AFGce		
r   r   )__name__
__module____qualname____doc__r   realpositivearg_constraintssupporthas_rsampler   r   r+   rH   rR   r   rW   rY   r`   r   no_gradrF   rf   rh   propertyrp   rr   ru   __classcell__)rL   s   @r   r	   r	   l   s@   " *..AUAUVOGK )-		B	B 	B  ~		B
 
	B	 )f ) ) 3 3 3 JV J J U]]_",%**,   O f   f   

& 

 

r   )r   )r0   typingr   r   	torch.jitr   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   __all__r   _I0_COEF_SMALL_I0_COEF_LARGE_I1_COEF_SMALL_I1_COEF_LARGEr   r   r"   jitscript_if_tracingrA   r	    r   r   <module>r      s         + 9 B ,

 ~.~.. 9 9n
| n
r   