solvr 0.2.0

Advanced computing library for real-world problem solving - optimization, differential equations, interpolation, statistics, and more
Documentation
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Laplace (double exponential) distribution.
use crate::DType;

use crate::stats::error::{StatsError, StatsResult};
use crate::stats::{ContinuousDistribution, Distribution};
use numr::algorithm::special::SpecialFunctions;
use numr::error::Result;
use numr::ops::{ScalarOps, TensorOps};
use numr::runtime::{Runtime, RuntimeClient};
use numr::tensor::Tensor;

/// Laplace (double exponential) distribution.
///
/// The Laplace distribution is a continuous probability distribution with PDF:
///
/// f(x; μ, b) = 1/(2b) * exp(-|x - μ|/b)
///
/// where:
/// - μ is the location parameter (mean, median, mode)
/// - b > 0 is the scale parameter
///
/// It can be thought of as two exponential distributions spliced at the mean,
/// or as the distribution of the difference of two i.i.d. exponential random variables.
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use solvr::stats::{Laplace, ContinuousDistribution};
///
/// let l = Laplace::new(0.0, 1.0)?;  // standard Laplace
/// println!("PDF at 0: {}", l.pdf(0.0));     // 0.5
/// println!("CDF at 0: {}", l.cdf(0.0));     // 0.5
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Laplace {
    /// Location parameter (mean)
    loc: f64,
    /// Scale parameter
    scale: f64,
}

impl Laplace {
    /// Create a new Laplace distribution.
    ///
    /// # Arguments
    ///
    /// * `loc` - Location parameter (mean, median, mode)
    /// * `scale` - Scale parameter (must be > 0)
    pub fn new(loc: f64, scale: f64) -> StatsResult<Self> {
        if scale <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "scale".to_string(),
                value: scale,
                reason: "scale parameter must be positive".to_string(),
            });
        }
        Ok(Self { loc, scale })
    }

    /// Create the standard Laplace distribution (loc=0, scale=1).
    pub fn standard() -> Self {
        Self {
            loc: 0.0,
            scale: 1.0,
        }
    }

    /// Get the location parameter.
    pub fn loc(&self) -> f64 {
        self.loc
    }

    /// Get the scale parameter.
    pub fn scale(&self) -> f64 {
        self.scale
    }
}

impl Distribution for Laplace {
    fn mean(&self) -> f64 {
        self.loc
    }

    fn var(&self) -> f64 {
        2.0 * self.scale * self.scale
    }

    fn entropy(&self) -> f64 {
        // Entropy = ln(2be) = 1 + ln(2b)
        1.0 + (2.0 * self.scale).ln()
    }

    fn median(&self) -> f64 {
        self.loc
    }

    fn mode(&self) -> f64 {
        self.loc
    }

    fn skewness(&self) -> f64 {
        0.0
    }

    fn kurtosis(&self) -> f64 {
        // Excess kurtosis is 3
        3.0
    }
}

impl ContinuousDistribution for Laplace {
    fn pdf(&self, x: f64) -> f64 {
        let z = (x - self.loc).abs() / self.scale;
        (-z).exp() / (2.0 * self.scale)
    }

    fn log_pdf(&self, x: f64) -> f64 {
        let z = (x - self.loc).abs() / self.scale;
        -z - (2.0 * self.scale).ln()
    }

    fn cdf(&self, x: f64) -> f64 {
        let z = (x - self.loc) / self.scale;
        if z < 0.0 {
            0.5 * z.exp()
        } else {
            1.0 - 0.5 * (-z).exp()
        }
    }

    fn sf(&self, x: f64) -> f64 {
        let z = (x - self.loc) / self.scale;
        if z < 0.0 {
            1.0 - 0.5 * z.exp()
        } else {
            0.5 * (-z).exp()
        }
    }

    fn ppf(&self, p: f64) -> StatsResult<f64> {
        if !(0.0..=1.0).contains(&p) {
            return Err(StatsError::InvalidParameter {
                name: "p".to_string(),
                value: p,
                reason: "probability must be in [0, 1]".to_string(),
            });
        }
        if p == 0.0 {
            return Ok(f64::NEG_INFINITY);
        }
        if p == 1.0 {
            return Ok(f64::INFINITY);
        }

        let x = if p <= 0.5 {
            self.loc + self.scale * (2.0 * p).ln()
        } else {
            self.loc - self.scale * (2.0 * (1.0 - p)).ln()
        };
        Ok(x)
    }

    fn isf(&self, p: f64) -> StatsResult<f64> {
        self.ppf(1.0 - p)
    }

    // ========================================================================
    // Tensor Methods - All computation stays on device using numr ops
    // ========================================================================

