|
| 1 | +import numpy as np |
| 2 | +import numpy.testing as npt |
| 3 | +import pandas as pd |
| 4 | +from scipy.stats import qmc, uniform, lognorm |
| 5 | +import simdec as sd |
| 6 | + |
| 7 | +# Testing fix for issue #43 |
| 8 | + |
| 9 | + |
| 10 | +def test_sensitivity_indices_43(): |
| 11 | + m = 13 |
| 12 | + sampler = qmc.Sobol(d=2, scramble=True, seed=42) |
| 13 | + sample = sampler.random_base2(m=m) |
| 14 | + |
| 15 | + # deposit_0: uniform(500, 1500) |
| 16 | + deposit_0 = uniform.ppf(sample[:, 0], loc=500, scale=1000) |
| 17 | + |
| 18 | + # interest_rate: lognormal |
| 19 | + sigma = 0.5 |
| 20 | + mu = np.log(0.01) + sigma**2 |
| 21 | + interest_rate = lognorm.ppf(sample[:, 1], s=sigma, scale=np.exp(mu)) |
| 22 | + |
| 23 | + deposit_20 = deposit_0 * (1 + interest_rate) ** 20 |
| 24 | + |
| 25 | + inputs = pd.DataFrame({"deposit_0": deposit_0, "interest_rate": interest_rate}) |
| 26 | + output = pd.Series(deposit_20, name="deposit_20") |
| 27 | + |
| 28 | + res = sd.sensitivity_indices(inputs, output) |
| 29 | + |
| 30 | + # MATLAB Results |
| 31 | + expected_si = np.array([0.7101, 0.2739]) |
| 32 | + expected_foe = np.array([0.7028, 0.2666]) |
| 33 | + expected_soe_12 = 0.0146 |
| 34 | + |
| 35 | + npt.assert_allclose(res.si, expected_si, atol=3e-2) |
| 36 | + npt.assert_allclose(res.first_order, expected_foe, atol=3e-2) |
| 37 | + npt.assert_allclose(res.second_order[0, 1], expected_soe_12, atol=1e-2) |
0 commit comments