Example #1
0
    def test_end_to_end(self):
        """
        Tests PenalizedMLE with bootstrapping and increased sample size
        and asserts that the known b-value and (approx) rate
        are within a narrow range (0.4 - 0.6 quantiles)
        """
        poly1 = Polygon([
            Point(20.0, 30.0),
            Point(20.0, 40.0),
            Point(30.0, 40.0),
            Point(30.0, 30.0)
        ])
        self.config = {
            "b_prior": 0.9,
            "reference_magnitude": 3.0,
            "b_prior_weight": 25.0,
            "a_prior": 0.0,
            "a_prior_weight": 0.0,
            "area": area_of_polygon(poly1),
            "mmax": 8.0
        }

        sample_size = [5, 10, 20, 50, 100, 200, 500, 1000]
        expected_b = 0.9
        for sample in sample_size:
            outputs = []
            expected_rate = (float(sample) /
                             float(self.catalogue.get_number_events())) * 100.
            for i in range(1000):
                idx = np.arange(self.catalogue.get_number_events())
                np.random.shuffle(idx)
                idx = idx[:sample]
                new_cat = deepcopy(self.catalogue)
                new_cat.select_catalogue_events(np.sort(idx))
                mle = PenalizedMLE()
                bval, sigmab, rate, sigma_rate = mle.calculate(
                    new_cat, self.config, self.completeness)
                outputs.append([bval, sigmab, rate, sigma_rate])
            outputs = np.array(outputs)
            mean_b = np.mean(outputs[:, 0])
            mean_sigmab = np.mean(outputs[:, 1])
            mean_rate = np.mean(outputs[:, 2])
            mean_sigma_rate = np.mean(outputs[:, 3])
            # Assert that b-value is in the expected range
            l_b, u_b = (norm.ppf(0.4, loc=mean_b, scale=mean_sigmab),
                        norm.ppf(0.6, loc=mean_b, scale=mean_sigmab))
            self.assertTrue((0.9 >= l_b) and (0.9 <= u_b))
            # Assert that rate is in the expected range
            l_b, u_b = (norm.ppf(0.4, loc=mean_rate, scale=mean_sigma_rate),
                        norm.ppf(0.6, loc=mean_rate, scale=mean_sigma_rate))
            self.assertTrue((expected_rate >= l_b) and (expected_rate <= u_b))
    def test_end_to_end(self):
        """
        Tests PenalizedMLE with bootstrapping and increased sample size
        and asserts that the known b-value and (approx) rate
        are within a narrow range (0.4 - 0.6 quantiles)
        """
        poly1 = Polygon([Point(20.0, 30.0), Point(20.0, 40.0),
                         Point(30.0, 40.0), Point(30.0, 30.0)])
        self.config = {"b_prior": 0.9, "reference_magnitude": 3.0,
                       "b_prior_weight": 25.0, "a_prior": 0.0,
                       "a_prior_weight": 0.0,
                       "area": area_of_polygon(poly1), "mmax": 8.0}

        sample_size = [5, 10, 20, 50, 100, 200, 500, 1000]
        expected_b = 0.9
        for sample in sample_size:
            outputs = []
            expected_rate = (float(sample) /
                             float(self.catalogue.get_number_events())) * 100.
            for i in range(1000):
                idx = np.arange(self.catalogue.get_number_events())
                np.random.shuffle(idx)
                idx = idx[:sample]
                new_cat = deepcopy(self.catalogue)
                new_cat.select_catalogue_events(np.sort(idx))
                mle = PenalizedMLE()
                bval, sigmab, rate, sigma_rate = mle.calculate(
                    new_cat,
                    self.config,
                    self.completeness)
                outputs.append([bval, sigmab, rate, sigma_rate])
            outputs = np.array(outputs)
            mean_b = np.mean(outputs[:, 0])
            mean_sigmab = np.mean(outputs[:, 1])
            mean_rate = np.mean(outputs[:, 2])
            mean_sigma_rate = np.mean(outputs[:, 3])
            # Assert that b-value is in the expected range
            l_b, u_b = (norm.ppf(0.4, loc=mean_b, scale=mean_sigmab),
                        norm.ppf(0.6, loc=mean_b, scale=mean_sigmab))
            self.assertTrue((0.9 >= l_b) and (0.9 <= u_b))
            # Assert that rate is in the expected range
            l_b, u_b = (norm.ppf(0.4, loc=mean_rate, scale=mean_sigma_rate),
                        norm.ppf(0.6, loc=mean_rate, scale=mean_sigma_rate))
            self.assertTrue((expected_rate >= l_b) and (expected_rate <= u_b))