-
Notifications
You must be signed in to change notification settings - Fork 18
/
Generaldistribution.py
53 lines (39 loc) · 1.57 KB
/
Generaldistribution.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
from pathlib import Path
class Distribution:
""" Generic distribution class for calculating and
visualizing a probability distribution.
Attributes:
mean (float) representing the mean value of the distribution
stdev (float) representing the standard deviation of the distribution
data_list (list of floats) extracted from the data file
"""
def __init__(self, mu=0, sigma=1):
self.mean = mu
self.stdev = sigma
self.data = []
def read_data_file(self, file_name):
"""Function to read in data from a txt file.
The txt file should have one number (float) per line.
The numbers are stored in the data attribute.
Args:
file_name (string): name of a file to read from
Returns:
None
"""
if file_name == 'demo_gaussian_data':
dirname = Path(__file__).parent.absolute()
file_name = Path(dirname, 'numbers.txt')
elif file_name == 'demo_binomial_data':
dirname = Path(__file__).parent.absolute()
file_name = Path(dirname, 'numbers_binomial.txt')
elif file_name == 'demo_exponential_data':
dirname = Path(__file__).parent.absolute()
file_name = Path(dirname, 'numbers_exponential.txt')
with open(file_name) as file:
data_list = []
line = file.readline()
while line:
data_list.append(int(line))
line = file.readline()
file.close()
self.data = data_list