-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpfabada.py
254 lines (196 loc) · 11 KB
/
pfabada.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""
FABADA is a non-parametric noise reduction technique based on Bayesian
inference that iteratively evaluates possibles moothed models of
the data introduced, obtaining an estimation of the underlying
signal that is statistically compatible with the noisy measurements.
based on P.M. Sanchez-Alarcon, Y. Ascasibar, 2022
"Fully Adaptive Bayesian Algorithm for Data Analysis. FABADA"
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
"""
"""PFABADA(parametric Fabada) is a somewhat optimized noise reduction technique based on FABADA
that uses known properties of signals and skimage's sigma estimator to converge on a best
fit approximation in the presence of unknown variance and normalization parameters,
generalizing the fabada approach for most data streams, and using numba for acceleration.
Copywrite 2022 Joshuah Rainstar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
#version idk 11/14/2022 this fixes any divide by zero issues
"""
The approach the authors use for the stopping criterion is based on chi-square statistics, where they calculate a chi-square statistic (chi2_data) and the associated PDF. This approach may be perfectly fine in theory, but it's clear that it's causing numerical stability issues in practice.
Given that we're already in a Bayesian setting, one option might be to replace this with a more Bayesian-style criterion. For instance, we could consider stopping the iterations when the posterior distribution has stabilized, as measured by some appropriate distance metric (Kullback-Leibler divergence might be a reasonable choice here).
This kind of approach would go something like this:
Calculate the posterior distribution after each iteration.
Measure the Kullback-Leibler (KL) divergence between the posterior distributions from successive iterations.
Stop iterating when the KL divergence drops below some threshold, indicating that the posterior distribution has effectively stabilized.
The KL divergence between two Gaussian distributions with means µ1, µ2 and standard deviations σ1, σ2 is given by:
python
Copy code
def kl_divergence(mu1, mu2, sigma1, sigma2):
return np.log(sigma2/sigma1) + (sigma1**2 + (mu1 - mu2)**2)/(2*sigma2**2) - 0.5
We could then check the KL divergence after each iteration:
kl_divergence = kl_divergence(mu_current, mu_previous, sigma_current, sigma_previous)
if kl_divergence < threshold:
break # Stop iterating
Implementing this in your code might look something like this:
python
Copy code
while 1:
# ... existing code ...
# after calculating new posterior_mean and posterior_variance,
# calculate the KL divergence from the previous iteration
if iteration > 0: # don't calculate KL divergence on the first iteration
kl_divergence = kl_divergence(mu_previous, posterior_mean, sigma_previous, posterior_variance)
if kl_divergence < threshold: # threshold is a hyperparameter you'll need to set
break
# store current posterior_mean and posterior_variance for next iteration
mu_previous = posterior_mean.copy()
sigma_previous = posterior_variance.copy()
# ... remaining code ...
Yes, you could initialize mu_previous and sigma_previous to arrays of ones,
or you could initialize them as copies of your initial posterior_mean and posterior_variance, prior to entering the loop. Either approach could work.
"""
import numpy
import numba
import scipy
from pywt import dwtn
@numba.jit(numba.float64[:](numba.float64[:]),cache=True,nogil=True)
def numba_fabada(data: numpy.ndarray, sigma: numpy.float64) -> (numpy.ndarray):
x = numpy.zeros_like(data,dtype=numpy.float64)
x[:] = data.copy()
x[numpy.where(numpy.isnan(data))] = 0
iterations: int = 1
N = x.size
max_iterations = 1000
bayesian_weight = numpy.zeros_like(x,dtype=numpy.float64)
bayesian_model = numpy.zeros_like(x,dtype=numpy.float64)
model_weight = numpy.zeros_like(x,dtype=numpy.float64)
# pre-declaring all arrays allows their memory to be allocated in advance
posterior_mean = numpy.zeros_like(x,dtype=numpy.float64)
posterior_mean[:] = x.copy()
initial_evidence = numpy.zeros_like(x,dtype=numpy.float64)
evidence = numpy.zeros_like(x,dtype=numpy.float64)
prior_mean = numpy.zeros_like(x,dtype=numpy.float64)
prior_variance = numpy.zeros_like(x,dtype=numpy.float64)
posterior_variance = numpy.zeros_like(x,dtype=numpy.float64)
chi2_data_previous = 0
chi2_data_derivative_previous = 0
tolerance = 1e-15
chi2_data_min = 0
data_variance = numpy.zeros_like(x,dtype=numpy.float64)
data_variance.fill(sigma**2)
data_variance[numpy.where(numpy.isnan(data))] = 1e-15
data_variance[data_variance==0] = 1e-15
posterior_variance[:] = data_variance.copy()
prior_variance[:] = data_variance.copy()
prior_mean[:] = x.copy()
# fabada figure 14
#formula 3, but for initial assumptions
upper = numpy.square(numpy.sqrt(data_variance)*-1)
lower = 2 * data_variance
first = (-upper / lower)
second = numpy.sqrt(2 * numpy.pi) * data_variance
evidence[:] = numpy.exp(first) / second
initial_evidence[:] = evidence.copy()
evidence_previous = numpy.mean(evidence)
while 1:
# GENERATES PRIORS
for i in numba.prange(N - 1):
prior_mean[i] = (posterior_mean[i - 1] + posterior_mean[i] + posterior_mean[i + 1]) / 3
prior_mean[0] = (posterior_mean[0] + (posterior_mean[1] + posterior_mean[2]) / 2) / 3
prior_mean[-1] = (posterior_mean[-1] + (posterior_mean[-2] + posterior_mean[-3]) / 2) / 3
prior_variance = posterior_variance.copy() #if this is an array, you must use .copy() or it will
#cause any changes made to posterior_variance to also automatically be applied to prior
#variance, making these variables redundant.
# APPLY BAYES' THEOREM
# fabada figure 8?
for i in numba.prange(N):
if prior_variance[i] > 0:
# posterior_variance[i] = 1 / (1 / data_variance[i] + 1 / prior_variance[i])
posterior_variance[i] = (data_variance[i] * prior_variance[i])/(data_variance[i] + prior_variance[i])
else:
posterior_variance[i] = 0
#saves on instructions- replaces three divisions, 1 add with one mult, 1 div, 1 add
# fabada figure 7
for i in numba.prange(N):
if prior_variance[i] > 0 and posterior_variance[i] > 0:
posterior_mean[i] = (
((prior_mean[i] / prior_variance[i]) + (x[i] / data_variance[i])) * posterior_variance[i])
else:
posterior_mean[i] = prior_mean[i] #the variance cannot be reduced further
upper = numpy.square(prior_mean - x)
lower = 2 * (prior_variance + data_variance)
first =((-upper/lower))
second = numpy.sqrt(2*numpy.pi) * prior_variance + data_variance
evidence = numpy.exp(first) / second
# fabada figure 6: probability distribution calculation
evidence_derivative = numpy.mean(evidence) - evidence_previous
evidence_previous = numpy.mean(evidence)
# EVALUATE CHI2
chi2_data = numpy.sum((x - posterior_mean) ** 2 / data_variance) / N
chi2_data_derivative = chi2_data - chi2_data_previous
chi2_data_previous = chi2_data # update for next iteration
# Calculate second derivative (rate of change of the first derivative)
chi2_data_snd_derivative = chi2_data_derivative - chi2_data_derivative_previous
chi2_data_derivative_previous = chi2_data_derivative # update for next iteration
if iterations == 1:
chi2_data_min = chi2_data
# COMBINE MODELS FOR THE ESTIMATION
for i in numba.prange(N):
model_weight[i] = evidence[i] * chi2_data
for i in numba.prange(N):
bayesian_weight[i] = bayesian_weight[i] + model_weight[i]
bayesian_model[i] = bayesian_model[i] + (model_weight[i] * posterior_mean[i])
if ((chi2_data > 1) and (evidence_derivative < 0) and (chi2_data_snd_derivative< tolerance)) \
or (iterations > max_iterations): # don't overfit the data
break
iterations = iterations + 1
# COMBINE ITERATION ZERO
for i in numba.prange(N):
model_weight[i] = initial_evidence[i] * chi2_data_min
for i in numba.prange(N):
bayesian_weight[i] = bayesian_weight[i]+ model_weight[i]
bayesian_model[i] = bayesian_model[i] + (model_weight[i] * x[i])
for i in numba.prange(N):
if bayesian_weight[i] > 0:
x[i] = bayesian_model[i] / bayesian_weight[i]
else:
x[i] = x[i]
return x
#here is the logic needed for a 2d nearest neighbors smoothing algo.
#this, along with replacing loop-specific logic in 1d with 2d, allows fabada to be generalized to the 2d domain.
normal = posterior_mean.copy()
transposed = posterior_mean.copy()
transposed = transposed.T
transposed_raveled = numpy.ravel(transposed)
normal_raveled = numpy.ravel(normal)
target_a = numpy.zeros_like(normal_raveled)
target_b = numpy.zeros_like(normal_raveled)
# GENERATES PRIORS
for i in range(elements - 1):
target_a[:] = (normal_raveled[i - 1] + normal_raveled[i] + normal_raveled[i + 1]) / 3
target_a[0] = (normal_raveled[0] + (normal_raveled[1] + normal_raveled[2]) / 2) / 3
target_a[-1] = (normal_raveled[-1] + (normal_raveled[-2] + normal_raveled[-3]) / 2) / 3
normal_raveled[:] = target_a[:]
for i in range(elements - 1):
target_b[:] = (transposed_raveled[i - 1] + transposed_raveled[i] + transposed_raveled[i + 1]) / 3
target_b[0] = (transposed_raveled[0] + (transposed_raveled[1] + transposed_raveled[2]) / 2) / 3
target_b[-1] = (transposed_raveled[-1] + (transposed_raveled[-2] + transposed_raveled[-3]) / 2) / 3
transposed_raveled[:] = target_b[:]
transposed = transposed.T
prior_mean = (transposed + normal)/2