L ia ddlZddlmZddlmZmZddlmZddlZ ddl m Z ddl m Z ddlmZddlmZmZdd lmZerddlmZdd lmZmZd gZd d Z d!dZde j8de j8d e j8fdZde j8de j8de j8d ee j8e j8ffdZeGddZ eGddZ!edddddddZ"y)"N) dataclass) TYPE_CHECKINGAny)Callable)ConfidenceInterval)check_random_state)BootstrapResult)qmc bootstrap)_transition_to_rng) DecimalNumber IntNumber sobol_indicesreturnctj|}tj|ddtj|ddzzzd|ddzztj|dzz}|S)aIshigami function. .. math:: Y(\mathbf{x}) = \sin x_1 + 7 \sin^2 x_2 + 0.1 x_3^4 \sin x_1 with :math:`\mathbf{x} \in [-\pi, \pi]^3`. Parameters ---------- x : array_like ([x1, x2, x3], n) Returns ------- f : array_like (n,) Function evaluation. References ---------- .. [1] Ishigami, T. and T. Homma. "An importance quantification technique in uncertainty analysis for computer models." IEEE, :doi:`10.1109/ISUMA.1990.151285`, 1990. rg?)np atleast_2dsin)xf_evals g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_sensitivity_analysis.py f_ishigamirso0 aA qt bffQqTlAo   1q/BFF1Q4L ( ) McPt|}tjd|z|dj|j}|j d|d} t |D]$\}}|j|dd|f|dd|f<& |S#t$r}d}t||d}~wwxYw)aASample two matrices A and B. Uses a Sobol' sequence with 2`d` columns to have 2 uncorrelated matrices. This is more efficient than using 2 random draw of Sobol'. See sec. 5 from [1]_. Output shape is (d, n). References ---------- .. [1] Saltelli, A., P. Annoni, I. Azzini, F. Campolongo, M. Ratto, and S. Tarantola. "Variance based sensitivity analysis of model output. Design and estimator for the total sensitivity index." Computer Physics Communications, 181(2):259-270, :doi:`10.1016/j.cpc.2009.09.018`, 2010. r@)dseedbitsNz4Each distribution in `dists` must have method `ppf`.) lenr SobolrandomTreshape enumerateppfAttributeError ValueError) ndistsrngr A_Bd_distexcmessages r sample_A_Br5:s* E A ))ac" - 4 4Q 7 9 9C ++aB C+!%( .HB#ae*-C2J . J +H!s*+s2B B%B  B%ABc|j\}}tj||ddf}tj|}|||||f<|S)aAB matrix. AB: rows of B into A. Shape (d, d, n). - Copy A into d "pages" - In the first page, replace 1st rows of A with 1st row of B. ... - In the dth page, replace dth row of A with dth row of B. - return the stack of pages r)shapertilearange)r6r7r r-ABis r sample_ABr>[sJ 77DAq Q1I B ! AtBq!tH Irf_Af_Bf_ABctj||gd}tj|||z zd|z }dtj||z dzdz|z }|j|jfS)aSaltelli2010 formulation. .. math:: S_i = \frac{1}{N} \sum_{j=1}^N f(\mathbf{B})_j (f(\mathbf{AB}^{(i)})_j - f(\mathbf{A})_j) .. math:: S_{T_i} = \frac{1}{N} \sum_{j=1}^N (f(\mathbf{A})_j - f(\mathbf{AB}^{(i)})_j)^2 Parameters ---------- f_A, f_B : array_like (s, n) Function values at A and B, respectively f_AB : array_like (d, s, n) Function values at each of the AB pages Returns ------- s, st : array_like (s, d) First order and total order Sobol' indices. References ---------- .. [1] Saltelli, A., P. Annoni, I. Azzini, F. Campolongo, M. Ratto, and S. Tarantola. "Variance based sensitivity analysis of model output. Design and estimator for the total sensitivity index." Computer Physics Communications, 181(2):259-270, :doi:`10.1016/j.cpc.2009.09.018`, 2010. rr#axisr#g?r)rvarmeanr')r?r@rArFssts r saltelli_2010rJlsnJ &&#s' *C tcz",s2A rwwd q(r2 2S 8B 339rc"eZdZUeed<eed<y)BootstrapSobolResult first_order total_orderN)__name__ __module__ __qualname__r __annotations__rrrLrLs    rrLcTeZdZUejed<ejed<eed<ejed<ejed<ejed<dZejdzed<dZejdzed <dZ ejdzed <dZ e dzed < dd d ddde fdZ y) SobolResultrMrN_indices_method_f_A_f_B_f_ABN_A_B_AB_bootstrap_resultconfidence_levelr n_resamplesrrcFfd}jjd}ttj|g|d||j }|_t t|jjd|jjd|jd|jd}t t|jjd|jjd|jd|jd}t||S)a`Bootstrap Sobol' indices to provide confidence intervals. Parameters ---------- confidence_level : float, default: ``0.95`` The confidence level of the confidence intervals. n_resamples : int, default: ``999`` The number of resamples performed to form the bootstrap distribution of the indices. Returns ------- res : BootstrapSobolResult Bootstrap result containing the confidence intervals and the bootstrap distribution of the indices. An object with attributes: first_order : BootstrapResult Bootstrap result of the first order indices. total_order : BootstrapResult Bootstrap result of the total order indices. See `BootstrapResult` for more details. cjdd|f}jdd|f}jd|f}j|||S)N.)rWrXrYrV)idxf_A_f_B_f_AB_selfs r statisticz(SobolResult.bootstrap..statisticsN99QV$D99QV$DJJsCx(E''dE: :rrBCa)rgmethodr_r^bootstrap_resultr)confidence_intervalbootstrap_distributionstandard_error)rMrN)rWr9r rr;r]r rrklowhighrlrmrL)rfr^r_rgr-resrMrNs` rr zSobolResult.bootstraps< ; IIOOA  YYq\Ni#-!33   "%% 2''++A.0G0G0L0LQ0O!$'#=#=a#@--a0  & 2''++A.0G0G0L0LQ0O!$'#=#=a#@--a0  $#  r)gffffff?i)rOrPrQrndarrayrRrrZr[r\r]r rLr rSrrrUrUs ** ** :: B T  B T !Cd !04-4-1#&? )? !?   ? rrU random_stateF) replace_docrJ)r.rir/c t|}t|}||dz zdk(r||k7r td|}t|sdti} |j }||nX|tj} t| jhdk7r'd tjt}t|fd } trk| td fd t||| \} } t| | } | }|jd|k7r tdfd}| }|| }nd} tdtj ddd\}}}|jd|k7sG|j|jk7s.|j|jk(s|jd|zdk7r t|tj"||gdj%dd}||z}||z}||z}tj&dd5| |||\}}ddddtj(|<dtj(|<t+||| |||}tr|j-t+   t/di|S#t $r"}|dt|d}t||d}~wwxYw#t $r}t||d}~wwxYw#1swYxYw)a6Global sensitivity indices of Sobol'. Parameters ---------- func : callable or dict(str, array_like) If `func` is a callable, function to compute the Sobol' indices from. Its signature must be:: func(x: ArrayLike) -> ArrayLike with ``x`` of shape ``(d, n)`` and output of shape ``(s, n)`` where: - ``d`` is the input dimensionality of `func` (number of input variables), - ``s`` is the output dimensionality of `func` (number of output variables), and - ``n`` is the number of samples (see `n` below). Function evaluation values must be finite. If `func` is a dictionary, contains the function evaluations from three different arrays. Keys must be: ``f_A``, ``f_B`` and ``f_AB``. ``f_A`` and ``f_B`` should have a shape ``(s, n)`` and ``f_AB`` should have a shape ``(d, s, n)``. This is an advanced feature and misuse can lead to wrong analysis. n : int Number of samples used to generate the matrices ``A`` and ``B``. Must be a power of 2. The total number of points at which `func` is evaluated will be ``n*(d+2)``. dists : list(distributions), optional List of each parameter's distribution. The distribution of parameters depends on the application and should be carefully chosen. Parameters are assumed to be independently distributed, meaning there is no constraint nor relationship between their values. Distributions must be an instance of a class with a ``ppf`` method. Must be specified if `func` is a callable, and ignored otherwise. method : Callable or str, default: 'saltelli_2010' Method used to compute the first and total Sobol' indices. If a callable, its signature must be:: func(f_A: np.ndarray, f_B: np.ndarray, f_AB: np.ndarray) -> Tuple[np.ndarray, np.ndarray] with ``f_A, f_B`` of shape ``(s, n)`` and ``f_AB`` of shape ``(d, s, n)``. These arrays contain the function evaluations from three different sets of samples. The output is a tuple of the first and total indices with shape ``(s, d)``. This is an advanced feature and misuse can lead to wrong analysis. rng : `numpy.random.Generator`, optional Pseudorandom number generator state. When `rng` is None, a new `numpy.random.Generator` is created using entropy from the operating system. Types other than `numpy.random.Generator` are passed to `numpy.random.default_rng` to instantiate a ``Generator``. .. versionchanged:: 1.15.0 As part of the `SPEC-007 `_ transition from use of `numpy.random.RandomState` to `numpy.random.Generator`, this keyword was changed from `random_state` to `rng`. For an interim period, both keywords will continue to work, although only one may be specified at a time. After the interim period, function calls using the `random_state` keyword will emit warnings. Following a deprecation period, the `random_state` keyword will be removed. Returns ------- res : SobolResult An object with attributes: first_order : ndarray of shape (s, d) First order Sobol' indices. total_order : ndarray of shape (s, d) Total order Sobol' indices. And method: bootstrap(confidence_level: float, n_resamples: int) -> BootstrapSobolResult A method providing confidence intervals on the indices. See `scipy.stats.bootstrap` for more details. The bootstrapping is done on both first and total order indices, and they are available in `BootstrapSobolResult` as attributes ``first_order`` and ``total_order``. Notes ----- The Sobol' method [1]_, [2]_ is a variance-based Sensitivity Analysis which obtains the contribution of each parameter to the variance of the quantities of interest (QoIs; i.e., the outputs of `func`). Respective contributions can be used to rank the parameters and also gauge the complexity of the model by computing the model's effective (or mean) dimension. .. note:: Parameters are assumed to be independently distributed. Each parameter can still follow any distribution. In fact, the distribution is very important and should match the real distribution of the parameters. It uses a functional decomposition of the variance of the function to explore .. math:: \mathbb{V}(Y) = \sum_{i}^{d} \mathbb{V}_i (Y) + \sum_{i= 2**12``. The more complex the model is, the more samples will be needed. Even for a purely additive model, the indices may not sum to 1 due to numerical noise. References ---------- .. [1] Sobol, I. M.. "Sensitivity analysis for nonlinear mathematical models." Mathematical Modeling and Computational Experiment, 1:407-414, 1993. .. [2] Sobol, I. M. (2001). "Global sensitivity indices for nonlinear mathematical models and their Monte Carlo estimates." Mathematics and Computers in Simulation, 55(1-3):271-280, :doi:`10.1016/S0378-4754(00)00270-6`, 2001. .. [3] Saltelli, A. "Making best use of model evaluations to compute sensitivity indices." Computer Physics Communications, 145(2):280-297, :doi:`10.1016/S0010-4655(02)00280-1`, 2002. .. [4] Saltelli, A., M. Ratto, T. Andres, F. Campolongo, J. Cariboni, D. Gatelli, M. Saisana, and S. Tarantola. "Global Sensitivity Analysis. The Primer." 2007. .. [5] Saltelli, A., P. Annoni, I. Azzini, F. Campolongo, M. Ratto, and S. Tarantola. "Variance based sensitivity analysis of model output. Design and estimator for the total sensitivity index." Computer Physics Communications, 181(2):259-270, :doi:`10.1016/j.cpc.2009.09.018`, 2010. .. [6] Ishigami, T. and T. Homma. "An importance quantification technique in uncertainty analysis for computer models." IEEE, :doi:`10.1109/ISUMA.1990.151285`, 1990. Examples -------- The following is an example with the Ishigami function [6]_ .. math:: Y(\mathbf{x}) = \sin x_1 + 7 \sin^2 x_2 + 0.1 x_3^4 \sin x_1, with :math:`\mathbf{x} \in [-\pi, \pi]^3`. This function exhibits strong non-linearity and non-monotonicity. Remember, Sobol' indices assumes that samples are independently distributed. In this case we use a uniform distribution on each marginals. >>> import numpy as np >>> from scipy.stats import sobol_indices, uniform >>> rng = np.random.default_rng() >>> def f_ishigami(x): ... f_eval = ( ... np.sin(x[0]) ... + 7 * np.sin(x[1])**2 ... + 0.1 * (x[2]**4) * np.sin(x[0]) ... ) ... return f_eval >>> indices = sobol_indices( ... func=f_ishigami, n=1024, ... dists=[ ... uniform(loc=-np.pi, scale=2*np.pi), ... uniform(loc=-np.pi, scale=2*np.pi), ... uniform(loc=-np.pi, scale=2*np.pi) ... ], ... rng=rng ... ) >>> indices.first_order array([0.31637954, 0.43781162, 0.00318825]) >>> indices.total_order array([0.56122127, 0.44287857, 0.24229595]) Confidence interval can be obtained using bootstrapping. >>> boot = indices.bootstrap() Then, this information can be easily visualized. >>> import matplotlib.pyplot as plt >>> fig, axs = plt.subplots(1, 2, figsize=(9, 4)) >>> _ = axs[0].errorbar( ... [1, 2, 3], indices.first_order, fmt='o', ... yerr=[ ... indices.first_order - boot.first_order.confidence_interval.low, ... boot.first_order.confidence_interval.high - indices.first_order ... ], ... ) >>> axs[0].set_ylabel("First order Sobol' indices") >>> axs[0].set_xlabel('Input parameters') >>> axs[0].set_xticks([1, 2, 3]) >>> _ = axs[1].errorbar( ... [1, 2, 3], indices.total_order, fmt='o', ... yerr=[ ... indices.total_order - boot.total_order.confidence_interval.low, ... boot.total_order.confidence_interval.high - indices.total_order ... ], ... ) >>> axs[1].set_ylabel("Total order Sobol' indices") >>> axs[1].set_xlabel('Input parameters') >>> axs[1].set_xticks([1, 2, 3]) >>> plt.tight_layout() >>> plt.show() .. note:: By default, `scipy.stats.uniform` has support ``[0, 1]``. Using the parameters ``loc`` and ``scale``, one obtains the uniform distribution on ``[loc, loc + scale]``. This result is particularly interesting because the first order index :math:`S_{x_3} = 0` whereas its total order is :math:`S_{T_{x_3}} = 0.244`. This means that higher order interactions with :math:`x_3` are responsible for the difference. Almost 25% of the observed variance on the QoI is due to the correlations between :math:`x_3` and :math:`x_1`, although :math:`x_3` by itself has no impact on the QoI. The following gives a visual explanation of Sobol' indices on this function. Let's generate 1024 samples in :math:`[-\pi, \pi]^3` and calculate the value of the output. >>> from scipy.stats import qmc >>> n_dim = 3 >>> p_labels = ['$x_1$', '$x_2$', '$x_3$'] >>> sample = qmc.Sobol(d=n_dim, seed=rng).random(1024) >>> sample = qmc.scale( ... sample=sample, ... l_bounds=[-np.pi, -np.pi, -np.pi], ... u_bounds=[np.pi, np.pi, np.pi] ... ) >>> output = f_ishigami(sample.T) Now we can do scatter plots of the output with respect to each parameter. This gives a visual way to understand how each parameter impacts the output of the function. >>> fig, ax = plt.subplots(1, n_dim, figsize=(12, 4)) >>> for i in range(n_dim): ... xi = sample[:, i] ... ax[i].scatter(xi, output, marker='+') ... ax[i].set_xlabel(p_labels[i]) >>> ax[0].set_ylabel('Y') >>> plt.tight_layout() >>> plt.show() Now Sobol' goes a step further: by conditioning the output value by given values of the parameter (black lines), the conditional output mean is computed. It corresponds to the term :math:`\mathbb{E}(Y|x_i)`. Taking the variance of this term gives the numerator of the Sobol' indices. >>> mini = np.min(output) >>> maxi = np.max(output) >>> n_bins = 10 >>> bins = np.linspace(-np.pi, np.pi, num=n_bins, endpoint=False) >>> dx = bins[1] - bins[0] >>> fig, ax = plt.subplots(1, n_dim, figsize=(12, 4)) >>> for i in range(n_dim): ... xi = sample[:, i] ... ax[i].scatter(xi, output, marker='+') ... ax[i].set_xlabel(p_labels[i]) ... for bin_ in bins: ... idx = np.where((bin_ <= xi) & (xi <= bin_ + dx)) ... xi_ = xi[idx] ... y_ = output[idx] ... ave_y_ = np.mean(y_) ... ax[i].plot([bin_ + dx/2] * 2, [mini, maxi], c='k') ... ax[i].scatter(bin_ + dx/2, ave_y_, c='r') >>> ax[0].set_ylabel('Y') >>> plt.tight_layout() >>> plt.show() Looking at :math:`x_3`, the variance of the mean is zero leading to :math:`S_{x_3} = 0`. But we can further observe that the variance of the output is not constant along the parameter values of :math:`x_3`. This heteroscedasticity is explained by higher order interactions. Moreover, an heteroscedasticity is also noticeable on :math:`x_1` leading to an interaction between :math:`x_3` and :math:`x_1`. On :math:`x_2`, the variance seems to be constant and thus null interaction with this parameter can be supposed. This case is fairly simple to analyse visually---although it is only a qualitative analysis. Nevertheless, when the number of input parameters increases such analysis becomes unrealistic as it would be difficult to conclude on high-order terms. Hence the benefit of using Sobol' indices. rrzGThe balance properties of Sobol' points require 'n' to be a power of 2.rJz, is not a valid 'method'. It must be one of z or a callable.N>r?r@rAzAIf 'method' is a callable, it must have the following signature: c@tj|||S)zmWrap indices method to ensure proper output dimension. 1D when single output, 2D otherwise. r?r@rA)rsqueeze)r?r@rAindices_method_s rindices_methodz%sobol_indices..indices_methodps zz/csFGGrz2'dists' must be defined when 'func' is a callable.c:tj|SN)rr)rfuncs r wrapped_funcz#sobol_indices..wrapped_func}s==a) )r)r-r.r/)r6r7zN'func' output should have a shape ``(s, -1)`` with ``s`` the number of output.c|j\}}}tj|ddj|||z}|}tj|jd||fddS)Nrr#)r9rmoveaxisr()r<r r-rAr}s rfuncABzsobol_indices..funcABsahhGAq!RB'//1Q37B#D;;t||RAJ7Q? ?rzWhen 'func' is a dictionary, it must contain the following keys: 'f_A', 'f_B' and 'f_AB'.'f_A' and 'f_B' should have a shape ``(s, n)`` and 'f_AB' should have a shape ``(d, s, n)``.c"|jSr{)copy)arrs rzsobol_indices..s SXXZrr?r@rAr#rCrDignore)divideinvalidrv)rMrNrVrWrXrY)rZr[r\rS)rintr,callablerJlowerKeyErrorsetinspect signature parametersr5r>r9maprrrGr(errstateisfinitedictupdaterU)r|r-r.rir/n_indices_methodsr3r4sigryr6r7r<r?rr@rArGrMrNrprxr}s` @@rrrsG~ S !C QB "q&MQ 17 "   A F  ]  /\\^F-f5O!0 s~~ "8 8&00?@B W% %H~ =D  *AU41 a 1o 99Q<1 (   @ 1obz 1   / !7U T%[$v,:NCd 99Q<1  SYY 6 cii'4::b>A+=+BW% % 77C:G , 4 4R ;D4KC4KCDLD Hh 7O#1cs#N [O./K[))*-.K[))* &    C~      U /*(+?< W%3 .  /~ /W%3 . /$OOs< J&7/KK1& K/K  K K. K))K.1K:)rz npt.ArrayLikerznpt.NDArray[np.inexact[Any]]r{)#r dataclassesrtypingrrcollections.abcrnumpyrscipy.stats._commonrscipy.stats._qmcrscipy.stats._resamplingr scipy.statsr r scipy._lib._utilr numpy.typingnptr r__all__rr5rqr>tuplerJrLrUrrSrrrs!%$2/3&/9  H B rzz", ,**,,.JJ, 2::rzz !",^ !! !  K K  K ZN6   Y7Yr