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
//! F distribution (Fisher-Snedecor).
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;

/// F distribution (Fisher-Snedecor distribution).
///
/// The F distribution with d1 and d2 degrees of freedom has PDF:
///
/// f(x) = √((d1*x)^d1 * d2^d2 / (d1*x + d2)^(d1+d2)) / (x * B(d1/2, d2/2))
///
/// The F distribution arises as the ratio of two chi-squared random variables
/// divided by their degrees of freedom.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use solvr::stats::{FDistribution, ContinuousDistribution};
///
/// // ANOVA F-test with 3 and 20 degrees of freedom
/// let f = FDistribution::new(3.0, 20.0)?;
/// let f_stat = 3.5;
/// let p_value = f.sf(f_stat); // Right-tail probability
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct FDistribution {
    /// Numerator degrees of freedom (d1)
    d1: f64,
    /// Denominator degrees of freedom (d2)
    d2: f64,
    /// Log of normalizing constant
    log_norm: f64,
}

impl FDistribution {
    /// Create a new F distribution.
    ///
    /// # Arguments
    ///
    /// * `d1` - Numerator degrees of freedom (must be positive)
    /// * `d2` - Denominator degrees of freedom (must be positive)
    ///
    /// # Errors
    ///
    /// Returns an error if either parameter is not positive.
    pub fn new(d1: f64, d2: f64) -> StatsResult<Self> {
        if d1 <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "d1".to_string(),
                value: d1,
                reason: "numerator df must be positive".to_string(),
            });
        }
        if d2 <= 0.0 {
            return Err(StatsError::InvalidParameter {
                name: "d2".to_string(),
                value: d2,
                reason: "denominator df must be positive".to_string(),
            });
        }
        if !d1.is_finite() || !d2.is_finite() {
            return Err(StatsError::InvalidParameter {
                name: "d1/d2".to_string(),
                value: d1,
                reason: "parameters must be finite".to_string(),
            });
        }

        // log_norm = (d1/2)*ln(d1) + (d2/2)*ln(d2) - ln(B(d1/2, d2/2))
        let log_norm =
            (d1 / 2.0) * d1.ln() + (d2 / 2.0) * d2.ln() - special::lbeta(d1 / 2.0, d2 / 2.0);

        Ok(Self { d1, d2, log_norm })
    }

    /// Get the numerator degrees of freedom.
    pub fn dfn(&self) -> f64 {
        self.d1
    }

    /// Get the denominator degrees of freedom.
    pub fn dfd(&self) -> f64 {
        self.d2
    }
}

impl Distribution for FDistribution {
    fn mean(&self) -> f64 {
        if self.d2 > 2.0 {
            self.d2 / (self.d2 - 2.0)
        } else {
            f64::NAN
        }
    }

    fn var(&self) -> f64 {
        if self.d2 > 4.0 {
            let num = 2.0 * self.d2 * self.d2 * (self.d1 + self.d2 - 2.0);
            let denom = self.d1 * (self.d2 - 2.0).powi(2) * (self.d2 - 4.0);
            num / denom
        } else {
            f64::NAN
        }
    }

    fn entropy(&self) -> f64 {
        let half_d1 = self.d1 / 2.0;
        let half_d2 = self.d2 / 2.0;
        let half_sum = (self.d1 + self.d2) / 2.0;

        (self.d1 / self.d2).ln()
            + special::lbeta(half_d1, half_d2)
            + (1.0 - half_d1) * special::digamma(half_d1)
            - (1.0 + half_d2) * special::digamma(half_d2)
            + half_sum * special::digamma(half_sum)
    }

    fn median(&self) -> f64 {
        self.ppf(0.5).unwrap_or(self.mean())
    }

    fn mode(&self) -> f64 {
        if self.d1 > 2.0 {
            ((self.d1 - 2.0) / self.d1) * (self.d2 / (self.d2 + 2.0))
        } else {
            0.0
        }
    }

    fn skewness(&self) -> f64 {
        if self.d2 > 6.0 {
            let num = (2.0 * self.d1 + self.d2 - 2.0) * (8.0 * (self.d2 - 4.0)).sqrt();
            let denom = (self.d2 - 6.0) * (self.d1 * (self.d1 + self.d2 - 2.0)).sqrt();
            num / denom
        } else {
            f64::NAN
        }
    }

    fn kurtosis(&self) -> f64 {
        if self.d2 > 8.0 {
            let a = self.d1;
            let b = self.d2;
            let num = 12.0 * a * (5.0 * b - 22.0) * (a + b - 2.0) + (b - 4.0) * (b - 2.0).powi(2);
            let denom = a * (b - 6.0) * (b - 8.0) * (a + b - 2.0);
            12.0 * num / denom
        } else {
            f64::NAN
        }
    }
}

impl ContinuousDistribution for FDistribution {
    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 half_d1 = self.d1 / 2.0;
        let half_d2 = self.d2 / 2.0;

