a
    /'a                     @   s  d dl Z d dlmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZmZmZmZmZ d dl	mZ d dl	mZ dZee d dZed	d
dgZedg dZedg dZejereeeddZne  Zded< e!dg  e!dg  e!dg  d\Z"Z#de_$g dZ%g dZ&g dZ'dd e (e%e&e'D Z)d\Z*Z+dd e,d D Z-d!d" Z.d#d$ Z/d%d& Z0d'd( Z1d)d* Z2d+d, Z3d-d. Z4d>d/d0Z5d1d2 Z6d3d4 Z7d5d6 Z8d7d8 Z9d9d: Z:d;d< Z;e<d=kre;  dS )?    N)
namedtuple)Pool	cpu_count)gammapisqrtquadinfmpfmp)npdf)ncdfz&data/studentized_range_mpmath_ref.json   MPResultsrc_case	mp_resultCdfCase)qkvexpected_atolexpected_rtol
MomentCase)mr   r   r   r   rmodez]!!!!!! THIS FILE WAS AUTOGENERATED BY RUNNING `python studentized_range_mpmath_ref.py` !!!!!!COMMENTcdf_datapdf_datamoment_data)dy=r!      )g?r      
   )   r$      )r%   r$   r&   2   d   x   c                 C   s"   g | ]}t g |ttR  qS  )r   general_atolgeneral_rtol).0caser*   r*   Mlib/python3.9/site-packages/scipy/stats/tests/studentized_range_mpmath_ref.py
<listcomp>A   s   r0   )&.>r1   c                 C   s   g | ]}t |d dttqS )r%   r$   )r   mom_atolmom_rtol)r-   ir*   r*   r/   r0   K   s      c                  C   s@   t tdd } tjt| dd W d   n1 s20    Y  dS )z3Writes the current res_dict to the target JSON filewr      )indentN)openresults_filepathjsondumpres_dict)fr*   r*   r/   
write_dataQ   s    r?   c                 C   s   t |  S )zConverts a namedtuple to a dict)dict_asdict)Znamed_tupler*   r*   r/   to_dictW   s    rB   c                 C   s   t | jt| jdS )z;Formats an MPResult namedtuple into a dict for JSON dumping)r   r   )rB   r   floatr   )r   r*   r*   r/   mp_res_to_dict\   s    rD   c                    sh   t t t   fdd  fddfdd}t|dtgt tgdd	d
}|S )z7Straightforward implementation of studentized range CDFc                    s(   t |t||   t|  d   S Nr   phiPhiszr   r   r*   r/   innerj   s    zcdf_mp.<locals>.innerc                    s&   | d  t t|    | | S rE   rG   r   rI   rM   nur*   r/   outerm   s    zcdf_mp.<locals>.outerc                    sB   t dt   d   td dd d    | | S Nr7   r   r   r   r   rI   r   rP   rQ   r*   r/   wholep   s
    zcdf_mp.<locals>.wholer   gauss-legendrer$   methodZ	maxdegreer
   r   r	   r   r   rP   rU   resr*   rM   r   rP   rQ   r   r/   cdf_mpf   s    r]   c                    sh   t t t   fdd  fddfdd}t|dtgt tgdd	d
}|S )z7Straightforward implementation of studentized range PDFc                    s8   t ||   t | t||   t|  d   S Nr7   rF   rI   rL   r*   r/   rM   }   s    zpdf_mp.<locals>.innerc                    s"   |  t t|    | | S NrN   rI   rO   r*   r/   rQ      s    zpdf_mp.<locals>.outerc                    sJ   t dt    d  d   td dd d    | | S rR   rS   rI   rT   r*   r/   rU      s
    "zpdf_mp.<locals>.wholer   rV   r$   rW   rY   rZ   r*   r\   r/   pdf_mpy   s    r`   c                    sz   t t t   fdd  fddfddfdd}t|d	tgd	tgt tgd
