-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid calling
log(0)
when generating gaussian random variables (#662)
* owl_stats_ziggurat: avoid log(0) sfmt_f64_1 is documented to include 0, which would result in `log(0) = neg_infinity`. use sfmt_f64_3 instead which is documented to return `(0, 1)` instead. This should also match the paper, which says "UNI floats it to (0,1)": > Marsaglia, George, and Wai Wan Tsang. > "The ziggurat method for generating random variables." > Journal of statistical software 5 (2000): 1-7. See #661 Signed-off-by: Edwin Török <[email protected]> * Owl_base_stats_dist_uniform: add a function that returns a random float in (0,1) Signed-off-by: Edwin Török <[email protected]> * Owl_base_stats_dist_gaussian.{std_gaussian_rvs,gaussian_rvs}: avoid infinity on Random.float returning 0 #661 Signed-off-by: Edwin Török <[email protected]> --------- Signed-off-by: Edwin Török <[email protected]>
- Loading branch information
1 parent
c66069e
commit 621cbff
Showing
3 changed files
with
15 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* OWL - OCaml Scientific Computing | ||
* Copyright (c) 2016-2022 Liang Wang <[email protected]> | ||
*) | ||
|
||
open Owl_base_stats_dist_uniform | ||
let _u1 = ref 0. | ||
|
||
let _u2 = ref 0. | ||
|
@@ -20,8 +20,8 @@ let std_gaussian_rvs () = | |
!_z1) | ||
else ( | ||
_case := true; | ||
_u1 := Random.float 1.; | ||
_u2 := Random.float 1.; | ||
_u1 := rand01_exclusive (); | ||
_u2 := rand01_exclusive (); | ||
_z0 := sqrt (~-.2. *. log !_u1) *. cos (2. *. Owl_const.pi *. !_u2); | ||
_z1 := sqrt (~-.2. *. log !_u1) *. sin (2. *. Owl_const.pi *. !_u2); | ||
!_z0) | ||
|
@@ -35,8 +35,8 @@ let gaussian_rvs ~mu ~sigma = | |
mu +. (sigma *. !_z1)) | ||
else ( | ||
_case := true; | ||
_u1 := Random.float 1.; | ||
_u2 := Random.float 1.; | ||
_u1 := rand01_exclusive (); | ||
_u2 := rand01_exclusive (); | ||
_z0 := sqrt (~-.2. *. log !_u1) *. cos (2. *. Owl_const.pi *. !_u2); | ||
_z1 := sqrt (~-.2. *. log !_u1) *. sin (2. *. Owl_const.pi *. !_u2); | ||
mu +. (sigma *. !_z0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters