a
    Pf?                     @   s  d dl Zd dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ ejeddgddgddgd	dggdd gd
eddgddgddgd	dggddgd
eddgddgddgd	dggddgd
eddgddgddgd	dggdd gd
eddgddgddgd	dggddgd
eddgddgddgd	dggddgd
eddgddgddgddggddgd
eddgddgddgd	dggd dgd
eddgddgddgd	dggddgd
g	ddd Zejdd Zejdd Zdd Zdd Zdd Zejd d!d"gd#d$ Zejd%d"d!gd&d' Zejd(ed)d*d+ Zd,d- Zd.d/ Zd0d1 Zejd2d3d4 d5d4 gd6d7 Z ejd2d8d4 d9d4 gd:d; Z!G d<d= d=Z"dS )>    N)IS64)	DataFrameIndex
MultiIndexSeries
date_range)	safe_sort               columnsC      ?g               @g      @g      @g       @X)paramsc                 C   s   | j S )zPairwise frames test_pairwise)param)request r   Z/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/window/test_pairwise.pypairwise_frames   s    r   c                   C   s(   t ddgddgddgddggddgdS )z'Pairwise target frame for test_pairwiser	   r
   r   r   r   r   r   r   r   r   r   r   pairwise_target_frame#   s    r   c                   C   s(   t g dg dg dg dgg ddS )z&Pairwise other frame for test_pairwise)Nr   r   )Nr   r	   )N   r	   )Nr   r   )YZr   r   r   r   r   r   r   pairwise_other_frame)   s    r   c              	   C   sd   | }|t jdt| }|jddd|}t|j	d t |dd  |dd  d  d S Nr	   2      windowZmin_periodsi)r   r   )
nprandomdefault_rngstandard_normallenrollingcovtmassert_almost_equalilocseriesABresultr   r   r   test_rolling_cov2   s    r5   c              	   C   sd   | }|t jdt| }|jddd|}t|j	d t 
|dd  |dd  d  d S r    )r&   r'   r(   r)   r*   r+   corrr-   r.   r/   Zcorrcoefr0   r   r   r   test_rolling_corr:   s    r7   c                  C   sx   t tjdtjdtdddd} |  }tj| d d< tj|d d< | jt| dd		|}t
|jd
 | 	| d S )N   dtypez
2020-01-01Zperiodsindexr   
   r   r#   r%   )r   r&   arangefloat64r   copynanr+   r*   r6   r-   r.   r/   )abr4   r   r   r   !test_rolling_corr_bias_correctionB   s    rE   funcr,   r6   c                 C   sn   t |jddd|  }|jtd dfdf }|jd|_t |d jddd| |d }tj||dd d S )Nr>   r   r#   r   FZcheck_names)getattrr+   locslicer=   Z	droplevelr-   assert_series_equal)rF   framer4   expectedr   r   r   test_rolling_pairwise_cov_corrO   s
     rN   methodc                    s    d t jdd }t  jdd} fdd}t|| t||   ttj	d
jjjdt  jdd}t fdd	 D }t|| d S )
Nr   r>   r$   c                    s   t jdd | S )Nr>   rP   rH   r+   x)rO   r1   r   r   <lambda>^       z(test_flex_binary_frame.<locals>.<lambda>r	   r=   r   c                    s,   i | ]$}|t  | jd d| qS )r>   rP   rQ   ).0k)rL   frame2rO   r   r   
<dictcomp>l   rU   z*test_flex_binary_frame.<locals>.<dictcomp>)rH   r+   applyr-   assert_frame_equalrA   r   r&   r'   r(   r)   shaper=   r   )rO   rL   resZres2expZres3r   )rL   rY   rO   r1   r   test_flex_binary_frameX   s"    r`   r$      c                 C   s@   t td}t td}|j| dj|d  s<J d S )Nr8   rP   )other)r   r&   Zzerosr?   r+   r6   Zisnaall)r$   srb   r   r   r   $test_rolling_corr_with_zero_varianceq   s    re   c               
   C   s   t tddgddgddgddgd	d
