Example #1
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.
        """

        vuln_function = shapes.VulnerabilityFunction([
                (0.10, (0.05, 0.30)),
                (0.30, (0.10, 0.30)),
                (0.50, (0.15, 0.20)),
                (1.00, (0.30, 0.20))])

        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,
                self.EpsilonProvider(expected_asset, epsilons),
                expected_asset), atol=0.0001))
Example #2
0
    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)))
Example #3
0
    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)))
Example #4
0
    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.
        """

        vuln_function = shapes.VulnerabilityFunction([
                (0.10, (0.00, 0.30)),
                (0.30, (0.10, 0.30))])

        epsilons = [0.5377]
        expected_asset = object()

        gmfs = {"IMLs": (0.1000, )}

        self.assertEqual(0.0, prob._compute_loss_ratios(
                vuln_function, gmfs, self.EpsilonProvider(
                expected_asset, epsilons), expected_asset)[0])
Example #5
0
    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)))
Example #6
0
 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)
Example #7
0
 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)