June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,20 @@
|
|||
using Distributions, IterTools
|
||||
|
||||
newv(n::Int, p::Float64) = rand(Bernoulli(p), n)
|
||||
runs(v::Vector{Int}) = sum((a & ~b) for (a, b) in zip(v, IterTools.chain(v[2:end], v[1])))
|
||||
|
||||
mrd(n::Int, p::Float64) = runs(newv(n, p)) / n
|
||||
|
||||
nrep = 500
|
||||
|
||||
for p in 0.1:0.2:1
|
||||
lim = p * (1 - p)
|
||||
|
||||
println()
|
||||
for ex in 10:2:14
|
||||
n = 2 ^ ex
|
||||
sim = mean(mrd.(n, p) for _ in 1:nrep)
|
||||
@printf("nrep = %3i\tp = %4.2f\tn = %5i\np · (1 - p) = %5.3f\tsim = %5.3f\tΔ = %3.1f%%\n",
|
||||
nrep, p, n, lim, sim, lim > 0 ? abs(sim - lim) / lim * 100 : sim * 100)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
// version 1.2.10
|
||||
|
||||
import java.util.Random
|
||||
|
||||
val rand = Random()
|
||||
const val RAND_MAX = 32767
|
||||
|
||||
// just generate 0s and 1s without storing them
|
||||
fun runTest(p: Double, len: Int, runs: Int): Double {
|
||||
var cnt = 0
|
||||
val thresh = (p * RAND_MAX).toInt()
|
||||
for (r in 0 until runs) {
|
||||
var x = 0
|
||||
var i = len
|
||||
while (i-- > 0) {
|
||||
val y = if (rand.nextInt(RAND_MAX + 1) < thresh) 1 else 0
|
||||
if (x < y) cnt++
|
||||
x = y
|
||||
}
|
||||
}
|
||||
return cnt.toDouble() / runs / len
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("running 1000 tests each:")
|
||||
println(" p\t n\tK\tp(1-p)\t diff")
|
||||
println("------------------------------------------------")
|
||||
val fmt = "%.1f\t%6d\t%.4f\t%.4f\t%+.4f (%+.2f%%)"
|
||||
for (ip in 1..9 step 2) {
|
||||
val p = ip / 10.0
|
||||
val p1p = p * (1.0 - p)
|
||||
var n = 100
|
||||
while (n <= 100_000) {
|
||||
val k = runTest(p, n, 1000)
|
||||
println(fmt.format(p, n, k, p1p, k - p1p, (k - p1p) / p1p * 100))
|
||||
n *= 10
|
||||
}
|
||||
println()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue