rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use crate::distributions::{Distribution, DistributionTrait, DistributionUtils};
use crate::error::{RusTorchError, RusTorchResult};
/// Beta Distribution - torch.distributions.Beta compatible
/// ベータ分布 - torch.distributions.Beta互換
///
/// Implements the Beta distribution with concentration parameters α and β.
/// 濃度パラメータαとβを持つベータ分布を実装
use crate::tensor::Tensor;
use num_traits::Float;
use std::marker::PhantomData;

/// Beta Distribution
/// ベータ分布
///
/// The Beta distribution is defined by:
/// - concentration1 (α): first shape parameter (must be positive)
/// - concentration0 (β): second shape parameter (must be positive)
///
/// PDF: f(x) = (Γ(α+β) / (Γ(α)Γ(β))) * x^(α-1) * (1-x)^(β-1) for x ∈ \[0,1\]
#[derive(Debug, Clone)]
pub struct Beta<T: Float> {
    /// First concentration parameter (α)
    /// 第一濃度パラメータ (α)
    pub concentration1: Tensor<T>,

    /// Second concentration parameter (β)
    /// 第二濃度パラメータ (β)
    pub concentration0: Tensor<T>,

    /// Base distribution properties
    /// 基本分布特性
    pub base: Distribution,

    /// Phantom data for type parameter
    /// 型パラメータ用のファントムデータ
    _phantom: PhantomData<T>,
}

