One occasionally needs a normal random variable whilst doing the Javascript, and whilst there's a Math function for uniform rv (Math.random(), to wit), the WWW or the Freemasons or somebody didn't provide a function for Gaussian rv (fer cat's sake, dudes: Math.randomn(). How hard would that have been?).
Anyhoo, I'm sure there's something in math.js for this, if you're not into the whole brevity thing. OTOP, you could cut and paste about 10 lines of code into your app to do the deed.
Where can one find such magical lines of code?
It's funny you asked.
Here's a liddle' function I wrote for generating a Gaussian random variable in Javascript (otherwise known as generating a normal random variable in Javascript, otherwise known as generating a standard normal random variable in Javascript, otherwise known as packing your post with keytext the Google rankbots might latch onto). This uses the Box-Muller method (otherwise known as the polar method). There's a nice explanation of Box-Muller in Ross; a somewhat less-nice explanation in Press:
The function returns a standard normal rv -- that is, Gaussian distributed with mean zero and unit variance. You can convert this to a Gaussian rv having arbitrary mean and variance using the following result from probability theory: X ~ N(0,1) => [aX+b] ~ N(b,a^2).
In code form:
Easy peasy.
Anyhoo, I'm sure there's something in math.js for this, if you're not into the whole brevity thing. OTOP, you could cut and paste about 10 lines of code into your app to do the deed.
Where can one find such magical lines of code?
It's funny you asked.
Here's a liddle' function I wrote for generating a Gaussian random variable in Javascript (otherwise known as generating a normal random variable in Javascript, otherwise known as generating a standard normal random variable in Javascript, otherwise known as packing your post with keytext the Google rankbots might latch onto). This uses the Box-Muller method (otherwise known as the polar method). There's a nice explanation of Box-Muller in Ross; a somewhat less-nice explanation in Press:
function standard_normal_rv()
{
var v1 = 2 * Math.random() - 1;
var v2 = 2 * Math.random() - 1;
var s = v1*v1 + v2*v2;
while (s > 1) {
v1 = 2 * Math.random() - 1;
v2 = 2 * Math.random() - 1;
s = v1*v1 + v2*v2;
}
return v1 * (Math.sqrt(-2*Math.log(s)/s));
}
{
var v1 = 2 * Math.random() - 1;
var v2 = 2 * Math.random() - 1;
var s = v1*v1 + v2*v2;
while (s > 1) {
v1 = 2 * Math.random() - 1;
v2 = 2 * Math.random() - 1;
s = v1*v1 + v2*v2;
}
return v1 * (Math.sqrt(-2*Math.log(s)/s));
}
The function returns a standard normal rv -- that is, Gaussian distributed with mean zero and unit variance. You can convert this to a Gaussian rv having arbitrary mean and variance using the following result from probability theory: X ~ N(0,1) => [aX+b] ~ N(b,a^2).
In code form:
function normal_rv(mean,variance)
{
return (Math.sqrt(variance)*standard_normal_rv() + mean);
}
{
return (Math.sqrt(variance)*standard_normal_rv() + mean);
}
Easy peasy.
No comments:
Post a Comment