Skip to content

Commit

Permalink
Rename method and add docstring.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannis-vm committed Nov 21, 2024
1 parent f115815 commit be551e2
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions pelicun/uq.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,57 @@ def constant_parameters(self) -> bool:
assert self.theta.ndim in {1, 2}
return self.theta.ndim == 1

def _prepare_arrays(self, values: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
def _prepare_theta_and_truncation_limit_arrays(
self, values: np.ndarray
) -> tuple[np.ndarray, np.ndarray]:
"""
Prepare the `theta` and `truncation_limits` arrays.
Prepare the `theta` and `truncation_limits` arrays for use in
calculations. This method adjusts the shape and size of the
`theta` and `truncation_limits` attributes to ensure
compatibility with the provided `values` array. The
adjustments enable support for two approaches:
* Constant parameters: The parameters remain the same
across all realizations.
* Variable parameters: The parameters vary across
realizations.
Depending on whether the random variable uses constant or
variable parameters, the method ensures that the arrays are
correctly sized and broadcasted as needed.
Parameters
----------
values : np.ndarray
Array of values for which the `theta` and
`truncation_limits` need to be prepared. The size of
`values` determines how the attributes are adjusted.
Returns
-------
tuple
A tuple containing:
* `theta` (np.ndarray): Adjusted array of parameters.
* `truncation_limits` (np.ndarray): Adjusted array of
truncation limits.
Raises
------
ValueError
If the number of elements in `values` does not match the
number of rows of the `theta` attribute or if the
`truncation_limits` array is incompatible with the `theta`
array.
Notes
-----
The method ensures that `truncation_limits` are broadcasted to
match the shape of `theta` if needed. For constant parameters,
a single-row `theta` is expanded to a 2D array. For variable
parameters, the number of rows in `theta` must match the size
of `values`.
"""
theta = self.theta
assert theta is not None
truncation_limits = self.truncation_limits
Expand Down Expand Up @@ -1483,7 +1533,9 @@ def cdf(self, values: np.ndarray) -> np.ndarray:
1D float ndarray containing CDF values
"""
theta, truncation_limits = self._prepare_arrays(values)
theta, truncation_limits = self._prepare_theta_and_truncation_limit_arrays(
values
)
mu, sig = theta.T

if np.any(~np.isnan(self.truncation_limits)):
Expand Down Expand Up @@ -1534,7 +1586,9 @@ def inverse_transform(self, values: np.ndarray) -> np.ndarray:
too small
"""
theta, truncation_limits = self._prepare_arrays(values)
theta, truncation_limits = self._prepare_theta_and_truncation_limit_arrays(
values
)
mu, sig = theta.T

if np.any(~np.isnan(self.truncation_limits)):
Expand Down Expand Up @@ -1670,7 +1724,9 @@ def cdf(self, values: np.ndarray) -> np.ndarray:
1D float ndarray containing CDF values
"""
theta, truncation_limits = self._prepare_arrays(values)
theta, truncation_limits = self._prepare_theta_and_truncation_limit_arrays(
values
)
theta, beta = theta.T

if np.any(~np.isnan(self.truncation_limits)):
Expand Down Expand Up @@ -1718,7 +1774,9 @@ def inverse_transform(self, values: np.ndarray) -> np.ndarray:
Inverse CDF values
"""
theta, truncation_limits = self._prepare_arrays(values)
theta, truncation_limits = self._prepare_theta_and_truncation_limit_arrays(
values
)
theta, beta = theta.T

if np.any(~np.isnan(self.truncation_limits)):
Expand Down

0 comments on commit be551e2

Please sign in to comment.