def test_loss_ratios_boundaries(self): # loss ratio is zero if the gmf iml is below the minimum iml # defined by the function min iml in this case is 0.01 self.assertTrue(numpy.allclose(numpy.array([0.0, 0.0, 0.0]), prob.compute_loss_ratios(self.vuln_function, {"IMLs": (0.0001, 0.0002, 0.0003)}))) # loss ratio is equal to the maximum iml defined by the # function is greater than that max iml in this case is 0.52 self.assertTrue(numpy.allclose(numpy.array([0.52, 0.52]), prob.compute_loss_ratios(self.vuln_function, {"IMLs": (0.525, 0.53)})))
def test_when_the_mean_is_zero_the_loss_ratio_is_zero(self): """In sampled based, when an interpolated mean loss ratio is zero, the resulting loss ratio is also zero. This is how the interpolation is done: mean_ratio = vuln_function.ordinate_for(ground_motion_field) In this case, the first IML from the GMFs is 0.10 and the mean loss ratio in the vulnerability function for that IML is zero. So the resulting loss ratio must be zero. """ imls = [0.10, 0.30] loss_ratios = [0.00, 0.10] covs = [0.30, 0.30] vuln_function = shapes.VulnerabilityFunction(imls, loss_ratios, covs) epsilons = [0.5377] expected_asset = object() gmfs = {"IMLs": (0.1000, )} self.assertEqual(0.0, prob.compute_loss_ratios( vuln_function, gmfs, EpsilonProvider( expected_asset, epsilons), expected_asset)[0])
def test_with_valid_covs_we_sample_the_loss_ratios(self): """With valid covs we need to sample loss ratios. If the vulnerability function has some covs greater than 0.0 we need to use a different algorithm (sampled based) to compute the loss ratios. """ imls = [0.10, 0.30, 0.50, 1.00] loss_ratios = [0.05, 0.10, 0.15, 0.30] covs = [0.30, 0.30, 0.20, 0.20] vuln_function = shapes.VulnerabilityFunction(imls, loss_ratios, covs) epsilons = [0.5377, 1.8339, -2.2588, 0.8622, 0.3188, -1.3077, \ -0.4336, 0.3426, 3.5784, 2.7694] expected_asset = object() gmfs = {"IMLs": (0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595)} self.assertTrue(numpy.allclose(numpy.array([0.0722, 0.4106, 0.1800, 0.1710, 0.2508, 0.0395, 0.1145, 0.2883, 0.4734, 0.4885]), prob.compute_loss_ratios(vuln_function, gmfs, EpsilonProvider(expected_asset, epsilons), expected_asset), atol=0.0001))
def compute_stddev_loss(vuln_function, ground_motion_field_set, epsilon_provider, asset): """Compute the standard deviation of the losses for the given asset using the related ground motion field set and vulnerability function. :param vuln_function: the vulnerability function used to compute the loss ratios. :type vuln_function: :py:class:`openquake.shapes.VulnerabilityFunction` :param ground_motion_field_set: the set of ground motion fields used to compute the loss ratios. :type ground_motion_field_set: :py:class:`dict` with the following keys: **IMLs** - tuple of ground motion fields (float) :param epsilon_provider: service used to get the epsilon when using the sampled based algorithm. :type epsilon_provider: object that defines an :py:meth:`epsilon` method :param asset: the asset used to compute the loss ratios and losses. :type asset: :py:class:`dict` as provided by :py:class:`openquake.parser.exposure.ExposurePortfolioFile` """ loss_ratios = prob.compute_loss_ratios( vuln_function, ground_motion_field_set, epsilon_provider, asset) return _stddev_loss_from_loss_ratios(loss_ratios, asset)
def test_builds_the_cumulative_histogram(self): loss_ratios = prob.compute_loss_ratios( self.vuln_function_1, self.gmfs, None, None) loss_ratios_range = prob._compute_loss_ratios_range(loss_ratios) self.assertTrue(numpy.allclose(self.cum_histogram, prob._compute_cumulative_histogram( loss_ratios, loss_ratios_range)))
def test_loss_ratios_boundaries(self): """Loss ratios generation given a GMFs and a vulnerability function. The vulnerability function used in this test has all covs equal to zero, so the mean based algorithm is used. This test checks the boundary conditions. The resulting loss ratio is zero if the GMF is below the minimum IML defined the vulnerability function. The resulting loss ratio is equal to the maximum loss ratio defined by the function if the GMF is greater than the maximum IML defined. """ # min IML in this case is 0.01 self.assertTrue(numpy.allclose(numpy.array([0.0, 0.0, 0.0]), prob.compute_loss_ratios(self.vuln_function_1, {"IMLs": (0.0001, 0.0002, 0.0003)}, None, None))) # max IML in this case is 0.52 self.assertTrue(numpy.allclose(numpy.array([0.700, 0.700]), prob.compute_loss_ratios(self.vuln_function_1, {"IMLs": (0.525, 0.530)}, None, None)))
def compute_loss_ratios(self, asset, gmf_slice): """For a given asset and ground motion field, computes the loss ratios used to obtain the related loss ratio curve and aggregate loss curve.""" epsilon_provider = general.EpsilonProvider(self.params) vuln_function = self.vuln_curves.get(asset["vulnerabilityFunctionReference"], None) if not vuln_function: LOGGER.error( "Unknown vulnerability function %s for asset %s" % (asset["vulnerabilityFunctionReference"], asset["assetID"]) ) return None return prob.compute_loss_ratios(vuln_function, gmf_slice, epsilon_provider, asset)
def test_computes_the_aggregate_loss_curve(self): # no epsilon_provided is needed because the vulnerability # function has all the covs equal to zero loss_ratios_1 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_1, None, self.asset_1) loss_ratios_2 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_2, None, self.asset_2) loss_ratios_3 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_3, None, self.asset_3) loss_ratios_4 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_4, None, self.asset_4) loss_ratios_5 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_5, None, self.asset_5) loss_ratios_6 = prob.compute_loss_ratios( self.vuln_function_2, self.gmfs_6, None, self.asset_6) aggregate_curve = prob.AggregateLossCurve() aggregate_curve.append(loss_ratios_1 * self.asset_1["assetValue"]) aggregate_curve.append(loss_ratios_2 * self.asset_2["assetValue"]) aggregate_curve.append(loss_ratios_3 * self.asset_3["assetValue"]) aggregate_curve.append(loss_ratios_4 * self.asset_4["assetValue"]) aggregate_curve.append(loss_ratios_5 * self.asset_5["assetValue"]) aggregate_curve.append(loss_ratios_6 * self.asset_6["assetValue"]) expected_losses = numpy.array((7.2636, 57.9264, 187.4893, 66.9082, 47.0280, 248.7796, 23.2329, 121.3514, 177.4167, 259.2902, 77.7080, 127.7417, 18.9470, 339.5774, 151.1763, 6.1881, 71.9168, 97.9514, 56.4720, 11.6513)) self.assertTrue(numpy.allclose( expected_losses, aggregate_curve.losses)) expected_curve = shapes.Curve([(39.52702042, 0.99326205), (106.20489077, 0.917915), (172.88276113, 0.77686984), (239.56063147, 0.52763345), (306.23850182, 0.22119922)]) self.assertEqual(expected_curve, aggregate_curve.compute( 200, 50, number_of_samples=6))
def test_loss_ratios_computation_using_gmfs(self): """Loss ratios generation given a GMFs and a vulnerability function. The vulnerability function used in this test has all covs equal to zero, so the mean based algorithm is used. It basically takes each IML defined in the GMFs and interpolates them using the given vulnerability function. """ # manually computed values by Vitor Silva expected_loss_ratios = numpy.array([0.0605584000000000, 0.273100266666667, 0.0958560000000000, 0.0184384000000000, 0.270366933333333, 0.0, 0.0252480000000000, 0.0795669333333333, 0.0529024000000000, 0.0, 0.0154928000000000, 0.00222080000000000, 0.0109232000000000, 0.0, 0.0, 0.0, 0.0175088000000000, 0.0230517333333333, 0.00300480000000000, 0.0, 0.0475973333333333, 0.0, 0.00794400000000000, 0.00213120000000000, 0.0, 0.0172848000000000, 0.00908640000000000, 0.0365850666666667, 0.0, 0.0, 0.0238096000000000, 0.0, 0.0, 0.0, 0.0, 0.00782080000000000, 0.0115952000000000, 0.0, 0.0, 0.0, 0.0, 0.0619504000000000, 0.0, 0.0118976000000000, 0.0329968000000000, 0.0, 0.00245600000000000, 0.0, 0.0, 0.0, 0.0, 0.0114608000000000, 0.00217600000000000, 0.0131856000000000, 0.0, 0.0, 0.186080000000000, 0.0, 0.00413600000000000, 0.0196480000000000, 0.104992000000000, 0.0, 0.0, 0.00498720000000000, 0.0, 0.0, 0.0, 0.00612960000000000, 0.0, 0.0450453333333333, 0.0143728000000000, 0.0, 0.00546880000000000, 0.0, 0.0, 0.0, 0.00838080000000000, 0.0, 0.00201920000000000, 0.0, 0.0112816000000000, 0.0110128000000000, 0.106928000000000, 0.0, 0.0, 0.0113376000000000, 0.0, 0.0118080000000000, 0.0, 0.427215466666667, 0.00366560000000000, 0.0, 0.161776000000000, 0.0212384000000000, 0.0107216000000000, 0.0, 0.00392320000000000, 0.0, 0.0697610666666667, 0.0, 0.00906400000000000, 0.0, 0.0, 0.0455712000000000, 0.0, 0.00508800000000000, 0.00278080000000000, 0.0136896000000000, 0.0, 0.0, 0.0118752000000000, 0.0, 0.0925280000000000, 0.0458960000000000, 0.00676800000000000, 0.0, 0.0, 0.00438240000000000, 0.0, 0.0232218666666667, 0.0, 0.00530080000000000, 0.0, 0.0, 0.0, 0.0, 0.00953440000000000, 0.0, 0.0, 0.0268101333333333, 0.0369098666666667, 0.0, 0.00125760000000000, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.290899733333333, 0.0, 0.0, 0.0, 0.0, 0.0348064000000000, 0.0279392000000000, 0.00296000000000000, 0.0171504000000000, 0.0147760000000000, 0.0, 0.00870560000000000, 0.00627520000000000, 0.0, 0.00522240000000000, 0.00293760000000000, 0.0, 0.0, 0.0, 0.0259749333333333, 0.0101504000000000, 0.00326240000000000, 0.0, 0.00804480000000000, 0.0, 0.0216528000000000, 0.0, 0.0, 0.0, 0.0578208000000000, 0.0939840000000000, 0.0, 0.0345898666666667, 0.0106544000000000, 0.00313920000000000, 0.0, 0.0, 0.00164960000000000, 0.0238405333333333, 0.0, 0.0238714666666667, 0.0189648000000000, 0.0162320000000000, 0.0, 0.0, 0.0293466666666667, 0.0142608000000000, 0.0, 0.00179520000000000, 0.0119984000000000, 0.0, 0.0, 0.0, 0.0, 0.0501648000000000, 0.00209760000000000, 0.00503200000000000, 0.00150400000000000, 0.0, 0.146192000000000, 0.0, 0.00325120000000000, 0.0, 0.0, 0.0344970666666667, 0.0, 0.0, 0.00879520000000000, 0.0146976000000000, 0.00306080000000000, 0.0, 0.0, 0.00158240000000000, 0.0810400000000000, 0.0, 0.00307200000000000, 0.0199728000000000]) # the length of the result is the length of the gmf self.assertTrue(numpy.allclose(expected_loss_ratios, prob.compute_loss_ratios(self.vuln_function_1, self.gmfs, None, None)))
def test_an_empty_gmfs_produces_an_empty_set(self): self.assertEqual(0, prob.compute_loss_ratios( self.vuln_function_1, {"IMLs": ()}, None, None).size)
def test_an_empty_function_produces_an_empty_set(self): self.assertEqual(0, prob.compute_loss_ratios( shapes.EMPTY_CURVE, self.gmfs, None, None).size)
def test_an_empty_gmfs_produces_an_empty_set(self): self.assertEqual([], prob.compute_loss_ratios( self.vuln_function, {"IMLs": ()}))
def test_builds_the_cumulative_histogram(self): self.assertTrue(numpy.allclose(self.cum_histogram, prob.compute_cumulative_histogram( prob.compute_loss_ratios(self.vuln_function, self.gmfs), prob.compute_loss_ratios_range(self.vuln_function))))
def test_loss_ratios_computation_using_gmfs(self): # manually computed values by Vitor Silva expected_loss_ratios = numpy.array([0.0605584000000000, 0.273100266666667, 0.0958560000000000, 0.0184384000000000, 0.270366933333333, 0.0, 0.0252480000000000, 0.0795669333333333, 0.0529024000000000, 0.0, 0.0154928000000000, 0.00222080000000000, 0.0109232000000000, 0.0, 0.0, 0.0, 0.0175088000000000, 0.0230517333333333, 0.00300480000000000, 0.0, 0.0475973333333333, 0.0, 0.00794400000000000, 0.00213120000000000, 0.0, 0.0172848000000000, 0.00908640000000000, 0.0365850666666667, 0.0, 0.0, 0.0238096000000000, 0.0, 0.0, 0.0, 0.0, 0.00782080000000000, 0.0115952000000000, 0.0, 0.0, 0.0, 0.0, 0.0619504000000000, 0.0, 0.0118976000000000, 0.0329968000000000, 0.0, 0.00245600000000000, 0.0, 0.0, 0.0, 0.0, 0.0114608000000000, 0.00217600000000000, 0.0131856000000000, 0.0, 0.0, 0.186080000000000, 0.0, 0.00413600000000000, 0.0196480000000000, 0.104992000000000, 0.0, 0.0, 0.00498720000000000, 0.0, 0.0, 0.0, 0.00612960000000000, 0.0, 0.0450453333333333, 0.0143728000000000, 0.0, 0.00546880000000000, 0.0, 0.0, 0.0, 0.00838080000000000, 0.0, 0.00201920000000000, 0.0, 0.0112816000000000, 0.0110128000000000, 0.106928000000000, 0.0, 0.0, 0.0113376000000000, 0.0, 0.0118080000000000, 0.0, 0.427215466666667, 0.00366560000000000, 0.0, 0.161776000000000, 0.0212384000000000, 0.0107216000000000, 0.0, 0.00392320000000000, 0.0, 0.0697610666666667, 0.0, 0.00906400000000000, 0.0, 0.0, 0.0455712000000000, 0.0, 0.00508800000000000, 0.00278080000000000, 0.0136896000000000, 0.0, 0.0, 0.0118752000000000, 0.0, 0.0925280000000000, 0.0458960000000000, 0.00676800000000000, 0.0, 0.0, 0.00438240000000000, 0.0, 0.0232218666666667, 0.0, 0.00530080000000000, 0.0, 0.0, 0.0, 0.0, 0.00953440000000000, 0.0, 0.0, 0.0268101333333333, 0.0369098666666667, 0.0, 0.00125760000000000, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.290899733333333, 0.0, 0.0, 0.0, 0.0, 0.0348064000000000, 0.0279392000000000, 0.00296000000000000, 0.0171504000000000, 0.0147760000000000, 0.0, 0.00870560000000000, 0.00627520000000000, 0.0, 0.00522240000000000, 0.00293760000000000, 0.0, 0.0, 0.0, 0.0259749333333333, 0.0101504000000000, 0.00326240000000000, 0.0, 0.00804480000000000, 0.0, 0.0216528000000000, 0.0, 0.0, 0.0, 0.0578208000000000, 0.0939840000000000, 0.0, 0.0345898666666667, 0.0106544000000000, 0.00313920000000000, 0.0, 0.0, 0.00164960000000000, 0.0238405333333333, 0.0, 0.0238714666666667, 0.0189648000000000, 0.0162320000000000, 0.0, 0.0, 0.0293466666666667, 0.0142608000000000, 0.0, 0.00179520000000000, 0.0119984000000000, 0.0, 0.0, 0.0, 0.0, 0.0501648000000000, 0.00209760000000000, 0.00503200000000000, 0.00150400000000000, 0.0, 0.146192000000000, 0.0, 0.00325120000000000, 0.0, 0.0, 0.0344970666666667, 0.0, 0.0, 0.00879520000000000, 0.0146976000000000, 0.00306080000000000, 0.0, 0.0, 0.00158240000000000, 0.0810400000000000, 0.0, 0.00307200000000000, 0.0199728000000000]) # the length of the result is the length of the gmf self.assertTrue(numpy.allclose(expected_loss_ratios, prob.compute_loss_ratios(self.vuln_function, self.gmfs)))