    fn pdf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        x: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + RuntimeClient<R>,
    {
        // f(x) = 1/(2b) * exp(-|x - μ|/b)
        let centered = client.sub_scalar(x, self.loc)?;
        let abs_centered = client.abs(&centered)?;
        let z = client.mul_scalar(&abs_centered, -1.0 / self.scale)?;
        let exp_term = client.exp(&z)?;
        client.mul_scalar(&exp_term, 1.0 / (2.0 * self.scale))
    }

    fn log_pdf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        x: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + RuntimeClient<R>,
    {
        // log(f(x)) = -|x - μ|/b - ln(2b)
        let centered = client.sub_scalar(x, self.loc)?;
        let abs_centered = client.abs(&centered)?;
        let z = client.mul_scalar(&abs_centered, -1.0 / self.scale)?;
        let constant = -(2.0 * self.scale).ln();
        client.add_scalar(&z, constant)
    }

    fn cdf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        x: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + SpecialFunctions<R> + RuntimeClient<R>,
    {
        // CDF(x) = 0.5 * exp(z) if z < 0, else 1 - 0.5*exp(-z) where z = (x-μ)/b
        let centered = client.sub_scalar(x, self.loc)?;
        let z = client.mul_scalar(&centered, 1.0 / self.scale)?;

        // For positive z: 1 - 0.5*exp(-z) = 0.5 + 0.5*(1 - exp(-z))
        // For negative z: 0.5*exp(z)
        // Approximate: 0.5 + 0.5*sign(z)*(1 - exp(-|z|))
        // = 0.5 + 0.5*(2*sigmoid(2*z) - 1) where sigmoid(t) = 1/(1+exp(-t))
        // For now compute: 0.5 - 0.5*exp(z) for z < 0, and 1 - 0.5*exp(-z) for z >= 0
        // Simpler formula: CDF(x) = 0.5*(1 + sign(z)*(-1 + 2*exp(min(z,0))))
        // = 0.5 + 0.5*sign(z) - 0.5*exp(z) for z < 0
        // = 1 - 0.5*exp(-z) for z >= 0
        // We can use: if z < 0 then 0.5*exp(z) else 1 - 0.5*exp(-z)
        // = 0.5*exp(min(z, -z)) if z < 0 else with adjusted sign
        // Let's just compute as: where z < 0: 0.5*exp(z); where z >= 0: 1 - 0.5*exp(-z)
        // For tensor computation without branching, use smooth approximation or compute both

        // For z < 0: 0.5*exp(z), For z > 0: 1 - 0.5*exp(-z)
        // Blend using: 0.5*exp(z) + 0.5*(1 - exp(-z))*heaviside(z)
        // Where heaviside(z) ≈ sigmoid(kz) for large k
        // Use the closed-form for both cases:
        // For z < 0: 0.5*exp(z), for z >= 0: 1 - 0.5*exp(-z)
        // We can unify as: 0.5 + 0.5*sign(z)*(1 - exp(-|z|))
        // which equals: 0.5*(1 + sign(z)) - 0.5*sign(z)*exp(-|z|)
        // Simplified: we compute both branches and blend with sign

        let abs_z = client.abs(&z)?;
        let neg_abs_z = client.mul_scalar(&abs_z, -1.0)?;
        let exp_neg_abs = client.exp(&neg_abs_z)?;
        let sign_z = client.sign(&z)?;

        // result = 0.5 + 0.5*sign(z) - sign(z)*0.5*exp(-|z|)
        //        = 0.5 + 0.5*sign(z)*(1 - exp(-|z|))
        let one_minus_exp = client.add_scalar(&client.mul_scalar(&exp_neg_abs, -1.0)?, 1.0)?;
        let signed_term = client.mul(&sign_z, &one_minus_exp)?;
        let half_signed = client.mul_scalar(&signed_term, 0.5)?;
        client.add_scalar(&half_signed, 0.5)
    }

    fn sf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        x: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + SpecialFunctions<R> + RuntimeClient<R>,
    {
        // SF(x) = 1 - CDF(x) = 0.5*exp(|x-μ|/b) - 0.5 = 0.5*(exp(-z) - 1) for z < 0, 0.5*exp(-z) for z > 0
        // where z = (x-μ)/b = 0.5*exp(-|z|)
        let centered = client.sub_scalar(x, self.loc)?;
        let z = client.mul_scalar(&centered, 1.0 / self.scale)?;
        let abs_z = client.abs(&z)?;
        let exp_neg_abs = client.exp(&client.mul_scalar(&abs_z, -1.0)?)?;
        client.mul_scalar(&exp_neg_abs, 0.5)
    }

    fn log_cdf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        x: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + SpecialFunctions<R> + RuntimeClient<R>,
    {
        // log(CDF) = log(cdf(x))
        let cdf = self.cdf_tensor(x, client)?;
        client.log(&cdf)
    }

