def __init__(self, exp_input_var, impf_input_var, haz_input_var): """Initialize UncCalcImpact Sets the uncertainty input variables, the impact metric_names, and the units. Parameters ---------- exp_input_var : climada.engine.uncertainty.input_var.InputVar or climada.entity.Exposure Exposure uncertainty variable or Exposure impf_input_var : climada.engine.uncertainty.input_var.InputVar or climada.entity.ImpactFuncSet Impact function set uncertainty variable or Impact function set haz_input_var : climada.engine.uncertainty.input_var.InputVar or climada.hazard.Hazard Hazard uncertainty variable or Hazard """ Calc.__init__(self) self.input_var_names = ('exp_input_var', 'impf_input_var', 'haz_input_var') self.exp_input_var = InputVar.var_to_inputvar(exp_input_var) self.impf_input_var = InputVar.var_to_inputvar(impf_input_var) self.haz_input_var = InputVar.var_to_inputvar(haz_input_var) self.metric_names = ('aai_agg', 'freq_curve', 'at_event', 'eai_exp', 'tot_value') self.value_unit = self.exp_input_var.evaluate().value_unit self.check_distr()
def test_plot_pass(self): impf = impf_dem() distr_dict = { "x_paa": sp.stats.beta(0.5, 1), "x_mdd": sp.stats.uniform(0.8, 1.2), "x_lit": sp.stats.randint(0, 10) } impf_iv = InputVar(impf, distr_dict) self.assertIsNotNone(impf_iv.plot()) plt.close()
def test_var_to_inputvar(self): exp = exp_dem() distr_dict = {"x_exp": sp.stats.uniform(0.8, 1.2)} var = InputVar.var_to_inputvar(exp) self.assertDictEqual(var.distr_dict, {}) self.assertTrue(isinstance(var.func(), Exposures)) iv_var = InputVar.var_to_inputvar(InputVar(exp, distr_dict)) self.assertDictEqual(iv_var.distr_dict, distr_dict) self.assertTrue(isinstance(iv_var, InputVar))
def test_evaluate_pass(self): impf = impf_dem distr_dict = { "x_paa": sp.stats.beta(0.5, 1), "x_mdd": sp.stats.uniform(0.8, 0.4) } impf_iv = InputVar(impf, distr_dict) #Direct function evaluate impf_eval = impf_iv.func(**{'x_paa': 0.8, 'x_mdd': 1.1}) impf_true = impf_dem(x_paa=0.8, x_mdd=1.1) self.assertEqual(impf_eval.size(), impf_true.size()) impf_func1 = impf_eval.get_func()['TC'][1] impf_func2 = impf_true.get_func()['TC'][1] np.testing.assert_array_equal(impf_func1.intensity, impf_func2.intensity) np.testing.assert_array_equal(impf_func1.mdd, impf_func2.mdd) np.testing.assert_array_equal(impf_func1.paa, impf_func2.paa) self.assertEqual(impf_func1.id, impf_func2.id) self.assertEqual(impf_func1.haz_type, impf_func2.haz_type) #Specific evaluate impf_eval = impf_iv.evaluate(x_paa=0.8, x_mdd=1.1) impf_true = impf_dem(x_paa=0.8, x_mdd=1.1) self.assertEqual(impf_eval.size(), impf_true.size()) impf_func1 = impf_eval.get_func()['TC'][1] impf_func2 = impf_true.get_func()['TC'][1] np.testing.assert_array_equal(impf_func1.intensity, impf_func2.intensity) np.testing.assert_array_equal(impf_func1.mdd, impf_func2.mdd) np.testing.assert_array_equal(impf_func1.paa, impf_func2.paa) self.assertEqual(impf_func1.id, impf_func2.id) self.assertEqual(impf_func1.haz_type, impf_func2.haz_type) #Average evaluate (default) impf_eval = impf_iv.evaluate() impf_true = impf_dem(x_paa=0.3333333333333333, x_mdd=1.0) self.assertEqual(impf_eval.size(), impf_true.size()) impf_func1 = impf_eval.get_func()['TC'][1] impf_func2 = impf_true.get_func()['TC'][1] np.testing.assert_array_almost_equal(impf_func1.intensity, impf_func2.intensity) np.testing.assert_array_almost_equal(impf_func1.mdd, impf_func2.mdd) np.testing.assert_array_almost_equal(impf_func1.paa, impf_func2.paa) self.assertEqual(impf_func1.id, impf_func2.id) self.assertEqual(impf_func1.haz_type, impf_func2.haz_type)
def test_init_pass(self): impf = impf_dem distr_dict = { "x_paa": sp.stats.beta(0.5, 1), "x_mdd": sp.stats.uniform(0.8, 1.2) } impf_iv = InputVar(impf, distr_dict) self.assertListEqual(impf_iv.labels, ['x_paa', 'x_mdd']) self.assertTrue(isinstance(impf_iv.distr_dict, dict))
def __init__(self, haz_input_var, ent_input_var, haz_fut_input_var=None, ent_fut_input_var=None): """Initialize UncCalcCostBenefit Sets the uncertainty input variables, the cost benefit metric_names, and the units. Parameters ---------- haz_input_var : climada.engine.uncertainty.input_var.InputVar or climada.hazard.Hazard Hazard uncertainty variable or Hazard for the present Hazard in climada.engine.CostBenefit.calc ent_input_var : climada.engine.uncertainty.input_var.InputVar or climada.entity.Entity Entity uncertainty variable or Entity for the present Entity in climada.engine.CostBenefit.calc haz_fut_input_var: climada.engine.uncertainty.input_var.InputVar or climada.hazard.Hazard, optional Hazard uncertainty variable or Hazard for the future Hazard The Default is None. ent_fut_input_var : climada.engine.uncertainty.input_var.InputVar or climada.entity.Entity, optional Entity uncertainty variable or Entity for the future Entity in climada.engine.CostBenefit.calc """ Calc.__init__(self) self.input_var_names = ('haz_input_var', 'ent_input_var', 'haz_fut_input_var', 'ent_fut_input_var') self.haz_input_var = InputVar.var_to_inputvar(haz_input_var) self.ent_input_var = InputVar.var_to_inputvar(ent_input_var) self.haz_fut_input_var = InputVar.var_to_inputvar(haz_fut_input_var) self.ent_fut_input_var = InputVar.var_to_inputvar(ent_fut_input_var) self.metric_names = ('tot_climate_risk', 'benefit', 'cost_ben_ratio', 'imp_meas_present', 'imp_meas_future') self.value_unit = self.ent_input_var.evaluate().exposures.value_unit self.check_distr()
def make_input_vars(): exp = exp_dem exp_distr = { "x_exp": sp.stats.uniform(0.8, 2), } exp_unc = InputVar(exp, exp_distr) impf = impf_dem impf_distr = { "x_paa": sp.stats.beta(0.5, 1), "x_mdd": sp.stats.uniform(0.8, 1.2) } impf_unc = InputVar(impf, impf_distr) haz = haz_dem haz_distr = { "x_haz": sp.stats.alpha(a=2, loc=1, scale=1), } haz_unc = InputVar(haz, haz_distr) return exp_unc, impf_unc, haz_unc
def make_costben_iv(): entdem = ent_dem() ent_iv = InputVar.ent(impf_set=entdem.impact_funcs, disc_rate=entdem.disc_rates, exp_list=[entdem.exposures], meas_set=entdem.measures, bounds_noise=[0.3, 1.9], bounds_cost=[0.5, 1.5], bounds_impfi=[-2, 5], haz_id_dict={'TC': [1]}) entfutdem = ent_fut_dem() entfut_iv = InputVar.entfut(impf_set=entfutdem.impact_funcs, exp_list=[entfutdem.exposures], meas_set=entfutdem.measures, bounds_eg=[0.8, 1.5], bounds_mdd=[0.7, 0.9], bounds_paa=[1.3, 2], haz_id_dict={'TC': [1]}) return ent_iv, entfut_iv