impl<T: Float + 'static> Beta<T>
where
    T: rand::distributions::uniform::SampleUniform + num_traits::FromPrimitive + std::fmt::Display,
{
    /// Create a new Beta distribution
    /// 新しいベータ分布を作成
    ///
    /// # Arguments
    /// * `concentration1` - First concentration (α) parameter tensor
    /// * `concentration0` - Second concentration (β) parameter tensor
    /// * `validate_args` - Whether to validate parameters
    pub fn new(
        concentration1: Tensor<T>,
        concentration0: Tensor<T>,
        validate_args: bool,
    ) -> RusTorchResult<Self> {
        if validate_args {
            DistributionUtils::validate_positive(&concentration1, "concentration1")?;
            DistributionUtils::validate_positive(&concentration0, "concentration0")?;
        }

        // Determine batch shape from broadcasting concentration parameters
        let batch_shape =
            Distribution::broadcast_shapes(concentration1.shape(), concentration0.shape())?;
        let event_shape = vec![]; // Beta is a univariate distribution

        Ok(Self {
            concentration1,
            concentration0,
            base: Distribution::new(batch_shape, event_shape, validate_args),
            _phantom: PhantomData,
        })
    }

    /// Create Beta distribution with scalar parameters
    /// スカラーパラメータでベータ分布を作成
    pub fn from_scalars(alpha: T, beta: T, validate_args: bool) -> RusTorchResult<Self> {
        let alpha_tensor = Tensor::from_vec(vec![alpha], vec![]);
        let beta_tensor = Tensor::from_vec(vec![beta], vec![]);
        Self::new(alpha_tensor, beta_tensor, validate_args)
    }

    /// Uniform Beta distribution (α=1, β=1) - equivalent to Uniform(0,1)
    /// 一様ベータ分布 (α=1, β=1) - Uniform(0,1)と等価
    pub fn uniform(validate_args: bool) -> RusTorchResult<Self> {
        Self::from_scalars(T::one(), T::one(), validate_args)
    }

    /// Symmetric Beta distribution with equal parameters (α=β)
    /// 等しいパラメータを持つ対称ベータ分布 (α=β)
    pub fn symmetric(concentration: T, validate_args: bool) -> RusTorchResult<Self> {
        Self::from_scalars(concentration, concentration, validate_args)
    }

    /// Log-beta function: log(B(α, β)) = log(Γ(α)) + log(Γ(β)) - log(Γ(α+β))
    /// ログベータ関数: log(B(α, β)) = log(Γ(α)) + log(Γ(β)) - log(Γ(α+β))
    fn log_beta(&self) -> RusTorchResult<Tensor<T>> {
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        let result_data: Vec<T> = alpha_data
            .iter()
            .zip(beta_data.iter().cycle())
            .map(|(&a, &b)| {
                // Use Stirling's approximation for log-gamma
                Self::log_gamma_stirling(a) + Self::log_gamma_stirling(b)
                    - Self::log_gamma_stirling(a + b)
            })
            .collect();

        Ok(Tensor::from_vec(
            result_data,
            self.concentration1.shape().to_vec(),
        ))
    }

    /// Log-gamma function using Stirling's approximation
    /// スターリング近似を使ったログガンマ関数
    fn log_gamma_stirling(x: T) -> T {
        if x < T::one() {
            // For x < 1, use recurrence relation: Γ(x) = Γ(x+1) / x
            Self::log_gamma_stirling(x + T::one()) - x.ln()
        } else if x < T::from(12.0).unwrap() {
            // Use series expansion for better accuracy
            let mut result = T::zero();
            let mut term = x;

            // Use recurrence to get to x >= 12
            while term < T::from(12.0).unwrap() {
                result = result - term.ln();
                term = term + T::one();
            }

            result + Self::stirling_approx(term)
        } else {
            Self::stirling_approx(x)
        }
    }

    /// Stirling's approximation for log(Γ(x))
    /// log(Γ(x))のスターリング近似
    fn stirling_approx(x: T) -> T {
        let half = T::from(0.5).unwrap();
        let two = T::from(2.0).unwrap();
        let pi = T::from(std::f64::consts::PI).unwrap();
        let twelve = T::from(12.0).unwrap();

        // Stirling: log(Γ(x)) ≈ (x - 0.5) * log(x) - x + 0.5 * log(2π) + 1/(12x)
        (x - half) * x.ln() - x + half * (two * pi).ln() + T::one() / (twelve * x)
    }

    /// Digamma function (ψ) - derivative of log-gamma
    /// ディガンマ関数 (ψ) - ログガンマの導関数
    fn digamma(x: T) -> T {
        if x < T::from(6.0).unwrap() {
            // For small x, use recurrence relation
            Self::digamma(x + T::one()) - T::one() / x
        } else {
            // Asymptotic expansion for large x
            let inv_x = T::one() / x;
            let inv_x_sq = inv_x * inv_x;

            x.ln() - T::from(0.5).unwrap() * inv_x - inv_x_sq / T::from(12.0).unwrap()
                + inv_x_sq * inv_x_sq / T::from(120.0).unwrap()
        }
    }

    /// Sample using rejection sampling (for general case)
    /// 棄却サンプリングを使用してサンプル生成(一般的なケース)
    fn sample_rejection(&self, shape: &[usize]) -> RusTorchResult<Tensor<T>> {
        let total_size: usize = shape.iter().product();
        let mut samples = Vec::with_capacity(total_size);

        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        for i in 0..total_size {
            let alpha = alpha_data[i % alpha_data.len()];
            let beta = beta_data[i % beta_data.len()];

            // For beta distribution, we can use gamma sampling: X = G1/(G1+G2)
            // where G1 ~ Gamma(α,1) and G2 ~ Gamma(β,1)
            let g1 = Self::sample_gamma_marsaglia(alpha);
            let g2 = Self::sample_gamma_marsaglia(beta);

            samples.push(g1 / (g1 + g2));
        }

        Ok(Tensor::from_vec(samples, shape.to_vec()))
    }

    /// Sample from Gamma distribution using Marsaglia-Tsang method
    /// マルサグリア・ツァング法を使ってガンマ分布からサンプル
    fn sample_gamma_marsaglia(shape: T) -> T {
        if shape < T::one() {
            // For shape < 1, use transformation: if Y ~ Gamma(shape+1), then Y * U^(1/shape) ~ Gamma(shape)
            let y = Self::sample_gamma_marsaglia(shape + T::one());
            let u = DistributionUtils::random_uniform_scalar::<T>();
            y * u.powf(T::one() / shape)
        } else {
            // Marsaglia-Tsang algorithm for shape >= 1
            #[allow(clippy::many_single_char_names)]
            // Mathematical variables d, c, x, v, u are conventional in statistical algorithms
            {
                let d = shape - T::from(1.0 / 3.0).unwrap();
                let c = T::one() / (T::from(9.0).unwrap() * d).sqrt();

                loop {
                    let mut x = DistributionUtils::random_normal_scalar::<T>();
                    let v = (T::one() + c * x).powi(3);

                    if v <= T::zero() {
                        continue;
                    }

                    x = x * x;
                    let u = DistributionUtils::random_uniform_scalar::<T>();

                    if u < T::one() - T::from(0.0331).unwrap() * x * x {
                        return d * v;
                    }

                    if u.ln() < T::from(0.5).unwrap() * x + d * (T::one() - v + v.ln()) {
                        return d * v;
                    }
                }
            }
        }
    }
}

