a
    Qe'                     @   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)Z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varZv1Zv2Zv3Zv12Zv13Zv23Zs_firstZs_secondZs_total r   _/var/www/sunrise/env/lib/python3.9/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   locZ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   )selfr>   r?   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)idsc                 C   s  t jd}t|d| j|d}|jdkrH|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sJ t|j	tsJ |j	jjjd dksJ |j	jjd j|jjksJ |jjjj|jjksJ |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	dtspJ t|j	tsJ d S )N	   ;m~|W2LE    rD   ndistsrandom_stater%   r   r   {Gz?Zatolc   )n_resamplesr   g?)Zconfidence_levelrO   )r   randomdefault_rngr   rJ   __name__r   first_ordertotal_orderZ_bootstrap_resultZ	bootstrap
isinstancer   r   Zconfidence_intervallowshaper   high)r@   r    rD   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 )NrF   rG   r   r'   )rI   rJ   rK   r=   r   )r)   r   r[   f_Af_Bf_ABrH   r   rL   rM   )rD   rI   rK   )r   rP   rQ   r   uniformr   r   r
   r	   reshaper   r   rS   )
r@   r    rY   rI   rJ   r>   r?   rB   rD   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 t j	t j	t j	t
t j	t j	f d
 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]   r^   r_   r   sstr   r   r   jansen_sobol   s    "z2TestSobolIndices.test_method.<locals>.jansen_sobolrF   rG   )rD   rI   rJ   methodrK   r   rL   rM   r   )r]   r^   r_   returnc                    s    | ||S Nr   r\   rg   r   r   jansen_sobol_typed   s    z8TestSobolIndices.test_method.<locals>.jansen_sobol_typedr   )r   rP   rQ   r   r	   rJ   r   rS   rT   Zndarrayr   )r@   r    rY   r$   rl   _r   rk   r   test_method   s&    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 )
NrF   c                 S   s   t | d S )Ni  r!   r#   r   r   r   <lambda>       z5TestSobolIndices.test_normalization.<locals>.<lambda>rG   rH   r   rL   rM   r   r   rP   rQ   r   rJ   r   rS   rT   r@   r    rY   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_constrF   rG   rH   r   )r   r   r   r   rL   rM   rr   )r@   r    ru   rY   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 )NrF   i   rH   r   g-C6?rM   r   )	r   rP   rQ   r   r	   rJ   r   rS   rT   rs   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 s60    Y  t jt|d$ tdtdd gd W d    n1 sx0    Y  d}t jt|d$ td	tt gd W d    n1 s0    Y  t jt|d$ td
tt gd W d    n1 s0    Y  d}t jt|d tdtdd W d    n1 sD0    Y  d}t jt|d" tdtdd d W d    n1 s0    Y  d}t jt|d tdtd W d    n1 s0    Y  dd }d}t jt|d$ td|t gd W d    n1 s0    Y  d}t jt|d* tdg g dt gd W d    n1 sh0    Y  t jt|d. tdddgdgg ddd W d    n1 s0    Y  t jt|d0 tdddgddgg ddd W d    n1 s0    Y  d S )Nz3Each distribution in `dists` must have method `ppf`)matchr   r`   )rI   rD   rJ   c                 S   s   | S rj   r   ro   r   r   r   rp      rq   z.TestSobolIndices.test_raises.<locals>.<lambda>z The balance properties of Sobol'r*   gffffff@z'toto' is not a valid 'method'Ztoto)rI   rD   rh   z!must have the following signaturec                 S   s   | S rj   r   ro   r   r   r   rp     rq   z1'dists' must be defined when 'func' is a callable)rI   rD   c                 S   s   |  ddS )Nr[   r   )ra   ro   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]   r_   r   r)   )r   r-   r*   r   r\   r   )r   r-   r*   )pytestZraises
ValueErrorr   r	   r   r`   )r@   messagery   r   r   r   test_raises   sP    ,224.2,&&&zTestSobolIndices.test_raisesN)rR   
__module____qualname__r   r`   r   r   rJ   rC   rz   markZxfail_on_32bitZparametrizer	   paramr%   ZslowrZ   rb   rn   rt   rv   rw   r}   r   r   r   r   r&   2   s&   
3#

r&   )typingr   numpyr   Znumpy.testingr   r   rz   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   

