dsnd-probability-daivyd 0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dsnd_probability_daivyd-0.2/PKG-INFO +4 -0
- dsnd_probability_daivyd-0.2/README.md +7 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd/Binomialdistribution.py +97 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd/Gaussiandistribution.py +173 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd/Generaldistribution.py +37 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd/__init__.py +4 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd.egg-info/PKG-INFO +4 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd.egg-info/SOURCES.txt +12 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd.egg-info/dependency_links.txt +1 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd.egg-info/not-zip-safe +1 -0
- dsnd_probability_daivyd-0.2/dsnd_probability_daivyd.egg-info/top_level.txt +1 -0
- dsnd_probability_daivyd-0.2/setup.cfg +7 -0
- dsnd_probability_daivyd-0.2/setup.py +9 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
from .Generaldistribution import Distribution
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Binomial(Distribution):
|
|
7
|
+
"""Binomial distribution class for calculating and
|
|
8
|
+
visualizing a Binomial distribution.
|
|
9
|
+
|
|
10
|
+
Attributes:
|
|
11
|
+
mean (float) representing the mean value of the distribution
|
|
12
|
+
stdev (float) representing the standard deviation of the distribution
|
|
13
|
+
data_list (list of floats) a list of floats to be extracted from the data file
|
|
14
|
+
p (float) representing the probability of an event occurring
|
|
15
|
+
n (int) the total number of trials
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, prob=0.5, size=20):
|
|
20
|
+
self.p = prob
|
|
21
|
+
self.n = size
|
|
22
|
+
|
|
23
|
+
Distribution.__init__(self, self.calculate_mean(), self.calculate_stdev())
|
|
24
|
+
|
|
25
|
+
def calculate_mean(self):
|
|
26
|
+
"""Function to calculate the mean from p and n"""
|
|
27
|
+
self.mean = self.p * self.n
|
|
28
|
+
return self.mean
|
|
29
|
+
|
|
30
|
+
def calculate_stdev(self):
|
|
31
|
+
"""Function to calculate the standard deviation from p and n."""
|
|
32
|
+
self.stdev = math.sqrt(self.n * self.p * (1 - self.p))
|
|
33
|
+
return self.stdev
|
|
34
|
+
|
|
35
|
+
def replace_stats_with_data(self):
|
|
36
|
+
"""Function to calculate p and n from the data set"""
|
|
37
|
+
self.n = len(self.data)
|
|
38
|
+
self.p = sum(self.data) / self.n
|
|
39
|
+
self.mean = self.calculate_mean()
|
|
40
|
+
self.stdev = self.calculate_stdev()
|
|
41
|
+
|
|
42
|
+
return self.p, self.n
|
|
43
|
+
|
|
44
|
+
def plot_bar(self):
|
|
45
|
+
"""Function to output a histogram of the instance variable data."""
|
|
46
|
+
plt.bar(x=['0', '1'], height=[
|
|
47
|
+
self.data.count(0), self.data.count(1)
|
|
48
|
+
])
|
|
49
|
+
plt.title('Bar Chart of Data')
|
|
50
|
+
plt.xlabel('outcome')
|
|
51
|
+
plt.ylabel('count')
|
|
52
|
+
|
|
53
|
+
def pdf(self, k):
|
|
54
|
+
"""Probability density function calculator for the binomial distribution."""
|
|
55
|
+
a = math.factorial(self.n) / (math.factorial(k) * (math.factorial(self.n - k)))
|
|
56
|
+
b = (self.p ** k) * (1 - self.p) ** (self.n - k)
|
|
57
|
+
|
|
58
|
+
return a * b
|
|
59
|
+
|
|
60
|
+
def plot_bar_pdf(self):
|
|
61
|
+
"""Function to plot the pdf of the binomial distribution"""
|
|
62
|
+
x = []
|
|
63
|
+
y = []
|
|
64
|
+
|
|
65
|
+
for i in range(self.n + 1):
|
|
66
|
+
x.append(i)
|
|
67
|
+
y.append(self.pdf(i))
|
|
68
|
+
|
|
69
|
+
plt.bar(x, y)
|
|
70
|
+
plt.title('Distribution of Outcomes')
|
|
71
|
+
plt.ylabel('Probability')
|
|
72
|
+
plt.xlabel('Outcome')
|
|
73
|
+
|
|
74
|
+
plt.show()
|
|
75
|
+
|
|
76
|
+
return x, y
|
|
77
|
+
|
|
78
|
+
def __add__(self, other):
|
|
79
|
+
"""Function to add together two Binomial distributions with equal p"""
|
|
80
|
+
try:
|
|
81
|
+
assert self.p == other.p, "p values are not equal"
|
|
82
|
+
except AssertionError as error:
|
|
83
|
+
raise
|
|
84
|
+
|
|
85
|
+
result = Binomial()
|
|
86
|
+
result.n = self.n + other.n
|
|
87
|
+
result.p = self.p
|
|
88
|
+
result.mean = result.calculate_mean()
|
|
89
|
+
result.stdev = result.calculate_stdev()
|
|
90
|
+
|
|
91
|
+
return result
|
|
92
|
+
|
|
93
|
+
def __repr__(self):
|
|
94
|
+
"""Function to output the characteristics of the Binomial instance"""
|
|
95
|
+
return "mean {}, standard deviation {}, p {}, n {}".format(
|
|
96
|
+
self.mean, self.stdev, self.p, self.n
|
|
97
|
+
)
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
from .Generaldistribution import Distribution
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Gaussian(Distribution):
|
|
7
|
+
"""Gaussian distribution class for calculating and
|
|
8
|
+
visualizing a Gaussian distribution.
|
|
9
|
+
|
|
10
|
+
Attributes:
|
|
11
|
+
mean (float) representing the mean value of the distribution
|
|
12
|
+
stdev (float) representing the standard deviation of the distribution
|
|
13
|
+
data_list (list of floats) a list of floats extracted from the data file
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, mu=0, sigma=1):
|
|
18
|
+
|
|
19
|
+
Distribution.__init__(self, mu, sigma)
|
|
20
|
+
|
|
21
|
+
def calculate_mean(self):
|
|
22
|
+
"""Function to calculate the mean of the data set.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
None
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
float: mean of the data set
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
avg = 1.0 * sum(self.data) / len(self.data)
|
|
33
|
+
|
|
34
|
+
self.mean = avg
|
|
35
|
+
|
|
36
|
+
return self.mean
|
|
37
|
+
|
|
38
|
+
def calculate_stdev(self, sample=True):
|
|
39
|
+
"""Function to calculate the standard deviation of the data set.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
sample (bool): whether the data represents a sample or population
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
float: standard deviation of the data set
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
if sample:
|
|
50
|
+
n = len(self.data) - 1
|
|
51
|
+
else:
|
|
52
|
+
n = len(self.data)
|
|
53
|
+
|
|
54
|
+
mean = self.calculate_mean()
|
|
55
|
+
|
|
56
|
+
sigma = 0
|
|
57
|
+
|
|
58
|
+
for d in self.data:
|
|
59
|
+
sigma += (d - mean) ** 2
|
|
60
|
+
|
|
61
|
+
sigma = math.sqrt(sigma / n)
|
|
62
|
+
|
|
63
|
+
self.stdev = sigma
|
|
64
|
+
|
|
65
|
+
return self.stdev
|
|
66
|
+
|
|
67
|
+
def plot_histogram(self):
|
|
68
|
+
"""Function to output a histogram of the instance variable data using
|
|
69
|
+
matplotlib pyplot library.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
None
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
None
|
|
76
|
+
|
|
77
|
+
"""
|
|
78
|
+
plt.hist(self.data)
|
|
79
|
+
plt.title("Histogram of Data")
|
|
80
|
+
plt.xlabel("data")
|
|
81
|
+
plt.ylabel("count")
|
|
82
|
+
|
|
83
|
+
def pdf(self, x):
|
|
84
|
+
"""Probability density function calculator for the gaussian distribution.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
x (float): point for calculating the probability density function
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
float: probability density function output
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
return (1.0 / (self.stdev * math.sqrt(2 * math.pi))) * math.exp(
|
|
95
|
+
-0.5 * ((x - self.mean) / self.stdev) ** 2
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
def plot_histogram_pdf(self, n_spaces=50):
|
|
99
|
+
"""Function to plot the normalized histogram of the data and a plot of the
|
|
100
|
+
probability density function along the same range
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
n_spaces (int): number of data points
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
list: x values for the pdf plot
|
|
107
|
+
list: y values for the pdf plot
|
|
108
|
+
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
mu = self.mean
|
|
112
|
+
sigma = self.stdev
|
|
113
|
+
|
|
114
|
+
min_range = min(self.data)
|
|
115
|
+
max_range = max(self.data)
|
|
116
|
+
|
|
117
|
+
# calculates the interval between x values
|
|
118
|
+
interval = 1.0 * (max_range - min_range) / n_spaces
|
|
119
|
+
|
|
120
|
+
x = []
|
|
121
|
+
y = []
|
|
122
|
+
|
|
123
|
+
# calculate the x values to visualize
|
|
124
|
+
for i in range(n_spaces):
|
|
125
|
+
tmp = min_range + interval * i
|
|
126
|
+
x.append(tmp)
|
|
127
|
+
y.append(self.pdf(tmp))
|
|
128
|
+
|
|
129
|
+
# make the plots
|
|
130
|
+
fig, axes = plt.subplots(2, sharex=True)
|
|
131
|
+
fig.subplots_adjust(hspace=0.5)
|
|
132
|
+
axes[0].hist(self.data, density=True)
|
|
133
|
+
axes[0].set_title("Normed Histogram of Data")
|
|
134
|
+
axes[0].set_ylabel("Density")
|
|
135
|
+
|
|
136
|
+
axes[1].plot(x, y)
|
|
137
|
+
axes[1].set_title(
|
|
138
|
+
"Normal Distribution for \n Sample Mean and Sample Standard Deviation"
|
|
139
|
+
)
|
|
140
|
+
axes[0].set_ylabel("Density")
|
|
141
|
+
plt.show()
|
|
142
|
+
|
|
143
|
+
return x, y
|
|
144
|
+
|
|
145
|
+
def __add__(self, other):
|
|
146
|
+
"""Function to add together two Gaussian distributions
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
other (Gaussian): Gaussian instance
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
Gaussian: Gaussian distribution
|
|
153
|
+
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
result = Gaussian()
|
|
157
|
+
result.mean = self.mean + other.mean
|
|
158
|
+
result.stdev = math.sqrt(self.stdev**2 + other.stdev**2)
|
|
159
|
+
|
|
160
|
+
return result
|
|
161
|
+
|
|
162
|
+
def __repr__(self):
|
|
163
|
+
"""Function to output the characteristics of the Gaussian instance
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
None
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
string: characteristics of the Gaussian
|
|
170
|
+
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
return "mean {}, standard deviation {}".format(self.mean, self.stdev)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
class Distribution:
|
|
2
|
+
|
|
3
|
+
def __init__(self, mu=0, sigma=1):
|
|
4
|
+
"""Generic distribution class for calculating and
|
|
5
|
+
visualizing a probability distribution.
|
|
6
|
+
|
|
7
|
+
Attributes:
|
|
8
|
+
mean (float) representing the mean value of the distribution
|
|
9
|
+
stdev (float) representing the standard deviation of the distribution
|
|
10
|
+
data_list (list of floats) a list of floats extracted from the data file
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
self.mean = mu
|
|
14
|
+
self.stdev = sigma
|
|
15
|
+
self.data = []
|
|
16
|
+
|
|
17
|
+
def read_data_file(self, file_name):
|
|
18
|
+
"""Function to read in data from a txt file. The txt file should have
|
|
19
|
+
one number (float) per line. The numbers are stored in the data attribute.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
file_name (string): name of a file to read from
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
None
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
with open(file_name) as file:
|
|
30
|
+
data_list = []
|
|
31
|
+
line = file.readline()
|
|
32
|
+
while line:
|
|
33
|
+
data_list.append(int(line))
|
|
34
|
+
line = file.readline()
|
|
35
|
+
file.close()
|
|
36
|
+
|
|
37
|
+
self.data = data_list
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.cfg
|
|
3
|
+
setup.py
|
|
4
|
+
dsnd_probability_daivyd/Binomialdistribution.py
|
|
5
|
+
dsnd_probability_daivyd/Gaussiandistribution.py
|
|
6
|
+
dsnd_probability_daivyd/Generaldistribution.py
|
|
7
|
+
dsnd_probability_daivyd/__init__.py
|
|
8
|
+
dsnd_probability_daivyd.egg-info/PKG-INFO
|
|
9
|
+
dsnd_probability_daivyd.egg-info/SOURCES.txt
|
|
10
|
+
dsnd_probability_daivyd.egg-info/dependency_links.txt
|
|
11
|
+
dsnd_probability_daivyd.egg-info/not-zip-safe
|
|
12
|
+
dsnd_probability_daivyd.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dsnd_probability_daivyd
|