-
Notifications
You must be signed in to change notification settings - Fork 0
/
pykelihood_distributions.py
69 lines (58 loc) · 2.52 KB
/
pykelihood_distributions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from scipy.stats import genpareto, genextreme
from pykelihood.distributions import ScipyDistribution
from pykelihood.parameters import ConstantParameter
from pykelihood.utils import ifnone
import numpy as np
class GEV_reparametrized_loc(ScipyDistribution):
params_names = ("r", "scale", "shape", "p")
base_module = genextreme
def __init__(self, r=0.0, scale=1.0, shape=0.0, p=1 / 200):
super(GEV_reparametrized_loc, self).__init__(r, scale, shape, ConstantParameter(p))
def _to_scipy_args(self, r=None, scale=None, shape=None):
scale = ifnone(scale, self.scale())
shape = ifnone(shape, self.shape())
r = ifnone(r, self.r())
p = float(self.p())
loc = r - (scale / shape) * ((-np.log(1 - p)) ** (-shape) - 1) if shape != 0 else r - scale * (
-(np.log(-np.log(1 - p))))
if shape is not None:
shape = -shape
return {
"c": ifnone(shape, -self.shape()),
"loc": ifnone(loc, loc),
"scale": scale
}
class GEV_reparametrized_p(ScipyDistribution):
params_names = ("loc", "p", "shape", "r")
base_module = genextreme
def __init__(self, loc=0.0, p=1/200, shape=0.0, r=1.0):
super(GEV_reparametrized_p, self).__init__(loc, p, shape, ConstantParameter(r))
def _to_scipy_args(self, loc=None, p=None, shape=None):
loc = ifnone(loc, self.loc())
shape = ifnone(shape, self.shape())
r = float(self.r())
p = ifnone(p, self.p())
scale = (r - loc) * shape / ((-np.log(1 - p)) ** (-shape) - 1) if shape != 0 else (r - loc) / (-(np.log(-np.log(1 - p))))
if shape is not None:
shape = -shape
return {
"c": ifnone(shape, -self.shape()),
"loc": loc,
"scale": ifnone(scale, scale),
}
class GPD_reparametrized_r_loc(ScipyDistribution):
params_names = ("r", "scale", "shape", "p")
base_module = genpareto
def __init__(self, r=0.0, scale=1.0, shape=0.0, p=1 / 200):
super(GPD_reparametrized_r_loc, self).__init__(r, scale, shape, ConstantParameter(p))
def _to_scipy_args(self, r=None, scale=None, shape=None):
scale = ifnone(scale, self.scale())
shape = ifnone(shape, self.shape())
r = ifnone(r, self.r())
p = float(self.p())
loc = r - (p ** -shape - 1) * scale / shape if shape != 0 else r + scale * np.log(p)
return {
"c": ifnone(shape, self.shape()),
"loc": loc,
"scale": ifnone(scale, scale),
}