Skip to content

Commit

Permalink
Add input validation for dl1_dp1 and dl2_dp2
Browse files Browse the repository at this point in the history
  • Loading branch information
BerriJ committed Feb 26, 2025
1 parent df6159e commit 113c78e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/rolch/base/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,21 @@ def dl2_dpp(
) -> np.ndarray:
"""Take the first derivative of the likelihood function with respect to both parameters."""

def _validate_dln_dpn_inputs(
self, y: np.ndarray, theta: np.ndarray, param: int
) -> None:
if param >= self.n_params:
raise ValueError(
f"{self.__class__.__name__} has only {self.n_params} distribution parameters.\nYou have passed {param}. Please remember we start counting at 0."
)

def _validate_dl2_dpp_inputs(
self, y: np.ndarray, theta: np.ndarray, params: Tuple[int, int]
) -> None:
if max(params) >= self.n_params:
raise ValueError(
f"{self.__class__.__name__} has only {self.n_params} distribution parameters.\nYou have passed {params}. Please remember we start counting at 0."
)
if params[0] == params[1]:
raise ValueError("Cross derivatives must use different parameters.")

Expand Down
2 changes: 2 additions & 0 deletions src/rolch/distributions/gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def gamlss_to_scipy(
return alpha, loc, scale

def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma = self.theta_to_params(theta)

if param == 0:
Expand All @@ -103,6 +104,7 @@ def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarra
)

def dl2_dp2(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma = self.theta_to_params(theta)
if param == 0:
# MU
Expand Down
2 changes: 2 additions & 0 deletions src/rolch/distributions/johnsonsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def theta_to_params(
return mu, sigma, nu, tau

def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma, nu, tau = self.theta_to_params(theta)

if param == 0:
Expand Down Expand Up @@ -91,6 +92,7 @@ def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarra
return dldt

def dl2_dp2(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma, nu, tau = self.theta_to_params(theta)
if param == 0:
# MU
Expand Down
2 changes: 2 additions & 0 deletions src/rolch/distributions/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def theta_to_params(self, theta: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
return mu, sigma

def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma = self.theta_to_params(theta)

if param == 0:
Expand All @@ -39,6 +40,7 @@ def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarra
return ((y - mu) ** 2 - sigma**2) / (sigma**3)

def dl2_dp2(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
_, sigma = self.theta_to_params(theta)
if param == 0:
# MU
Expand Down
2 changes: 2 additions & 0 deletions src/rolch/distributions/studentt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def theta_to_params(
return mu, sigma, nu

def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
mu, sigma, nu = self.theta_to_params(theta)

if param == 0:
Expand Down Expand Up @@ -73,6 +74,7 @@ def dl1_dp1(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarra
) / 2

def dl2_dp2(self, y: np.ndarray, theta: np.ndarray, param: int = 0) -> np.ndarray:
self._validate_dln_dpn_inputs(y, theta, param)
_, sigma, nu = self.theta_to_params(theta)
if param == 0:
# MU
Expand Down

0 comments on commit 113c78e

Please sign in to comment.