o
    h'                     @   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           axis)nppiarraysum)abvarv1v2v3v12v13v23s_firsts_seconds_total r)   f/var/www/html/scripts/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   r0   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      )r5      	      d   )e   h   k   n   )f   i   l   o   )g   j   m   p   AB)r   r   r
   r   )selfrJ   rK   refABr)   r)   r*   test_sample_AB8   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    rP   ndistsrandom_stater0   r   r   {Gz?atolc   )n_resamplesr   g?)confidence_levelr_   )r   randomdefault_rngr   rY   __name__r   first_ordertotal_order_bootstrap_result	bootstrap
isinstancer   r   confidence_intervallowshaper   high)rL   r+   rP   rngr/   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 )NrU   rV   r   r2   )rX   rY   rZ   rI   r   )r5   r   rp   f_Af_Bf_ABrW   r   r[   r\   )rP   rX   rZ   )r   ra   rb   r   uniformr   r   r
   r	   reshaper   r   rd   )
rL   r+   rm   rX   rY   rJ   rK   rN   rP   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   rp   r   r   r   rp   )r   r   meanT)rr   rs   rt   r   sstr)   r)   r*   jansen_sobol   s   "z2TestSobolIndices.test_method.<locals>.jansen_sobolrU   rV   )rP   rX   rY   methodrZ   r   r[   r\   r   rr   rs   rt   returnc                    s    | ||S Nr)   rq   r|   r)   r*   jansen_sobol_typed   s   z8TestSobolIndices.test_method.<locals>.jansen_sobol_typedr   )r   ra   rb   r   r	   rY   r   rd   re   ndarrayr   )rL   r+   rm   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 )
NrU   c                 S   s   t | d S )Ni  r,   r.   r)   r)   r*   <lambda>   s    z5TestSobolIndices.test_normalization.<locals>.<lambda>rV   rW   r   r[   r\   r   r   ra   rb   r   rY   r   rd   re   rL   r+   rm   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   r7   r,   r-   r)   r)   r*   f_ishigami_vec_const   s   zETestSobolIndices.test_constant_function.<locals>.f_ishigami_vec_constrU   rV   rW   r   )r   r   r   r   r[   r\   r   )rL   r+   r   rm   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 )NrU   i   rW   r   g-C6?r\   r   )	r   ra   rb   r   r	   rY   r   rd   re   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   ru   )rX   rP   rY   c                 S      | S r   r)   r   r)   r)   r*   r          z.TestSobolIndices.test_raises.<locals>.<lambda>z The balance properties of Sobol'r6   gffffff@z'toto' is not a valid 'method'toto)rX   rP   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)rX   rP   c                 S   s   |  ddS )Nrp   r   )rv   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)rr   rt   r   r5   )r   r9   r6   r   rq   r   )r   r9   r6   )pytestraises
ValueErrorr   r	   r   ru   )rL   messager   r)   r)   r*   test_raises   sf   $zTestSobolIndices.test_raisesN)rc   
__module____qualname__r   ru   r   r   rY   rO   r   markxfail_on_32bitparametrizer	   paramr0   slowro   rw   r   r   r   r   r   r)   r)   r)   r*   r1   2   s(    
3#

r1   )typingr   numpyr   numpy.testingr   r   r   scipyr   scipy.statsr   scipy.stats._resamplingr   !scipy.stats._sensitivity_analysisr   r	   r
   r   fixturer+   r0   r1   r)   r)   r)   r*   <module>   s    

