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
//! Log-normal distribution.
use crate::DType;

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

/// Log-normal distribution.
///
/// A log-normal distribution is the distribution of a random variable whose
/// logarithm is normally distributed. If X ~ LogNormal(μ, σ), then ln(X) ~ Normal(μ, σ).
///
/// f(x) = (1 / (xσ√(2π))) exp(-(ln(x) - μ)² / (2σ²))  for x > 0
///
/// # Parameters
///
/// * `mu` (μ) - Mean of the underlying normal distribution (NOT the mean of X)
/// * `sigma` (σ) - Standard deviation of the underlying normal (must be positive)
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use solvr::stats::{LogNormal, ContinuousDistribution, Distribution};
///
/// // Standard log-normal (μ=0, σ=1)
/// let ln = LogNormal::standard();
/// println!("Mean: {}", ln.mean()); // exp(0.5) ≈ 1.649
/// println!("Median: {}", ln.median()); // exp(0) = 1
///
/// // Custom parameters
/// let ln = LogNormal::new(0.0, 0.5)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct LogNormal {
    /// Location parameter (μ) - mean of ln(X)
    mu: f64,
    /// Scale parameter (σ) - std of ln(X)
    sigma: f64,
}

impl LogNormal {
    /// Create a new log-normal distribution.
    ///
    /// # Arguments
    ///
    /// * `mu` - Mean of the underlying normal distribution
    /// * `sigma` - Standard deviation of the underlying normal (must be positive)
    pub fn new(mu: f64, sigma: f64) -> StatsResult<Self> {
        if sigma <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "sigma".to_string(),
                value: sigma,
                reason: "must be positive".to_string(),
            });
        }
        if !mu.is_finite() {
            return Err(StatsError::InvalidParameter {
                name: "mu".to_string(),
                value: mu,
                reason: "must be finite".to_string(),
            });
        }
        Ok(Self { mu, sigma })
    }

    /// Create a standard log-normal distribution (μ=0, σ=1).
    pub fn standard() -> Self {
        Self {
            mu: 0.0,
            sigma: 1.0,
        }
    }

    /// Create a log-normal distribution from desired mean and variance.
    ///
    /// Given desired mean m and variance v of X, computes μ and σ.
    pub fn from_mean_var(mean: f64, var: f64) -> StatsResult<Self> {
        if mean <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "mean".to_string(),
                value: mean,
                reason: "must be positive".to_string(),
            });
        }
        if var <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "var".to_string(),
                value: var,
                reason: "must be positive".to_string(),
            });
        }

        // σ² = ln(1 + v/m²)
        // μ = ln(m) - σ²/2
        let sigma_sq = (1.0 + var / (mean * mean)).ln();
        let sigma = sigma_sq.sqrt();
        let mu = mean.ln() - sigma_sq / 2.0;

        Self::new(mu, sigma)
    }

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

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

impl Distribution for LogNormal {
    fn mean(&self) -> f64 {
        (self.mu + self.sigma * self.sigma / 2.0).exp()
    }

    fn var(&self) -> f64 {
        let sigma_sq = self.sigma * self.sigma;
        ((2.0 * self.mu + sigma_sq).exp()) * (sigma_sq.exp() - 1.0)
    }

    fn entropy(&self) -> f64 {
        // H = μ + 0.5 + ln(σ) + 0.5*ln(2π)
        self.mu + 0.5 + self.sigma.ln() + LN_SQRT_2PI
    }

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

    fn mode(&self) -> f64 {
        (self.mu - self.sigma * self.sigma).exp()
    }

    fn skewness(&self) -> f64 {
        let sigma_sq = self.sigma * self.sigma;
        (sigma_sq.exp() + 2.0) * (sigma_sq.exp() - 1.0).sqrt()
    }

    fn kurtosis(&self) -> f64 {
        let sigma_sq = self.sigma * self.sigma;
        (4.0 * sigma_sq).exp() + 2.0 * (3.0 * sigma_sq).exp() + 3.0 * (2.0 * sigma_sq).exp() - 6.0
    }
}

