Fri, 16 Jan 2026 19:39:22 -0500
Lipschitz estimation attempt (incomplete, not implemented for sliding. Doesn't work anyway for basic FB either.)
| 0 | 1 | //! Random distribution wrappers and implementations |
| 2 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
3 | use alg_tools::types::*; |
| 0 | 4 | use numeric_literals::replace_float_literals; |
| 5 | use rand::Rng; | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
6 | use rand_distr::{Distribution, Normal, NormalError, StandardNormal}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
7 | use serde::ser::{SerializeStruct, Serializer}; |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
8 | use serde::{Deserialize, Serialize}; |
| 0 | 9 | |
| 10 | /// Wrapper for [`Normal`] that can be serialized by serde. | |
|
23
9869fa1e0ccd
Print out experiment information when running it
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
11 | #[derive(Debug)] |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
12 | pub struct SerializableNormal<T: Float>(Normal<T>) |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
13 | where |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
14 | StandardNormal: Distribution<T>; |
| 0 | 15 | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
16 | impl<T: Float> Distribution<T> for SerializableNormal<T> |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
17 | where |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
18 | StandardNormal: Distribution<T>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
19 | { |
| 0 | 20 | fn sample<R>(&self, rng: &mut R) -> T |
| 21 | where | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
22 | R: Rng + ?Sized, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
23 | { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
24 | self.0.sample(rng) |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
25 | } |
| 0 | 26 | } |
| 27 | ||
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
28 | impl<T: Float> SerializableNormal<T> |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
29 | where |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
30 | StandardNormal: Distribution<T>, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
31 | { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
32 | pub fn new(mean: T, std_dev: T) -> Result<SerializableNormal<T>, NormalError> { |
| 0 | 33 | Ok(SerializableNormal(Normal::new(mean, std_dev)?)) |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | impl<F> Serialize for SerializableNormal<F> | |
| 38 | where | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
39 | StandardNormal: Distribution<F>, |
| 0 | 40 | F: Float + Serialize, |
| 41 | { | |
| 42 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
| 43 | where | |
| 44 | S: Serializer, | |
| 45 | { | |
| 46 | let mut s = serializer.serialize_struct("Normal", 2)?; | |
| 47 | s.serialize_field("mean", &self.0.mean())?; | |
| 48 | s.serialize_field("std_dev", &self.0.std_dev())?; | |
| 49 | s.end() | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | /// Salt-and-pepper noise distribution | |
| 54 | /// | |
| 55 | /// This is the distribution that outputs each $\\{-m,0,m\\}$ with the corresponding | |
| 56 | /// probabilities $\\{1-p, p/2, p/2\\}$. | |
| 57 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
58 | pub struct SaltAndPepper<T: Float> { |
| 0 | 59 | /// The magnitude parameter $m$ |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
60 | magnitude: T, |
| 0 | 61 | /// The probability parameter $p$ |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
62 | probability: T, |
| 0 | 63 | } |
| 64 | ||
| 65 | /// Error for [`SaltAndPepper`]. | |
| 66 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |
| 67 | pub enum SaltAndPepperError { | |
| 68 | /// The probability parameter $p$ is not in the range [0, 1]. | |
| 69 | InvalidProbability, | |
| 70 | } | |
| 71 | impl std::error::Error for SaltAndPepperError {} | |
| 72 | ||
| 73 | impl std::fmt::Display for SaltAndPepperError { | |
| 74 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| 75 | f.write_str(match self { | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
76 | SaltAndPepperError::InvalidProbability => { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
77 | " The probability parameter is not in the range [0, 1]." |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
78 | } |
| 0 | 79 | }) |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | #[replace_float_literals(T::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
84 | impl<T: Float> SaltAndPepper<T> { |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
85 | pub fn new(magnitude: T, probability: T) -> Result<SaltAndPepper<T>, SaltAndPepperError> { |
| 0 | 86 | if probability > 1.0 || probability < 0.0 { |
| 87 | Err(SaltAndPepperError::InvalidProbability) | |
| 88 | } else { | |
| 89 | Ok(SaltAndPepper { magnitude, probability }) | |
| 90 | } | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | #[replace_float_literals(T::cast_from(literal))] | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
95 | impl<T: Float> Distribution<T> for SaltAndPepper<T> { |
| 0 | 96 | fn sample<R>(&self, rng: &mut R) -> T |
| 97 | where | |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
98 | R: Rng + ?Sized, |
| 0 | 99 | { |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
100 | let (p, sign): (float, bool) = rng.random(); |
| 0 | 101 | match (p < self.probability.as_(), sign) { |
|
61
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
102 | (false, _) => 0.0, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
103 | (true, true) => self.magnitude, |
|
4f468d35fa29
General forward operators, separation of measures into own crate, and other architecture improvements to support the pointsource_pde crate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
104 | (true, false) => -self.magnitude, |
| 0 | 105 | } |
| 106 | } | |
| 107 | } |