main.js (2851B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var factory = require( './factory.js' ); 24 25 26 // MAIN // 27 28 /** 29 * Generates a standard normally distributed random number. 30 * 31 * ## Method 32 * 33 * The basic Ziggurat method works as follows: 34 * 35 * 36 * ```tex 37 * x_{C-1}(r) \left[ f(0) - f\left( x_{C-1}(r) \right) \right] - V(r) = 0 38 * ``` 39 * 40 * where 41 * 42 * ```tex 43 * V(r) = r \; f(r) + \int_r^\infty \; f(x) \; dx 44 * ``` 45 * 46 * and \\( r \\) denotes the right-most \\( x_1 \\). 47 * 48 * - We then use the following rejection algorithm: 49 * 50 * - Draw a box \\( B_i \\) at random with probability \\( \tfrac{1}{C} \\). 51 * - Draw a random number from the box as \\( z = U_0 x_i \\) for \\( i > 0 \\) and \\( z = U_0 V / f(x_1) \\). 52 * - If \\( z < x_{i+1} \\), accept \\( z \\). 53 * - If \\( i = 0 \\), accept a \\( v \\) by transforming the tail of the normal distribution to the unit interval and then use rejection technique by Marsaglia, G. (1964) to generate a standard normal variable. Otherwise, if \\( i > 0 \\) and \\( U_1 \left[ f(x_i) - f(x_{i+1})\right] < f(z) - f(x_{i+1}) \\) accept \\( z \\). 54 * - Go back to the first step. 55 * 56 * - The improved version by Doornik (2005) changes step four in order to correct a deficiency of the original Ziggurat algorithm. The updated version requires the generation of two random numbers, a uniform variable drawn from \\( U(-1,1) \\) and the last seven bits of a random integer. 57 * 58 * ## References 59 * 60 * - Doornik, Jurgen A. 2005. "An Improved Ziggurat Method to Generate Normal Random Samples." <https://www.doornik.com/research/ziggurat.pdf>. 61 * - Marsaglia, George, and Wai Wan Tsang. 2000. "The Ziggurat Method for Generating Random Variables." _Journal of Statistical Software_ 5 (1): 1–7. doi:[10.18637/jss.v005.i08](http://dx.doi.org/10.18637/jss.v005.i08). 62 * - Marsaglia, George. 1964. "Generating a Variable from the Tail of the Normal Distribution." _Technometrics_ 6 (1): 101–2. doi:[10.1080/00401706.1964.10490150](http://dx.doi.org/10.1080/00401706.1964.10490150). 63 * 64 * 65 * @name randn 66 * @type {PRNG} 67 * @returns {number} pseudorandom number 68 * 69 * @example 70 * var r = randn(); 71 * // returns <number> 72 */ 73 var randn = factory(); 74 75 76 // EXPORTS // 77 78 module.exports = randn;