o
    '&i(                     @   s   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	d
d Zdd ZG dd dZdS )    )TupleN)assert_allcloseassert_array_less)stats)sobol_indices)BootstrapResult)BootstrapSobolResult
f_ishigami	sample_AB
sample_A_Bsession)scopec                  C   s   d} d}d| d d  |t jd  d  |d t jd  d  }d|t jd  d  |d t jd  d	  }| d d }d
}d
}|d t jd  d d }d
}t |||g| }	t dd|g|d|g||dgg| }
|	|
jdd }|	|fS )z_Reference values for Ishigami from Saltelli2007.

    Chapter 4, exercise 5 pages 179-182.
    g      @g?      ?               2   r      g           Zaxis)nppiarraysum)abvarv1v2Zv3Zv12Zv13Zv23Zs_firstZs_secondZs_total r!   VC:\wamp64\www\opt\env\Lib\site-packages\scipy/stats/tests/test_sensitivity_analysis.pyishigami_ref_indices   s&   8,r#   c                 C   s   t | }||fS )zOutput of shape (2, n).r	   xresr!   r!   r"   f_ishigami_vec,   s   r(   c                   @   s   e Zd Zejej dej dgd Zdd Ze	j
de	j
jdee	jee	j
jdgd	d
gddd Zdd Zdd Zdd Zdd Ze	j
ddd Zdd ZdS )TestSobolIndicesr   locscale   c                 C   s   t g dg dg dg}|d }t g dg dg dgg dg dg dgg dg dg dgg}t||d}t|| d S )	N)r   r      
   )r   r   r      )r-      	      d   )e   h   k   n   )f   i   l   o   )g   j   m   p   AB)r   r   r
   r   )selfrB   rC   refABr!   r!   r"   test_sample_AB8   s0   zTestSobolIndices.test_sample_ABz!Can't create large array for testfunc)ZmarksZscalarZvector)Zidsc                 C   s  t jd}t|d| j|d}|jdkr$|d |d g|d |d gg}t|j|d dd t|j|d dd |j	d u s?J |j