impl<T: Float + 'static> DistributionTrait<T> for Beta<T>
where
    T: rand::distributions::uniform::SampleUniform + num_traits::FromPrimitive + std::fmt::Display,
{
    fn sample(&self, shape: Option<&[usize]>) -> RusTorchResult<Tensor<T>> {
        let sample_shape = self.base.expand_shape(shape);
        self.sample_rejection(&sample_shape)
    }

    fn log_prob(&self, value: &Tensor<T>) -> RusTorchResult<Tensor<T>> {
        let value_data = value.data.as_slice().unwrap();
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        let log_beta_vals = self.log_beta()?;
        let log_beta_data = log_beta_vals.data.as_slice().unwrap();

        let neg_inf = T::neg_infinity();
        let result_data: Vec<T> = value_data
            .iter()
            .zip(alpha_data.iter().cycle())
            .zip(beta_data.iter().cycle())
            .zip(log_beta_data.iter().cycle())
            .map(|(((&v, &a), &b), &lb)| {
                if v <= T::zero() || v >= T::one() {
                    neg_inf
                } else {
                    // log p(x) = (α-1)*log(x) + (β-1)*log(1-x) - log(B(α,β))
                    (a - T::one()) * v.ln() + (b - T::one()) * (T::one() - v).ln() - lb
                }
            })
            .collect();

        Ok(Tensor::from_vec(result_data, value.shape().to_vec()))
    }

    fn cdf(&self, value: &Tensor<T>) -> RusTorchResult<Tensor<T>> {
        let value_data = value.data.as_slice().unwrap();
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        let result_data: Vec<T> = value_data
            .iter()
            .zip(alpha_data.iter().cycle())
            .zip(beta_data.iter().cycle())
            .map(|((&v, &a), &b)| {
                if v <= T::zero() {
                    T::zero()
                } else if v >= T::one() {
                    T::one()
                } else {
                    // Use incomplete beta function approximation
                    Self::incomplete_beta_approx(v, a, b)
                }
            })
            .collect();

        Ok(Tensor::from_vec(result_data, value.shape().to_vec()))
    }

    fn icdf(&self, _value: &Tensor<T>) -> RusTorchResult<Tensor<T>> {
        // ICDF for Beta distribution requires numerical methods
        Err(RusTorchError::UnsupportedOperation(
            "ICDF for Beta distribution not implemented - requires numerical methods",
        ))
    }

    fn mean(&self) -> RusTorchResult<Tensor<T>> {
        // Mean = α / (α + β)
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        let mean_data: Vec<T> = alpha_data
            .iter()
            .zip(beta_data.iter().cycle())
            .map(|(&a, &b)| a / (a + b))
            .collect();

        Ok(Tensor::from_vec(
            mean_data,
            self.concentration1.shape().to_vec(),
        ))
    }

    fn variance(&self) -> RusTorchResult<Tensor<T>> {
        // Variance = (α*β) / ((α+β)²*(α+β+1))
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();

        let var_data: Vec<T> = alpha_data
            .iter()
            .zip(beta_data.iter().cycle())
            .map(|(&a, &b)| {
                let sum = a + b;
                (a * b) / (sum * sum * (sum + T::one()))
            })
            .collect();

        Ok(Tensor::from_vec(
            var_data,
            self.concentration1.shape().to_vec(),
        ))
    }

    fn entropy(&self) -> RusTorchResult<Tensor<T>> {
        let alpha_data = self.concentration1.data.as_slice().unwrap();
        let beta_data = self.concentration0.data.as_slice().unwrap();
        let log_beta_vals = self.log_beta()?;
        let log_beta_data = log_beta_vals.data.as_slice().unwrap();

        let result_data: Vec<T> = alpha_data
            .iter()
            .zip(beta_data.iter().cycle())
            .zip(log_beta_data.iter().cycle())
            .map(|((&a, &b), &lb)| {
                let sum = a + b;
                // Entropy = log(B(α,β)) - (α-1)*ψ(α) - (β-1)*ψ(β) + (α+β-2)*ψ(α+β)
                lb - (a - T::one()) * Self::digamma(a) - (b - T::one()) * Self::digamma(b)
                    + (sum - T::from(2.0).unwrap()) * Self::digamma(sum)
            })
            .collect();

        Ok(Tensor::from_vec(
            result_data,
            self.concentration1.shape().to_vec(),
        ))
    }
}

