-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGaussian.py
More file actions
213 lines (200 loc) · 8.73 KB
/
Copy pathGaussian.py
File metadata and controls
213 lines (200 loc) · 8.73 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
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
from scipy.stats import norm
import math
from scipy.stats import truncnorm
class Gaussian:
def __init__(self, mu: float, sigma: float):
self.mu = mu
self.sigma = sigma
self.sigma2 = sigma**2
# Non-centered second moment
self.ncsm = self.mu**2+self.sigma2
def eval(self, x: [float | list[float]]) -> [float | list[float]]:
return norm.pdf(x, self.mu, self.sigma)
def sample(self, size=1):
return norm.rvs(self.mu, self.sigma, size=size)
def trunc(self, a: float, b: float):
mean = self.mu # Mean of the original Gaussian distribution
std_dev = self.sigma # Standard deviation of the original Gaussian distribution
# Create a truncated normal distribution object
trunc_norm = truncnorm((a - mean) / std_dev, (b - mean) / std_dev, loc=mean, scale=std_dev)
return( Gaussian(trunc_norm.mean(),trunc_norm.std()) )
def __mul__(self, other):
if isinstance(other, (int, float)):
if other == math.inf:
return Uniform()
else:
return Gaussian(other*self.mu, abs(other)*self.sigma)
else:
if self.sigma == 0.0 or other.sigma == 0.0:
mu = self.mu/((self.sigma**2/other.sigma**2) + 1) if self.sigma == 0.0 else other.mu/((other.sigma**2/self.sigma**2) + 1)
sigma = 0.0
else:
_tau, _pi = self.tau + other.tau, self.pi + other.pi
mu, sigma = Gaussian.mu_sigma(_tau, _pi)
if sigma == math.inf:
return Uniform()
elif sigma == 0:
return PointMass(mu)
return Gaussian(mu, sigma)
# OLD
if (other == 0):
return PointMass(0)
if (other == 1):
return self
elif (isinstance(other, (int, float))):
# source: https://math.stackexchange.com/questions/275648/multiplication-of-a-random-variable-with-constant
return Gaussian(self.mu*other, self.sigma*other)
elif (isinstance(other, PointMass)):
return PointMass(self.mu*other.mu)
elif (isinstance(other, Uniform)):
return self
elif (isinstance(other, Gaussian)):
return Gaussian.mul(self, other)
else:
raise NotImplementedError(f"Multiplying Gaussian by element of type {type(other)} is unsupported. Value of other: {other}")
def __rmul__(self, other):
return self.__mul__(other)
def __matmul__(self, other):
return self.__mul__(other)
def __rmatmul__(self, other):
return self.__rmul__(other)
def __truediv__(self, other):
if (other == 0):
raise ZeroDivisionError(f"Tried to divide a Gaussian by zero")
if (other == 1):
return self
elif (isinstance(other, (int, float))):
return Gaussian(self.mu/other, self.sigma/other)
elif (isinstance(other, PointMass)):
assert self.sigma2 != 0, f"This might be a pointmass: {self}"
raise ZeroDivisionError()
#raise NotImplementedError(f"Dividing Gaussian by element of type {type(other)} is unsupported. Value of other: {other}")
# De donde salio esto: ???
#return PointMass(other.mu/((other.sigma**2/self.sigma**2) + 1))
elif (isinstance(other, Uniform)):
return self
elif (isinstance(other, Gaussian)):
return Gaussian.div(self, other)
else:
raise NotImplementedError(f"Dividing Gaussian by element of type {type(other)} is unsupported. Value of other: {other}")
def __add__(self, other):
if (other == 0):
return self
elif (isinstance(other, Gaussian)):
return Gaussian(self.mu+other.mu, math.sqrt(self.sigma2+other.sigma2))
else:
raise NotImplementedError(f"Adding Gaussian with element of type {type(other)} is unsupported. Value of other: {other}")
def __radd__(self, other):
if (other == 0):
return self
elif (isinstance(other, Gaussian)):
return Gaussian(self.mu+other.mu, math.sqrt(self.sigma2+other.sigma2))
else:
raise NotImplementedError(f"Adding Gaussian with element of type {type(other)} is unsupported. Value of other: {other}")
def __sub__(self, other):
if (other == 0):
return self
elif (isinstance(other, Gaussian)):
return Gaussian(self.mu-other.mu, math.sqrt(self.sigma2+other.sigma2))
else:
raise NotImplementedError(f"Substracting element of type {type(other)} from Gaussian is unsupported. Value of other: {other}")
def __repr__(self):
return f"N(mu={self.mu:.4f}, var={self.sigma2:.4f})"
def __format__(self, format_spec):
return self.__repr__().__format__(format_spec)
@staticmethod
def mul(norm1, norm2):
sigma2Star = (1/norm1.sigma2 + 1/norm2.sigma2)**(-1) #c*N(mu, sigma) = N(c*mu, c*sigma) #TODO: agregar al pdf
muStar = norm1.mu/norm1.sigma2 + norm2.mu/norm2.sigma2
muStar *= sigma2Star
sigma = math.sqrt(sigma2Star)
if sigma==math.inf:
return Uniform()
elif sigma==0:
return PointMass(muStar)
else:
return Gaussian(muStar, sigma)
#c = Gaussian(norm2.mu, math.sqrt(norm1.sigma2+norm2.sigma2)).eval(norm1.mu) #result should be multiplied by c, but is proportional to not multiplying by it
return Gaussian(muStar, math.sqrt(sigma2Star))
@staticmethod
def div(norm1, norm2):
_tau = norm1.tau - norm2.tau; _pi = norm1.pi - norm2.pi
mu, sigma = Gaussian.mu_sigma(_tau, _pi)
if sigma==math.inf:
return Uniform()
elif sigma==0:
return PointMass(mu)
else:
return Gaussian(mu, sigma)
# OLD
sigma2Star = (1/norm1.sigma2 - 1/norm2.sigma2)**(-1) #c*N(mu, sigma) = N(c*mu, c*sigma) #TODO: agregar al pdf
muStar = norm1.mu/norm1.sigma2 - norm2.mu/norm2.sigma2
muStar *= sigma2Star
#c = Gaussian(norm2.mu, math.sqrt(norm1.sigma2+norm2.sigma2)).eval(norm1.mu) #result should be multiplied by c, but is proportional to not multiplying by it
return Gaussian(muStar, math.sqrt(sigma2Star))
@staticmethod
def mu_sigma(tau_,pi_):
if pi_ > 0.0:
sigma = math.sqrt(1/pi_)
mu = tau_ / pi_
elif pi_ + 1e-5 < 0.0:
raise ValueError(" sigma should be greater than 0 ")
else:
sigma = math.inf
mu = 0.0
return mu, sigma
@property
def tau(self):
if self.sigma > 0.0:
return self.mu * (self.sigma**-2)
else:
return math.inf
@property
def pi(self):
if self.sigma > 0.0:
return self.sigma**-2
else:
return math.inf
class Uniform(Gaussian):
def __init__(self):
super(Uniform, self).__init__(0, math.inf)
def __truediv__(self, other):
raise NotImplementedError("Can't divide a Uniform (inf uncertainty) by anything bc denominator must have bigger uncertainty") #TODO: ahcer esto bien
def __repr__(self):
return f"Uniform()"
class PointMass(Gaussian):
def __init__(self, mu):
super(PointMass, self).__init__(mu, 0)
def __mul__(self, other):
if (other == 0):
return PointMass(0)
elif (other == 1):
return self
elif (isinstance(other, (int, float))):
# source: https://math.stackexchange.com/questions/275648/multiplication-of-a-random-variable-with-constant
return PointMass(self.mu*other)
elif (isinstance(other, PointMass)):
return PointMass(self.mu*other.mu)
elif (isinstance(other, Uniform)):
return self
elif (isinstance(other, Gaussian)):
return PointMass(self.mu/((self.sigma**2/other.sigma**2) + 1))
else:
raise NotImplementedError(f"Adding PointMass with element of type {type(other)} is unsupported. Value of other: {other}")
def __truediv__(self, other):
if (other == 0):
raise ZeroDivisionError(f"Tried to divide a PointMass by zero")
if (other == 1):
return self
elif (isinstance(other, (int, float))):
return PointMass(self.mu/other)
if (isinstance(other, PointMass)):
if self.mu != other.mu:
raise ZeroDivisionError()
return Uniform()
elif (isinstance(other, Gaussian)):
return self
else:
raise NotImplementedError(f"Dividing PointMass by element of type {type(other)} is unsupported. Value of other: {other}")
def __repr__(self):
return f"PointMass({self.mu:.4f})"