Skip to content

Commit 10b16cf

Browse files
authored
Merge pull request #119 from viccollado/feature/extremes
Fixing return period plot errors in NonStatGEV
2 parents d09ee5c + 2647d80 commit 10b16cf

File tree

1 file changed

+135
-8
lines changed

1 file changed

+135
-8
lines changed

bluemath_tk/distributions/nonstat_gev.py

Lines changed: 135 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,34 @@ def _auxmin_loglikelihood(
15931593
) -> float:
15941594
"""
15951595
Function used for minimizing in the 'self._fit' where the Negative loglikelihood of the GEV will be minimized
1596+
1597+
Parameters
1598+
----------
1599+
x : np.ndarray
1600+
Parameter vector to optimize
1601+
nmu : int, default=0
1602+
Number of harmonics included in location
1603+
npsi : int, default=0
1604+
Number of harmonics included in scale
1605+
ngamma : int, default=0
1606+
Number of harmonics included in shape
1607+
ntrend_loc : int, default=0
1608+
Whether to include trends in location
1609+
list_loc : list, default=[]
1610+
List of covariates indices to include in location
1611+
ntrend_sc : int, default=0
1612+
Whether to include trends in scale
1613+
list_sc : list, default=[]
1614+
List of covariates indices to include in scale
1615+
ntrend_sh : int, default=0
1616+
Whether to include trends in shape
1617+
list_sh : list, default=[]
1618+
List of covariates indices to include in shape
1619+
1620+
Return
1621+
------
1622+
f : float
1623+
Negative loglikelihood value of the Non-stationary GEV
15961624
"""
15971625
# Cheking the inputs
15981626
covariates_loc = self.covariates.iloc[:, list_loc].values
@@ -2081,6 +2109,34 @@ def _auxmin_loglikelihood_grad(
20812109
) -> np.ndarray:
20822110
"""
20832111
Function used for minimizing in the 'self._optimize_parameters' where the Negative loglikelihood of the GEV will be minimized
2112+
2113+
Parameters
2114+
----------
2115+
x : np.ndarray
2116+
Parameter vector to optimize
2117+
nmu : int, default=0
2118+
Number of harmonics included in location
2119+
npsi : int, default=0
2120+
Number of harmonics included in scale
2121+
ngamma : int, default=0
2122+
Number of harmonics included in shape
2123+
ntrend_loc : int, default=0
2124+
Whether to include trends in location
2125+
list_loc : list, default=[]
2126+
List of covariates indices to include in location
2127+
ntrend_sc : int, default=0
2128+
Whether to include trends in scale
2129+
list_sc : list, default=[]
2130+
List of covariates indices to include in scale
2131+
ntrend_sh : int, default=0
2132+
Whether to include trends in shape
2133+
list_sh : list, default=[]
2134+
List of covariates indices to include in shape
2135+
2136+
Return
2137+
------
2138+
Jx : np.ndarray
2139+
Gradient of negative loglikelihood value of the Non-stationary GEV
20842140
"""
20852141
# Cheking the inputs
20862142
covariates_loc = self.covariates.iloc[:, list_loc].values
@@ -4677,6 +4733,8 @@ def plot(self, return_plot=True):
46774733
"""
46784734
Plot the location, scale and shape parameters, also the PP plot and QQ plot
46794735
4736+
Return period plot is plotted if and only if no covariates and trends are included
4737+
46804738
Parameters
46814739
----------
46824740
return_plot : bool, default=True
@@ -5218,7 +5276,7 @@ def plot(self, return_plot=True):
52185276

52195277
def QQplot(self):
52205278
"""
5221-
Print QQ plot
5279+
QQ plot
52225280
"""
52235281
Ze = -np.log(-np.log(np.arange(1, len(self.xt) + 1) / (len(self.xt) + 1)))
52245282
Zm = self.kt * self._Zstandardt()
@@ -5271,6 +5329,11 @@ def QQplot(self):
52715329
def _Zstandardt(self):
52725330
"""
52735331
Calculates the standardized variable corresponding to the given parameters
5332+
5333+
Return
5334+
------
5335+
Zt :
5336+
Standarized variable of the given parameters
52745337
"""
52755338

52765339
Zt = np.zeros(len(self.xt))
@@ -5317,9 +5380,14 @@ def _Zstandardt(self):
53175380

53185381
return Zt
53195382

5320-
def _Dzweibull(self):
5383+
def _Dzweibull(self) -> np.ndarray:
53215384
"""
53225385
Calculates the derivatives of the standardized maximum with respect to parameters
5386+
5387+
Return
5388+
------
5389+
Dq : np.ndarray
5390+
Derivative of standarized variable of the given parameters
53235391
"""
53245392

53255393
nd = len(self.t)
@@ -5533,9 +5601,18 @@ def _Dzweibull(self):
55335601

55345602
return Dq
55355603

5536-
def _Dmupsiepst(self):
5604+
def _Dmupsiepst(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
55375605
"""
5538-
Calculates the derivatives of the standardized maximum with respect to parameters
5606+
Calculates the derivatives of the standarized maximum with respect to parameters
5607+
5608+
Return
5609+
------
5610+
Dermut : np.ndarray
5611+
Derivative of standarized maximum of location
5612+
Derpsit : np.ndarray
5613+
Derivative of standarized maximum of scale
5614+
Derepst : np.ndarray
5615+
Derivative of standarized maximum of shape
55395616
"""
55405617

55415618
t = self.t % 1
@@ -5667,9 +5744,14 @@ def _Dmupsiepst(self):
56675744

56685745
return Dermut, Derpsit, Derepst
56695746

5670-
def _DQuantile(self):
5747+
def _DQuantile(self) -> np.ndarray:
56715748
"""
56725749
Calculates the quantile derivative associated with a given parameterization with respect model parameters
5750+
5751+
Return
5752+
------
5753+
Dq : np.ndarray
5754+
Quantile derivative
56735755
"""
56745756

56755757
t = self.t % 1
@@ -5955,6 +6037,11 @@ def PPplot(self):
59556037
def _CDFGEVt(self):
59566038
"""
59576039
Calculates the GEV distribution function corresponding to the given parameters
6040+
6041+
Return
6042+
------
6043+
F : np.ndarray
6044+
Cumulative distribution function values of Non-stationary GEV for the data
59586045
"""
59596046

59606047
F = np.zeros(len(self.xt))
@@ -6007,7 +6094,12 @@ def _CDFGEVt(self):
60076094

60086095
def ReturnPeriodPlot(self, annualplot=True):
60096096
"""
6010-
Funtion to plot the Aggregated Return period plot for each month and if annualplot, the annual Return period (default True)
6097+
Funtion to plot the Aggregated Return period plot for each month and the annual Return period
6098+
6099+
Parameters
6100+
----------
6101+
annualplot : bool, default=True
6102+
Whether to plot the annual return period plot
60116103
"""
60126104

60136105
# Ts = np.array([2, 5, 10, 20, 25, 50, 75, 100, 200, 300, 400, 500])
@@ -6136,6 +6228,39 @@ def _aggquantile(
61366228
):
61376229
"""
61386230
Function to compute the aggregated quantile for certain parameters
6231+
6232+
Parameters
6233+
----------
6234+
q :
6235+
Quantile value
6236+
t0 :
6237+
Starting point of integration interval
6238+
t1 :
6239+
Ending point of integration interval
6240+
beta0 : default=None
6241+
Stationary part of location parameter
6242+
beta : default=None
6243+
Harmonic part of location parameter
6244+
alpha0 : default=None,
6245+
Stationary part of scale parameter
6246+
alpha : default=None
6247+
Harmonic part of scale parameter
6248+
gamma0: default=None
6249+
Stationary part of shape parameter
6250+
gamma : default=None,
6251+
Harmonic part of shape parameter
6252+
betaT : default=None
6253+
Trend part of location parameter
6254+
alphaT : default=None
6255+
Trend part of scale parameter
6256+
gammaT : default=None
6257+
Trend part of shape parameter
6258+
beta_cov : default=None
6259+
Covariate part of location parameter
6260+
alpha_cov : default=None
6261+
Covariate part of scale parameter
6262+
gamma_cov : default=None
6263+
Covariate part of shape parameter
61396264
"""
61406265
if beta0 is None:
61416266
beta0 = self.beta0
@@ -6157,6 +6282,8 @@ def _aggquantile(
61576282
beta_cov = self.beta_cov
61586283
if alpha_cov is None:
61596284
alpha_cov = self.alpha_cov
6285+
if gamma_cov is None:
6286+
gamma_cov = self.gamma_cov
61606287

61616288
q = np.array([q])
61626289
t0 = np.array([t0])
@@ -6209,7 +6336,7 @@ def _aggquantile(
62096336
),
62106337
0,
62116338
1,
6212-
)
6339+
)[0]
62136340
std = quad(
62146341
lambda x: np.exp(
62156342
self._parametro(
@@ -6225,7 +6352,7 @@ def _aggquantile(
62256352
),
62266353
0,
62276354
1,
6228-
)
6355+
)[0]
62296356

62306357
for il in range(m):
62316358
# for jl in range(n)

0 commit comments

Comments
 (0)