|
15 | 15 | "This is a tutorial for the unsequa module in CLIMADA. A detailled description can be found in [Kropf (2021)](https://eartharxiv.org/repository/view/3123/)." |
16 | 16 | ] |
17 | 17 | }, |
18 | | - { |
19 | | - "cell_type": "markdown", |
20 | | - "metadata": {}, |
21 | | - "source": [ |
22 | | - "# Table of Contents\n", |
23 | | - "- [Unsequa - a module for uncertainty and sensitivity analysis](#unsequa-a-module-for-uncertainty-and-sensitivity-analysis)\n", |
24 | | - " - [Uncertainty and sensitivity analysis](#uncertainty-and-sensitivity-analysis)\n", |
25 | | - " - [Unsequa Module Structure](#unsequa-module-structure)\n", |
26 | | - " - [InputVar](#inputvar)\n", |
27 | | - " - [Example - custom continuous uncertainty parameter](#example-custom-continuous-uncertainty-parameter)\n", |
28 | | - " - [Example - custom categorical uncertainty parameter](#example-custom-categorical-uncertainty-parameter)\n", |
29 | | - " - [UncOutput](#uncoutput)\n", |
30 | | - " - [Example from file](#example-from-file)\n", |
31 | | - " - [CalcImpact](#calcimpact)\n", |
32 | | - " - [Set the InputVars](#set-the-inputvars)\n", |
33 | | - " - [Compute uncertainty and sensitivity using default methods](#compute-uncertainty-and-sensitivity-using-default-methods)\n", |
34 | | - " - [A few non-default parameters](#a-few-non-default-parameters)\n", |
35 | | - " - [CalcDeltaImpact](#calcdeltaimpact)\n", |
36 | | - " - [Set the Input Vars](#set-the-input-vars)\n", |
37 | | - " - [Compute uncertainty and sensitivity](#compute-uncertainty-and-sensitivity)\n", |
38 | | - " - [CalcCostBenefit](#calccostbenefit)\n", |
39 | | - " - [Set the Input Vars](#set-the-input-vars)\n", |
40 | | - " - [Compute cost benefit uncertainty and sensitivity using default methods](#compute-cost-benefit-uncertainty-and-sensitivity-using-default-methods)\n", |
41 | | - " - [Advanced examples](#advanced-examples)\n", |
42 | | - " - [Coupled variables](#coupled-variables)\n", |
43 | | - " - [Many scenarios of hazards and exposures](#many-scenarios-of-hazards-and-exposures)\n", |
44 | | - " - [Input variable: Repeated loading of files made efficient](#input-variable-repeated-loading-of-files-made-efficient)" |
45 | | - ] |
46 | | - }, |
47 | 18 | { |
48 | 19 | "cell_type": "markdown", |
49 | 20 | "metadata": {}, |
|
183 | 154 | "outputs": [], |
184 | 155 | "source": [ |
185 | 156 | "import warnings\n", |
186 | | - "warnings.filterwarnings('ignore') #Ignore warnings for making the tutorial's pdf. \n", |
| 157 | + "warnings.filterwarnings('ignore') #Ignore warnings for making the tutorial's pdf.\n", |
187 | 158 | "\n", |
188 | 159 | "#Define the base exposure\n", |
189 | 160 | "from climada.util.constants import EXP_DEMO_H5\n", |
|
346 | 317 | "\n", |
347 | 318 | "# Define the function\n", |
348 | 319 | "# Note that this here works, but might be slow because the method LitPop is called everytime the the function\n", |
349 | | - "# is evaluated, and LitPop is relatively slow. \n", |
| 320 | + "# is evaluated, and LitPop is relatively slow.\n", |
350 | 321 | "def litpop_cat(m, n):\n", |
351 | 322 | " exp = Litpop.from_countries('CHE', res_arcsec=150, exponent=[m, n])\n", |
352 | 323 | " return exp" |
|
372 | 343 | " for n in range(n_min, n_max + 1):\n", |
373 | 344 | " exp_mn = LitPop.from_countries('CHE', res_arcsec=150, exponents=[m, n]);\n", |
374 | 345 | " litpop_dict[(m, n)] = exp_mn\n", |
375 | | - " \n", |
| 346 | + "\n", |
376 | 347 | "def litpop_cat(m, n, litpop_dict=litpop_dict):\n", |
377 | 348 | " return litpop_dict[(m, n)]" |
378 | 349 | ] |
|
992 | 963 | "from climada.hazard import Hazard\n", |
993 | 964 | "\n", |
994 | 965 | "def impf_func(G=1, v_half=84.7, vmin=25.7, k=3, _id=1):\n", |
995 | | - " \n", |
| 966 | + "\n", |
996 | 967 | " def xhi(v, v_half, vmin):\n", |
997 | 968 | " return max([(v - vmin), 0]) / (v_half - vmin)\n", |
998 | 969 | "\n", |
999 | 970 | " def sigmoid_func(v, G, v_half, vmin, k):\n", |
1000 | 971 | " return G * xhi(v, v_half, vmin)**k / (1 + xhi(v, v_half, vmin)**k)\n", |
1001 | 972 | "\n", |
1002 | 973 | " #In-function imports needed only for parallel computing on Windows\n", |
1003 | | - " import numpy as np \n", |
1004 | | - " from climada.entity import ImpactFunc, ImpactFuncSet \n", |
| 974 | + " import numpy as np\n", |
| 975 | + " from climada.entity import ImpactFunc, ImpactFuncSet\n", |
1005 | 976 | " intensity_unit = 'm/s'\n", |
1006 | 977 | " intensity = np.linspace(0, 150, num=100)\n", |
1007 | 978 | " mdd = np.repeat(1, len(intensity))\n", |
|
1015 | 986 | "exp_base = Exposures.from_hdf5(EXP_DEMO_H5)\n", |
1016 | 987 | "#It is a good idea to assign the centroids to the base exposures in order to avoid repeating this\n", |
1017 | 988 | "# potentially costly operation for each sample.\n", |
1018 | | - "exp_base.assign_centroids(haz) \n", |
| 989 | + "exp_base.assign_centroids(haz)\n", |
1019 | 990 | "def exp_base_func(x_exp, exp_base):\n", |
1020 | 991 | " exp = exp_base.copy()\n", |
1021 | 992 | " exp.gdf.value *= x_exp\n", |
|
1066 | 1037 | "import scipy as sp\n", |
1067 | 1038 | "from climada.engine.unsequa import InputVar\n", |
1068 | 1039 | "\n", |
1069 | | - "exp_distr = {\"x_exp\": sp.stats.beta(10, 1.1)} #This is not really a reasonable distribution but is used \n", |
| 1040 | + "exp_distr = {\"x_exp\": sp.stats.beta(10, 1.1)} #This is not really a reasonable distribution but is used\n", |
1070 | 1041 | " #here to show that you can use any scipy distribution.\n", |
1071 | | - " \n", |
| 1042 | + "\n", |
1072 | 1043 | "exp_iv = InputVar(exp_func, exp_distr)\n", |
1073 | 1044 | "\n", |
1074 | 1045 | "impf_distr = {\n", |
|
2128 | 2099 | ], |
2129 | 2100 | "source": [ |
2130 | 2101 | "# Compute also the distribution of the metric `eai_exp`\n", |
2131 | | - "# To speed-up the comutations, we can use more than one process \n", |
| 2102 | + "# To speed-up the comutations, we can use more than one process\n", |
2132 | 2103 | "# Note that for large dataset a single process might be more efficient\n", |
2133 | 2104 | "import time\n", |
2134 | 2105 | "\n", |
|
2244 | 2215 | }, |
2245 | 2216 | "outputs": [], |
2246 | 2217 | "source": [ |
2247 | | - "# Use the method 'rbd_fast' which is recommend in pair with 'latin'. In addition, change one of the kwargs \n", |
| 2218 | + "# Use the method 'rbd_fast' which is recommend in pair with 'latin'. In addition, change one of the kwargs\n", |
2248 | 2219 | "# (M=15) of the salib sampling method.\n", |
2249 | 2220 | "output_imp2 = calc_imp2.sensitivity(output_imp2, sensitivity_method='rbd_fast', sensitivity_kwargs = {'M': 15})" |
2250 | 2221 | ] |
|
2431 | 2402 | "from climada.hazard import Centroids, TCTracks, Hazard, TropCyclone\n", |
2432 | 2403 | "\n", |
2433 | 2404 | "def impf_func(G=1, v_half=84.7, vmin=25.7, k=3, _id=1):\n", |
2434 | | - " \n", |
| 2405 | + "\n", |
2435 | 2406 | " def xhi(v, v_half, vmin):\n", |
2436 | 2407 | " return max([(v - vmin), 0]) / (v_half - vmin)\n", |
2437 | 2408 | "\n", |
|
2488 | 2459 | "exp_base = Exposures.from_hdf5(EXP_DEMO_H5)\n", |
2489 | 2460 | "#It is a good idea to assign the centroids to the base exposures in order to avoid repeating this\n", |
2490 | 2461 | "# potentially costly operation for each sample.\n", |
2491 | | - "exp_base.assign_centroids(haz) \n", |
| 2462 | + "exp_base.assign_centroids(haz)\n", |
2492 | 2463 | "def exp_base_func(x_exp, exp_base):\n", |
2493 | 2464 | " exp = exp_base.copy()\n", |
2494 | 2465 | " exp.gdf.value *= x_exp\n", |
|
2506 | 2477 | "import scipy as sp\n", |
2507 | 2478 | "from climada.engine.unsequa import InputVar\n", |
2508 | 2479 | "\n", |
2509 | | - "exp_distr = {\"x_exp\": sp.stats.beta(10, 1.1)} #This is not really a reasonable distribution but is used \n", |
| 2480 | + "exp_distr = {\"x_exp\": sp.stats.beta(10, 1.1)} #This is not really a reasonable distribution but is used\n", |
2510 | 2481 | " #here to show that you can use any scipy distribution.\n", |
2511 | | - " \n", |
| 2482 | + "\n", |
2512 | 2483 | "exp_iv = InputVar(exp_func, exp_distr)\n", |
2513 | 2484 | "\n", |
2514 | 2485 | "impf_distr = {\n", |
|
2602 | 2573 | ], |
2603 | 2574 | "source": [ |
2604 | 2575 | "from climada.engine.unsequa import CalcDeltaImpact\n", |
2605 | | - "calc_imp = CalcDeltaImpact(exp_iv, impf_iv, haz, \n", |
| 2576 | + "calc_imp = CalcDeltaImpact(exp_iv, impf_iv, haz,\n", |
2606 | 2577 | " exp_iv, impf_iv, haz_fut_iv)" |
2607 | 2578 | ] |
2608 | 2579 | }, |
|
2822 | 2793 | "# Entity today has an uncertainty in the total asset value\n", |
2823 | 2794 | "def ent_today_func(x_ent):\n", |
2824 | 2795 | " #In-function imports needed only for parallel computing on Windows\n", |
2825 | | - " from climada.entity import Entity \n", |
2826 | | - " from climada.util.constants import ENT_DEMO_TODAY \n", |
| 2796 | + " from climada.entity import Entity\n", |
| 2797 | + " from climada.util.constants import ENT_DEMO_TODAY\n", |
2827 | 2798 | " entity = Entity.from_excel(ENT_DEMO_TODAY)\n", |
2828 | 2799 | " entity.exposures.ref_year = 2018\n", |
2829 | 2800 | " entity.exposures.gdf.value *= x_ent\n", |
|
2832 | 2803 | "# Entity in the future has a +- 10% uncertainty in the cost of all the adapatation measures\n", |
2833 | 2804 | "def ent_fut_func(m_fut_cost):\n", |
2834 | 2805 | " #In-function imports needed only for parallel computing on Windows\n", |
2835 | | - " from climada.entity import Entity \n", |
2836 | | - " from climada.util.constants import ENT_DEMO_FUTURE \n", |
| 2806 | + " from climada.entity import Entity\n", |
| 2807 | + " from climada.util.constants import ENT_DEMO_FUTURE\n", |
2837 | 2808 | " entity = Entity.from_excel(ENT_DEMO_FUTURE)\n", |
2838 | | - " entity.exposures.ref_year = 2040 \n", |
| 2809 | + " entity.exposures.ref_year = 2040\n", |
2839 | 2810 | " for meas in entity.measures.get_measure('TC'):\n", |
2840 | 2811 | " meas.cost *= m_fut_cost\n", |
2841 | 2812 | " return entity\n", |
|
2844 | 2815 | "# The hazard intensity in the future is also uncertainty by a multiplicative factor\n", |
2845 | 2816 | "def haz_fut(x_haz_fut, haz_base):\n", |
2846 | 2817 | " #In-function imports needed only for parallel computing on Windows\n", |
2847 | | - " import copy \n", |
2848 | | - " from climada.hazard import Hazard \n", |
2849 | | - " from climada.util.constants import HAZ_DEMO_H5 \n", |
| 2818 | + " import copy\n", |
| 2819 | + " from climada.hazard import Hazard\n", |
| 2820 | + " from climada.util.constants import HAZ_DEMO_H5\n", |
2850 | 2821 | " haz = copy.deepcopy(haz_base)\n", |
2851 | 2822 | " haz.intensity = haz.intensity.multiply(x_haz_fut)\n", |
2852 | 2823 | " return haz\n", |
|
5100 | 5071 | } |
5101 | 5072 | ], |
5102 | 5073 | "source": [ |
5103 | | - "# The impact_meas_present and impact_meas_future provide values of the cost_meas, risk_transf, risk, \n", |
| 5074 | + "# The impact_meas_present and impact_meas_future provide values of the cost_meas, risk_transf, risk,\n", |
5104 | 5075 | "# and cost_ins for each measure\n", |
5105 | 5076 | "output_cb.get_uncertainty(metric_list=['imp_meas_present']).tail()" |
5106 | 5077 | ] |
|
5286 | 5257 | "metadata": {}, |
5287 | 5258 | "outputs": [], |
5288 | 5259 | "source": [ |
5289 | | - "#Define the input variable \n", |
| 5260 | + "#Define the input variable\n", |
5290 | 5261 | "from climada.entity import ImpactFuncSet, Exposures\n", |
5291 | 5262 | "from climada.entity.impact_funcs.storm_europe import ImpfStormEurope\n", |
5292 | 5263 | "from climada.hazard import Hazard\n", |
|
5306 | 5277 | "\n", |
5307 | 5278 | "\n", |
5308 | 5279 | "def haz_func(cnt, i_haz, haz_list=haz_list):\n", |
5309 | | - " haz = copy.deepcopy(haz_list[int(cnt)]) #use the same parameter name accross input variables \n", |
| 5280 | + " haz = copy.deepcopy(haz_list[int(cnt)]) #use the same parameter name accross input variables\n", |
5310 | 5281 | " haz.intensity *= i_haz\n", |
5311 | 5282 | " return haz\n", |
5312 | 5283 | "\n", |
|
5588 | 5559 | "\n", |
5589 | 5560 | "\n", |
5590 | 5561 | "def impf_func(G=1, v_half=84.7, vmin=25.7, k=3, _id=1):\n", |
5591 | | - " \n", |
| 5562 | + "\n", |
5592 | 5563 | " def xhi(v, v_half, vmin):\n", |
5593 | 5564 | " return max([(v - vmin), 0]) / (v_half - vmin)\n", |
5594 | 5565 | "\n", |
5595 | 5566 | " def sigmoid_func(v, G, v_half, vmin, k):\n", |
5596 | 5567 | " return G * xhi(v, v_half, vmin)**k / (1 + xhi(v, v_half, vmin)**k)\n", |
5597 | 5568 | "\n", |
5598 | 5569 | " #In-function imports needed only for parallel computing on Windows\n", |
5599 | | - " import numpy as np \n", |
5600 | | - " from climada.entity import ImpactFunc, ImpactFuncSet \n", |
| 5570 | + " import numpy as np\n", |
| 5571 | + " from climada.entity import ImpactFunc, ImpactFuncSet\n", |
5601 | 5572 | " imp_fun = ImpactFunc()\n", |
5602 | 5573 | " imp_fun.haz_type = 'WS'\n", |
5603 | 5574 | " imp_fun.id = _id\n", |
|
5715 | 5686 | "name": "python", |
5716 | 5687 | "nbconvert_exporter": "python", |
5717 | 5688 | "pygments_lexer": "ipython3", |
5718 | | - "version": "3.9.16" |
| 5689 | + "version": "3.9.18" |
5719 | 5690 | }, |
5720 | 5691 | "latex_envs": { |
5721 | 5692 | "LaTeX_envs_menu_present": true, |
|
0 commit comments