-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussiandistribution_solution.py
95 lines (65 loc) · 2.34 KB
/
gaussiandistribution_solution.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
import math
from generaldistribution_solution import Distribution
class Gaussian(Distribution):
"""
Gaussian distribution class for calculating and visualizing a Gaussian 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) a list of floats extracted from the data file
"""
def __init__(self, mu=0, sigma=1):
Distribution.__init__(self, mu, sigma)
def calculate_mean(self):
"""
Function to calculate the mean of the data set.
Args:
None
Returns:
float: Mean of the data set.
"""
avg = 1.0 * sum(self.data) / len(self.data)
self.mean = avg
return self.mean
def calculate_stdev(self, sample=True):
"""
Function to calculate the standard deviation of the data set.
Args:
sample: bool. Whether the data represents a sample or population.
Returns:
float: Standard deviation of the data set.
"""
if sample:
n = len(self.data) - 1
else:
n = len(self.data)
mean = self.calculate_mean()
sigma = 0
for d in self.data:
sigma += (d - mean) ** 2
sigma = math.sqrt(sigma / n)
self.stdev = sigma
return self.stdev
def pdf(self, x):
"""
Probability density function calculator for the gaussian distribution.
Args:
x: float. Point for calculating the probability density function.
Returns:
float: probability density function output.
"""
return (1.0 / (self.stdev * math.sqrt(2 * math.pi))) * math.exp(
-0.5 * ((x - self.mean) / self.stdev) ** 2
)
def __add__(self, other):
"""
Function to add together two Gaussian distributions.
Args:
other: Gaussian. Gaussian instance.
Returns:
Gaussian: Gaussian distribution.
"""
result = Gaussian()
result.mean = self.mean + other.mean
result.stdev = math.sqrt(self.stdev ** 2 + other.stdev ** 2)
return result