    fn ppf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        p: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + SpecialFunctions<R> + RuntimeClient<R>,
    {
        // PPF(p) = μ + b*ln(2p) if p <= 0.5, else μ - b*ln(2(1-p))
        // Unified formula: PPF(p) = μ - b*sign(p - 0.5)*ln(2*min(p, 1-p))

        // Compute p - 0.5
        let p_minus_half = client.add_scalar(p, -0.5)?;
        let sign_p = client.sign(&p_minus_half)?;

        // Compute 1 - p
        let neg_p = client.mul_scalar(p, -1.0)?;
        let one_minus_p = client.add_scalar(&neg_p, 1.0)?;

        // Compute min(p, 1-p)
        let min_p = client.minimum(p, &one_minus_p)?;

        // Compute 2*min(p, 1-p) and its log
        let two_min_p = client.mul_scalar(&min_p, 2.0)?;
        let ln_two_min_p = client.log(&two_min_p)?;

        // result = μ - b*sign(p - 0.5)*ln(2*min(p, 1-p))
        let signed_ln = client.mul(&sign_p, &ln_two_min_p)?;
        let scaled = client.mul_scalar(&signed_ln, self.scale)?;
        let neg_scaled = client.mul_scalar(&scaled, -1.0)?;
        client.add_scalar(&neg_scaled, self.loc)
    }

    fn isf_tensor<R: Runtime<DType = DType>, C>(
        &self,
        p: &Tensor<R>,
        client: &C,
    ) -> Result<Tensor<R>>
    where
        C: TensorOps<R> + ScalarOps<R> + SpecialFunctions<R> + RuntimeClient<R>,
    {
        // ISF(p) = PPF(1 - p)
        let one_minus_p = client.rsub_scalar(p, 1.0)?;
        self.ppf_tensor(&one_minus_p, client)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_laplace_creation() {
        assert!(Laplace::new(0.0, 1.0).is_ok());
        assert!(Laplace::new(0.0, 0.0).is_err());
        assert!(Laplace::new(0.0, -1.0).is_err());
    }

    #[test]
    fn test_laplace_pdf() {
        let l = Laplace::standard();

        // PDF at 0 is 0.5
        assert!((l.pdf(0.0) - 0.5).abs() < 1e-10);

        // Symmetric
        assert!((l.pdf(1.0) - l.pdf(-1.0)).abs() < 1e-10);

        // PDF at 1 is 0.5 * exp(-1) ≈ 0.184
        assert!((l.pdf(1.0) - 0.5 * (-1.0_f64).exp()).abs() < 1e-10);
    }

    #[test]
    fn test_laplace_cdf() {
        let l = Laplace::standard();

        // CDF at median is 0.5
        assert!((l.cdf(0.0) - 0.5).abs() < 1e-10);

        // CDF at 0 from both sides
        assert!((l.cdf(0.0) - 0.5).abs() < 1e-10);

        // CDF(-1) = 0.5 * exp(-1) ≈ 0.184
        assert!((l.cdf(-1.0) - 0.5 * (-1.0_f64).exp()).abs() < 1e-10);

        // CDF(1) = 1 - 0.5 * exp(-1) ≈ 0.816
        assert!((l.cdf(1.0) - (1.0 - 0.5 * (-1.0_f64).exp())).abs() < 1e-10);
    }

    #[test]
    fn test_laplace_ppf() {
        let l = Laplace::standard();

        // PPF(0.5) = 0 (median)
        assert!((l.ppf(0.5).unwrap() - 0.0).abs() < 1e-10);

        // Round-trip
        for &x in &[-2.0, -1.0, 0.0, 1.0, 2.0] {
            let p = l.cdf(x);
            assert!((l.ppf(p).unwrap() - x).abs() < 1e-10);
        }
    }

    #[test]
    fn test_laplace_moments() {
        let l = Laplace::new(5.0, 2.0).unwrap();

        assert!((l.mean() - 5.0).abs() < 1e-10);
        assert!((l.var() - 8.0).abs() < 1e-10); // 2 * 2^2 = 8
        assert!((l.skewness() - 0.0).abs() < 1e-10);
        assert!((l.kurtosis() - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_laplace_sf() {
        let l = Laplace::standard();

        // SF + CDF = 1
        for &x in &[-2.0, -1.0, 0.0, 1.0, 2.0] {
            assert!((l.sf(x) + l.cdf(x) - 1.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_laplace_entropy() {
        let l = Laplace::standard();

        // Entropy of standard Laplace is 1 + ln(2)
        assert!((l.entropy() - (1.0 + 2.0_f64.ln())).abs() < 1e-10);
    }

    #[test]
    fn test_laplace_median_mode() {
        let l = Laplace::new(3.0, 2.0).unwrap();

        assert!((l.median() - 3.0).abs() < 1e-10);
        assert!((l.mode() - 3.0).abs() < 1e-10);
    }
}