
    xf'                         d dl mZ d dlZd dlmZm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mZmZ  ej&                  d	      d
        Zd Z G d d      Zy)    )TupleN)assert_allcloseassert_array_less)stats)sobol_indices)BootstrapResult)BootstrapSobolResult
f_ishigami	sample_AB
sample_A_Bsession)scopec                  
   d} d}d| dz  dz  z   |t         j                  dz  z  dz  z   |dz  t         j                  dz  z  dz  z   }d|t         j                  dz  z  dz  z   |dz  t         j                  dz  z  d	z  z   }| dz  dz  }d
}d
}|dz  t         j                  dz  z  dz  dz  }d
}t        j                  |||g      |z  }	t        j                  dd|g|d|g||dgg      |z  }
|	|
j                  d      z   }|	|fS )z_Reference values for Ishigami from Saltelli2007.

    Chapter 4, exercise 5 pages 179-182.
    g      @g?      ?               2   r      g           axis)nppiarraysum)abvarv1v2v3v12v13v23s_firsts_seconds_totals               M/usr/lib/python3/dist-packages/scipy/stats/tests/test_sensitivity_analysis.pyishigami_ref_indicesr,      s/    	AA
1Q,255!8A
%1RUUAXb(8
8C	qz!|	ad255!8mB.	.B	
AaB	
B
C
Q$ruuax-/#
C
ChhB|$S(Gxx	R	b#	c2  	H
 !,,GG    c                      t        |       }||fS )zOutput of shape (2, n).r
   xress     r+   f_ishigami_vecr3   ,   s    
Q-C8Or-   c            	          e Zd Z ej                  ej                   dej                  z        gdz  Zd Ze	j                  j                  d      e	j                  j                  de e	j                  ee	j                  j                         gdd	g
      d               Zd Zd Zd Zd Ze	j                  j                  d      d        Zd Zy)TestSobolIndicesr   locscale   c                     t        j                  g dg dg dg      }|dz   }t        j                  g dg dg dgg dg dg dgg dg dg dgg      }t        ||      }t        ||       y )	N)r   r      
   )r   r   r      )r9      	      d   )e   h   k   n   )f   i   l   o   )g   j   m   p   AB)r   r   r   r   )selfrO   rP   refABs        r+   test_sample_ABzTestSobolIndices.test_sample_AB8   s    HH

 Ghh" " "$%

 a C r-   z!Can't create large array for testfunc)marksscalarvector)idsc                    t         j                  j                  d      }t        |d| j                  |      }|j
                  dk(  r|d   |d   g|d   |d   gg}t        |j                  |d   d       t        |j                  |d   d       |j                  J |j                  d	
      }t        |t              sJ t        |j                  t              sJ |j                  j                  j                  j                   d   dk(  sJ |j                  j                  j                  d   j                   |j                  j                   k(  sJ |j                  j                  j                  j                   |j                  j                   k(  sJ |j                  j                  j                  j                   |j                  j                   k(  sJ t#        |j                  j                  j                  |j                         t#        |j                  |j                  j                  j$                         t#        |j                  j                  j                  |j                         t#        |j                  |j                  j                  j$                         t        |j                  dd	      t              sJ t        |j                  t              sJ y )N	   ;m~|W2LE    rU   ndistsrandom_stater3   r   r   {Gz?atolc   )n_resamplesr   g?)confidence_levelre   )r   randomdefault_rngr   r_   __name__r   first_ordertotal_order_bootstrap_result	bootstrap
isinstancer	   r   confidence_intervallowshaper   high)rQ   r,   rU   rngr2   bootstrap_ress         r+   test_ishigamizTestSobolIndices.test_ishigamiO   s    ii##$JK**
 ==,,)!,.B1.EF)!,.B1.EF$ 
 	)=a)@tL)=a)@tL$$,,,"5-)=>>>#//AAA$$88<<BB1EJJJ$$88<<Q?EE//''( 	( ( ((<<@@FF//''( 	( (((<<@@FF//''( 	( ( 	%%99==s	
 	OO]66JJOO	
 	%%99==s	
 	OO]66JJOO	

 MM3BM? 
 	
 
 #//AAAr-   c                    t         j                  j                  d      }d}t        j                  t         j
                   dt         j
                  z        t        j                  t         j
                   dt         j
                  z        t        j                  t         j
                   dt         j
                  z        g}t        |||      \  }}t        ||      }t        |      j                  dd      t        |      j                  dd      t        |      j                  d	      d
}t        ||||      }	t        |	j                  |d   d       t        |||      }	t        |	j                  |d   d       y )Nr[   r\   r   r6   )r^   r_   r`   rN   r   )r9   r   rw   f_Af_Bf_ABr]   r   ra   rb   )rU   r^   r`   )r   rg   rh   r   uniformr   r   r   r
   reshaper   r   rj   )
