U
    3d1                     @   s   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 d dl	m
Z
mZmZmZmZmZmZmZmZ ejdd\ZZeddd f Zej\ZZd	d
 Zdd Zdd Zdd Zdd Zdd Zej !de j"egdd Z#dd Z$dd Z%dS )    N)assert_almost_equal)assert_array_almost_equal)assert_array_equal)datasets)	empirical_covarianceEmpiricalCovarianceShrunkCovarianceshrunk_covariance
LedoitWolfledoit_wolfledoit_wolf_shrinkageOASoasT)Z
return_X_yc               	   C   s  t  } | t tt}t|| jd t| |d t| j|ddd t| j|ddd t| j|ddd t| j|ddd t	t
 | j|d	d W 5 Q R X | t}t|dksttd d df d
}t  } | | tt|| jd t| t|d t| jt|ddd tddd}t  } d}tjt|d | | W 5 Q R X t| jtjdtjd tddgddgg}tddgddgg}tt|| t dd} | t t| jttjd  d S )N   r   Zspectral)ZnormZ	frobeniusF)Zscaling)ZsquaredZfoo      r   BOnly one sample available. You may want to reshape your data arraymatchr   r   shapeZdtypeg      ?g      пTassume_centered)r   fitXr   r   covariance_r   Z
error_normpytestraisesNotImplementedErrormahalanobisnpZaminAssertionErrorreshapearangewarnsUserWarningzerosfloat64Zasarrayr   Z	location_r   )covemp_covZ
mahal_distX_1d	X_1samplewarn_msgZ	X_integerresult r1   L/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/covariance/tests/test_covariance.pytest_covariance    s>    




r3   c                  C   s   t dd} | t ttttdd| jd t  } | t tttt| jd t dd} | t ttt| jd td d df d}t dd} | | tt|| jd t ddd	} | t | jd kst	d S )
Ng      ?	shrinkager   g        r   r   g333333?F)r5   store_precision)
r   r   r   r   r	   r   r   r%   
precision_r$   )r+   r-   r1   r1   r2   test_shrunk_covarianceO   s0    

  

  




r8   c            
   	   C   s  t t jdd } tdd}||  |j}|| }tt| dd| tt| ddd| t| dd\}}t	||j
d t||j t|jdd}||  t	|j
|j
d t d d df d	}tdd}|| t|dd\}}t	||j
d t||j t	|d
  t |j
d tddd}||  t|| |d |jd ksRtt }|t  t|j|d t|jtt  t|jtt d  t|t |d tt \}}t	||j
d t||j t|jd}|t  t	|j
|j
d t d d df d	}t }|| t|\}}t	||j
d t||j t	t||j
d tddd}t }d}	tjt|	d || W 5 Q R X t	|j
tjdtjd tdd}|t  t|t |d |jd kstd S )Nr   ZaxisTr      )r   
block_sizer   r5   r   r      Fr6   r   r   r4   r   r   r   r   r   r6   )r   meanr
   r   
shrinkage_scorer   r   r   r   r   r   r%   sum	n_samplesr7   r$   r   r#   r&   r   r'   r(   r)   r*   )

X_centeredlwrA   score_Zlw_cov_from_mleZlw_shrinkage_from_mlescovr-   r.   r/   r1   r1   r2   test_ledoit_wolfp   s|    



  









rI   c                 C   s   | j \}}t| dd}t|| }| }|jd d |d   |8  < |d  | }| d }d||  tt|j|| |d   }t	||}	|	| }
|
S )NFr   r   r=   g      ?)
r   r   r#   tracecopyZflatrC   dotTmin)r   rD   
n_featuresr,   muZdelta_deltaZX2Zbeta_betar5   r1   r1   r2   _naive_ledoit_wolf_shrinkage   s     

rS   c                  C   s<   t d d d df } t }||  |j}t|t|  d S )Nr   )r   r
   r   rA   r   rS   )ZX_smallrF   rA   r1   r1   r2   test_ledoit_wolf_small   s
    
rT   c                  C   sb   t jd} | jdd}tdd|}t|jt dd |j}tdd|}t|j| d S )Nr   )
      )sizerU   )r;   rV      )	r#   randomZRandomStatenormalr
   r   r   r   Zeye)rngr   rF   r+   r1   r1   r2   test_ledoit_wolf_large   s    r\   ledoit_wolf_fitting_functionc              	   C   s0   t d}tjtdd | | W 5 Q R X dS )zDCheck that we validate X and raise proper error with 0-sample array.)r   r=   zFound array with 0 sampler   N)r#   r)   r   r    
ValueError)r]   ZX_emptyr1   r1   r2   test_ledoit_wolf_empty_array   s    
r_   c            
   	   C   s  t t jdd } tdd}||  |j}|| }t| dd\}}t||jd t	||j t
|jdd}||  t|j|jd t d d ddf }tdd}|| t|dd\}}t||jd t	||j t|d  t |jd td	dd
}||  t	|| |d |jd ks*tt }|t  t	|j|d t	|t |d tt \}}t||jd t	||j t
|jd}|t  t|j|jd t d d df d}t }|| t|\}}t||jd t	||j tt||jd tddd}t }d}	tjt|	d || W 5 Q R X t|jtjdtjd td	d}|t  t	|t |d |jd kstd S )Nr   r9   Tr   r   r<   r   r=   Fr>   r4   r   r   r   r   r   r   r?   )r   r@   r   r   rA   rB   r   r   r   r   r   rC   rD   r7   r$   r%   r   r#   r&   r   r'   r(   r)   r*   )
rE   ZoarA   rG   Zoa_cov_from_mleZoa_shrinkage_from_mlerH   r-   r.   r/   r1   r1   r2   test_oas  sb    











r`   c               	   C   sV   t  t} dtjd  d}tjt|d  | tddddf  W 5 Q R X dS )z@Checks that EmpiricalCovariance validates data with mahalanobis.z'X has 2 features, but \w+ is expecting r   z features as inputr   Nr=   )r   r   r   r   r   r    r^   r"   )r+   msgr1   r1   r2   .test_EmpiricalCovariance_validates_mahalanobisK  s    rb   )&Znumpyr#   r   Zsklearn.utils._testingr   r   r   Zsklearnr   Zsklearn.covariancer   r   r   r	   r
   r   r   r   r   Zload_diabetesr   _r-   r   rD   rO   r3   r8   rI   rS   rT   r\   markZparametrizer   r_   r`   rb   r1   r1   r1   r2   <module>   s,   ,
/!V
 

I