solvr 0.2.0-beta.2

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
422
423
424
425
//! Beta distribution.
use crate::DType;

use super::special;
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;

/// Beta distribution on [0, 1].
///
/// The beta distribution with shape parameters α and β has PDF:
///
/// f(x) = x^(α-1) (1-x)^(β-1) / B(α, β)  for 0 < x < 1
///
/// where B(α, β) is the beta function.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use solvr::stats::{Beta, ContinuousDistribution, Distribution};
///
/// // Uniform distribution as Beta(1, 1)
/// let b = Beta::new(1.0, 1.0)?;
/// assert!((b.pdf(0.5) - 1.0).abs() < 1e-10);
///
/// // Arcsine distribution as Beta(0.5, 0.5)
/// let b = Beta::new(0.5, 0.5)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Beta {
    /// Shape parameter α
    alpha: f64,
    /// Shape parameter β
    beta: f64,
    /// Log of normalizing constant: -ln(B(α, β))
    log_norm: f64,
}

impl Beta {
    /// Create a new beta distribution with shape parameters α and β.
    ///
    /// # Arguments
    ///
    /// * `alpha` - First shape parameter (must be positive)
    /// * `beta` - Second shape parameter (must be positive)
    ///
    /// # Errors
    ///
    /// Returns an error if parameters are not positive.
    pub fn new(alpha: f64, beta: f64) -> StatsResult<Self> {
        if alpha <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "alpha".to_string(),
                value: alpha,
                reason: "must be positive".to_string(),
            });
        }
        if beta <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "beta".to_string(),
                value: beta,
                reason: "must be positive".to_string(),
            });
        }
        if !alpha.is_finite() || !beta.is_finite() {
            return Err(StatsError::InvalidParameter {
                name: "alpha/beta".to_string(),
                value: alpha,
                reason: "parameters must be finite".to_string(),
            });
        }

        let log_norm = -special::lbeta(alpha, beta);
        Ok(Self {
            alpha,
            beta,
            log_norm,
        })
    }

    /// Get the first shape parameter α.
    pub fn alpha(&self) -> f64 {
        self.alpha
    }

    /// Get the second shape parameter β.
    pub fn beta(&self) -> f64 {
        self.beta
    }
}

impl Distribution for Beta {
    fn mean(&self) -> f64 {
        self.alpha / (self.alpha + self.beta)
    }

    fn var(&self) -> f64 {
        let sum = self.alpha + self.beta;
        (self.alpha * self.beta) / (sum * sum * (sum + 1.0))
    }

    fn entropy(&self) -> f64 {
        let sum = self.alpha + self.beta;
        special::lbeta(self.alpha, self.beta)
            - (self.alpha - 1.0) * special::digamma(self.alpha)
            - (self.beta - 1.0) * special::digamma(self.beta)
            + (sum - 2.0) * special::digamma(sum)
    }

    fn median(&self) -> f64 {
        // No closed form in general; use PPF
        // Approximation for α, β > 1
        if self.alpha > 1.0 && self.beta > 1.0 {
            (self.alpha - 1.0 / 3.0) / (self.alpha + self.beta - 2.0 / 3.0)
        } else {
            self.ppf(0.5).unwrap_or(self.mean())
        }
    }

    fn mode(&self) -> f64 {
        if self.alpha > 1.0 && self.beta > 1.0 {
            (self.alpha - 1.0) / (self.alpha + self.beta - 2.0)
        } else if self.alpha <= 1.0 && self.beta > 1.0 {
            0.0
        } else if self.alpha > 1.0 && self.beta <= 1.0 {
            1.0
        } else {
            // α ≤ 1 and β ≤ 1: bimodal at 0 and 1, return NaN
            f64::NAN
        }
    }

    fn skewness(&self) -> f64 {
        let sum = self.alpha + self.beta;
        2.0 * (self.beta - self.alpha) * (sum + 1.0).sqrt()
            / ((sum + 2.0) * (self.alpha * self.beta).sqrt())
    }

    fn kurtosis(&self) -> f64 {
        let sum = self.alpha + self.beta;
        let num = 6.0
            * ((self.alpha - self.beta).powi(2) * (sum + 1.0)
                - self.alpha * self.beta * (sum + 2.0));
        let denom = self.alpha * self.beta * (sum + 2.0) * (sum + 3.0);
        num / denom
    }
}

impl ContinuousDistribution for Beta {
    fn pdf(&self, x: f64) -> f64 {
        if x <= 0.0 || x >= 1.0 {
            // Handle boundary cases based on parameters
            if x == 0.0 && self.alpha < 1.0 {
                return f64::INFINITY;
            }
            if x == 1.0 && self.beta < 1.0 {
                return f64::INFINITY;
            }
            if x == 0.0 && self.alpha == 1.0 {
                return self.log_norm.exp();
            }
            if x == 1.0 && self.beta == 1.0 {
                return self.log_norm.exp();
            }
            return 0.0;
        }
        self.log_pdf(x).exp()
    }

    fn log_pdf(&self, x: f64) -> f64 {
        if x <= 0.0 || x >= 1.0 {
            return f64::NEG_INFINITY;
        }
        self.log_norm + (self.alpha - 1.0) * x.ln() + (self.beta - 1.0) * (1.0 - x).ln()
    }