        self.log_norm + (half_d1 - 1.0) * x.ln()
            - (half_d1 + half_d2) * (self.d1 * x + self.d2).ln()
    }

    fn cdf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return 0.0;
        }
        // CDF = I_{d1*x/(d1*x+d2)}(d1/2, d2/2)
        let t = self.d1 * x / (self.d1 * x + self.d2);
        special::betainc(self.d1 / 2.0, self.d2 / 2.0, t)
    }

    fn sf(&self, x: f64) -> f64 {
        if x <= 0.0 {
            return 1.0;
        }
        // SF = I_{d2/(d1*x+d2)}(d2/2, d1/2)
        let t = self.d2 / (self.d1 * x + self.d2);
        special::betainc(self.d2 / 2.0, self.d1 / 2.0, t)
    }

    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);
        }

        // Solve: I_t(d1/2, d2/2) = p for t, then x = d2*t / (d1*(1-t))
        let t = special::betaincinv(self.d1 / 2.0, self.d2 / 2.0, p);
        let x = self.d2 * t / (self.d1 * (1.0 - t));

        Ok(x)
    }

    // ========================================================================
    // 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 + (d1/2 - 1)*ln(x) - (d1/2 + d2/2)*ln(d1*x + d2)
        // 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 + (d1/2 - 1)*ln(x) - (d1/2 + d2/2)*ln(d1*x + d2)
        let ln_x = client.log(x)?;
        let term1 = client.mul_scalar(&ln_x, self.d1 / 2.0 - 1.0)?;

        let d1_x = client.mul_scalar(x, self.d1)?;
        let d1_x_plus_d2 = client.add_scalar(&d1_x, self.d2)?;
        let ln_term = client.log(&d1_x_plus_d2)?;
        let term2 = client.mul_scalar(&ln_term, -(self.d1 / 2.0 + self.d2 / 2.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 = I_{d1*x/(d1*x+d2)}(d1/2, d2/2)
        let d1_x = client.mul_scalar(x, self.d1)?;
        let d1_x_plus_d2 = client.add_scalar(&d1_x, self.d2)?;
        let t = client.div(&d1_x, &d1_x_plus_d2)?;

        let a = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.d1 / 2.0, client.device());
        let b = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.d2 / 2.0, client.device());
        client.betainc(&a, &b, &t)
    }

    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 = I_{d2/(d1*x+d2)}(d2/2, d1/2)
        let d1_x = client.mul_scalar(x, self.d1)?;
        let d1_x_plus_d2 = client.add_scalar(&d1_x, self.d2)?;
        let d2_tensor = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.d2, client.device());
        let t = client.div(&d2_tensor, &d1_x_plus_d2)?;

        let a = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.d2 / 2.0, client.device());
        let b = Tensor::<R>::full_scalar(x.shape(), x.dtype(), self.d1 / 2.0, client.device());
        client.betainc(&a, &b, &t)
    }

    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(...))
        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>,
    {
        // x = d2*t / (d1*(1-t)) where t = betaincinv(d1/2, d2/2, p)
        let a = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.d1 / 2.0, client.device());
        let b = Tensor::<R>::full_scalar(p.shape(), p.dtype(), self.d2 / 2.0, client.device());
        let t = client.betaincinv(&a, &b, p)?;

        // 1 - t
        let neg_t = client.mul_scalar(&t, -1.0)?;
        let one_minus_t = client.add_scalar(&neg_t, 1.0)?;

        let numerator = client.mul_scalar(&t, self.d2)?;
        let denominator = client.mul_scalar(&one_minus_t, self.d1)?;
        client.div(&numerator, &denominator)
    }

    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 neg_p = client.mul_scalar(p, -1.0)?;
        let one_minus_p = client.add_scalar(&neg_p, 1.0)?;
        self.ppf_tensor(&one_minus_p, client)
    }
}

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

    #[test]
    fn test_f_creation() {
        let f = FDistribution::new(5.0, 10.0).unwrap();
        assert!((f.dfn() - 5.0).abs() < 1e-10);
        assert!((f.dfd() - 10.0).abs() < 1e-10);

        assert!(FDistribution::new(0.0, 10.0).is_err());
        assert!(FDistribution::new(5.0, 0.0).is_err());
        assert!(FDistribution::new(-1.0, 10.0).is_err());
    }

    #[test]
    fn test_f_moments() {
        let f = FDistribution::new(5.0, 10.0).unwrap();

        // Mean = d2 / (d2 - 2) = 10/8 = 1.25
        assert!((f.mean() - 1.25).abs() < 1e-10);

        // Mode = ((d1-2)/d1) * (d2/(d2+2)) = (3/5) * (10/12) = 0.5
        assert!((f.mode() - 0.5).abs() < 1e-10);

        // Test low df cases
        let f = FDistribution::new(2.0, 3.0).unwrap();
        // d2 = 3 > 2, so mean exists: d2/(d2-2) = 3/1 = 3
        assert!((f.mean() - 3.0).abs() < 1e-10);

        // Mean is undefined when d2 <= 2
        let f = FDistribution::new(5.0, 2.0).unwrap();
        assert!(f.mean().is_nan());
    }

    #[test]
    fn test_f_cdf() {
        let f = FDistribution::new(5.0, 10.0).unwrap();

        assert!((f.cdf(0.0) - 0.0).abs() < 1e-10);

        // F(5,10) critical value at p=0.95 is approximately 3.33
        assert!((f.cdf(3.33) - 0.95).abs() < 0.01);
    }

    #[test]
    fn test_f_ppf() {
        let f = FDistribution::new(5.0, 10.0).unwrap();

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

        // Critical value F(5,10, 0.95) ≈ 3.33
        assert!((f.ppf(0.95).unwrap() - 3.33).abs() < 0.1);
    }

    #[test]
    fn test_f_sf() {
        let f = FDistribution::new(5.0, 10.0).unwrap();

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