Tue, 01 Aug 2023 10:32:12 +0300
merge
0 | 1 | //! Implementation of the gaussian kernel. |
2 | ||
3 | use float_extras::f64::erf; | |
4 | use numeric_literals::replace_float_literals; | |
5 | use serde::Serialize; | |
6 | use alg_tools::types::*; | |
7 | use alg_tools::euclidean::Euclidean; | |
8 | use alg_tools::norms::*; | |
9 | use alg_tools::loc::Loc; | |
10 | use alg_tools::sets::Cube; | |
11 | use alg_tools::bisection_tree::{ | |
12 | Support, | |
13 | Constant, | |
14 | Bounds, | |
15 | LocalAnalysis, | |
16 | GlobalAnalysis, | |
17 | Weighted, | |
18 | Bounded, | |
19 | }; | |
33 | 20 | use alg_tools::mapping::{Apply, Differentiable}; |
0 | 21 | use alg_tools::maputil::array_init; |
22 | ||
33 | 23 | use crate::types::Lipschitz; |
0 | 24 | use crate::fourier::Fourier; |
25 | use super::base::*; | |
26 | use super::ball_indicator::CubeIndicator; | |
27 | ||
28 | /// Storage presentation of the the anisotropic gaussian kernel of `variance` $σ^2$. | |
29 | /// | |
30 | /// This is the function $f(x) = C e^{-\\|x\\|\_2^2/(2σ^2)}$ for $x ∈ ℝ^N$ | |
31 | /// with $C=1/(2πσ^2)^{N/2}$. | |
32 | #[derive(Copy,Clone,Debug,Serialize,Eq)] | |
33 | pub struct Gaussian<S : Constant, const N : usize> { | |
34 | /// The variance $σ^2$. | |
35 | pub variance : S, | |
36 | } | |
37 | ||
38 | impl<S1, S2, const N : usize> PartialEq<Gaussian<S2, N>> for Gaussian<S1, N> | |
39 | where S1 : Constant, | |
40 | S2 : Constant<Type=S1::Type> { | |
41 | fn eq(&self, other : &Gaussian<S2, N>) -> bool { | |
42 | self.variance.value() == other.variance.value() | |
43 | } | |
44 | } | |
45 | ||
46 | impl<S1, S2, const N : usize> PartialOrd<Gaussian<S2, N>> for Gaussian<S1, N> | |
47 | where S1 : Constant, | |
48 | S2 : Constant<Type=S1::Type> { | |
49 | ||
50 | fn partial_cmp(&self, other : &Gaussian<S2, N>) -> Option<std::cmp::Ordering> { | |
51 | // A gaussian is ≤ another gaussian if the Fourier transforms satisfy the | |
52 | // corresponding inequality. That in turns holds if and only if the variances | |
53 | // satisfy the opposite inequality. | |
54 | let σ1sq = self.variance.value(); | |
55 | let σ2sq = other.variance.value(); | |
56 | σ2sq.partial_cmp(&σ1sq) | |
57 | } | |
58 | } | |
59 | ||
60 | ||
61 | #[replace_float_literals(S::Type::cast_from(literal))] | |
62 | impl<'a, S, const N : usize> Apply<&'a Loc<S::Type, N>> for Gaussian<S, N> | |
63 | where S : Constant { | |
64 | type Output = S::Type; | |
65 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
66 | // (unless the cut-off ε=0). | |
67 | #[inline] | |
68 | fn apply(&self, x : &'a Loc<S::Type, N>) -> Self::Output { | |
69 | let d_squared = x.norm2_squared(); | |
70 | let σ2 = self.variance.value(); | |
71 | let scale = self.scale(); | |
72 | (-d_squared / (2.0 * σ2)).exp() / scale | |
73 | } | |
74 | } | |
75 | ||
76 | impl<S, const N : usize> Apply<Loc<S::Type, N>> for Gaussian<S, N> | |
77 | where S : Constant { | |
78 | type Output = S::Type; | |
79 | #[inline] | |
80 | fn apply(&self, x : Loc<S::Type, N>) -> Self::Output { | |
81 | self.apply(&x) | |
82 | } | |
83 | } | |
84 | ||
33 | 85 | #[replace_float_literals(S::Type::cast_from(literal))] |
86 | impl<'a, S, const N : usize> Differentiable<&'a Loc<S::Type, N>> for Gaussian<S, N> | |
87 | where S : Constant { | |
88 | type Output = Loc<S::Type, N>; | |
89 | #[inline] | |
90 | fn differential(&self, x : &'a Loc<S::Type, N>) -> Self::Output { | |
91 | x * (self.apply(x) / self.variance.value()) | |
92 | } | |
93 | } | |
94 | ||
95 | impl<S, const N : usize> Differentiable<Loc<S::Type, N>> for Gaussian<S, N> | |
96 | where S : Constant { | |
97 | type Output = Loc<S::Type, N>; | |
98 | // This is not normalised to neither to have value 1 at zero or integral 1 | |
99 | // (unless the cut-off ε=0). | |
100 | #[inline] | |
101 | fn differential(&self, x : Loc<S::Type, N>) -> Self::Output { | |
102 | x * (self.apply(&x) / self.variance.value()) | |
103 | } | |
104 | } | |
105 | ||
106 | #[replace_float_literals(S::Type::cast_from(literal))] | |
107 | impl<S, const N : usize> Lipschitz<L2> for Gaussian<S, N> | |
108 | where S : Constant { | |
109 | type FloatType = S::Type; | |
110 | fn lipschitz_factor(&self, L2 : L2) -> Option<Self::FloatType> { | |
111 | // f(x)=f_1(‖x‖_2/σ) * √(2π) / √(2πσ)^N, where f_1 is one-dimensional Gaussian with | |
112 | // variance 1. The Lipschitz factor of f_1 is e^{-1/2}/√(2π), see, e.g., | |
113 | // https://math.stackexchange.com/questions/3630967/is-the-gaussian-density-lipschitz-continuous | |
114 | // Thus the Lipschitz factor we want is e^{-1/2} / (√(2πσ)^N * σ). | |
115 | Some((-0.5).exp() / (self.scale() * self.variance.value().sqrt())) | |
116 | } | |
117 | } | |
0 | 118 | |
119 | #[replace_float_literals(S::Type::cast_from(literal))] | |
120 | impl<'a, S, const N : usize> Gaussian<S, N> | |
121 | where S : Constant { | |
122 | ||
123 | /// Returns the (reciprocal) scaling constant $1/C=(2πσ^2)^{N/2}$. | |
124 | #[inline] | |
125 | pub fn scale(&self) -> S::Type { | |
126 | let π = S::Type::PI; | |
127 | let σ2 = self.variance.value(); | |
128 | (2.0*π*σ2).powi(N as i32).sqrt() | |
129 | } | |
130 | } | |
131 | ||
132 | impl<'a, S, const N : usize> Support<S::Type, N> for Gaussian<S, N> | |
133 | where S : Constant { | |
134 | #[inline] | |
135 | fn support_hint(&self) -> Cube<S::Type,N> { | |
136 | array_init(|| [S::Type::NEG_INFINITY, S::Type::INFINITY]).into() | |
137 | } | |
138 | ||
139 | #[inline] | |
140 | fn in_support(&self, _x : &Loc<S::Type,N>) -> bool { | |
141 | true | |
142 | } | |
143 | } | |
144 | ||
145 | #[replace_float_literals(S::Type::cast_from(literal))] | |
146 | impl<S, const N : usize> GlobalAnalysis<S::Type, Bounds<S::Type>> for Gaussian<S, N> | |
147 | where S : Constant { | |
148 | #[inline] | |
149 | fn global_analysis(&self) -> Bounds<S::Type> { | |
150 | Bounds(0.0, 1.0/self.scale()) | |
151 | } | |
152 | } | |
153 | ||
154 | impl<S, const N : usize> LocalAnalysis<S::Type, Bounds<S::Type>, N> for Gaussian<S, N> | |
155 | where S : Constant { | |
156 | #[inline] | |
157 | fn local_analysis(&self, cube : &Cube<S::Type, N>) -> Bounds<S::Type> { | |
158 | // The function is maximised/minimised where the 2-norm is minimised/maximised. | |
159 | let lower = self.apply(cube.maxnorm_point()); | |
160 | let upper = self.apply(cube.minnorm_point()); | |
161 | Bounds(lower, upper) | |
162 | } | |
163 | } | |
164 | ||
165 | #[replace_float_literals(C::Type::cast_from(literal))] | |
166 | impl<'a, C : Constant, const N : usize> Norm<C::Type, L1> | |
167 | for Gaussian<C, N> { | |
168 | #[inline] | |
169 | fn norm(&self, _ : L1) -> C::Type { | |
170 | 1.0 | |
171 | } | |
172 | } | |
173 | ||
174 | #[replace_float_literals(C::Type::cast_from(literal))] | |
175 | impl<'a, C : Constant, const N : usize> Norm<C::Type, Linfinity> | |
176 | for Gaussian<C, N> { | |
177 | #[inline] | |
178 | fn norm(&self, _ : Linfinity) -> C::Type { | |
179 | self.bounds().upper() | |
180 | } | |
181 | } | |
182 | ||
183 | #[replace_float_literals(C::Type::cast_from(literal))] | |
184 | impl<'a, C : Constant, const N : usize> Fourier<C::Type> | |
185 | for Gaussian<C, N> { | |
186 | type Domain = Loc<C::Type, N>; | |
187 | type Transformed = Weighted<Gaussian<C::Type, N>, C::Type>; | |
188 | ||
189 | #[inline] | |
190 | fn fourier(&self) -> Self::Transformed { | |
191 | let π = C::Type::PI; | |
192 | let σ2 = self.variance.value(); | |
193 | let g = Gaussian { variance : 1.0 / (4.0*π*π*σ2) }; | |
194 | g.weigh(g.scale()) | |
195 | } | |
196 | } | |
197 | ||
198 | /// Representation of the “cut” gaussian $f χ\_{[-a, a]^n}$ | |
199 | /// where $a>0$ and $f$ is a gaussian kernel on $ℝ^n$. | |
200 | pub type BasicCutGaussian<C, S, const N : usize> = SupportProductFirst<CubeIndicator<C, N>, | |
201 | Gaussian<S, N>>; | |
202 | ||
203 | ||
33 | 204 | /// This implements $g := χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ where $a,b>0$ and $f$ is |
205 | /// a gaussian kernel on $ℝ^n$. For an expression for $g$, see Lemma 3.9 in the manuscript. | |
0 | 206 | #[replace_float_literals(F::cast_from(literal))] |
207 | impl<'a, F : Float, R, C, S, const N : usize> Apply<&'a Loc<F, N>> | |
208 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
209 | where R : Constant<Type=F>, | |
210 | C : Constant<Type=F>, | |
211 | S : Constant<Type=F> { | |
212 | ||
213 | type Output = F; | |
214 | ||
215 | #[inline] | |
216 | fn apply(&self, y : &'a Loc<F, N>) -> F { | |
217 | let Convolution(ref ind, | |
218 | SupportProductFirst(ref cut, | |
219 | ref gaussian)) = self; | |
220 | let a = cut.r.value(); | |
221 | let b = ind.r.value(); | |
222 | let σ = gaussian.variance.value().sqrt(); | |
223 | let t = F::SQRT_2 * σ; | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
224 | let c = 0.5; // 1/(σ√(2π) * σ√(π/2) = 1/2 |
0 | 225 | |
226 | // This is just a product of one-dimensional versions | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
227 | y.product_map(|x| { |
0 | 228 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); |
229 | let c2 = a.min(b - x); | |
230 | if c1 >= c2 { | |
231 | 0.0 | |
232 | } else { | |
233 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
234 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
235 | debug_assert!(e2 >= e1); | |
236 | c * (e2 - e1) | |
237 | } | |
31
6105b5cd8d89
Simplify and fix cut gaussian indicator convolution scaling
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
238 | }) |
0 | 239 | } |
240 | } | |
241 | ||
242 | impl<F : Float, R, C, S, const N : usize> Apply<Loc<F, N>> | |
243 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
244 | where R : Constant<Type=F>, | |
245 | C : Constant<Type=F>, | |
246 | S : Constant<Type=F> { | |
247 | ||
248 | type Output = F; | |
249 | ||
250 | #[inline] | |
251 | fn apply(&self, y : Loc<F, N>) -> F { | |
252 | self.apply(&y) | |
253 | } | |
254 | } | |
255 | ||
33 | 256 | /// This implements the differential of $g := χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ where $a,b>0$ |
257 | /// and $f$ is a gaussian kernel on $ℝ^n$. For an expression for the value of $g$, from which the | |
258 | /// derivative readily arises (at points of differentiability), see Lemma 3.9 in the manuscript. | |
259 | #[replace_float_literals(F::cast_from(literal))] | |
260 | impl<'a, F : Float, R, C, S, const N : usize> Differentiable<&'a Loc<F, N>> | |
261 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
262 | where R : Constant<Type=F>, | |
263 | C : Constant<Type=F>, | |
264 | S : Constant<Type=F> { | |
265 | ||
266 | type Output = Loc<F, N>; | |
267 | ||
268 | #[inline] | |
269 | fn differential(&self, y : &'a Loc<F, N>) -> Loc<F, N> { | |
270 | let Convolution(ref ind, | |
271 | SupportProductFirst(ref cut, | |
272 | ref gaussian)) = self; | |
273 | let a = cut.r.value(); | |
274 | let b = ind.r.value(); | |
275 | let σ = gaussian.variance.value().sqrt(); | |
276 | let t = F::SQRT_2 * σ; | |
277 | let c = 0.5; // 1/(σ√(2π) * σ√(π/2) = 1/2 | |
278 | let c_div_t = c / t; | |
279 | ||
280 | // Calculate the values for all component functions of the | |
281 | // product. This is just the loop from apply above. | |
282 | let unscaled_vs = y.map(|x| { | |
283 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
284 | let c2 = a.min(b - x); | |
285 | if c1 >= c2 { | |
286 | 0.0 | |
287 | } else { | |
288 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
289 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
290 | debug_assert!(e2 >= e1); | |
291 | c * (e2 - e1) | |
292 | } | |
293 | }); | |
294 | // This computes the gradient for each coordinate | |
295 | product_differential(y, &unscaled_vs, |x| { | |
296 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
297 | let c2 = a.min(b - x); | |
298 | if c1 >= c2 { | |
299 | 0.0 | |
300 | } else { | |
301 | // erf'(z) = (2/√π)*exp(-z^2), and we get extra factor -1/√(2*σ) = -1/t | |
302 | // from the chain rule | |
303 | let de1 = (-(c1/t).powi(2)).exp(); | |
304 | let de2 = (-(c2/t).powi(2)).exp(); | |
305 | c_div_t * (de1 - de2) | |
306 | } | |
307 | }) | |
308 | } | |
309 | } | |
310 | ||
311 | impl<F : Float, R, C, S, const N : usize> Differentiable<Loc<F, N>> | |
312 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
313 | where R : Constant<Type=F>, | |
314 | C : Constant<Type=F>, | |
315 | S : Constant<Type=F> { | |
316 | ||
317 | type Output = Loc<F, N>; | |
318 | ||
319 | #[inline] | |
320 | fn differential(&self, y : Loc<F, N>) -> Loc<F, N> { | |
321 | self.differential(&y) | |
322 | } | |
323 | } | |
324 | ||
325 | #[replace_float_literals(F::cast_from(literal))] | |
326 | impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L2> | |
327 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
328 | where R : Constant<Type=F>, | |
329 | C : Constant<Type=F>, | |
330 | S : Constant<Type=F> { | |
331 | type FloatType = F; | |
332 | ||
333 | fn lipschitz_factor(&self, L2 : L2) -> Option<F> { | |
334 | todo!("This requirement some error function work.") | |
335 | } | |
336 | } | |
337 | ||
0 | 338 | impl<F : Float, R, C, S, const N : usize> |
339 | Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
340 | where R : Constant<Type=F>, | |
341 | C : Constant<Type=F>, | |
342 | S : Constant<Type=F> { | |
343 | ||
344 | #[inline] | |
345 | fn get_r(&self) -> F { | |
346 | let Convolution(ref ind, | |
347 | SupportProductFirst(ref cut, ..)) = self; | |
348 | ind.r.value() + cut.r.value() | |
349 | } | |
350 | } | |
351 | ||
352 | impl<F : Float, R, C, S, const N : usize> Support<F, N> | |
353 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
354 | where R : Constant<Type=F>, | |
355 | C : Constant<Type=F>, | |
356 | S : Constant<Type=F> { | |
357 | #[inline] | |
358 | fn support_hint(&self) -> Cube<F, N> { | |
359 | let r = self.get_r(); | |
360 | array_init(|| [-r, r]).into() | |
361 | } | |
362 | ||
363 | #[inline] | |
364 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
365 | let r = self.get_r(); | |
366 | y.iter().all(|x| x.abs() <= r) | |
367 | } | |
368 | ||
369 | #[inline] | |
370 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
371 | let r = self.get_r(); | |
372 | // From c1 = -a.min(b + x) and c2 = a.min(b - x) with c_1 < c_2, | |
373 | // solve bounds for x. that is 0 ≤ a.min(b + x) + a.min(b - x). | |
374 | // If b + x ≤ a and b - x ≤ a, the sum is 2b ≥ 0. | |
375 | // If b + x ≥ a and b - x ≥ a, the sum is 2a ≥ 0. | |
376 | // If b + x ≤ a and b - x ≥ a, the sum is b + x + a ⟹ need x ≥ -a - b = -r. | |
377 | // If b + x ≥ a and b - x ≤ a, the sum is a + b - x ⟹ need x ≤ a + b = r. | |
378 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
379 | } | |
380 | } | |
381 | ||
382 | impl<F : Float, R, C, S, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
383 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
384 | where R : Constant<Type=F>, | |
385 | C : Constant<Type=F>, | |
386 | S : Constant<Type=F> { | |
387 | #[inline] | |
388 | fn global_analysis(&self) -> Bounds<F> { | |
389 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
390 | } | |
391 | } | |
392 | ||
393 | impl<F : Float, R, C, S, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
394 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
395 | where R : Constant<Type=F>, | |
396 | C : Constant<Type=F>, | |
397 | S : Constant<Type=F> { | |
398 | #[inline] | |
399 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
400 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
401 | let lower = self.apply(cube.maxnorm_point()); | |
402 | let upper = self.apply(cube.minnorm_point()); | |
403 | Bounds(lower, upper) | |
404 | } | |
405 | } | |
406 |