    fn cdf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            0.0
        } else if x >= 1.0 {
            1.0
        } else {
            special::betainc(self.alpha, self.beta, x)
        }
    }

    fn sf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            1.0
        } else if x >= 1.0 {
            0.0
        } else {
            // SF(x; α, β) = I_{1-x}(β, α)
            special::betainc(self.beta, self.alpha, 1.0 - x)
        }
    }

    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(1.0);
        }
        Ok(special::betaincinv(self.alpha, self.beta, 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>,
    {
        // log(PDF) = log_norm + (α-1)*ln(x) + (β-1)*ln(1-x)
        // PDF = exp(log_pdf)
        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(PDF) = log_norm + (α-1)*ln(x) + (β-1)*ln(1-x)
        let ln_x = client.log(x)?;
        let term1 = client.mul_scalar(&ln_x, self.alpha - 1.0)?;

        // 1 - x = -x + 1
        let neg_x = client.mul_scalar(x, -1.0)?;
        let one_minus_x = client.add_scalar(&neg_x, 1.0)?;
        let ln_one_minus_x = client.log(&one_minus_x)?;
        let term2 = client.mul_scalar(&ln_one_minus_x, self.beta - 1.0)?;

        let result = client.add(&term1, &term2)?;
        client.add_scalar(&result, self.log_norm)
    }

    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) = betainc(α, β, x)
        let alpha_t = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.alpha, client.device());
        let beta_t = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.beta, client.device());
        client.betainc(&alpha_t, &beta_t, x)
    }

    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) = betainc(β, α, 1-x)
        let one_minus_x = client.mul_scalar(x, -1.0)?;
        let one_minus_x = client.add_scalar(&one_minus_x, 1.0)?;
        let alpha_t = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.alpha, client.device());
        let beta_t = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.beta, client.device());
        client.betainc(&beta_t, &alpha_t, &one_minus_x)
    }

    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(betainc(α, β, 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) = betaincinv(α, β, p)
        let alpha_t = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.alpha, client.device());
        let beta_t = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.beta, client.device());
        client.betaincinv(&alpha_t, &beta_t, p)
    }

    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) = betaincinv(α, β, 1-p)
        let one_minus_p = client.mul_scalar(p, -1.0)?;
        let one_minus_p = client.add_scalar(&one_minus_p, 1.0)?;
        let alpha_t = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.alpha, client.device());
        let beta_t = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.beta, client.device());
        client.betaincinv(&alpha_t, &beta_t, &one_minus_p)
    }
}

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

    #[test]
    fn test_beta_creation() {
        let b = Beta::new(2.0, 3.0).unwrap();
        assert!((b.alpha() - 2.0).abs() < 1e-10);
        assert!((b.beta() - 3.0).abs() < 1e-10);

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

    #[test]
    fn test_beta_uniform() {
        // Beta(1, 1) = Uniform(0, 1)
        let b = Beta::new(1.0, 1.0).unwrap();

        assert!((b.mean() - 0.5).abs() < 1e-10);
        assert!((b.var() - 1.0 / 12.0).abs() < 1e-10);
        assert!((b.pdf(0.5) - 1.0).abs() < 1e-10);
        assert!((b.cdf(0.5) - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_beta_moments() {
        let b = Beta::new(2.0, 5.0).unwrap();

        // Mean = α/(α+β) = 2/7
        assert!((b.mean() - 2.0 / 7.0).abs() < 1e-10);

        // Var = αβ/((α+β)²(α+β+1)) = 10/(49*8) = 10/392
        assert!((b.var() - 10.0 / 392.0).abs() < 1e-10);

        // Mode = (α-1)/(α+β-2) = 1/5
        assert!((b.mode() - 0.2).abs() < 1e-10);
    }

    #[test]
    fn test_beta_pdf() {
        let b = Beta::new(2.0, 2.0).unwrap();

        // Symmetric around 0.5
        assert!((b.pdf(0.3) - b.pdf(0.7)).abs() < 1e-10);

        // Mode at 0.5
        let mode_pdf = b.pdf(0.5);
        assert!(b.pdf(0.3) < mode_pdf);
        assert!(b.pdf(0.7) < mode_pdf);

        // PDF = 0 outside [0, 1]
        assert!((b.pdf(-0.1) - 0.0).abs() < 1e-10);
        assert!((b.pdf(1.1) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_beta_cdf() {
        let b = Beta::new(2.0, 2.0).unwrap();

        assert!((b.cdf(0.0) - 0.0).abs() < 1e-10);
        assert!((b.cdf(0.5) - 0.5).abs() < 1e-6); // Symmetric
        assert!((b.cdf(1.0) - 1.0).abs() < 1e-10);

        // CDF is monotonic
        assert!(b.cdf(0.3) < b.cdf(0.5));
        assert!(b.cdf(0.5) < b.cdf(0.7));
    }

    #[test]
    fn test_beta_ppf() {
        let b = Beta::new(2.0, 5.0).unwrap();

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

        assert!(b.ppf(-0.1).is_err());
        assert!(b.ppf(1.1).is_err());
    }

    #[test]
    fn test_beta_sf() {
        let b = Beta::new(2.0, 3.0).unwrap();

        // SF + CDF = 1
        for x in [0.2, 0.4, 0.6, 0.8] {
            assert!((b.sf(x) + b.cdf(x) - 1.0).abs() < 1e-10);
        }
    }
}