Spaces:
Running
Running
File size: 9,215 Bytes
e60e568 |
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
'''
This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).
PM4Py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PM4Py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PM4Py. If not, see <https://www.gnu.org/licenses/>.
'''
import numpy as np
from pm4py.objects.random_variables.constant0.random_variable import Constant0
from pm4py.objects.random_variables.deterministic.random_variable import Deterministic
from pm4py.objects.random_variables.exponential.random_variable import Exponential
from pm4py.objects.random_variables.normal.random_variable import Normal
from pm4py.objects.random_variables.uniform.random_variable import Uniform
from pm4py.objects.random_variables.lognormal.random_variable import LogNormal
from pm4py.objects.random_variables.gamma.random_variable import Gamma
class RandomVariable(object):
def __init__(self):
self.random_variable = None
def read_from_string(self, distribution_type, distribution_parameters):
"""
Read the random variable from string
Parameters
-----------
distribution_type
Distribution type
distribution_parameters
Distribution parameters splitted by ;
"""
if distribution_type == "NORMAL":
self.random_variable = Normal()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "UNIFORM":
self.random_variable = Uniform()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "EXPONENTIAL":
self.random_variable = Exponential()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "LOGNORMAL":
self.random_variable = LogNormal()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "GAMMA":
self.random_variable = Gamma()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "DETERMINISTIC":
self.random_variable = Deterministic()
self.random_variable.read_from_string(distribution_parameters)
elif distribution_type == "IMMEDIATE":
self.random_variable = Constant0()
def get_distribution_type(self):
"""
Get current distribution type
Returns
-----------
distribution_type
String representing the distribution type
"""
if self.random_variable is not None:
return self.random_variable.get_distribution_type()
def get_transition_type(self):
"""
Get the type of transition associated to the current distribution
Returns
-----------
transition_type
String representing the type of the transition
"""
if self.random_variable is not None:
return self.random_variable.get_transition_type()
def get_distribution_parameters(self):
"""
Get a string representing distribution parameters
Returns
-----------
distribution_parameters
String representing distribution parameters
"""
if self.random_variable is not None:
return self.random_variable.get_distribution_parameters()
def calculate_loglikelihood(self, values):
"""
Calculate log likelihood
Parameters
------------
values
Empirical values to work on
Returns
------------
likelihood
Log likelihood that the values follows the distribution
"""
if self.random_variable is not None:
return self.random_variable.calculate_loglikelihood(values)
def calculate_parameters(self, values, parameters=None, force_distribution=None):
"""
Calculate parameters of the current distribution
Parameters
-----------
values
Empirical values to work on
parameters
Possible parameters of the algorithm
force_distribution
If provided, distribution to force usage (e.g. EXPONENTIAL)
"""
if parameters is None:
parameters = {}
debug_mode = parameters["debug"] if "debug" in parameters else False
if self.random_variable is not None:
self.random_variable.calculate_parameters(values)
else:
norm = Normal()
unif = Uniform()
expon = Exponential()
constant = Constant0()
lognormal = LogNormal()
gamma = Gamma()
if not force_distribution or not force_distribution == "EXPONENTIAL":
likelihoods = list()
likelihoods.append([constant, constant.calculate_loglikelihood(values)])
if force_distribution == "NORMAL" or force_distribution is None:
norm.calculate_parameters(values)
likelihoods.append([norm, norm.calculate_loglikelihood(values)])
if force_distribution == "UNIFORM" or force_distribution is None:
unif.calculate_parameters(values)
likelihoods.append([unif, unif.calculate_loglikelihood(values)])
if force_distribution == "EXPONENTIAL" or force_distribution is None:
expon.calculate_parameters(values)
likelihoods.append([expon, expon.calculate_loglikelihood(values)])
likelihoods = [x for x in likelihoods if str(x[1]) != 'nan']
likelihoods = sorted(likelihoods, key=lambda x: x[1], reverse=True)
if debug_mode:
print("likelihoods = ", likelihoods)
self.random_variable = likelihoods[0][0]
else:
avg_values = np.average(values)
if values and avg_values > 0.00000:
expon.scale = avg_values
self.random_variable = expon
else:
self.random_variable = constant
def get_value(self):
"""
Get a random value following the distribution
Returns
-----------
value
Value obtained following the distribution
"""
if self.random_variable is not None:
return self.random_variable.get_value()
def get_values(self, no_values=400):
"""
Get some random values following the distribution
Parameters
-----------
no_values
Number of values to return
Returns
----------
values
Values extracted according to the probability distribution
"""
if self.random_variable is not None:
return self.random_variable.get_values(no_values=no_values)
def get_weight(self):
"""
Getter of weight
Returns
----------
weight
Weight of the transition
"""
if self.random_variable is not None:
return self.random_variable.get_weight()
def set_weight(self, weight):
"""
Setter of the weight
Parameters
-----------
weight
Weight of the transition
"""
if self.random_variable is not None:
self.random_variable.set_weight(weight)
def get_priority(self):
"""
Getter of the priority
Returns
-----------
priority
Priority of the transition
"""
if self.random_variable is not None:
return self.random_variable.get_priority()
def set_priority(self, priority):
"""
Setter of the priority variable
Parameters
------------
priority
Priority of the transition
"""
if self.random_variable is not None:
self.random_variable.set_priority(priority)
def __str__(self):
"""
Returns a representation of the current object
Returns
----------
repr
Representation of the current object
"""
if self.random_variable is not None:
return str(self.random_variable)
else:
return "UNINITIALIZED"
def __repr__(self):
"""
Returns a representation of the current object
Returns
----------
repr
Representation of the current object
"""
if self.random_variable is not None:
return repr(self.random_variable)
else:
return "UNINITIALIZED"
|