dd}|S )z.Implementation of the studentized range momentc                    s8   t || |  t | t|| |  t|  d   S r^   rF   r   rJ   rK   )r   r*   r/   rM      s    zmoment_mp.<locals>.innerc                    s$   | t t|   | || S r_   rN   ra   rO   r*   r/   rQ      s    zmoment_mp.<locals>.outerc                    sL   t dt    d  d   td dd d    | || S rR   rS   ra   rT   r*   r/   pdf   s
    "
zmoment_mp.<locals>.pdfc                    s   |   | || S r_   r*   ra   )r   rb   r*   r/   rU      s    zmoment_mp.<locals>.wholer   rV   r$   rW   rY   )r   r   rP   rU   r[   r*   )rM   r   r   rP   rQ   rb   r/   	moment_mp   s    rc   c                    sD   | t vrt|  dt| tt fddt |  }t|dkS )znSearches the results dict for a result in the set that matches a case.
    Returns True if such a case exists.z not present in data structure!c                    s   | d  kS )Nr   r*   )r[   Z	case_dictr*   r/   <lambda>       zresult_exists.<locals>.<lambda>r   )r=   
ValueErrorrB   listfilterlen)set_keyr.   Zexisting_resr*   rd   r/   result_exists   s    
rl   c                 C   sL   t  }|| }td|d  d| dt  |  d ||tt| |fS )zARuns the single passed case, returning an mp dictionary and indexz	Finished r   /z in batch. (Took zs))timeperf_counterprintrD   r   )r.   
run_lambdark   indextotal_casest_startr[   r*   r*   r/   run   s    

ru   c                 C   s$   | \}}}t | || t  dS )zSA callback for completed jobs. Inserts and writes a calculated result
     to file.N)r=   insertr?   )r[   rr   rk   Zresult_dictr*   r*   r/   write_result   s    
rw   c                    s~    fddt  D }tt t|  dt  d g }tt}|D ]}||jt|td qN|	  |
  dS )z)Runs an array of cases and writes to filec                    s,   g | ]$\}}t |s||t fqS r*   )rl   rj   )r-   rr   r.   casesrq   rk   r*   r/   r0      s   
zrun_cases.<locals>.<listcomp>rm   z? cases won't be calculated because their results already exist.)argscallbackN)	enumeraterp   rj   r   	num_poolsappendZapply_asyncru   rw   closejoin)ry   rq   rk   Zjob_argZjobsZpoolr.   r*   rx   r/   	run_cases   s    $r   c                 C   s   t | j| j| jS r_   )r`   r   r   r   r.   r*   r*   r/   run_pdf   s    r   c                 C   s   t | j| j| jS r_   )r]   r   r   r   r   r*   r*   r/   run_cdf   s    r   c                 C   s   t | j| j| jS r_   )rc   r   r   r   r   r*   r*   r/   
run_moment   s    r   c                  C   s   t  } dtt tt }td| d tdtt d tttd tdtt d tttd	 td
tt d ttt	d tdt  |   d d S )Nr7   zProcessing z test caseszRunning 1st batch (z' PDF cases). These take about 30s each.r   zRunning 2nd batch (z' CDF cases). These take about 30s each.r   zRunning 3rd batch (zH moment cases). These take about anywhere from a few hours to days each.r    zTest data generated in rJ   )
rn   ro   rj   cdf_pdf_casesmoment_casesrp   r   r   r   r   )rt   rs   r*   r*   r/   main   s    r   __main__)r   r   )=	itertoolscollectionsr   r;   rn   osZmultiprocessingr   r   Zmpmathr   r   r   r   r	   r
   r   r   rG   r   rH   r:   maxr}   r   r   r   pathisfileloadr9   r=   r@   
setdefaultr+   r,   ZdpsZcp_qZcp_kZcp_nuproductr   r2   r3   ranger   r?   rB   rD   r]   r`   rc   rl   ru   rw   r   r   r   r   r   __name__r*   r*   r*   r/   <module>   sh   $


