]> gitweb.factorcode.org Git - factor.git/commitdiff
random: adding laplace, cauchy, chi-square, student's t, and inverse gamma distributions.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 15 Apr 2012 04:00:19 +0000 (21:00 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 15 Apr 2012 04:00:19 +0000 (21:00 -0700)
basis/random/random.factor

index 107c090afd16c7b2bf151a1e74358011417ec96b..cdce8e4afaf705626a6e71c3f116375a9e992e2f 100644 (file)
@@ -225,6 +225,23 @@ ERROR: too-many-samples seq n ;
 : triangular-random-float ( low high -- n )
     2dup + 2 /f (triangular-random-float) ;
 
+: laplace-random-float ( mean scale -- n )
+    random-unit dup 0.5 <
+    [ 2 * log ] [ 1 swap - 2 * log neg ] if * + ;
+
+: cauchy-random-float ( median scale -- n )
+    random-unit 0.5 - pi * tan * + ;
+
+: chi-square-random-float ( dof -- n )
+    [ 0.5 ] dip 2 * gamma-random-float ;
+
+: student-t-random-float ( dof -- n )
+    [ 0 1 normal-random-float ] dip
+    [ chi-square-random-float ] [ / ] bi sqrt / ;
+
+: inv-gamma-random-float ( shape scale -- n )
+    recip gamma-random-float recip ;
+
 {
     { [ os windows? ] [ "random.windows" require ] }
     { [ os unix? ] [ "random.unix" require ] }