Skip to content

Commit 3f30b04

Browse files
committed
Update docs and links in tutorial
1 parent 6cce5a9 commit 3f30b04

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

climada/util/calibrate/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Input:
4949
Exposures object to compute impacts from
5050
data : pandas.Dataframe
5151
The data to compare computed impacts to. Index: Event IDs matching the IDs of
52-
``hazard``. Columns: Arbitrary columns. NaN values in the data frame have
52+
:py:attr:`hazard`. Columns: Arbitrary columns. NaN values in the data frame have
5353
special meaning: Corresponding impact values computed by the model are ignored
5454
in the calibration.
5555
impact_func_creator : Callable

climada/util/calibrate/ensemble.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ def to_csv(self, filepath: Path | str):
205205
)
206206
self.data.to_csv(filepath, index=None)
207207

208-
def _to_impf_sets(self, impact_func_creator) -> list[ImpactFuncSet]:
208+
def _to_impf_sets(
209+
self, impact_func_creator: Callable[..., ImpactFuncSet]
210+
) -> list[ImpactFuncSet]:
209211
"""Return a list of impact functions created from the stored parameters"""
210212
return [
211213
impact_func_creator(**row["Parameters"]) for _, row in self.data.iterrows()
@@ -453,18 +455,32 @@ def plot_category(
453455

454456
@dataclass
455457
class EnsembleOptimizer(ABC):
456-
"""Abstract base class for defining an ensemble optimizer
458+
"""Abstract base class for defining an ensemble optimizer.
459+
460+
An ensemble optimizer uses a user-defined optimizer type to run multiple calibration
461+
tasks. The tasks are defined by the :py:attr:`samples` attribute: For each entry in
462+
:py:attr:`samples`, a new :py:class:`~climada.util.calibrate.base.Input` is created
463+
and passed to an instance of :py:attr:`optimizer_type`. Derived classes need to set
464+
the :py:attr:`samples` during initialization and define the
465+
:py:meth:`input_from_sample` method.
466+
467+
The calibration tasks can be conducted in parallel by executing :py:meth:`run` with
468+
``processes`` set to a value larger than 1.
457469
458470
Attributes
459471
----------
460472
input : Input
461-
The input for the optimization
473+
The generic input for the optimization
462474
optimizer_type : type[Optimizer]
463475
The type of the optimizer to use for each calibration task
464476
optimizer_init_kwargs
465-
Keyword argument for initializing the calibration optimizer
477+
Keyword argument for initializing an instance of the chosen
478+
:py:attr:`optimizer_type`.
466479
samples : list of list of tuple(int, int)
467-
The samples for each calibration task
480+
The samples for each calibration task. Each entry is a list of tuples that
481+
encode row and column indices of the ``Input``
482+
:py:attr:`~climada.util.calibrate.base.Input.data` that are selected for the
483+
particular calibration task. See :py:func:`sample_data`.
468484
"""
469485

470486
input: Input
@@ -551,18 +567,23 @@ def _update_init_kwargs(
551567

552568
@dataclass
553569
class AverageEnsembleOptimizer(EnsembleOptimizer):
554-
"""An optimizer for the average ensemble
570+
"""An optimizer for the "average ensemble".
571+
572+
This optimizer samples a fraction of the original events in
573+
:py:attr:`~climada.util.calibrate.ensemble.EnsembleOptimizer.input`.
574+
:py:attr:`~climada.util.calibrate.base.Input.data`.
555575
556576
Attributes
557577
----------
558578
sample_fraction : float
559-
The fraction of data points to use for each calibration
579+
The fraction of data points to use for each calibration. For values > 1,
580+
:py:attr:`replace` must be ``True``.
560581
ensemble_size : int
561-
The number of calibration tasks to perform
582+
The number of calibration tasks to perform (and hence size of the ensemble).
562583
random_state : int
563584
The seed for the random number generator selecting the samples
564585
replace : bool
565-
If samples of the impact data should be drawn with replacement
586+
If samples of the input data should be drawn with replacement
566587
"""
567588

568589
sample_fraction: InitVar[float] = 0.8
@@ -600,13 +621,18 @@ def input_from_sample(self, sample: list[tuple[int, int]]):
600621

601622
@dataclass
602623
class TragedyEnsembleOptimizer(EnsembleOptimizer):
603-
"""An optimizer for the ensemble of tragedies
624+
"""An optimizer for the "ensemble of tragedies".
625+
626+
Each sample (and thus calibration task) of this optimizer only contains a single
627+
event from :py:attr:`~climada.util.calibrate.ensemble.EnsembleOptimizer.input`.
628+
:py:attr:`~climada.util.calibrate.base.Input.data`.
604629
605630
Attributes
606631
----------
607632
ensemble_size : int, optional
608633
The number of calibration tasks to perform. Defaults to ``None``, which means
609634
one for each data point. Must be smaller or equal to the number of data points.
635+
If smaller, random events will be left out from the ensemble calibration.
610636
random_state : int
611637
The seed for the random number generator selecting the samples
612638
"""

doc/api/climada/climada.util.calibrate.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _calibration-module:
2+
13
==================================
24
Impact Function Calibration Module
35
==================================
@@ -34,7 +36,14 @@ Calibration based on the ``scipy.optimize`` module.
3436
Ensemble Optimizers
3537
-------------------
3638

37-
Calibration creating ensembles of impact functions.
39+
Ensemble optimizers calibrate an ensemble of optimized parameter sets from subsets of the original input by employing multiple instances of the above "default" optimizers.
40+
This gives a better sense of uncertainty in the calibration results:
41+
By only selecting a subset of events to calibrate on, and by repeating this process for several times, one receives a varying set of impact functions that may spread considerably, as some events might dominate the calibration.
42+
We distinguish two cases:
43+
The :py:class:`~climada.util.calibrate.ensemble.AverageEnsembleOptimizer` samples a subset of all events with or without replacement.
44+
The resulting "average ensemble" contains uncertainty information on the average impact function for all events.
45+
The :py:class:`~climada.util.calibrate.ensemble.TragedyEnsembleOptimizer` calibrates one impact function for each single event.
46+
The resulting "ensemble of tragedies" encodes the inter-event uncertainty.
3847

3948
.. automodule:: climada.util.calibrate.ensemble
4049
:members:

doc/user-guide/climada_util_calibrate.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
"source": [
88
"# Impact Function Calibration\n",
99
"\n",
10-
"CLIMADA provides the [`climada.util.calibrate`](../climada/climada.util.calibrate) module for calibrating impact functions based on impact data.\n",
10+
"CLIMADA provides the [`climada.util.calibrate`](calibration-module) module for calibrating impact functions based on impact data.\n",
1111
"This tutorial will guide through the usage of this module by calibrating an impact function for tropical cyclones (TCs).\n",
1212
"\n",
13-
"For further information on the classes available from the module, see its [documentation](../climada/climada.util.calibrate).\n",
13+
"For further information on the classes available from the module, see its [documentation](calibration-module).\n",
1414
"\n",
1515
"## Overview\n",
1616
"\n",
1717
"The basic idea of the calibration is to find a set of parameters for an impact function that minimizes the deviation between the calculated impact and some impact data.\n",
1818
"For setting up a calibration task, users have to supply the following information:\n",
1919
"\n",
20-
"* Hazard and Exposure (as usual, see [the tutorial](../tutorial/1_main_climada.ipynb#tutorial-an-example-risk-assessment))\n",
20+
"* Hazard and Exposure (as usual, see [the tutorial](1_main_climada.ipynb#tutorial-an-example-risk-assessment))\n",
2121
"* The impact data to calibrate the model to\n",
2222
"* An impact function definition depending on the calibrated parameters\n",
2323
"* Bounds and constraints of the calibrated parameters (depending on the calibration algorithm)\n",

0 commit comments

Comments
 (0)