def test_bcr_classical(self): vulnerability_function_rm = ( scientific.VulnerabilityFunction( 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) vulnerability_function_rf = ( scientific.VulnerabilityFunction( 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.035, 0.07, 0.14, 0.28, 0.56], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) asset_value = 2. retrofitting_cost = .1 interest_rate = 0.05 asset_life_expectancy = 40 hazard = [ (0.001, 0.0398612669790014), (0.01, 0.0398612669790014), (0.05, 0.0397287574802989), (0.1, 0.0296134266256125), (0.15, 0.0198273287564916), (0.2, 0.0130622701614519), (0.25, 0.00865538795000043), (0.3, 0.00589852059368967), (0.35, 0.00406169858951178), (0.4, 0.00281172717952682), (0.45, 0.00199511741777669), (0.5, 0.00135870597284571), (0.55, 0.000989667841573727), (0.6, 0.000757544444296432), (0.7, 0.000272824002045979), (0.8, 0.0), (0.9, 0.0), (1.0, 0.0)] original_loss_ratio_curve = scientific.classical( vulnerability_function_rm, hazard, steps=5) retrofitted_loss_ratio_curve = scientific.classical( vulnerability_function_rf, hazard, steps=5) eal_original = scientific.average_loss(*original_loss_ratio_curve) eal_retrofitted = scientific.average_loss( *retrofitted_loss_ratio_curve) bcr = scientific.bcr( eal_original, eal_retrofitted, interest_rate, asset_life_expectancy, asset_value, retrofitting_cost) self.assertAlmostEqual(0.009379, eal_original * asset_value, delta=0.0009) self.assertAlmostEqual(0.006586, eal_retrofitted * asset_value, delta=0.0009) self.assertAlmostEqual(0.483091, bcr, delta=0.009)
def test_bcr_classical(self): vulnerability_function_rm = ( scientific.VulnerabilityFunction( 'RM', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) vulnerability_function_rf = ( scientific.VulnerabilityFunction( 'RF', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.035, 0.07, 0.14, 0.28, 0.56], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) asset_value = 2. retrofitting_cost = .1 interest_rate = 0.05 asset_life_expectancy = 40 hazard_imls = [0.001, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.7, 0.8, 0.9, 1.0] poes = [0.039861266979, 0.039861266979, 0.0397287574803, 0.0296134266256, 0.0198273287565, 0.0130622701615, 0.00865538795, 0.00589852059369, 0.00406169858951, 0.00281172717953, 0.00199511741778, 0.00135870597285, 0.000989667841574, 0.000757544444296, 0.000272824002046, 0.0, 0.0, 0.] original_loss_ratio_curve = scientific.classical( vulnerability_function_rm, hazard_imls, poes, steps=5) retrofitted_loss_ratio_curve = scientific.classical( vulnerability_function_rf, hazard_imls, poes, steps=5) eal_original = scientific.average_loss(original_loss_ratio_curve) eal_retrofitted = scientific.average_loss(retrofitted_loss_ratio_curve) bcr = scientific.bcr( eal_original, eal_retrofitted, interest_rate, asset_life_expectancy, asset_value, retrofitting_cost) self.assertAlmostEqual(0.009379, eal_original * asset_value, delta=0.0009) self.assertAlmostEqual(0.006586, eal_retrofitted * asset_value, delta=0.0009) self.assertAlmostEqual(0.483091, bcr, delta=0.009)
def classical_risk( self, loss_type, assets, hazard_curve, eids=None, eps=None): """ :param str loss_type: the loss type considered :param assets: assets is an iterator over A :class:`openquake.risklib.scientific.Asset` instances :param hazard_curve: an array of poes :param eids: ignored, here only for API compatibility with other calculators :param eps: ignored, here only for API compatibility with other calculators :returns: a composite array (loss, poe) of shape (A, C) """ n = len(assets) vf = self.risk_functions[loss_type, 'vulnerability'] lratios = self.loss_ratios[loss_type] imls = self.hazard_imtls[vf.imt] values = get_values(loss_type, assets) lrcurves = numpy.array( [scientific.classical(vf, imls, hazard_curve, lratios)] * n) return rescale(lrcurves, values)
def test_beta_distribution(self): loss_ratio_curve = scientific.classical( scientific.VulnerabilityFunction( [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1], "BT"), self.hazard_curve, steps=5) poes = [ 0.039334753367700, 0.039125428171600, 0.037674168943300, 0.034759710983600, 0.031030531006800, 0.027179528786300, 0.023629919279000, 0.020549508446100, 0.017953286405900, 0.015789769371500, 0.013989999469800, 0.011228361585000, 0.009252778235140, 0.007776981119440, 0.006618721902640, 0.005678492205870, 0.004293209819490, 0.003423791350520, 0.002851589502850, 0.002371116350380, 0.001901538687150, 0.001145930527350, 0.000834074579570, 0.000755265952955, 0.000655382394929, 0.000422046545856, 0.000266286103069, 0.000124036890130, 3.28497166702e-05, 2.178664466e-06, 0.0] numpy.testing.assert_allclose(self.loss_ratios, loss_ratio_curve[0]) numpy.testing.assert_allclose(poes, loss_ratio_curve[1]) asset_value = 2. self.assertAlmostEqual( 0.264870863283, scientific.conditional_loss_ratio( self.loss_ratios, poes, 0.01) * asset_value)
def test_compute_loss_ratio_curve(self): hazard_imls = [0.01, 0.08, 0.17, 0.26, 0.36, 0.55, 0.7] hazard_curve = [0.99, 0.96, 0.89, 0.82, 0.7, 0.4, 0.01] imls = [0.1, 0.2, 0.4, 0.6] covs = [0.5, 0.3, 0.2, 0.1] loss_ratios = [0.05, 0.08, 0.2, 0.4] vulnerability_function = scientific.VulnerabilityFunction( 'VF', 'PGA', imls, loss_ratios, covs, "LN") loss_ratio_curve = scientific.classical( vulnerability_function, hazard_imls, hazard_curve, 2) expected_curve = [ (0.0, 0.96), (0.025, 0.96), (0.05, 0.91), (0.065, 0.87), (0.08, 0.83), (0.14, 0.75), (0.2, 0.60), (0.3, 0.47), (0.4, 0.23), (0.7, 0.00), (1.0, 0.00)] actual_poes_interp = interp1d(loss_ratio_curve[0], loss_ratio_curve[1]) for loss, poe in expected_curve: numpy.testing.assert_allclose( poe, actual_poes_interp(loss), atol=0.005)
def __call__(self, loss_type, assets, hazard_curve, _eps=None): """ :param str loss_type: the loss type considered :param assets: assets is an iterator over N :class:`openquake.risklib.scientific.Asset` instances :param hazard_curve: an array of poes :param _eps: ignored, here only for API compatibility with other calculators :returns: an array of shape (C, N, 2) """ n = len(assets) vf = self.risk_functions[loss_type] imls = self.hazard_imtls[vf.imt] values = get_values(loss_type, assets) lrcurves = numpy.array([ scientific.classical(vf, imls, hazard_curve, self.lrem_steps_per_interval) ] * n) # if in the future we wanted to implement insured_losses the # following lines could be useful # deductibles = [a.deductible(loss_type) for a in assets] # limits = [a.insurance_limit(loss_type) for a in assets] # insured_curves = rescale( # utils.numpy_map(scientific.insured_loss_curve, # lrcurves, deductibles, limits), values) return rescale(lrcurves, values).transpose(2, 0, 1)
def __call__(self, loss_type, assets, hazard_curve, _eps=None): """ :param str loss_type: the loss type considered :param assets: assets is an iterator over N :class:`openquake.risklib.scientific.Asset` instances :param hazard_curve: an array of poes :param _eps: ignored, here only for API compatibility with other calculators :returns: a :class:`openquake.risklib.scientific.Classical.Output` instance. """ n = len(assets) vf = self.risk_functions[loss_type] imls = self.hazard_imtls[vf.imt] values = get_values(loss_type, assets) lrcurves = numpy.array([ scientific.classical(vf, imls, hazard_curve, self.lrem_steps_per_interval) ] * n) if self.insured_losses: deductibles = [a.deductible(loss_type) for a in assets] limits = [a.insurance_limit(loss_type) for a in assets] insured_curves = rescale( utils.numpy_map(scientific.insured_loss_curve, lrcurves, deductibles, limits), values) else: insured_curves = None return rescale(lrcurves, values), insured_curves
def test_beta_distribution(self): loss_ratio_curve = scientific.classical( scientific.VulnerabilityFunction('VF', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1], "BT"), self.hazard_imls, self.hazard_curve, steps=5) poes = [ 0.039334753367700, 0.039125428171600, 0.037674168943300, 0.034759710983600, 0.031030531006800, 0.027179528786300, 0.023629919279000, 0.020549508446100, 0.017953286405900, 0.015789769371500, 0.013989999469800, 0.011228361585000, 0.009252778235140, 0.007776981119440, 0.006618721902640, 0.005678492205870, 0.004293209819490, 0.003423791350520, 0.002851589502850, 0.002371116350380, 0.001901538687150, 0.001145930527350, 0.000834074579570, 0.000755265952955, 0.000655382394929, 0.000422046545856, 0.000266286103069, 0.000124036890130, 3.28497166702e-05, 2.178664466e-06, 0.0 ] numpy.testing.assert_allclose(self.loss_ratios, loss_ratio_curve[0]) numpy.testing.assert_allclose(poes, loss_ratio_curve[1]) asset_value = 2. self.assertAlmostEqual( 0.264870863283, scientific.conditional_loss_ratio(self.loss_ratios, poes, 0.01) * asset_value)
def test_bcr_classical(self): vulnerability_function_rm = (scientific.VulnerabilityFunction( 'RM', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) vulnerability_function_rf = (scientific.VulnerabilityFunction( 'RF', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.035, 0.07, 0.14, 0.28, 0.56], [0.5, 0.4, 0.3, 0.2, 0.1], "LN")) asset_value = 2. retrofitting_cost = .1 interest_rate = 0.05 asset_life_expectancy = 40 hazard_imls = [ 0.001, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.7, 0.8, 0.9, 1.0 ] poes = [ 0.039861266979, 0.039861266979, 0.0397287574803, 0.0296134266256, 0.0198273287565, 0.0130622701615, 0.00865538795, 0.00589852059369, 0.00406169858951, 0.00281172717953, 0.00199511741778, 0.00135870597285, 0.000989667841574, 0.000757544444296, 0.000272824002046, 0.0, 0.0, 0. ] original_loss_ratio_curve = scientific.classical( vulnerability_function_rm, hazard_imls, poes, steps=5) retrofitted_loss_ratio_curve = scientific.classical( vulnerability_function_rf, hazard_imls, poes, steps=5) eal_original = scientific.average_loss(original_loss_ratio_curve) eal_retrofitted = scientific.average_loss(retrofitted_loss_ratio_curve) bcr = scientific.bcr(eal_original, eal_retrofitted, interest_rate, asset_life_expectancy, asset_value, retrofitting_cost) self.assertAlmostEqual(0.009379, eal_original * asset_value, delta=0.0009) self.assertAlmostEqual(0.006586, eal_retrofitted * asset_value, delta=0.0009) self.assertAlmostEqual(0.483091, bcr, delta=0.009)
def __call__(self, loss_type, assets, hazard_curve, _epsilons=None, _eids=None): """ :param str loss_type: the loss type considered :param assets: assets is an iterator over N :class:`openquake.risklib.scientific.Asset` instances :param hazard_curve: an array of poes :param _epsilons: ignored, here only for API compatibility with other calculators :returns: a :class:`openquake.risklib.scientific.Classical.Output` instance. """ n = len(assets) vf = self.risk_functions[loss_type] imls = self.hazard_imtls[vf.imt] curves = [ scientific.classical(vf, imls, hazard_curve, self.lrem_steps_per_interval) ] * n average_losses = utils.numpy_map(scientific.average_loss, curves) maps = scientific.loss_map_matrix(self.conditional_loss_poes, curves) values = get_values(loss_type, assets) if self.insured_losses and loss_type != 'occupants': deductibles = [a.deductible(loss_type) for a in assets] limits = [a.insurance_limit(loss_type) for a in assets] insured_curves = rescale( utils.numpy_map(scientific.insured_loss_curve, curves, deductibles, limits), values) average_insured_losses = utils.numpy_map(scientific.average_loss, insured_curves) else: insured_curves = None average_insured_losses = None return scientific.Output(assets, loss_type, loss_curves=rescale(numpy.array(curves), values), average_losses=values * average_losses, insured_curves=insured_curves, average_insured_losses=average_insured_losses, loss_maps=values * maps)
def test_compute_loss_ratio_curve(self): hazard_curve = [ (0.01, 0.99), (0.08, 0.96), (0.17, 0.89), (0.26, 0.82), (0.36, 0.70), (0.55, 0.40), (0.70, 0.01)] imls = [0.1, 0.2, 0.4, 0.6] covs = [0.5, 0.3, 0.2, 0.1] loss_ratios = [0.05, 0.08, 0.2, 0.4] vulnerability_function = scientific.VulnerabilityFunction( imls, loss_ratios, covs, "LN") # pre computed values just use one intermediate # values between the imls, so steps=2 lrem = [[1., 1., 1., 1.], [8.90868149e-01, 9.99932030e-01, 1., 1.], [4.06642478e-01, 9.27063668e-01, 1., 1.], [2.14297309e-01, 7.12442306e-01, 9.99999988e-01, 1.], [1.09131851e-01, 4.41652761e-01, 9.99997019e-01, 1.], [7.84971008e-03, 2.00321301e-02, 9.55620783e-01, 1.], [7.59869969e-04, 5.41393717e-04, 4.60560758e-01, 1.], [2.79797605e-05, 1.66547090e-06, 1.59210054e-02, 9.97702369e-01], [1.75697664e-06, 9.04938835e-09, 1.59710253e-04, 4.80110732e-01], [2.89163471e-09, 2.43138842e-14, 6.60395072e-11, 7.56938368e-09], [2.38464803e-11, 0., 1.11022302e-16, 0.]] loss_ratio_curve = scientific.classical( vulnerability_function, lrem, hazard_curve, 2) expected_curve = Curve([ (0.0, 0.96), (0.025, 0.96), (0.05, 0.91), (0.065, 0.87), (0.08, 0.83), (0.14, 0.75), (0.2, 0.60), (0.3, 0.47), (0.4, 0.23), (0.7, 0.00), (1.0, 0.00)]) for x_value in expected_curve.abscissae: numpy.testing.assert_allclose( expected_curve.ordinate_for(x_value), loss_ratio_curve.ordinate_for(x_value), atol=0.005)
def __call__(self, loss_type, assets, hazard_curve, _eps=None): """ :param str loss_type: the loss type considered :param assets: assets is an iterator over N :class:`openquake.risklib.scientific.Asset` instances :param hazard_curve: an array of poes :param _eps: ignored, here only for API compatibility with other calculators :returns: a :class:`openquake.risklib.scientific.Classical.Output` instance. """ n = len(assets) vf = self.risk_functions[loss_type] imls = self.hazard_imtls[vf.imt] curves = [scientific.classical( vf, imls, hazard_curve, self.lrem_steps_per_interval)] * n average_losses = utils.numpy_map(scientific.average_loss, curves) maps = scientific.loss_map_matrix(self.conditional_loss_poes, curves) values = get_values(loss_type, assets) if self.insured_losses and loss_type != 'occupants': deductibles = [a.deductible(loss_type) for a in assets] limits = [a.insurance_limit(loss_type) for a in assets] insured_curves = rescale( utils.numpy_map(scientific.insured_loss_curve, curves, deductibles, limits), values) average_insured_losses = utils.numpy_map( scientific.average_loss, insured_curves) else: insured_curves = None average_insured_losses = None return scientific.Output( assets, loss_type, loss_curves=rescale(numpy.array(curves), values), average_losses=values * average_losses, insured_curves=insured_curves, average_insured_losses=average_insured_losses, loss_maps=values * maps)
def test_lognormal_distribution(self): loss_ratio_curve = scientific.classical( scientific.VulnerabilityFunction( 'VF', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1]), self.hazard_imls, self.hazard_curve, steps=5) poes = [ 0.039334753367700, 0.039319630829000, 0.038454063967300, 0.035355568337500, 0.031080935951500, 0.026921966116900, 0.023309185424900, 0.020254928647300, 0.017692604455300, 0.015561622176500, 0.013804482989300, 0.011159985044500, 0.009272778209290, 0.007803862103290, 0.006601047489540, 0.005621048101030, 0.004262944952210, 0.003478101875460, 0.002916428961850, 0.002375461660340, 0.001854772287220, 0.001133190711620, 0.000862358303705, 0.000784269030443, 0.000660062215754, 0.000374938542785, 0.000230249004393, 0.000122823654476, 5.72790058705e-05, 2.35807221322e-05, 8.66392324535e-06] numpy.testing.assert_allclose(self.loss_ratios, loss_ratio_curve[0]) numpy.testing.assert_allclose(poes, loss_ratio_curve[1]) asset_value = 2. conditional_losses = dict([ (poe, scientific.conditional_loss_ratio( self.loss_ratios, poes, poe) * asset_value) for poe in [0.01, 0.02, 0.05]]) self.assertAlmostEqual(0.264586283238, conditional_losses[0.01]) self.assertAlmostEqual(0.141989823521, conditional_losses[0.02]) self.assertAlmostEqual(0.0, conditional_losses[0.05])
def test_lognormal_distribution(self): loss_ratio_curve = scientific.classical( scientific.VulnerabilityFunction('VF', 'PGA', [0.1, 0.2, 0.3, 0.45, 0.6], [0.05, 0.1, 0.2, 0.4, 0.8], [0.5, 0.4, 0.3, 0.2, 0.1]), self.hazard_imls, self.hazard_curve, steps=5) poes = [ 0.039334753367700, 0.039319630829000, 0.038454063967300, 0.035355568337500, 0.031080935951500, 0.026921966116900, 0.023309185424900, 0.020254928647300, 0.017692604455300, 0.015561622176500, 0.013804482989300, 0.011159985044500, 0.009272778209290, 0.007803862103290, 0.006601047489540, 0.005621048101030, 0.004262944952210, 0.003478101875460, 0.002916428961850, 0.002375461660340, 0.001854772287220, 0.001133190711620, 0.000862358303705, 0.000784269030443, 0.000660062215754, 0.000374938542785, 0.000230249004393, 0.000122823654476, 5.72790058705e-05, 2.35807221322e-05, 8.66392324535e-06 ] numpy.testing.assert_allclose(self.loss_ratios, loss_ratio_curve[0]) numpy.testing.assert_allclose(poes, loss_ratio_curve[1]) asset_value = 2. conditional_losses = dict([ (poe, scientific.conditional_loss_ratio(self.loss_ratios, poes, poe) * asset_value) for poe in [0.01, 0.02, 0.05] ]) self.assertAlmostEqual(0.264586283238, conditional_losses[0.01]) self.assertAlmostEqual(0.141989823521, conditional_losses[0.02]) self.assertAlmostEqual(0.0, conditional_losses[0.05])
def __call__(self, hazard_curves): return [scientific.classical( self.vulnerability_function, self.loss_ratio_exceedance_matrix, hazard_curve, self.steps) for hazard_curve in hazard_curves]