-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributions.py
More file actions
48 lines (42 loc) · 1.86 KB
/
distributions.py
File metadata and controls
48 lines (42 loc) · 1.86 KB
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
"""This module defines the class for different car distributions."""
import numpy as np
import math
import random
class Probability(object):
"""This class contains functions for different probability distributions."""
GAUSSIAN = 0
POISSON = 1
UNIFORM = 2
STANDARD = 3
_POISSON_MEAN = 5
_GAUSSIAN_MEAN = 10
_GUASSIAN_VAR = 1
_UNIFORM_MEAN = 0.1
def __init__(self):
"""Initialise the class"""
self.prob = 0
def get_value(self, distribution):
"""Given a distribution, return whether the event occurred or not"""
# Get the chance of the event occurring, generate a random number between 1 and 100, and
# return 1 if less than, 0 if greater than
return self.get_probability(distribution)
def get_probability(self, distribution):
"""Returns the percentage of an event occurring under the given distribution"""
# A random number is generated and used as input to the distribution
# the chance is returned as a percentage
if distribution == self.GAUSSIAN:
variable = np.random.normal(self._GAUSSIAN_MEAN, self._GUASSIAN_VAR, None)
prob = (1/(2*np.pi*self._GUASSIAN_VAR**2))*np.exp(-1*((variable - self._GAUSSIAN_MEAN)**2)/(2*self._GUASSIAN_VAR**2))
if random.randint(0, 100) <= prob*100:
return True
elif distribution == self.POISSON:
variable = np.random.poisson(self._POISSON_MEAN, None)
prob = np.exp(-1*self._POISSON_MEAN)*(np.power([self._POISSON_MEAN], variable)[0])/np.math.factorial(variable)
if random.randint(0, 100) <= prob*100:
return True
elif distribution == self.UNIFORM:
variable = 100*self._UNIFORM_MEAN
prob = variable
if random.randint(0,100) <= prob:
return True
return False