]> gitweb.factorcode.org Git - factor.git/commitdiff
random: adding rayleigh, gumbel, logistic, and power random floats.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 18 Apr 2012 21:37:27 +0000 (14:37 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 18 Apr 2012 21:37:27 +0000 (14:37 -0700)
basis/random/random-tests.factor
basis/random/random.factor

index f22b02a14731cecd2d2b4acf3203f48b534c0cf3..3bbcd943be4f3e81421cf23783d76a419fe4e4fa 100644 (file)
@@ -1,5 +1,5 @@
-USING: random sequences tools.test kernel math math.functions
-sets grouping random.private math.statistics ;
+USING: random sequences tools.test kernel math math.constants
+math.functions sets grouping random.private math.statistics ;
 IN: random.tests
 
 [ 4 ] [ 4 random-bytes length ] unit-test
@@ -76,3 +76,16 @@ IN: random.tests
     50000 [ 2 3 laplace-random-float ] replicate
     [ mean 2 .2 ~ ] [ std 2 sqrt 3 * .2 ~ ] bi
 ] unit-test
+
+{ t t }
+[
+    50000 [ 12 rayleigh-random-float ] replicate
+    [ mean pi 2 / sqrt 12 * .2 ~ ]
+    [ std 2 pi 2 / - sqrt 12 * .2 ~ ] bi
+] unit-test
+
+{ t t }
+[
+    50000 [ 3 4 logistic-random-float ] replicate
+    [ mean 3 .2 ~ ] [ std pi 4 * 3 sqrt / .2 ~ ] bi
+] unit-test
index cdce8e4afaf705626a6e71c3f116375a9e992e2f..a13f4065d7d3485c43aedc123e6475aa55b70ed5 100644 (file)
@@ -242,6 +242,18 @@ ERROR: too-many-samples seq n ;
 : inv-gamma-random-float ( shape scale -- n )
     recip gamma-random-float recip ;
 
+: rayleigh-random-float ( mode -- n )
+    random-unit log -2 * sqrt * ;
+
+: gumbel-random-float ( loc scale -- n )
+    random-unit log neg log * - ;
+
+: logistic-random-float ( loc scale -- n )
+    random-unit dup 1 swap - / log * + ;
+
+: power-random-float ( alpha -- n )
+    [ random-unit log exp 1 swap - ] dip recip ^ ;
+
 {
     { [ os windows? ] [ "random.windows" require ] }
     { [ os unix? ] [ "random.unix" require ] }