gddgg} | d jddd| d }tdd |D sdJ t tjdd} | d jddd| d }tdd |D sJ d S )NgF$?g)?gT5?g?'N?gFr1?g+	C-?gR	`?g1:b%?g9T!X4!?gXS_?gS= ?g\+Lt?r   r   T)centerr   c                 s   s"   | ]}t t |d kV  qdS r   Nr&   absZ
nan_to_numrW   rS   r   r   r   	<genexpr>   rU   z#test_corr_sanity.<locals>.<genexpr>r	   )   r	   c                 s   s"   | ]}t t |d kV  qdS rg   rh   rj   r   r   r   rk      rU   )r   r&   arrayr+   r6   rc   r'   r(   )dfr^   r   r   r   test_corr_sanityz   s     ro   c                  C   s   t g dg dd} t ddgddgd}| jddd|}t g d	}t|| t g d
g dd}| jddd|}t|| d S )Nr   r	   r   r   r   r	   r<   r   r   r   r	   r#   )NNr   r   Nr   )r   r+   r,   r-   rK   s1s2r4   rM   Zs2ar   r   r   test_rolling_cov_diff_length   s    rv   c                  C   s   t g dg dd} t ddgddgd}| jddd|}t g d	}t|| t g d
g dd}| jddd|}t|| d S )Nrp   rq   r<   r   r   r   r	   r#   )NNr   rr   )r   r+   r6   r-   rK   rs   r   r   r   test_rolling_corr_diff_length   s    rw   fc                 C   s   | j dddj| ddS Nr>   r   r#   Tpairwiser+   r,   rR   r   r   r   rT      rU   rT   c                 C   s   | j dddj| ddS ry   r+   r6   rR   r   r   r   rT      rU   c                 C   s   t ddgddgddgddggtdd	gd
dttdddd}t tdd	gd
dtj|j|jgdd
gddd}| |}t|| d S )Nr   r   r   r	   	   r%   r   r2   r3   foonamer
   barr   r=   namesr@   )r   r=   r:   )	r   r   ranger   from_productr=   r   r-   r\   )rx   rn   Zdf_expectedZ	df_resultr   r   r   2test_rolling_functions_window_non_shrinkage_binary   s    	r   c                 C   s   | j dddj| ddS ry   r|   rR   r   r   r   rT      rU   c                 C   s   | j dddj| ddS ry   r}   rR   r   r   r   rT      rU   c                 C   s   t  }t tdgddtg ddd}|d d|d< t t|j|jgd}t tj|j|jgddgdtdgdddd	}| |}t|| | |}t|| d S )
NrC   r   r   r   r   r@   r<   r   )r=   r   r:   )	r   r   Zastyper   r   r=   r   r-   r\   )rx   Zdf1Zdf2Zdf1_expectedZdf2_expectedZ
df1_resultZ
df2_resultr   r   r   *test_moment_functions_zero_length_pairwise   s    r   c                   @   s  e Zd Zejddd dd gdd Zejddd dd d	d d
d dd dd gdd Zejddd dd dd dd dd dd gdd Zejddd dd dd ej	dd ejj
e dddddd d d gd!d" Zejd#ejdd$d d%d d&d d'd d(d d)d gd*d+ Zejdd,d d-d d.d d/d d0d d1d gd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:S );TestPairwiserx   c                 C   s   |   S N)r,   rR   r   r   r   rT      rU   zTestPairwise.<lambda>c                 C   s   |   S r   )r6   rR   r   r   r   rT      rU   c                 C   sX   ||}t |j|j t |j|j ||}| j}| j}t j||dd d S NFZcheck_dtyper-   assert_index_equalr=   r   dropnavaluesassert_numpy_array_equalselfr   r   rx   r4   rM   r   r   r   test_no_flex   s    

zTestPairwise.test_no_flexc                 C   s   |   jddS NTrz   	expandingr,   rR   r   r   r   rT      rU   c                 C   s   |   jddS r   r   r6   rR   r   r   r   rT      rU   c                 C   s   | j ddjddS Nr   rP   Trz   r|   rR   r   r   r   rT      rU   c                 C   s   | j ddjddS r   r}   rR   r   r   r   rT      rU   c                 C   s   | j ddjddS Nr   comTrz   ewmr,   rR   r   r   r   rT      rU   c                 C   s   | j ddjddS r   r   r6   rR   r   r   r   rT      rU   c                 C   s   ||}t j|jjd |jdd t t|jjd t|j  t |j|j ||}| j}| j}t j	||dd d S Nr   FrG   r   r   
r-   r   r=   Zlevelsr   r   uniquer   r   r   r   r   r   r   test_pairwise_with_self   s    

z$TestPairwise.test_pairwise_with_selfc                 C   s   |   jddS NFrz   r   rR   r   r   r   rT     rU   c                 C   s   |   jddS r   r   rR   r   r   r   rT     rU   c                 C   s   | j ddjddS Nr   rP   Frz   r|   rR   r   r   r   rT     rU   c                 C   s   | j ddjddS r   r}   rR   r   r   r   rT     rU   c                 C   s   | j ddjddS Nr   r   Frz   r   rR   r   r   r   rT     rU   c                 C   s   | j ddjddS r   r   rR   r   r   r   rT     rU   c                 C   sX   ||}t |j|j t |j|j ||}| j}| j}t j||dd d S r   r   r   r   r   r   test_no_pairwise_with_self  s    

