B
    0d                 @   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dddddgZeddddddg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_$ddddgZ%d dd!gZ&d dd!d"d#d$g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/ Z0d0d1 Z1d2d3 Z2d4d5 Z3d6d7 Z4dGd8d9Z5d:d; Z6d<d= Z7d>d? Z8d@dA Z9dBdC Z:dDdE Z;e<dFkre;  dS )H    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vZexpected_atolZexpected_rtol
MomentCasemr)modez]!!!!!! THIS FILE WAS AUTOGENERATED BY RUNNING `python studentized_range_mpmath_ref.py` !!!!!!COMMENTcdf_datapdf_datamoment_data)gdy=gdy=   g?   
         2   d   x   c             C   s   g | ]}t |ttf qS  )r   general_atolgeneral_rtol).0caser&   r&   `/var/www/html/venv/lib/python3.7/site-packages/scipy/stats/tests/studentized_range_mpmath_ref.py
<listcomp>B   s   r,   )g&.>g&.>c             C   s   g | ]}t |d dttqS )r!   r    )r   mom_atolmom_rtol)r)   ir&   r&   r+   r,   L   s      c           	   C   s,   t tdd} tjt| dd W dQ R X dS )z3Writes the current res_dict to the target JSON filew)r      )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    r=   c             C   s   t | jt| jdS )z;Formats an MPResult namedtuple into a dict for JSON dumping)r   r   )r=   r   floatr   )r   r&   r&   r+   mp_res_to_dict\   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 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 )Nr   )r@   r   )rB   rC   )rD   nur&   r+   outerm   s    zcdf_mp.<locals>.outerc                sB   t dt   d   td dd d    | | S )Nr2   r   )r   r   r   )rB   rC   )r   rE   rF   r&   r+   wholep   s    zcdf_mp.<locals>.wholer   zgauss-legendrer    )method	maxdegree)r
   r   r	   )r   r   rE   rG   resr&   )rD   r   rE   rF   r   r+   cdf_mpf   s    
rK   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 )Nr2   )r@   rA   )rB   rC   )r   r   r&   r+   rD   }   s    zpdf_mp.<locals>.innerc                s"   |  t t|    | | S )N)r@   r   )rB   rC   )rD   rE   r&   r+   rF      s    zpdf_mp.<locals>.outerc                sJ   t dt    d  d   td dd d    | | S )Nr2   r   )r   r   r   )rB   rC   )r   rE   rF   r&   r+   rG      s    zpdf_mp.<locals>.wholer   zgauss-legendrer    )rH   rI   )r
   r   r	   )r   r   rE   rG   rJ   r&   )rD   r   rE   rF   r   r+   pdf_mpy   s    
rL   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 )Nr2   )r@   rA   )r   rB   rC   )r   r&   r+   rD      s    zmoment_mp.<locals>.innerc                s$   | t t|   | || S )N)r@   r   )r   rB   rC   )rD   rE   r&   r+   rF      s    zmoment_mp.<locals>.outerc                sL   t dt    d  d   td dd d    | || S )Nr2   r   )r   r   r   )r   rB   rC   )r   rE   rF   r&   r+   pdf   s    zmoment_mp.<locals>.pdfc                s   |   | || S )Nr&   )r   rB   rC   )r   rM   r&   r+   rG      s    zmoment_mp.<locals>.wholer   zgauss-legendrer    )rH   rI   )r
   r   r	   )r   r   rE   rG   rJ   r&   )rD   r   r   rE   rF   rM   r+   	moment_mp   s    
rN   c                sD   | t k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&   )rJ   )	case_dictr&   r+   <lambda>       zresult_exists.<locals>.<lambda>r   )r8   
ValueErrorr=   listfilterlen)set_keyr*   Zexisting_resr&   )rO   r+   result_exists   s    
rW   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printr?   r   )r*   
run_lambdarV   indextotal_casest_startrJ   r&   r&   r+   run   s    (r`   c             C   s$   | \}}}t | || t  dS )zSA callback for completed jobs. Inserts and writes a calculated result
     to file.N)r8   insertr:   )rJ   r]   rV   Zresult_dictr&   r&   r+   write_result   s    
rb   c                s    fddt  D }tt t|  dt  d g }tt}x"|D ]}||jt|td qPW |	  |
  dS )z)Runs an array of cases and writes to filec                s,   g | ]$\}}t |s||t fqS r&   )rW   rU   )r)   r]   r*   )casesr\   rV   r&   r+   r,      s   zrun_cases.<locals>.<listcomp>rX   z? cases won't be calculated because their results already exist.)argscallbackN)	enumerater[   rU   r   	num_poolsappendZapply_asyncr`   rb   closejoin)rc   r\   rV   Zjob_argjobspoolr*   r&   )rc   r\   rV   r+   	run_cases   s    $
rm   c             C   s   t | j| j| jS )N)rL   r   r   r   )r*   r&   r&   r+   run_pdf   s    rn   c             C   s   t | j| j| jS )N)rK   r   r   r   )r*   r&   r&   r+   run_cdf   s    ro   c             C   s   t | j| j| jS )N)rN   r   r   r   )r*   r&   r&   r+   
run_moment   s    rp   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 )Nr2   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 rB   )
rY   rZ   rU   cdf_pdf_casesmoment_casesr[   rm   rn   ro   rp   )r_   r^   r&   r&   r+   main   s    rs   __main__)r   r   )=	itertoolscollectionsr   r6   rY   osmultiprocessingr   r   Zmpmathr   r   r   r   r	   r
   r   r   r@   r   rA   r5   maxrg   r   r   r   pathisfileloadr4   r8   r;   
setdefaultr'   r(   ZdpsZcp_qZcp_kZcp_nuproductrq   r-   r.   rangerr   r:   r=   r?   rK   rL   rN   rW   r`   rb   rm   rn   ro   rp   rs   __name__r&   r&   r&   r+   <module>   s`   $



