o
    i)                     @   s   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 )    N)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varZv1Zv2Zv3Zv12Zv13Zv23Zs_firstZs_secondZs_total r   e/home/kim/smarthome/.venv/lib/python3.10/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
j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   )selfr?   r@   refABr   r   r   test_sample_AB6   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    rE   ndistsrngr%   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)rA   r    rE   rK   r$   Zbootstrap_resr   r   r   test_ishigamiM   sV   


zTestSobolIndices.test_ishigamic           
      C   s8  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 tt|	dd|d  tt|	dd|d  tt|	d	|d  d S )NrF   rG   r   r'   )rI   rJ   rK   r>   r   )r*   r   rZ   f_Af_Bf_ABrH   r   rL   rM   )rE   rI   rK   r\   r]   r^   )r   rP   rQ   r   uniformr   r
   r	   r   reshaper   r   rS   )
rA   r    rK   rI   rJ   r?   r@   rC   rE   r$   r   r   r   test_func_dict   s4   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   rZ   r   r   r   rZ   )r   r   meanT)r\   r]   r^   r   sstr   r   r   jansen_sobol   s   "z2TestSobolIndices.test_method.<locals>.jansen_sobolrF   rG   )rE   rI   rJ   methodrK   r   rL   rM   r   r\   r]   r^   returnc                    s    | ||S Nr   r[   rf   r   r   jansen_sobol_typed   s   z8TestSobolIndices.test_method.<locals>.jansen_sobol_typedr   )r   rP   rQ   r   r   rJ   r   rS   rT   Zndarraytuple)rA   r    rK   r$   rk   _r   rj   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 )
NrF   c                 S   s   t | d S )Ni  r!   r#   r   r   r   <lambda>   s    z5TestSobolIndices.test_normalization.<locals>.<lambda>rG   rH   r   rL   rM   r   r   rP   rQ   r   rJ   r   rS   rT   rA   r    rK   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   rq   )rA   r    rt   rK   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   rr   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   r_   )rI   rE   rJ   c                 S      | S ri   r   ro   r   r   r   rp         z.TestSobolIndices.test_raises.<locals>.<lambda>z The balance properties of Sobol'r+   gffffff@z'toto' is not a valid 'method'Ztoto)rI   rE   rg   z!must have the following signaturec                 S   rx   ri   r   ro   r   r   r   rp     ry   z1'dists' must be defined when 'func' is a callable)rI   rE   c                 S   s   |  ddS )NrZ   r   )r`   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_   )rA   messagerz   r   r   r   test_raises   sf   $zTestSobolIndices.test_raisesN)rR   
__module____qualname__r   r_   r   r   rJ   rD   r{   markZxslowZxfail_on_32bitZparametrizer   paramr%   ZslowrY   ra   rn   rs   ru   rv   r~   r   r   r   r   r&   0   s*    
3(#

r&   )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    