d	d
}t|tsLJ t|j	tsTJ |j	jjjd dks`J |j	jjd j|jjksnJ |jjjj|jjkszJ |jjjj|jjksJ t|jjj|j t|j|jjj t|jjj|j t|j|jjj t|j
dd	dtsJ t|j	tsJ d S )N	   ;m~|W2LE    rH   ndistsrandom_stater(   r   r   {Gz?Zatolc   )n_resamplesr   g?)Zconfidence_levelrR   )r   randomdefault_rngr   rM   __name__r   first_ordertotal_orderZ_bootstrap_resultZ	bootstrap
isinstancer   r   Zconfidence_intervallowshaper   high)rD   r#   rH   rngr'   Zbootstrap_resr!   r!   r"   test_ishigamiO   sV   


zTestSobolIndices.test_ishigamic           
      C   s   t jd}d}tjt j dt j dtjt j dt j dtjt j dt j dg}t|||d\}}t||d}t|	ddt|	ddt|	d	d
}t
||||d}	t|	j|d dd t
|||d}	t|	j|d dd d S )NrI   rJ   r   r*   )rL   rM   rN   rA   r   )r-   r   r^   f_Af_Bf_ABrK   r   rO   rP   )rH   rL   rN   )r   rS   rT   r   uniformr   r   r
   r	   reshaper   r   rV   )
rD   r#   r\   rL   rM   rB   rC   rF   rH   r'   r!   r!   r"   test_func_dict   s.   zTestSobolIndices.test_func_dictc              
      s   dd  t jd}ttd| j |d}t|j|d dd t|j|d	 dd d
t j	dt j	dt j	dt
t j	t j	f f fdd}ttd| j||d}d S )Nc                 S   sZ   t j| |gdd}|dt j|| d dd  | }t j| | |  dd| }|j|jfS )zaJansen for S and Sobol' for St.

            From Saltelli2010, table 2 formulations (c) and (e).)r   r^   r   r   r   r^   )r   r   meanT)r`   ra   rb   r   sstr!   r!   r"   jansen_sobol   s   "z2TestSobolIndices.test_method.<locals>.jansen_sobolrI   rJ   )rH   rL   rM   methodrN   r   rO   rP   r   r`   ra   rb   returnc                    s    | ||S Nr!   r_   rj   r!   r"   jansen_sobol_typed   s   z8TestSobolIndices.test_method.<locals>.jansen_sobol_typedr   )r   rS   rT   r   r	   rM   r   rV   rW   Zndarrayr   )rD   r#   r\   r'   ro   _r!   rn   r"   test_method   s2   
zTestSobolIndices.test_methodc                 C   sN   t jd}tdd d| j|d}t|j|d dd t|j|d	 dd d S )
NrI   c                 S   s   t | d S )Ni  r$   r&   r!   r!   r"   <lambda>   s    z5TestSobolIndices.test_normalization.<locals>.<lambda>rJ   rK   r   rO   rP   r   r   rS   rT   r   rM   r   rV   rW   rD   r#   r\   r'   r!   r!   r"   test_normalization   s   z#TestSobolIndices.test_normalizationc                 C   s~   dd }t jd}t|d| j|d}|d g d|d g|d g d|d gg}t|j|d d	d
 t|j|d d	d
 d S )Nc                 S   s   t | }||d d |fS )zOutput of shape (3, n).r   r/   r$   r%   r!   r!   r"   f_ishigami_vec_const   s   zETestSobolIndices.test_constant_function.<locals>.f_ishigami_vec_constrI   rJ   rK   r   )r   r   r   r   rO   rP   rt   )rD   r#   rw   r\   r'   Zishigami_vec_indicesr!   r!   r"   test_constant_function   s   z'TestSobolIndices.test_constant_functionc                 C   sJ   t jd}ttd| j|d}t|j|d dd t|j|d dd d S )NrI   i   rK   r   g-C6?rP   r   )	r   rS   rT   r   r	   rM   r   rV   rW   ru   r!   r!   r"   test_more_converged   s   z$TestSobolIndices.test_more_convergedc                 C   s  d}t jt|d tdtdd W d    n1 sw   Y  t jt|d tdtdd gd W d    n1 s<w   Y  d}t jt|d td	tt gd W d    n1 s_w   Y  t jt|d td
tt gd W d    n1 sw   Y  d}t jt|d tdtdd W d    n1 sw   Y  d}t jt|d tdtdd d W d    n1 sw   Y  d}t jt|d tdtd W d    n1 sw   Y  dd }d}t jt|d td|t gd W d    n	1 s	w   Y  d}t jt|d tdg g dt gd W d    n	1 s0w   Y  t jt|d tdddgdgg ddd W d    n	1 sWw   Y  t jt|d tdddgddgg ddd W d    d S 1 sw   Y  d S )Nz3Each distribution in `dists` must have method `ppf`)matchr   rc   )rL   rH   rM   c                 S      | S rm   r!   rr   r!   r!   r"   rs          z.TestSobolIndices.test_raises.<locals>.<lambda>z The balance properties of Sobol'r.   gffffff@z'toto' is not a valid 'method'Ztoto)rL   rH   rk   z!must have the following signaturec                 S   r{   rm   r!   rr   r!   r!   r"   rs     r|   z1'dists' must be defined when 'func' is a callable)rL   rH   c                 S   s   |  ddS )Nr^   r   )rd   rr   r!   r!   r"   func_wrong_shape_output  s   z=TestSobolIndices.test_raises.<locals>.func_wrong_shape_outputz!'func' output should have a shaper   zWhen 'func' is a dictionary)r`   rb   r   r-   )r   r1   r.   r   r_   r   )r   r1   r.   )pytestZraises
ValueErrorr   r	   r   rc   )rD   messager}   r!   r!   r"   test_raises   sf   $zTestSobolIndices.test_raisesN)rU   
__module____qualname__r   rc   r   r   rM   rG   r~   markZxfail_on_32bitZparametrizer	   paramr(   Zslowr]   re   rq   rv   rx   ry   r   r!   r!   r!   r"   r)   2   s(    
3#

r)   )typingr   numpyr   Znumpy.testingr   r   r~   Zscipyr   Zscipy.statsr   Zscipy.stats._resamplingr   Z!scipy.stats._sensitivity_analysisr   r	   r
   r   Zfixturer#   r(   r)   r!   r!   r!   r"   <module>   s    

