o
    h)                     @   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           axis)nppiarraysum)abvarv1v2v3v12v13v23s_firsts_seconds_total r(   _/var/www/vscode/kcb/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      )r4      	      d   )e   h   k   n   )f   i   l   o   )g   j   m   p   AB)r   r   r	   r   )selfrI   rJ   refABr(   r(   r)   test_sample_AB6   s0   zTestSobolIndices.test_sample_ABz!Can't create large array for testfunc)marksscalarvector)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    rO   ndistsrngr/   r   r   {Gz?atolc   )n_resamplesr   g?)confidence_levelr^   )r   randomdefault_rngr   rX   __name__r   first_ordertotal_order_bootstrap_result	bootstrap
isinstancer   r   confidence_intervallowshaper   high)rK   r*   rO   rY   r.   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 )NrT   rU   r   r1   )rW   rX   rY   rH   r   )r4   r   rn   f_Af_Bf_ABrV   r   rZ   r[   )rO   rW   rY   rp   rq   rr   )r   r`   ra   r   uniformr   r
   r	   r   reshaper   r   rc   )
rK   r*   rY   rW   rX   rI   rJ   rM   rO   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   rn   r   r   r   rn   )r   r   meanT)rp   rq   rr   r   sstr(   r(   r)   jansen_sobol   s   "z2TestSobolIndices.test_method.<locals>.jansen_sobolrT   rU   )rO   rW   rX   methodrY   r   rZ   r[   r   rp   rq   rr   returnc                    s    | ||S Nr(   ro   rz   r(   r)   jansen_sobol_typed   s   z8TestSobolIndices.test_method.<locals>.jansen_sobol_typedr   )r   r`   ra   r   r   rX   r   rc   rd   ndarraytuple)rK   r*   rY   r.   r   _r(   r~   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 )
NrT   c                 S   s   t | d S )Ni  r+   r-   r(   r(   r)   <lambda>   s    z5TestSobolIndices.test_normalization.<locals>.<lambda>rU   rV   r   rZ   r[   r   r   r`   ra   r   rX   r   rc   rd   rK   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   r6   r+   r,   r(   r(   r)   f_ishigami_vec_const   s   zETestSobolIndices.test_constant_function.<locals>.f_ishigami_vec_constrT   rU   rV   r   )r   r   r   r   rZ   r[   r   )rK   r*   r   rY   r.   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 )NrT   i   rV   r   g-C6?r[   r   )	r   r`   ra   r   r   rX   r   rc   rd   r   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   rs   )rW   rO   rX   c                 S      | S r}   r(   r   r(   r(   r)   r         z.TestSobolIndices.test_raises.<locals>.<lambda>z The balance properties of Sobol'r5   gffffff@z'toto' is not a valid 'method'toto)rW   rO   r{   z!must have the following signaturec                 S   r   r}   r(   r   r(   r(   r)   r     r   z1'dists' must be defined when 'func' is a callable)rW   rO   c                 S   s   |  ddS )Nrn   r   )rt   r   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)rp   rr   r   r4   )r   r8   r5   r   ro   r   )r   r8   r5   )pytestraises
ValueErrorr   r   r   rs   )rK   messager   r(   r(   r)   test_raises   sf   $zTestSobolIndices.test_raisesN)rb   
__module____qualname__r   rs   r   r   rX   rN   r   markxslowxfail_on_32bitparametrizer   paramr/   slowrm   ru   r   r   r   r   r   r(   r(   r(   r)   r0   0   s*    
3(#

r0   )numpyr   numpy.testingr   r   r   scipyr   scipy.statsr   scipy.stats._resamplingr   !scipy.stats._sensitivity_analysisr   r   r	   r
   fixturer*   r/   r0   r(   r(   r(   r)   <module>   s    