rQ   r,   rs   r^   r_   rO   rP   rS   rU   r2   s
             r+   test_func_dictzTestSobolIndices.test_func_dict   s3   ii##$JKMMruufAbeeG4MMruufAbeeG4MMruufAbeeG4
 AU=1a  a=((B/a=((B/rN**:6
 

 	)=a)@tL
 	)=a)@tLr-   c           
         d t         j                  j                  d      }t        t        d| j
                  |      }t        |j                  |d   d       t        |j                  |d   d       d	t         j                  d
t         j                  dt         j                  dt        t         j                  t         j                  f   ffd}t        t        d| j
                  ||      }y )Nc                     t        j                  | |gd      }|dt        j                  ||z
  dz  d      z  z
  |z  }t        j                  | | |z
  z  d      |z  }|j                  |j                  fS )zaJansen for S and Sobol' for St.

            From Saltelli2010, table 2 formulations (c) and (e).)r   rw   r   r   r   rw   )r   r!   meanT)ry   rz   r{   r!   ssts         r+   jansen_sobolz2TestSobolIndices.test_method.<locals>.jansen_sobol   so     &&#s'2Cs277C$J?<<<CAcDj)3c9B339r-   r[   r\   )rU   r^   r_   methodr`   r   ra   rb   r   ry   rz   r{   returnc                      | ||      S N )ry   rz   r{   r   s      r+   jansen_sobol_typedz8TestSobolIndices.test_method.<locals>.jansen_sobol_typed   s      S$//r-   r   )r   rg   rh   r   r
   r_   r   rj   rk   ndarrayr   )rQ   r,   rs   r2   r   _r   s         @r+   test_methodzTestSobolIndices.test_method   s    		 ii##$JKt**	
 	)=a)@tL)=a)@tL	0	0"$**	046JJ	02::rzz)*	0
 q**%	
r-   c                     t         j                  j                  d      }t        d d| j                  |      }t        |j                  |d   d       t        |j                  |d   d       y )	Nr[   c                     t        |       dz   S )Ni  r/   r1   s    r+   <lambda>z5TestSobolIndices.test_normalization.<locals>.<lambda>   s    :a=4/ r-   r\   r]   r   ra   rb   r   r   rg   rh   r   r_   r   rj   rk   rQ   r,   rs   r2   s       r+   test_normalizationz#TestSobolIndices.test_normalization   s^    ii##$JK/4**
 	)=a)@tL)=a)@tLr-   c                    d }t         j                  j                  d      }t        |d| j                  |      }|d   g d|d   g|d   g d|d   gg}t        |j                  |d   d	       t        |j                  |d   d	       y )
