def setUp(self): super(HarmoniserWithModelTestCase, self).setUp() native_measures_1 = self.measures[0:self.number_of_measures / 3] native_measures_2 = self.measures[ self.number_of_measures / 3:2 * self.number_of_measures / 3] target_measures = self.measures[2 * self.number_of_measures / 3:] emsr = EmpiricalMagnitudeScalingRelationship( native_measures=native_measures_1, target_measures=target_measures) self.a_model, _ = emsr.apply_regression_model( LinearModel) emsr = EmpiricalMagnitudeScalingRelationship( native_measures=native_measures_2, target_measures=target_measures) self.ya_model, _ = emsr.apply_regression_model( LinearModel)
def test_regression_with_initial_values(self): # Assess A = 0.85 B = 1.03 native_measures = catalogue.MagnitudeMeasure.make_from_lists( 'mb', np.random.uniform(3., 8.5, 1000), np.random.uniform(0.02, 0.2, 1000)) target_measures = catalogue.MagnitudeMeasure.make_from_lists( 'Mw', A + B * np.array([m.value for m in native_measures]), np.random.uniform(0.025, 0.2, 1000)) emsr = EmpiricalMagnitudeScalingRelationship( native_measures, target_measures) # Act _, output = emsr.apply_regression_model(LinearModel, initial_values=[0, 1]) # Assert self.assertTrue(allclose(np.array([A, B]), output.beta)) self.assertTrue(output.res_var < 1e-20)
def test_polynomial_regression(self): # Assess A = 0.046 B = 0.556 C = 0.673 native_measures = catalogue.MagnitudeMeasure.make_from_lists( 'mb', np.random.uniform(3., 8.5, 1000), np.random.uniform(0.02, 0.2, 1000)) target_measures = catalogue.MagnitudeMeasure.make_from_lists( 'Mw', (C + B * np.array([m.value for m in native_measures]) + A * (np.array([m.value for m in native_measures]) ** 2.)), np.random.uniform(0.025, 0.2, 1000)) emsr = EmpiricalMagnitudeScalingRelationship( native_measures, target_measures) # Act _, output = emsr.apply_regression_model(PolynomialModel, order=2) # Assert self.assertTrue(allclose(np.array([C, B, A]), output.beta)) self.assertTrue(output.res_var < 1e-20)
def perform_regression(self): """ Actually, perform the regression analysis. :returns: a list of n regression outputs where n is the number of models, and a regression output is a dictionary. In this dictionary the keys model and output are mapped into a considered output and its corresponding scipy output result """ self._emsr = EmpiricalMagnitudeScalingRelationship.make_from_measures( self._native_scale, self._target_scale, self.grouped_measures(), self._selector, self._mu_strategy) scipy_outputs = [] for model_class, model_kwargs in self._models: model, output = self._emsr.apply_regression_model(model_class, **model_kwargs) scipy_outputs.append({'model': model, 'output': output}) return scipy_outputs