diff --git a/swiftemulator/emulators/base.py b/swiftemulator/emulators/base.py index 6b1e4c8..1950128 100644 --- a/swiftemulator/emulators/base.py +++ b/swiftemulator/emulators/base.py @@ -80,7 +80,8 @@ def predict_values( self, independent: np.array, model_parameters: Dict[str, float] ) -> tuple[np.array, np.array]: """ - Predict values from the trained emulator contained within this object. + Predict values and the associated variance from the trained emulator contained + within this object. Parameters ---------- @@ -123,3 +124,49 @@ def predict_values( ) raise NotImplementedError + + def predict_values_no_error( + self, independent: np.array, model_parameters: Dict[str, float] + ) -> np.array: + """ + Predict values from the trained emulator contained within this object. + In cases where the error estimates are not required, this method is + significantly faster than predict_values(). + + Parameters + ---------- + + independent, np.array + Independent continuous variables to evaluate the emulator + at. If the emulator is discrete, these are only allowed to be + the discrete independent variables that the emulator was trained at + (disregarding the additional 'independent' model parameters, below.) + + model_parameters: Dict[str, float] + The point in model parameter space to create predicted + values at. + + Returns + ------- + + dependent_predictions, np.array + Array of predictions, if the emulator is a function f, these + are the predicted values of f(independent) evaluted at the position + of the input ``model_parameters``. + + Raises + ------ + + AttributeError + When the model has not been trained before trying to make a + prediction, or when attempting to evaluate the model at + disallowed independent variables. + """ + + if self.emulator is None: + raise AttributeError( + "Please train the emulator with fit_model before attempting " + "to make predictions." + ) + + raise NotImplementedError diff --git a/swiftemulator/emulators/gaussian_process.py b/swiftemulator/emulators/gaussian_process.py index 7d40183..8ee1fcc 100644 --- a/swiftemulator/emulators/gaussian_process.py +++ b/swiftemulator/emulators/gaussian_process.py @@ -231,7 +231,8 @@ def predict_values( self, independent: np.array, model_parameters: Dict[str, float] ) -> tuple[np.array, np.array]: """ - Predict values from the trained emulator contained within this object. + Predict values and the associated variance from the trained emulator + contained within this object. Parameters ---------- @@ -253,7 +254,7 @@ def predict_values( of the input model_parameters. dependent_prediction_errors, np.array - Errors on the model predictions. + Errors (variances) on the model predictions. """ if self.emulator is None: @@ -279,3 +280,55 @@ def predict_values( ) return model, errors + + def predict_values_no_error( + self, independent: np.array, model_parameters: Dict[str, float] + ) -> np.array: + """ + Predict values from the trained emulator contained within this object. + In cases where the error estimates are not required, this method is + significantly faster than predict_values(). + + Parameters + ---------- + + independent, np.array + Independent continuous variables to evaluate the emulator + at. + + model_parameters: Dict[str, float] + The point in model parameter space to create predicted + values at. + + Returns + ------- + + dependent_predictions, np.array + Array of predictions, if the emulator is a function f, these + are the predicted values of f(independent) evaluted at the position + of the input model_parameters. + """ + + if self.emulator is None: + raise AttributeError( + "Please train the emulator with fit_model before attempting " + "to make predictions." + ) + + model_parameter_array = np.array( + [model_parameters[parameter] for parameter in self.parameter_order] + ) + + t = np.empty( + (len(independent), len(model_parameter_array) + 1), dtype=np.float32 + ) + + for line, value in enumerate(independent): + t[line][0] = value + t[line][1:] = model_parameter_array + + model = self.emulator.predict( + y=self.dependent_variables, t=t, return_cov=False, return_var=False + ) + + return model