Skip to content

Commit

Permalink
Add the method predict_values_no_error() to the emulator object to pr…
Browse files Browse the repository at this point in the history
…ovide predictions without the associated errors, which can be a lot faster.
  • Loading branch information
MatthieuSchaller committed Aug 10, 2024
1 parent b65c35b commit f94c04f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
49 changes: 48 additions & 1 deletion swiftemulator/emulators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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
57 changes: 55 additions & 2 deletions swiftemulator/emulators/gaussian_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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:
Expand All @@ -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

0 comments on commit f94c04f

Please sign in to comment.