impl ContinuousDistribution for LogNormal {
    fn pdf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return 0.0;
        }
        self.log_pdf(x).exp()
    }

    fn log_pdf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return f64::NEG_INFINITY;
        }
        let log_x = x.ln();
        let z = (log_x - self.mu) / self.sigma;
        -log_x - LN_SQRT_2PI - self.sigma.ln() - 0.5 * z * z
    }

    fn cdf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return 0.0;
        }
        let z = (x.ln() - self.mu) / self.sigma;
        special::norm_cdf(z)
    }

    fn sf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return 1.0;
        }
        let z = (x.ln() - self.mu) / self.sigma;
        special::norm_cdf(-z)
    }

    fn ppf(&self, p: f64) -> StatsResult<f64> {
        if !(0.0..=1.0).contains(&p) {
            return Err(StatsError::InvalidProbability { value: p });
        }
        if p == 0.0 {
            return Ok(0.0);
        }
        if p == 1.0 {
            return Ok(f64::INFINITY);
        }
        let z = special::norm_ppf(p);
        Ok((self.mu + self.sigma * z).exp())
    }

    // ========================================================================
    // 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/(xσ√(2π)) exp(-(ln(x) - μ)² / (2σ²))
        self.log_pdf_tensor(x, client)
            .and_then(|log_pdf| client.exp(&log_pdf))
    }

    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)) = -ln(x) - ln(σ√(2π)) - (ln(x) - μ)² / (2σ²)
        let ln_x = client.log(x)?;

        let centered = client.sub_scalar(&ln_x, self.mu)?;
        let z = client.mul_scalar(&centered, 1.0 / self.sigma)?;
        let z_sq = client.square(&z)?;
        let neg_half_z_sq = client.mul_scalar(&z_sq, -0.5)?;

        let log_const = -self.sigma.ln() - LN_SQRT_2PI;
        let result = client.add_scalar(&neg_half_z_sq, log_const)?;
        client.sub(&result, &ln_x)
    }

    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) = Φ((ln(x) - μ) / σ) where Φ is the standard normal CDF
        let ln_x = client.log(x)?;
        let centered = client.sub_scalar(&ln_x, self.mu)?;
        let z = client.mul_scalar(&centered, 1.0 / self.sigma)?;

        // Use normal CDF formula: 0.5 * erfc(-z / sqrt(2))
        let z_scaled = client.mul_scalar(&z, -std::f64::consts::FRAC_1_SQRT_2)?;
        let erfc_val = client.erfc(&z_scaled)?;
        client.mul_scalar(&erfc_val, 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) = Φ(-(ln(x) - μ) / σ)
        let ln_x = client.log(x)?;
        let centered = client.sub_scalar(&ln_x, self.mu)?;
        let z = client.mul_scalar(&centered, 1.0 / self.sigma)?;

        // Use normal CDF formula for -z: 0.5 * erfc(z / sqrt(2))
        let z_scaled = client.mul_scalar(&z, std::f64::consts::FRAC_1_SQRT_2)?;
        let erfc_val = client.erfc(&z_scaled)?;
        client.mul_scalar(&erfc_val, 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(normal_cdf(...))
        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) = exp(μ + σ * Φ⁻¹(p))
        let two_p_minus_1 = client.sub_scalar(&client.mul_scalar(p, 2.0)?, 1.0)?;
        let erfinv_val = client.erfinv(&two_p_minus_1)?;
        let z = client.mul_scalar(&erfinv_val, std::f64::consts::SQRT_2)?;

        let log_result = client.add_scalar(&client.mul_scalar(&z, self.sigma)?, self.mu)?;
        client.exp(&log_result)
    }

    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::*;
    use std::f64::consts::PI;

    #[test]
    fn test_lognormal_creation() {
        let ln = LogNormal::new(0.0, 1.0).unwrap();
        assert!((ln.mu() - 0.0).abs() < 1e-10);
        assert!((ln.sigma() - 1.0).abs() < 1e-10);

        assert!(LogNormal::new(0.0, 0.0).is_err());
        assert!(LogNormal::new(0.0, -1.0).is_err());
    }

    #[test]
    fn test_lognormal_from_mean_var() {
        // Create with desired mean=2, var=1
        let ln = LogNormal::from_mean_var(2.0, 1.0).unwrap();

        // Verify the computed parameters give the correct moments
        assert!((ln.mean() - 2.0).abs() < 1e-6);
        assert!((ln.var() - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_lognormal_moments() {
        let ln = LogNormal::standard();

        // Mean = exp(μ + σ²/2) = exp(0.5) ≈ 1.6487
        assert!((ln.mean() - 0.5_f64.exp()).abs() < 1e-10);

        // Median = exp(μ) = 1
        assert!((ln.median() - 1.0).abs() < 1e-10);

        // Mode = exp(μ - σ²) = exp(-1) ≈ 0.3679
        assert!((ln.mode() - (-1.0_f64).exp()).abs() < 1e-10);
    }

    #[test]
    fn test_lognormal_pdf() {
        let ln = LogNormal::standard();

        // PDF at x=1: f(1) = 1/(1*1*√(2π)) * exp(-0/2) = 1/√(2π)
        let expected = 1.0 / (2.0 * PI).sqrt();
        assert!((ln.pdf(1.0) - expected).abs() < 1e-10);

        // PDF should be 0 for x ≤ 0
        assert!((ln.pdf(0.0) - 0.0).abs() < 1e-10);
        assert!((ln.pdf(-1.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_lognormal_cdf() {
        let ln = LogNormal::standard();

        // CDF(1) = Φ(0) = 0.5 (since ln(1) = 0)
        assert!((ln.cdf(1.0) - 0.5).abs() < 1e-10);

        // CDF at median should be 0.5
        assert!((ln.cdf(ln.median()) - 0.5).abs() < 1e-10);

        // CDF should be 0 for x ≤ 0
        assert!((ln.cdf(0.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_lognormal_ppf() {
        let ln = LogNormal::new(1.0, 0.5).unwrap();

        // PPF(0.5) = median = exp(μ)
        assert!((ln.ppf(0.5).unwrap() - ln.median()).abs() < 1e-10);

        // PPF should be inverse of CDF
        for p in [0.1, 0.25, 0.5, 0.75, 0.9] {
            let x = ln.ppf(p).unwrap();
            assert!((ln.cdf(x) - p).abs() < 1e-8, "Failed for p={}", p);
        }
    }

    #[test]
    fn test_lognormal_sf() {
        let ln = LogNormal::standard();

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