impl<T: Float + 'static> Beta<T>
where
    T: rand::distributions::uniform::SampleUniform + num_traits::FromPrimitive + std::fmt::Display,
{
    /// Incomplete beta function approximation
    /// 不完全ベータ関数近似
    fn incomplete_beta_approx(x: T, a: T, b: T) -> T {
        // Simple continued fraction approximation for incomplete beta
        let mut cf = T::one();
        let mut d = T::one() / (T::one() - (a + b) * x / (a + T::one()));
        cf = cf * d;

        for m in 1..50 {
            let m_f = T::from(m).unwrap();
            let two_m = T::from(2 * m).unwrap();

            let num = m_f * (b - m_f) * x / ((a + two_m - T::one()) * (a + two_m));
            d = T::one() / (T::one() + num * d);
            cf = cf * d;

            let num2 = -(a + m_f) * (a + b + m_f) * x / ((a + two_m) * (a + two_m + T::one()));
            d = T::one() / (T::one() + num2 * d);
            cf = cf * d;
        }

        let beta_approx =
            x.powf(a) * (T::one() - x).powf(b) / (a * Self::beta_function_approx(a, b));
        beta_approx * cf
    }

    /// Beta function approximation
    /// ベータ関数近似
    fn beta_function_approx(a: T, b: T) -> T {
        (Self::log_gamma_stirling(a) + Self::log_gamma_stirling(b)
            - Self::log_gamma_stirling(a + b))
        .exp()
    }
}

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

    #[test]
    fn test_beta_creation() {
        let alpha = Tensor::from_vec([2.0f32].to_vec(), vec![1]);
        let beta = Tensor::from_vec([3.0f32].to_vec(), vec![1]);

        let distribution = Beta::new(alpha, beta, true).unwrap();
        assert_eq!(distribution.base.batch_shape, vec![1]);
        assert_eq!(distribution.base.event_shape, Vec::<usize>::new());
    }

    #[test]
    fn test_uniform_beta() {
        let beta = Beta::<f32>::uniform(true).unwrap();
        let mean = beta.mean().unwrap();
        let var = beta.variance().unwrap();

        // For Beta(1,1): mean = 1/2, variance = 1/12
        assert_abs_diff_eq!(mean.data.as_slice().unwrap()[0], 0.5, epsilon = 1e-6);
        assert_abs_diff_eq!(var.data.as_slice().unwrap()[0], 1.0 / 12.0, epsilon = 1e-6);
    }

    #[test]
    fn test_beta_sampling() {
        let beta = Beta::<f32>::from_scalars(2.0, 5.0, true).unwrap();
        let samples = beta.sample(Some(&[1000])).unwrap();

        assert_eq!(samples.shape(), &[1000]);

        // Check all samples are in [0, 1] range
        let data = samples.data.as_slice().unwrap();
        for &x in data {
            assert!(x > 0.0 && x < 1.0);
        }

        // Basic statistical test - mean should be α/(α+β) = 2/7 ≈ 0.286
        let sample_mean: f32 = data.iter().sum::<f32>() / data.len() as f32;
        assert_abs_diff_eq!(sample_mean, 2.0 / 7.0, epsilon = 0.05);
    }

    #[test]
    fn test_beta_mean_variance() {
        let beta = Beta::<f32>::from_scalars(3.0, 7.0, true).unwrap();

        let mean = beta.mean().unwrap();
        let var = beta.variance().unwrap();

        // Mean = α/(α+β) = 3/10 = 0.3
        assert_abs_diff_eq!(mean.data.as_slice().unwrap()[0], 0.3, epsilon = 1e-6);

        // Variance = αβ/((α+β)²(α+β+1)) = 3*7/(10²*11) = 21/1100 = 0.019...
        let expected_var = 3.0 * 7.0 / (10.0 * 10.0 * 11.0);
        assert_abs_diff_eq!(
            var.data.as_slice().unwrap()[0],
            expected_var,
            epsilon = 1e-6
        );
    }

    #[test]
    fn test_beta_log_prob() {
        let beta = Beta::<f32>::uniform(true).unwrap(); // Beta(1,1) = Uniform(0,1)
        let values = Tensor::from_vec([-0.1f32, 0.5, 1.1].to_vec(), vec![3]);

        let log_probs = beta.log_prob(&values).unwrap();
        let log_prob_data = log_probs.data.as_slice().unwrap();

        // For Beta(1,1), log p(x) = 0 for x ∈ (0,1), -∞ otherwise
        assert_eq!(log_prob_data[0], f32::NEG_INFINITY); // x = -0.1 < 0
        assert_abs_diff_eq!(log_prob_data[1], 0.0, epsilon = 1e-5); // x = 0.5
        assert_eq!(log_prob_data[2], f32::NEG_INFINITY); // x = 1.1 > 1
    }

    #[test]
    fn test_symmetric_beta() {
        let beta = Beta::<f32>::symmetric(2.0, true).unwrap();
        let mean = beta.mean().unwrap();

        // For symmetric Beta(a,a), mean = a/(2a) = 1/2
        assert_abs_diff_eq!(mean.data.as_slice().unwrap()[0], 0.5, epsilon = 1e-6);
    }

    #[test]
    fn test_invalid_parameters() {
        // Test negative concentration parameters
        assert!(Beta::<f32>::from_scalars(-1.0, 2.0, true).is_err());
        assert!(Beta::<f32>::from_scalars(2.0, -1.0, true).is_err());
        assert!(Beta::<f32>::from_scalars(0.0, 1.0, true).is_err());
    }
}