Nc                 .    t        |       }||dz  dz   |fS )zOutput of shape (3, n).r   r<   r/   r0   s     r+   f_ishigami_vec_constzETestSobolIndices.test_constant_function.<locals>.f_ishigami_vec_const   s     Q-Ca"c))r-   r[   r\   r]   r   )r   r   r   r   ra   rb   r   )rQ   r,   r   rs   r2   ishigami_vec_indicess         r+   test_constant_functionz'TestSobolIndices.test_constant_function   s    	*
 ii##$JK%**
 &a()5I!5LM%a()5I!5LM 

 	)=a)@tL)=a)@tLr-   c                     t         j                  j                  d      }t        t        d| j
                  |      }t        |j                  |d   d       t        |j                  |d   d       y )Nr[   i   r]   r   g-C6?rb   r   )	r   rg   rh   r   r
   r_   r   rj   rk   r   s       r+   test_more_convergedz$TestSobolIndices.test_more_converged   s^    ii##$JKu**
 	)=a)@tL)=a)@tLr-   c                    d}t        j                  t        |      5  t        dt        d       d d d        t        j                  t        |      5  t        dt        d g       d d d        d}t        j                  t        |      5  t        dt        t        j                         g       d d d        t        j                  t        |      5  t        d	t        t        j                         g       d d d        d
}t        j                  t        |      5  t        dt        d       d d d        d}t        j                  t        |      5  t        dt        d        d d d        d}t        j                  t        |      5  t        dt               d d d        d }d}t        j                  t        |      5  t        d|t        j                         g       d d d        d}t        j                  t        |      5  t        dg g dt        j                         g       d d d        t        j                  t        |      5  t        dddgdgg dd       d d d        t        j                  t        |      5  t        dddgddgg dd       d d d        y # 1 sw Y   xY w# 1 sw Y   ]xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   bxY w# 1 sw Y   %xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nz3Each distribution in `dists` must have method `ppf`)matchr   r|   )r^   rU   r_   c                     | S r   r   r   s    r+   r   z.TestSobolIndices.test_raises.<locals>.<lambda>        r-   z The balance properties of Sobol'r;   gffffff@z'toto' is not a valid 'method'toto)r^   rU   r   z!must have the following signaturec                     | S r   r   r   s    r+   r   z.TestSobolIndices.test_raises.<locals>.<lambda>  r   r-   z1'dists' must be defined when 'func' is a callable)r^   rU   c                 &    | j                  dd      S )Nrw   r   )r}   r   s    r+   func_wrong_shape_outputz=TestSobolIndices.test_raises.<locals>.func_wrong_shape_output  s    99R##r-   z!'func' output should have a shaper   zWhen 'func' is a dictionary)ry   r{   r   r9   )r   r>   r;   r   rx   r   )r   r>   r;   )pytestraises
ValueErrorr   r
   r   r|   )rQ   messager   s      r+   test_raiseszTestSobolIndices.test_raises   s   H]]:W5 	AAJi@	A ]]:W5 	EAJ{mD	E 6]]:W5 	IAJu}}6GH	I ]]:W5 	KCj8IJ	K 4]]:W5 	?AJv>	? 7]]:W5 	DAJ{C	D G]]:W5 	0AJ/	0	$ 7]]:W5 	1%--/9J	
 1]]:W5 	"b1%--/9J	
 ]]:W5 	VQCF	 ]]:W5 	VQFIF	 	_	A 	A	E 	E	I 	I	K 	K	? 	?	D 	D	0 	0	 		 	
	 		 	s   J7K&K&K"K+K8L"L%LL+L77KKKK(+K58LLLL(+L47M N)ri   
__module____qualname__r   r|   r   r   r_   rT   r   markxfail_on_32bitparametrizer
   paramr3   slowru   r~   r   r   r   r   r   r   r-   r+   r5   r5   2   s     	255&"%%0	
E!. [[ CD[[	\V\\.8H8HIJx   
1B E1BfM>!
F	MM, [[ CD	M E	M7r-   r5   )typingr   numpyr   numpy.testingr   r   r   scipyr   scipy.statsr   scipy.stats._resamplingr   !scipy.stats._sensitivity_analysisr	   r
   r   r   fixturer,   r3   r5   r   r-   r+   <module>r      sT      <   % 3 
 i  !8| |r-   