-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPoissonTests.swift
More file actions
26 lines (21 loc) · 965 Bytes
/
PoissonTests.swift
File metadata and controls
26 lines (21 loc) · 965 Bytes
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
// Copyright © 2016 Aidan Gomez.
//
// This file is part of RandKit. The full RandKit copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
import XCTest
import RandKit
class PoissonTests: XCTestCase {
func testPoisson() {
let n = 10000
let λ: ContinuousValue = uniform(DBL_MIN...250)
let samples: [DiscreteValue] = (0..<n).map({ _ in poisson(frequency: λ) })
let mean = Double(samples.reduce(0, combine: +)) / Double(n)
let varianceSum = { (current: Double, val: DiscreteValue) in
current + (Double(val) - mean) * (Double(val) - mean)
}
let variance = samples.reduce(0.0, combine: varianceSum) / Double(n)
XCTAssertEqualWithAccuracy(mean, λ, accuracy: 0.007 * Double(n))
XCTAssertEqualWithAccuracy(variance, λ, accuracy: 0.007 * Double(n))
}
}