Skip to content

Commit ea0eb47

Browse files
committed
Update tutorial
1 parent 3d94151 commit ea0eb47

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

doc/tutorial/climada_util_calibrate.ipynb

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,8 @@
748748
"</div>"
749749
],
750750
"text/plain": [
751-
" country region_id cal_region2 year EM_ID ibtracsID \n",
752-
"326 MEX 484 NA1 2010 2010-0260 2010176N16278 \\\n",
751+
" country region_id cal_region2 year EM_ID ibtracsID \\\n",
752+
"326 MEX 484 NA1 2010 2010-0260 2010176N16278 \n",
753753
"331 ATG 28 NA1 2010 2010-0468 2010236N12341 \n",
754754
"334 MEX 484 NA1 2010 2010-0494 2010257N16282 \n",
755755
"339 LCA 662 NA1 2010 2010-0571 2010302N09306 \n",
@@ -777,8 +777,8 @@
777777
"464 DOM 214 NA1 2017 2017-0383 2017260N12310 \n",
778778
"465 PRI 630 NA1 2017 2017-0383 2017260N12310 \n",
779779
"\n",
780-
" emdat_impact reference_year emdat_impact_scaled climada_impact ... \n",
781-
"326 2.000000e+09 2014 2.485465e+09 2.478270e+09 ... \\\n",
780+
" emdat_impact reference_year emdat_impact_scaled climada_impact ... \\\n",
781+
"326 2.000000e+09 2014 2.485465e+09 2.478270e+09 ... \n",
782782
"331 1.260000e+07 2014 1.394594e+07 1.402875e+07 ... \n",
783783
"334 3.900000e+09 2014 4.846656e+09 4.857140e+09 ... \n",
784784
"339 5.000000e+05 2014 5.486675e+05 5.492871e+05 ... \n",
@@ -806,8 +806,8 @@
806806
"464 6.300000e+07 2014 5.481371e+07 5.493466e+07 ... \n",
807807
"465 6.800000e+10 2014 6.700905e+10 6.702718e+10 ... \n",
808808
"\n",
809-
" scale log_ratio unique_ID Associated_disaster Surge Rain Flood \n",
810-
"326 1.0 -0.002899 2010-0260MEX True False False True \\\n",
809+
" scale log_ratio unique_ID Associated_disaster Surge Rain Flood \\\n",
810+
"326 1.0 -0.002899 2010-0260MEX True False False True \n",
811811
"331 1.0 0.005920 2010-0468ATG True False False True \n",
812812
"334 1.0 0.002161 2010-0494MEX True False False True \n",
813813
"339 1.0 0.001129 2010-0571LCA True False False True \n",
@@ -1251,9 +1251,9 @@
12511251
"</div>"
12521252
],
12531253
"text/plain": [
1254-
"region_id 484 28 662 670 \n",
1254+
"region_id 484 28 662 670 \\\n",
12551255
"ibtracsID \n",
1256-
"2010176N16278 2.485465e+09 0.000000e+00 0.0000 0.00 \\\n",
1256+
"2010176N16278 2.485465e+09 0.000000e+00 0.0000 0.00 \n",
12571257
"2010236N12341 0.000000e+00 1.394594e+07 0.0000 0.00 \n",
12581258
"2010257N16282 4.846656e+09 0.000000e+00 0.0000 0.00 \n",
12591259
"2010302N09306 0.000000e+00 0.000000e+00 548667.5019 26706058.15 \n",
@@ -1271,9 +1271,9 @@
12711271
"2017242N16333 0.000000e+00 2.111764e+08 0.0000 0.00 \n",
12721272
"2017260N12310 0.000000e+00 0.000000e+00 0.0000 0.00 \n",
12731273
"\n",
1274-
"region_id 44 214 630 388 \n",
1274+
"region_id 44 214 630 388 \\\n",
12751275
"ibtracsID \n",
1276-
"2010176N16278 0.000000e+00 0.00 0.000000e+00 0.00 \\\n",
1276+
"2010176N16278 0.000000e+00 0.00 0.000000e+00 0.00 \n",
12771277
"2010236N12341 0.000000e+00 0.00 0.000000e+00 0.00 \n",
12781278
"2010257N16282 0.000000e+00 0.00 0.000000e+00 0.00 \n",
12791279
"2010302N09306 0.000000e+00 0.00 0.000000e+00 0.00 \n",
@@ -1291,9 +1291,9 @@
12911291
"2017242N16333 1.801876e+06 0.00 0.000000e+00 0.00 \n",
12921292
"2017260N12310 0.000000e+00 54813712.03 6.700905e+10 0.00 \n",
12931293
"\n",
1294-
"region_id 132 192 659 796 \n",
1294+
"region_id 132 192 659 796 \\\n",
12951295
"ibtracsID \n",
1296-
"2010176N16278 0.000 0.000000e+00 0.00 0.0 \\\n",
1296+
"2010176N16278 0.000 0.000000e+00 0.00 0.0 \n",
12971297
"2010236N12341 0.000 0.000000e+00 0.00 0.0 \n",
12981298
"2010257N16282 0.000 0.000000e+00 0.00 0.0 \n",
12991299
"2010302N09306 0.000 0.000000e+00 0.00 0.0 \n",
@@ -1419,7 +1419,7 @@
14191419
"name": "stdout",
14201420
"output_type": "stream",
14211421
"text": [
1422-
"2023-06-09 12:52:52,649 - climada.hazard.tc_tracks - WARNING - The cached IBTrACS data set dates from 2022-03-08 23:23:51 (older than 180 days). Very likely, a more recent version is available. Consider manually removing the file /Users/ldr.riedel/climada/data/IBTrACS.ALL.v04r00.nc and re-running this function, which will download the most recent version of the IBTrACS data set from the official URL.\n"
1422+
"2023-06-13 16:36:05,304 - climada.hazard.tc_tracks - WARNING - The cached IBTrACS data set dates from 2022-03-08 23:23:51 (older than 180 days). Very likely, a more recent version is available. Consider manually removing the file /Users/ldr.riedel/climada/data/IBTrACS.ALL.v04r00.nc and re-running this function, which will download the most recent version of the IBTrACS data set from the official URL.\n"
14231423
]
14241424
},
14251425
{
@@ -1534,8 +1534,9 @@
15341534
"import numpy as np\n",
15351535
"from climada.engine import Impact\n",
15361536
"\n",
1537+
"# TODO: Dont\n",
15371538
"# Make sure that Hazard.event_id matches indexes of 'data'\n",
1538-
"hazard.event_id = np.asarray(hazard.event_name)\n",
1539+
"# hazard.event_id = np.asarray(hazard.event_name)\n",
15391540
"\n",
15401541
"def cost_rmse(impact: Impact, data: pd.DataFrame):\n",
15411542
" \"\"\"A cost function computing the RMSE\"\"\"\n",
@@ -1624,12 +1625,14 @@
16241625
" cost_func=cost_rmse,\n",
16251626
" impact_func_gen=impact_func_tc,\n",
16261627
" bounds=bounds,\n",
1628+
" align=False,\n",
16271629
")\n",
1630+
"exposure.assign_centroids(hazard)\n",
16281631
"\n",
16291632
"# Create and run the optimizer\n",
16301633
"opt = BayesianOptimizer(input)\n",
1631-
"output = opt.run()\n",
1632-
"output.params # The optimal parameters"
1634+
"bayesian_output = opt.run()\n",
1635+
"bayesian_output.params # The optimal parameters"
16331636
]
16341637
},
16351638
{
@@ -1670,9 +1673,7 @@
16701673
}
16711674
],
16721675
"source": [
1673-
"from climada.entity import ImpfTropCyclone\n",
1674-
"\n",
1675-
"ImpfTropCyclone.from_emanuel_usa(**output.params).plot()"
1676+
"impact_func_tc(**bayesian_output.params).plot()"
16761677
]
16771678
},
16781679
{
@@ -1683,7 +1684,7 @@
16831684
"The Bayesian Optimizer returns the entire paramter space it sampled.\n",
16841685
"We can find out a lot about the relation of the fitted parameters by investigating how the target function value depends on them.\n",
16851686
"Since we only calibrated two values, we can plot them in a scatter plot.\n",
1686-
"To that end, retrieving the parameter space data via `BayesianOptimizerOutput.param_space_to_dataframe` will help.\n",
1687+
"To that end, retrieving the parameter space data via `BayesianOptimizerOutput.p_space_to_dataframe` will help.\n",
16871688
"Notice that the optimal parameter set is not necessarily the last entry in the parameter space!"
16881689
]
16891690
},
@@ -1717,6 +1718,12 @@
17171718
" <th>v_half</th>\n",
17181719
" <th>target</th>\n",
17191720
" </tr>\n",
1721+
" <tr>\n",
1722+
" <th>Iteration</th>\n",
1723+
" <th></th>\n",
1724+
" <th></th>\n",
1725+
" <th></th>\n",
1726+
" </tr>\n",
17201727
" </thead>\n",
17211728
" <tbody>\n",
17221729
" <tr>\n",
@@ -1791,18 +1798,19 @@
17911798
"</div>"
17921799
],
17931800
"text/plain": [
1794-
" scale v_half target\n",
1795-
"0 0.422852 115.236335 2.405716e-10\n",
1796-
"1 0.010113 63.279939 2.280614e-10\n",
1797-
"2 0.155288 37.177687 3.057439e-10\n",
1798-
"3 0.194398 68.653198 2.637483e-10\n",
1799-
"4 0.402800 92.674920 2.571778e-10\n",
1800-
".. ... ... ...\n",
1801-
"195 0.980632 47.015299 3.576957e-10\n",
1802-
"196 0.943001 147.501934 2.396817e-10\n",
1803-
"197 0.999377 70.913361 6.412774e-10\n",
1804-
"198 0.997574 64.547339 9.480593e-10\n",
1805-
"199 0.982757 39.948848 2.226480e-10\n",
1801+
" scale v_half target\n",
1802+
"Iteration \n",
1803+
"0 0.422852 115.236335 2.405716e-10\n",
1804+
"1 0.010113 63.279939 2.280614e-10\n",
1805+
"2 0.155288 37.177687 3.057439e-10\n",
1806+
"3 0.194398 68.653198 2.637483e-10\n",
1807+
"4 0.402800 92.674920 2.571778e-10\n",
1808+
"... ... ... ...\n",
1809+
"195 0.980632 47.015299 3.576957e-10\n",
1810+
"196 0.943001 147.501934 2.396817e-10\n",
1811+
"197 0.999377 70.913361 6.412774e-10\n",
1812+
"198 0.997574 64.547339 9.480593e-10\n",
1813+
"199 0.982757 39.948848 2.226480e-10\n",
18061814
"\n",
18071815
"[200 rows x 3 columns]"
18081816
]
@@ -1813,7 +1821,7 @@
18131821
}
18141822
],
18151823
"source": [
1816-
"p_space_df = output.p_space_to_dataframe()\n",
1824+
"p_space_df = bayesian_output.p_space_to_dataframe()\n",
18171825
"p_space_df"
18181826
]
18191827
},
@@ -1855,6 +1863,12 @@
18551863
" <th>v_half</th>\n",
18561864
" <th>target</th>\n",
18571865
" </tr>\n",
1866+
" <tr>\n",
1867+
" <th>Iteration</th>\n",
1868+
" <th></th>\n",
1869+
" <th></th>\n",
1870+
" <th></th>\n",
1871+
" </tr>\n",
18581872
" </thead>\n",
18591873
" <tbody>\n",
18601874
" <tr>\n",
@@ -1929,18 +1943,19 @@
19291943
"</div>"
19301944
],
19311945
"text/plain": [
1932-
" scale v_half target\n",
1933-
"116 0.990320 61.511633 1.013992e-09\n",
1934-
"108 0.988234 61.959510 1.013962e-09\n",
1935-
"110 0.982902 61.693392 1.013046e-09\n",
1936-
"170 0.978972 61.694825 1.011628e-09\n",
1937-
"119 0.997161 61.119598 1.007215e-09\n",
1938-
".. ... ... ...\n",
1939-
"91 0.755228 33.970485 2.088355e-10\n",
1940-
"194 0.980303 38.162301 1.980600e-10\n",
1941-
"66 0.565420 28.017858 1.792657e-10\n",
1942-
"74 0.564120 27.260708 1.737898e-10\n",
1943-
"174 0.968588 35.695063 1.692841e-10\n",
1946+
" scale v_half target\n",
1947+
"Iteration \n",
1948+
"116 0.990320 61.511633 1.013992e-09\n",
1949+
"108 0.988234 61.959510 1.013962e-09\n",
1950+
"110 0.982902 61.693392 1.013046e-09\n",
1951+
"170 0.978972 61.694825 1.011628e-09\n",
1952+
"119 0.997161 61.119598 1.007215e-09\n",
1953+
"... ... ... ...\n",
1954+
"91 0.755228 33.970485 2.088355e-10\n",
1955+
"194 0.980303 38.162301 1.980600e-10\n",
1956+
"66 0.565420 28.017858 1.792657e-10\n",
1957+
"74 0.564120 27.260708 1.737898e-10\n",
1958+
"174 0.968588 35.695063 1.692841e-10\n",
19441959
"\n",
19451960
"[200 rows x 3 columns]"
19461961
]
@@ -2422,10 +2437,9 @@
24222437
}
24232438
],
24242439
"source": [
2425-
"from climada.entity import ImpactFuncSet, ImpfTropCyclone\n",
24262440
"from climada.engine import ImpactCalc\n",
24272441
"\n",
2428-
"impf = ImpactFuncSet([ImpfTropCyclone.from_emanuel_usa(**output.params)])\n",
2442+
"impf = impact_func_tc(**bayesian_output.params)\n",
24292443
"impact = ImpactCalc(exposure, impf, hazard).impact(assign_centroids=False)\n",
24302444
"impact_data = impact.impact_at_reg(exposure.gdf[\"region_id\"])\n",
24312445
"impact_data"
@@ -2988,7 +3002,9 @@
29883002
"\n",
29893003
"So we present some ideas on how to continue and/or improve the calibration:\n",
29903004
"1. Use a different cost function\n",
2991-
"2. Employ the `ScipyMinimizeOptimizer` instead of the `BayesianOptimizer`"
3005+
"2. Also calibrate the `v_thresh` parameter. This requires adding constraints, because `v_thresh` < `v_half`.\n",
3006+
"3. Calibrate different impact functions for houses in Mexico and Puerto Rico within the same optimization task.\n",
3007+
"4. Employ the `ScipyMinimizeOptimizer` instead of the `BayesianOptimizer`"
29923008
]
29933009
},
29943010
{

0 commit comments

Comments
 (0)