def test_mean_predictor_estimate_coefficients_nans(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor, when one value from the input data is set to NaN. The coefficients are estimated using a linear model. """ data = [1, 1, 2.66666667, 1] cube = self.cube current_forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN) truth = cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "mean" estimate_coefficients_from_linear_model_flag = True current_forecast_predictor.data[0][0][0] = np.nan plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertArrayAlmostEqual(result, data)
def test_realizations_predictor_estimate_coefficients(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. The coefficients are estimated using a linear model. """ import imp try: statsmodels_found = imp.find_module('statsmodels') statsmodels_found = True except ImportError: statsmodels_found = False if statsmodels_found: data = [1., 1., 0.13559322, -0.11864407, 0.42372881, 0.69491525] else: data = [1, 1, 0, 1, 1, 1] current_forecast_predictor = self.cube truth = self.cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "realizations" no_of_realizations = 3 estimate_coefficients_from_linear_model_flag = True plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_realizations=no_of_realizations) self.assertArrayAlmostEqual(result, data)
def test_basic_members_predictor_value_check(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the individual ensemble members are used as predictors. As coefficients are not estimated using a linear model, the default values for the initial guess are used. """ data = [1, 1, 0, 1, 1, 1] cube = self.cube current_forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN) truth = cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "members" no_of_members = 3 estimate_coefficients_from_linear_model_flag = False plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_members=no_of_members) self.assertArrayAlmostEqual(result, data)
def test_basic_realizations_predictor_value_check(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the individual ensemble realizations are used as predictors. As coefficients are not estimated using a linear model, the default values for the initial guess are used. """ data = [ 0, 1, 0, np.sqrt(1. / self.no_of_realizations), np.sqrt(1. / self.no_of_realizations), np.sqrt(1. / self.no_of_realizations) ] predictor_of_mean_flag = "realizations" estimate_coefficients_from_linear_model_flag = False plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_realizations, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_realizations=self.no_of_realizations) self.assertArrayAlmostEqual(result, data)
def test_basic_mean_predictor(self): """ Test that the plugin returns a list containing the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. """ estimate_coefficients_from_linear_model_flag = False plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_mean, self.predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertIsInstance(result, np.ndarray)
def test_basic_mean_predictor_value_check(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. As coefficients are not estimated using a linear model, the default values for the initial guess are used. """ data = [0, 1, 0, 1] estimate_coefficients_from_linear_model_flag = False plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_mean, self.predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertArrayAlmostEqual(result, data)
def test_basic_realizations_predictor(self): """ Test that the plugin returns a list containing the initial guess for the calibration coefficients, when the individual ensemble realizations are used as predictors. """ predictor_of_mean_flag = "realizations" estimate_coefficients_from_linear_model_flag = False plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_realizations, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_realizations=self.no_of_realizations) self.assertIsInstance(result, np.ndarray)
def test_mean_predictor_estimate_coefficients(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. The coefficients are estimated using a linear model, where there is an offset of one between the truth and the forecast during the training period. Therefore, in this case the result of the linear regression is a gradient of 1 and an intercept of 1. """ data = np.array([0., 1., 1., 1.], dtype=np.float32) estimate_coefficients_from_linear_model_flag = True plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_mean, self.predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertArrayAlmostEqual(result, data)
def test_basic_mean_predictor(self): """ Test that the plugin returns a list containing the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. """ current_forecast_predictor = self.cube.collapsed( "realization", iris.analysis.MEAN) truth = self.cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "mean" estimate_coefficients_from_linear_model_flag = False plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertIsInstance(result, np.ndarray)
def test_realizations_predictor_estimate_coefficients(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. The coefficients are estimated using a linear model. In this case, the result of the linear regression is for an intercept of 0.333333 with different weights for the realizations because some of the realizations are closer to the truth, in this instance. """ data = [0., 1., 0.333333, 0., 0.333333, 0.666667] predictor_of_mean_flag = "realizations" estimate_coefficients_from_linear_model_flag = True plugin = Plugin(self.distribution, self.desired_units) result = plugin.compute_initial_guess( self.truth, self.current_forecast_predictor_realizations, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_realizations=self.no_of_realizations) self.assertArrayAlmostEqual(result, data)
def test_mean_predictor_estimate_coefficients(self): """ Test that the plugin returns the expected values for the initial guess for the calibration coefficients, when the ensemble mean is used as the predictor. The coefficients are estimated using a linear model. """ data = np.array([1, 1, 2.66663, 1], dtype=np.float32) current_forecast_predictor = self.cube.collapsed( "realization", iris.analysis.MEAN) truth = self.cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "mean" estimate_coefficients_from_linear_model_flag = True plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag) self.assertArrayAlmostEqual(result, data, decimal=5)
def test_basic_members_predictor(self): """ Test that the plugin returns a list containing the initial guess for the calibration coefficients, when the individual ensemble members are used as predictors. """ cube = self.cube current_forecast_predictor = cube.copy() truth = cube.collapsed("realization", iris.analysis.MAX) distribution = "gaussian" desired_units = "degreesC" predictor_of_mean_flag = "members" no_of_members = 3 estimate_coefficients_from_linear_model_flag = False plugin = Plugin(distribution, desired_units) result = plugin.compute_initial_guess( truth, current_forecast_predictor, predictor_of_mean_flag, estimate_coefficients_from_linear_model_flag, no_of_members=no_of_members) self.assertIsInstance(result, list)