-
Notifications
You must be signed in to change notification settings - Fork 4
/
MarkDistributions.py
225 lines (170 loc) · 6.33 KB
/
MarkDistributions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
__author__ = 'tjohnson'
import random
import math
class ParetoMarkDistribution:
def __init__(self,params):
"""
Pareto-distributed impact function
Must have rho>2
From Liniger thesis p. 37
Seems to be different than standard pareto distribution??
"""
self.setParams(params)
@staticmethod
def getNumParameters():
return 5
@staticmethod
def getParameterBounds():
#This specifies that alpha, is positive, but strictly speaking only one of alpha, beta, gamma have to be greater than zero.
#See Liniger thesis p. 34
return [[1e-5,None],[2.0+1e-5,None],[1e-5,None],[0,None],[0,None]]
def setParams(self, params):
"""
Set parameters with an iterable to support log-likelihood calculation
This will set:
mu=params[0]
rho=params[1]
alpha=params[2]
beta=params[3]
gamma=params[4]
"""
self.mu = params[0]
self.rho = params[1]
self.alpha=params[2]
self.beta=params[3]
self.gamma=params[4]
def getRandomValue(self):
randomVal=random.random()
return self.__inverseCumulativeDistribution(randomVal)
def __inverseCumulativeDistribution(self,x):
"""From wolfram alpha"""
firstMultiplier=(1.0-x)**(1.0/self.rho)-1.0
secondMultiplier=(1.0-x)**(-1.0/self.rho)
retval=-self.mu*firstMultiplier*secondMultiplier
return retval
def getDensityFunction(self,x):
"""Liniger f_u,p(x) (p. 21)"""
numerator=self.rho*self.mu**self.rho
denominator=(x+self.mu)**(self.rho+1)
return numerator/denominator
def getCumulativeDistributionFunction(self,x):
"""Liniger F_u,p(x)"""
return 1.0-(self.mu/(x+self.mu))**self.rho
def getImpactFunction(self,x):
"""Impact function for pareto distribution. Liniger g_k(x)"""
term1Numerator=(self.rho-1.0)*(self.rho-2.0)
term1Denominator=self.alpha*(self.rho-1.0)*(self.rho-2.0)+self.beta*self.mu*(self.rho-2.0)+2.0*self.gamma*self.mu*self.mu
term2=self.alpha+self.beta*x+self.gamma*x*x
return term1Numerator/term1Denominator*term2
class VoidMarkDistribution:
def __init__(self,params):
"""
Void impact function
From Liniger thesis p. 21
"""
self.setParams(params)
@staticmethod
def getNumParameters():
return 0
@staticmethod
def getParameterBounds():
return []
def setParams(self, params):
pass
def getRandomValue(self):
return 1.0
def getDensityFunction(self,x):
return 1.0
def getCumulativeDistributionFunction(self,x):
return 1.0
def getImpactFunction(self,x):
return 1.0
class _NonNormalizedExponentialImpactFunction3Params:
def __init__(self,params):
"""
Impact function for exponential distribution
Liniger p.35
"""
self.setParams(params)
@staticmethod
def getNumParameters():
return 3
@staticmethod
def getParameterBounds():
return [[1e-5,None]]*3
def setParams(self,params):
self.alpha=params[0]
self.beta=params[1]
self.gamma=params[2]
def getImpactFunction(self,lambd,x):
term1Numerator=lambd*lambd
term1Denominator=self.alpha*lambd*lambd+self.beta*lambd+2.0*self.gamma
term2=self.alpha+self.beta*x+self.gamma*x*x
return (term1Numerator/term1Denominator)*term2
class _NonNormalizedExponentialImpactFunction1Params:
def __init__(self, params):
"""
Impact function for exponential distribution
Liniger p.35
"""
self.setParams(params)
@staticmethod
def getNumParameters():
return 1
@staticmethod
def getParameterBounds():
return [[1e-5, None]]
def setParams(self, params):
self.alpha = params[0]
def getImpactFunction(self, lambd, x):
term1Numerator=lambd**self.alpha
term1Denominator=math.gamma(self.alpha+1.0)
term2=x**self.alpha
return (term1Numerator/term1Denominator)*term2
class _GenericExponentialMarkDistribution:
def __init__(self,params,impactFunctionClass):
"""
Exponential-distributed impact function
Must have lambda>0
Pass either _NonNormalizedExponentialImpactFunction3Params or _NonNormalizedExponentialImpactFunction1Param as impactFunctionClass
See Liniger thesis p.35
"""
self.impactFunctionClass=impactFunctionClass
self.setParams(params)
def getNumParameters(self):
return 1+self.impactFunctionClass.getNumParameters()
def getParameterBounds(self):
parameterBounds=[[1e-5,None]]+self.impactFunctionClass.getParameterBounds()
return parameterBounds
def setParams(self,params):
self.lambd=params[0]
self.impactFunctionClass.setParams(params[1:])
def getRandomValue(self):
return random.expovariate(self.lambd)
def getDensityFunction(self,x):
return self.lambd*math.exp(-self.lambd*x)
def getCumulativeDistributionFunction(self,x):
return 1.0-math.exp(-self.lambd*x)
def getImpactFunction(self,x):
return self.impactFunctionClass.getImpactFunction(self.lambd,x)
class ExponentialMarkDistribution1Param:
def __init__(self,params):
self.impactFunction=_NonNormalizedExponentialImpactFunction1Params(params[1:])
self.delegateMarkDistribution=_GenericExponentialMarkDistribution(params,self.impactFunction)
self.setParams(params)
@staticmethod
def getNumParameters():
return _NonNormalizedExponentialImpactFunction1Params.getNumParameters()+1
@staticmethod
def getParameterBounds():
return [[1e-5,None]]+_NonNormalizedExponentialImpactFunction1Params.getParameterBounds()
def setParams(self,params):
self.delegateMarkDistribution.setParams(params)
def getRandomValue(self):
return self.delegateMarkDistribution.getRandomValue()
def getDensityFunction(self,x):
return self.delegateMarkDistribution.getDensityFunction(x)
def getCumulativeDistributionFunction(self,x):
return self.delegateMarkDistribution.getCumulativeDistributionFunction(x)
def getImpactFunction(self,x):
return self.delegateMarkDistribution.getImpactFunction(x)