Skip to content

Commit c88d8d0

Browse files
committed
[VCF] Add complete GPD distribution class
1 parent 2a6c1e5 commit c88d8d0

File tree

1 file changed

+21
-17
lines changed
  • bluemath_tk/distributions

1 file changed

+21
-17
lines changed

bluemath_tk/distributions/gpd.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def param_names() -> List[str]:
8181
"""
8282
Name of parameters of GPD
8383
"""
84-
return ["location", "scale", "shape"]
84+
return ["loc", "scale", "shape"]
8585

8686
@staticmethod
8787
def pdf(
@@ -256,11 +256,11 @@ def qf(
256256

257257
# Gumbel case (shape = 0)
258258
if shape == 0.0:
259-
q = loc - scale * np.log(p)
259+
q = loc - scale * np.log(1 - p)
260260

261261
# General case (Weibull and Frechet, shape != 0)
262262
else:
263-
q = loc + scale * (p ** (-shape) - 1) / shape
263+
q = loc + scale * ((1 - p) ** (-shape) - 1) / shape
264264

265265
return q
266266

@@ -294,22 +294,23 @@ def nll(
294294
nll = np.inf # Return a large value for invalid scale
295295

296296
else:
297-
y = data - loc / scale
297+
y = (data - loc) / scale
298298

299-
# Gumbel case (shape = 0)
300-
if shape == 0.0:
301-
nll = data.shape[0] * np.log(scale) + np.sum(y)
299+
# # Gumbel case (shape = 0)
300+
# if shape == 0.0:
301+
# nll = data.shape[0] * np.log(scale) + np.sum(y)
302302

303303
# General case (Weibull and Frechet, shape != 0)
304+
# else:
305+
shape = np.maximum(shape, 1e-8) if shape > 0 else np.minimum(shape, -1e-8) # Avoid division by zero
306+
y = 1 + shape * y
307+
if np.min(y <= 0):
308+
nll = np.inf # Return a large value for invalid y
304309
else:
305-
y = 1 + shape * y
306-
if any(y <= 0):
307-
nll = np.inf # Return a large value for invalid y
308-
else:
309-
nll = (
310-
data.shape[0] * np.log(scale)
311-
+ (1 / shape + 1) * np.sum(np.log(y))
312-
)
310+
nll = (
311+
data.shape[0] * np.log(scale)
312+
+ (1 / shape + 1) * np.sum(np.log(y))
313+
)
313314

314315
return nll
315316

@@ -468,8 +469,11 @@ def median(loc: float = 0.0, scale: float = 1.0, shape: float = 0.0) -> float:
468469

469470
if scale <= 0:
470471
raise ValueError("Scale parameter must be > 0")
471-
472-
median = loc + scale * (2 ** shape - 1) / shape
472+
473+
if shape == 0:
474+
median = np.inf
475+
else:
476+
median = loc + scale * (2 ** shape - 1) / shape
473477

474478
return median
475479

0 commit comments

Comments
 (0)