src/kernels/gaussian.rs

branch
dev
changeset 34
efa60bc4f743
parent 33
aec67cdd6b14
child 35
b087e3eab191
equal deleted inserted replaced
33:aec67cdd6b14 34:efa60bc4f743
296 let c1 = -(a.min(b + x)); //(-a).max(-x-b); 296 let c1 = -(a.min(b + x)); //(-a).max(-x-b);
297 let c2 = a.min(b - x); 297 let c2 = a.min(b - x);
298 if c1 >= c2 { 298 if c1 >= c2 {
299 0.0 299 0.0
300 } else { 300 } else {
301 // erf'(z) = (2/√π)*exp(-z^2), and we get extra factor -1/√(2*σ) = -1/t 301 // erf'(z) = (2/√π)*exp(-z^2), and we get extra factor -1/(√2*σ) = -1/t
302 // from the chain rule 302 // from the chain rule (the minus comes from inside c_1 or c_2).
303 let de1 = (-(c1/t).powi(2)).exp(); 303 let de1 = (-(c1/t).powi(2)).exp();
304 let de2 = (-(c2/t).powi(2)).exp(); 304 let de2 = (-(c2/t).powi(2)).exp();
305 c_div_t * (de1 - de2) 305 c_div_t * (de1 - de2)
306 } 306 }
307 }) 307 })
321 self.differential(&y) 321 self.differential(&y)
322 } 322 }
323 } 323 }
324 324
325 #[replace_float_literals(F::cast_from(literal))] 325 #[replace_float_literals(F::cast_from(literal))]
326 impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L1>
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, L1 : L1) -> Option<F> {
334 // To get the product Lipschitz factor, we note that for any ψ_i, we have
335 // ∏_{i=1}^N φ_i(x_i) - ∏_{i=1}^N φ_i(y_i)
336 // = [φ_1(x_1)-φ_1(y_1)] ∏_{i=2}^N φ_i(x_i)
337 // + φ_1(y_1)[ ∏_{i=2}^N φ_i(x_i) - ∏_{i=2}^N φ_i(y_i)]
338 // = ∑_{j=1}^N [φ_j(x_j)-φ_j(y_j)]∏_{i > j} φ_i(x_i) ∏_{i < j} φ_i(y_i)
339 // Thus
340 // |∏_{i=1}^N φ_i(x_i) - ∏_{i=1}^N φ_i(y_i)|
341 // ≤ ∑_{j=1}^N |φ_j(x_j)-φ_j(y_j)| ∏_{j ≠ i} \max_i |φ_i|
342 //
343 // Thus we need 1D Lipschitz factors, and the maximum for φ = θ * ψ.
344 //
345 // We have
346 // θ * ψ(x) = 0 if c_1(x) ≥ c_2(x)
347 // = (1/2)[erf(c_2(x)/(√2σ)) - erf(c_1(x)/(√2σ))] if c_1(x) < c_2(x),
348 // where c_1(x) = max{-x-b,-a} = -min{b+x,a} and c_2(x)=min{b-x,a}, C is the Gaussian
349 // normalisation factor, and erf(s) = (2/√π) ∫_0^s e^{-t^2} dt.
350 // Thus, if c_1(x) < c_2(x) and c_1(y) < c_2(y), we have
351 // θ * ψ(x) - θ * ψ(y) = (1/√π)[∫_{c_1(x)/(√2σ)}^{c_1(y)/(√2σ) e^{-t^2} dt
352 // - ∫_{c_2(x)/(√2σ)}^{c_2(y)/(√2σ)] e^{-t^2} dt]
353 // Thus
354 // |θ * ψ(x) - θ * ψ(y)| ≤ (1/√π)/(√2σ)(|c_1(x)-c_1(y)|+|c_2(x)-c_2(y)|)
355 // ≤ 2(1/√π)/(√2σ)|x-y|
356 // ≤ √2/(√πσ)|x-y|.
357 //
358 // For the product we also need the value θ * ψ(0), which is
359 // (1/2)[erf(min{a,b}/(√2σ))-erf(max{-b,-a}/(√2σ)]
360 // = (1/2)[erf(min{a,b}/(√2σ))-erf(-min{a,b}/(√2σ))]
361 // = erf(min{a,b}/(√2σ))
362 //
363 // If c_1(x) ≥ c_2(x), then x ∉ [-(a+b), a+b]. If also y is outside that range,
364 // θ * ψ(x) = θ * ψ(y). If only y is in the range [-(a+b), a+b], we can replace
365 // x by -(a+b) or (a+b), either of which is closer to y and still θ * ψ(x)=0.
366 // Thus same calculations as above work for the Lipschitz factor.
367 let Convolution(ref ind,
368 SupportProductFirst(ref cut,
369 ref gaussian)) = self;
370 let a = cut.r.value();
371 let b = ind.r.value();
372 let σ = gaussian.variance.value().sqrt();
373 let π = F::PI;
374 let t = F::SQRT_2 * σ;
375 let l1d = F::SQRT_2 / (π.sqrt() * σ);
376 let e0 = F::cast_from(erf((a.min(b) / t).as_()));
377 Some(l1d * e0.powi(N as i32-1))
378 }
379 }
380
326 impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L2> 381 impl<'a, F : Float, R, C, S, const N : usize> Lipschitz<L2>
327 for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> 382 for Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>>
328 where R : Constant<Type=F>, 383 where R : Constant<Type=F>,
329 C : Constant<Type=F>, 384 C : Constant<Type=F>,
330 S : Constant<Type=F> { 385 S : Constant<Type=F> {
331 type FloatType = F; 386 type FloatType = F;
332 387 #[inline]
333 fn lipschitz_factor(&self, L2 : L2) -> Option<F> { 388 fn lipschitz_factor(&self, L2 : L2) -> Option<Self::FloatType> {
334 todo!("This requirement some error function work.") 389 self.lipschitz_factor(L1).map(|l1| l1 * <S::Type>::cast_from(N).sqrt())
335 } 390 }
336 } 391 }
337 392
338 impl<F : Float, R, C, S, const N : usize> 393 impl<F : Float, R, C, S, const N : usize>
339 Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>> 394 Convolution<CubeIndicator<R, N>, BasicCutGaussian<C, S, N>>

mercurial