Tue, 31 Dec 2024 09:34:24 -0500
Early transport sketches
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 | }; | |
32 | 20 | use alg_tools::mapping::{Apply, Differentiable}; |
0 | 21 | use alg_tools::maputil::array_init; |
22 | ||
32 | 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 | ||
32 | 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 | ||
32 | 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 π = F::PI; | |
224 | let t = F::SQRT_2 * σ; | |
225 | let c = σ * (8.0/π).sqrt(); | |
226 | ||
227 | // This is just a product of one-dimensional versions | |
228 | let unscaled = y.product_map(|x| { | |
229 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
230 | let c2 = a.min(b - x); | |
231 | if c1 >= c2 { | |
232 | 0.0 | |
233 | } else { | |
234 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
235 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
236 | debug_assert!(e2 >= e1); | |
237 | c * (e2 - e1) | |
238 | } | |
239 | }); | |
240 | ||
241 | unscaled / gaussian.scale() | |
242 | } | |
243 | } | |
244 | ||
245 | impl<F : Float, R, C, S, const N : usize> Apply<Loc<F, N>> | |
246 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
247 | where R : Constant<Type=F>, | |
248 | C : Constant<Type=F>, | |
249 | S : Constant<Type=F> { | |
250 | ||
251 | type Output = F; | |
252 | ||
253 | #[inline] | |
254 | fn apply(&self, y : Loc<F, N>) -> F { | |
255 | self.apply(&y) | |
256 | } | |
257 | } | |
258 | ||
32 | 259 | /// This implements the differential of $g := χ\_{[-b, b]^n} \* (f χ\_{[-a, a]^n})$ where $a,b>0$ |
260 | /// and $f$ is a gaussian kernel on $ℝ^n$. For an expression for the value of $g$, from which the | |
261 | /// derivative readily arises (at points of differentiability), see Lemma 3.9 in the manuscript. | |
262 | #[replace_float_literals(F::cast_from(literal))] | |
263 | impl<'a, F : Float, R, C, S, const N : usize> Differentiable<&'a Loc<F, N>> | |
264 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
265 | where R : Constant<Type=F>, | |
266 | C : Constant<Type=F>, | |
267 | S : Constant<Type=F> { | |
268 | ||
269 | type Output = Loc<F, N>; | |
270 | ||
271 | #[inline] | |
272 | fn differential(&self, y : &'a Loc<F, N>) -> Loc<F, N> { | |
273 | let Convolution(ref ind, | |
274 | SupportProductFirst(ref cut, | |
275 | ref gaussian)) = self; | |
276 | let a = cut.r.value(); | |
277 | let b = ind.r.value(); | |
278 | let σ = gaussian.variance.value().sqrt(); | |
279 | let π = F::PI; | |
280 | let t = F::SQRT_2 * σ; | |
281 | let c = σ * (8.0/π).sqrt(); | |
282 | let cd = (8.0).sqrt(); // σ * (8.0/π).sqrt() / t * (√2/π) | |
283 | ||
284 | // Calculate the values for all component functions of the | |
285 | // product. This is just the loop from apply above. | |
286 | let unscaled_vs = y.map(|x| { | |
287 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
288 | let c2 = a.min(b - x); | |
289 | if c1 >= c2 { | |
290 | 0.0 | |
291 | } else { | |
292 | let e1 = F::cast_from(erf((c1 / t).as_())); | |
293 | let e2 = F::cast_from(erf((c2 / t).as_())); | |
294 | debug_assert!(e2 >= e1); | |
295 | c * (e2 - e1) | |
296 | } | |
297 | }); | |
298 | // This computes the gradient for each coordinate | |
299 | product_differential(y, &unscaled_vs, |x| { | |
300 | let c1 = -(a.min(b + x)); //(-a).max(-x-b); | |
301 | let c2 = a.min(b - x); | |
302 | if c1 >= c2 { | |
303 | 0.0 | |
304 | } else { | |
305 | // erf'(z) = (2/√π)*exp(-z^2), and we get extra factor -1/√(2*σ) = -1/t | |
306 | // from the chain rule | |
307 | let de1 = (-(c1/t).powi(2)).exp(); | |
308 | let de2 = (-(c2/t).powi(2)).exp(); | |
309 | cd * (de1 - de2) | |
310 | } | |
311 | }) / gaussian.scale() | |
312 | } | |
313 | } | |
314 | ||
315 | impl<F : Float, R, C, S, const N : usize> Differentiable<Loc<F, N>> | |
316 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
317 | where R : Constant<Type=F>, | |
318 | C : Constant<Type=F>, | |
319 | S : Constant<Type=F> { | |
320 | ||
321 | type Output = Loc<F, N>; | |
322 | ||
323 | #[inline] | |
324 | fn differential(&self, y : Loc<F, N>) -> Loc<F, N> { | |
325 | self.differential(&y) | |
326 | } | |
327 | } | |
328 | ||
329 | #[replace_float_literals(F::cast_from(literal))] | |
330 | impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L2> | |
331 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
332 | where R : Constant<Type=F>, | |
333 | C : Constant<Type=F>, | |
334 | S : Constant<Type=F> { | |
335 | type FloatType = F; | |
336 | ||
337 | fn lipschitz_factor(&self, L2 : L2) -> Option<F> { | |
338 | todo!("This requirement some error function work.") | |
339 | } | |
340 | } | |
341 | ||
0 | 342 | impl<F : Float, R, C, S, const N : usize> |
343 | Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
344 | where R : Constant<Type=F>, | |
345 | C : Constant<Type=F>, | |
346 | S : Constant<Type=F> { | |
347 | ||
348 | #[inline] | |
349 | fn get_r(&self) -> F { | |
350 | let Convolution(ref ind, | |
351 | SupportProductFirst(ref cut, ..)) = self; | |
352 | ind.r.value() + cut.r.value() | |
353 | } | |
354 | } | |
355 | ||
356 | impl<F : Float, R, C, S, const N : usize> Support<F, N> | |
357 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
358 | where R : Constant<Type=F>, | |
359 | C : Constant<Type=F>, | |
360 | S : Constant<Type=F> { | |
361 | #[inline] | |
362 | fn support_hint(&self) -> Cube<F, N> { | |
363 | let r = self.get_r(); | |
364 | array_init(|| [-r, r]).into() | |
365 | } | |
366 | ||
367 | #[inline] | |
368 | fn in_support(&self, y : &Loc<F, N>) -> bool { | |
369 | let r = self.get_r(); | |
370 | y.iter().all(|x| x.abs() <= r) | |
371 | } | |
372 | ||
373 | #[inline] | |
374 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
375 | let r = self.get_r(); | |
376 | // From c1 = -a.min(b + x) and c2 = a.min(b - x) with c_1 < c_2, | |
377 | // solve bounds for x. that is 0 ≤ a.min(b + x) + a.min(b - x). | |
378 | // If b + x ≤ a and b - x ≤ a, the sum is 2b ≥ 0. | |
379 | // If b + x ≥ a and b - x ≥ a, the sum is 2a ≥ 0. | |
380 | // If b + x ≤ a and b - x ≥ a, the sum is b + x + a ⟹ need x ≥ -a - b = -r. | |
381 | // If b + x ≥ a and b - x ≤ a, the sum is a + b - x ⟹ need x ≤ a + b = r. | |
382 | cube.map(|c, d| symmetric_peak_hint(r, c, d)) | |
383 | } | |
384 | } | |
385 | ||
386 | impl<F : Float, R, C, S, const N : usize> GlobalAnalysis<F, Bounds<F>> | |
387 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
388 | where R : Constant<Type=F>, | |
389 | C : Constant<Type=F>, | |
390 | S : Constant<Type=F> { | |
391 | #[inline] | |
392 | fn global_analysis(&self) -> Bounds<F> { | |
393 | Bounds(F::ZERO, self.apply(Loc::ORIGIN)) | |
394 | } | |
395 | } | |
396 | ||
397 | impl<F : Float, R, C, S, const N : usize> LocalAnalysis<F, Bounds<F>, N> | |
398 | for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> | |
399 | where R : Constant<Type=F>, | |
400 | C : Constant<Type=F>, | |
401 | S : Constant<Type=F> { | |
402 | #[inline] | |
403 | fn local_analysis(&self, cube : &Cube<F, N>) -> Bounds<F> { | |
404 | // The function is maximised/minimised where the absolute value is minimised/maximised. | |
405 | let lower = self.apply(cube.maxnorm_point()); | |
406 | let upper = self.apply(cube.minnorm_point()); | |
407 | Bounds(lower, upper) | |
408 | } | |
409 | } | |
410 |