z'TestPairwise.test_no_pairwise_with_selfc                 C   s   |   j|ddS r   r   rS   yr   r   r   rT   (  rU   c                 C   s   |   j|ddS r   r   r   r   r   r   rT   )  rU   c                 C   s   | j ddj|ddS r   r|   r   r   r   r   rT   *  rU   c                 C   s   | j ddj|ddS r   r}   r   r   r   r   rT   -  rU   zPrecision issues on 32 bitF)reasonstrict)Zmarksc                 C   s   | j ddj|ddS r   r   r   r   r   r   rT   2  rU   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rT   3  rU   c                 C   sx   |||}t j|jjd |jdd t t|jjd t|j  |||}| j}| j}t j	||dd d S r   r   )r   r   r   r   rx   r4   rM   r   r   r   test_pairwise_with_other%  s    



z%TestPairwise.test_pairwise_with_otherzignore:RuntimeWarningc                 C   s   |   j|ddS r   r   r   r   r   r   rT   N  rU   c                 C   s   |   j|ddS r   r   r   r   r   r   rT   O  rU   c                 C   s   | j ddj|ddS r   r|   r   r   r   r   rT   P  rU   c                 C   s   | j ddj|ddS r   r}   r   r   r   r   rT   Q  rU   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rT   R  rU   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rT   S  rU   c                 C   s   |j jr|||nd }|d urX|j|j}|j |j }t|j| t|j | nptjtdd ||| W d    n1 s0    Y  tjtdd ||| W d    n1 s0    Y  d S )Nz'arg1' columns are not unique)matchz'arg2' columns are not unique)	r   Z	is_uniquer=   unionr-   r   pytestZraises
ValueError)r   r   r   rx   r4   Zexpected_indexZexpected_columnsr   r   r   test_no_pairwise_with_otherJ  s    (z(TestPairwise.test_no_pairwise_with_otherc                 C   s   |   |S r   r   r   r   r   r   rT   n  rU   c                 C   s   |   |S r   r   r   r   r   r   rT   o  rU   c                 C   s   | j dd|S Nr   rP   r|   r   r   r   r   rT   p  rU   c                 C   s   | j dd|S r   r}   r   r   r   r   rT   q  rU   c                 C   s   | j dd|S Nr   r   r   r   r   r   r   rT   r  rU   c                 C   s   | j dd|S r   r   r   r   r   r   rT   s  rU   c                 C   s   ||t g d}t|j|j t|j|j ||t g d}| j}| j}tj||dd |t g d|}t|j|j t|j|j |t g d|}| j}| j}tj||dd d S )N)r   r   r   r   Fr   )r   r-   r   r=   r   r   r   r   r   r   r   r   test_pairwise_with_seriesk  s    



z&TestPairwise.test_pairwise_with_seriesc                 C   sT   t tdtdddd}|d|}t tjgd tdddd}t|| d S )Nr   Z2020r;   r<   Z12h)	r   r   r   r+   r6   r&   rB   r-   rK   )r   rd   r4   rM   r   r   r   test_corr_freq_memory_error  s    z(TestPairwise.test_corr_freq_memory_errorc              	   C   s   t tdtdtdg}td}ttddd||d}|jdd		 }t tdtdtdtdg}t tdtdtdg}tt
td
tjtd
dtd
df||d}t|| d S )NabZxyZABr      r   rV   g?)alpha)r   r   g      @@gK\ǸO@)r   r   listr   r   r&   r?   Zreshaper   r,   ZvstackfullrB   r-   r\   )r   r   r=   rn   r4   rM   r   r   r   test_cov_mulittindex  s"    "

z!TestPairwise.test_cov_mulittindexc              	   C   s   t jddgddggddgd}ttd|d	}|d
 }ttjt jttj	dtj
ddddgd ddgd gg dd|d}t|| d S )NMNPQrC   rD   r   )r   r	   r   r   r   r9   r	   )NrC   rD   rV   )r   Zfrom_arraysr   r&   Zonesr+   r6   rB   repeatr?   Zint64r-   r\   )r   r   rn   r4   rM   r   r   r   $test_multindex_columns_pairwise_func  s    

z1TestPairwise.test_multindex_columns_pairwise_funcN)__name__
__module____qualname__r   markparametrizer   r   r   r   Zxfailr   r   filterwarningsr   r   r   r   r   r   r   r   r   r      s   





r   )#numpyr&   r   Zpandas.compatr   Zpandasr   r   r   r   r   Zpandas._testingZ_testingr-   Zpandas.core.algorithmsr   Zfixturer   r   r   r5   r7   rE   r   r   rN   r`   r   re   ro   rv   rw   r   r   r   r   r   r   r   <module>   sb   &&&&&&&&&







