Skip to content

Commit 9380b90

Browse files
committed
---
yaml --- r: 94139 b: refs/heads/try c: 1ee4291 h: refs/heads/master i: 94137: 062e3eb 94135: fb46b95 v: v3
1 parent 6f9b59a commit 9380b90

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
5-
refs/heads/try: 1d986de2488249763f730ba83fd3d8235391e74d
5+
refs/heads/try: 1ee42912e1c83106082012e9c23090934be4ae69
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/rand/distributions/gamma.rs

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! The Gamma distribution.
11+
//! The Gamma and derived distributions.
1212
1313
use rand::{Rng, Open01};
1414
use super::{IndependentSample, Sample, Exp};
@@ -169,6 +169,103 @@ impl IndependentSample<f64> for GammaLargeShape {
169169
}
170170
}
171171

172+
/// The chi-squared distribution `χ²(k)`, where `k` is the degrees of
173+
/// freedom.
174+
///
175+
/// For `k > 0` integral, this distribution is the sum of the squares
176+
/// of `k` independent standard normal random variables. For other
177+
/// `k`, this uses the equivalent characterisation `χ²(k) = Gamma(k/2,
178+
/// 2)`.
179+
///
180+
/// # Example
181+
///
182+
/// ```rust
183+
/// use std::rand;
184+
/// use std::rand::distributions::{ChiSquared, IndependentSample};
185+
///
186+
/// fn main() {
187+
/// let chi = ChiSquared::new(11.0);
188+
/// let v = chi.ind_sample(&mut rand::task_rng());
189+
/// println!("{} is from a χ²(11) distribution", v)
190+
/// }
191+
/// ```
192+
pub enum ChiSquared {
193+
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
194+
// e.g. when alpha = 1/2 as it would be for this case, so special-
195+
// casing and using the definition of N(0,1)^2 is faster.
196+
priv DoFExactlyOne,
197+
priv DoFAnythingElse(Gamma)
198+
}
199+
200+
impl ChiSquared {
201+
/// Create a new chi-squared distribution with degrees-of-freedom
202+
/// `k`. Fails if `k < 0`.
203+
pub fn new(k: f64) -> ChiSquared {
204+
if k == 1.0 {
205+
DoFExactlyOne
206+
} else {
207+
assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
208+
DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
209+
}
210+
}
211+
}
212+
impl Sample<f64> for ChiSquared {
213+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
214+
}
215+
impl IndependentSample<f64> for ChiSquared {
216+
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
217+
match *self {
218+
DoFExactlyOne => {
219+
// k == 1 => N(0,1)^2
220+
let norm = *rng.gen::<StandardNormal>();
221+
norm * norm
222+
}
223+
DoFAnythingElse(ref g) => g.ind_sample(rng)
224+
}
225+
}
226+
}
227+
228+
#[cfg(test)]
229+
mod test {
230+
use rand::*;
231+
use super::*;
232+
use iter::range;
233+
use option::{Some, None};
234+
235+
#[test]
236+
fn test_chi_squared_one() {
237+
let mut chi = ChiSquared::new(1.0);
238+
let mut rng = task_rng();
239+
for _ in range(0, 1000) {
240+
chi.sample(&mut rng);
241+
chi.ind_sample(&mut rng);
242+
}
243+
}
244+
#[test]
245+
fn test_chi_squared_small() {
246+
let mut chi = ChiSquared::new(0.5);
247+
let mut rng = task_rng();
248+
for _ in range(0, 1000) {
249+
chi.sample(&mut rng);
250+
chi.ind_sample(&mut rng);
251+
}
252+
}
253+
#[test]
254+
fn test_chi_squared_large() {
255+
let mut chi = ChiSquared::new(30.0);
256+
let mut rng = task_rng();
257+
for _ in range(0, 1000) {
258+
chi.sample(&mut rng);
259+
chi.ind_sample(&mut rng);
260+
}
261+
}
262+
#[test]
263+
#[should_fail]
264+
fn test_log_normal_invalid_dof() {
265+
ChiSquared::new(-1.0);
266+
}
267+
}
268+
172269
#[cfg(test)]
173270
mod bench {
174271
use super::*;

branches/try/src/libstd/rand/distributions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rand::{Rng, Rand};
2727
use clone::Clone;
2828

2929
pub use self::range::Range;
30-
pub use self::gamma::Gamma;
30+
pub use self::gamma::{Gamma, ChiSquared};
3131
pub use self::normal::{Normal, LogNormal};
3232
pub use self::exponential::Exp;
3333

0 commit comments

Comments
 (0)