tags:

views:

63

answers:

4

From a paper I'm reading right know:

...
S(t+1, k) = S(t, k) + ... + C*∆
...
∆ is a standard random variable with mean 0 and variance 1.
...

How to generate this series of random values with this mean and variance? If someone has links to a C or C++ library I would be glad but I wouldn't mind implementing it myself if someone tells me how to do it :)

+2  A: 

You can use the Box-Muller transform.

Suppose U1 and U2 are independent random variables that are uniformly distributed in the interval (0, 1]. Let

and

Then Z0 and Z1 are independent random variables with a normal distribution of standard deviation 1.

Mark Byers
@Mark He didnt say he needs a normal/Gaussian distribution, so just scaling up uniform would be less work. But if he needs Gaussian your's is the way to go
srean
+2  A: 

Do you have any restrictions on the distribution of \Delta ? if not you can just use a uniform distribution in [-sqrt(3), sqrt(3)]. The reason why this would work is because for an uniform distribution [a,b] the variance is 1/(12) (b-a)^2.

srean
@srean The paper I'm trying to replicate the results right now doesn't specify any kind of restriction on the distribution of ∆.
Vitor Py
Then what I suggested would be the simplest way to go. Just call your rand function and scale it accordingly, so that it is between `[-sqrt(3), sqrt(3)]` and not `[0,1]`
srean
@srean How did you find the sqrt(3) value?
Vitor Py
@Vitor. Look at the formula for the variance for uniform. It is `1/(12) (b-a)^2`. The simplest way to make mean `0` is to be symmetric, so we choose `a = -b`. Now substitute this in the variance formula and solve, you will get back sqrt(3). Hope this helped. I should have been clearer in my answer.
srean
@Vitor Let me know if there's anything more that I should add to the answer
srean
@srean That's all. Thank you.
Vitor Py
@Vitor Most welcome and thanks for the acceptance
srean
A: 

Waffles is a mature, stable C++ library that you can use. In particular, the noise function in the *waffles_generate* module will do what you want.

Aside from center and spread (mean and sd) also need to know the probability distribution that the random numbers are drawn from. If the paper you are reading doesn't say anything about this, and there's no other reasonable inference supported by context, then the author probably is referring to a normal distribution (gaussian)--because that's the most common, and because the two parameters one needs to completely specify a normal distribution are mean and sd. Many distributions are not specified this way--e.g., for a Gamma distribution, shape, scale, and rate are needed; to specify a Logistic, you need location and scale, etc.

doug
Its not necessary that the author of the paper implied a Gaussian random variable. In a lot of statistical analysis all you care about are the moments. In this particular case it seems just the first two moments. So @Vitor has the flexibility to choose the distribution that satisfies the requirement but is the simplest to implement.
srean
A: 

If all you want it a certain mean 0 and variance 1, probably the simplest is this. Do you have a uniform random number generator unif() that gives you numbers between 0 and 1? If you want the number to be very close to a normal distribution, can just add up 12 uniform(0,1) numbers and subtract 6. If you want it to be really exactly a normal distribution, you can use the Box-Muller transform, as Mark suggested, if you don't mind throwing in a log, a sine, and a cosine.

Mike Dunlavey
This equivalent to [http://stackoverflow.com/questions/4061446/generating-random-numbers-with-know-mean-and-variance/4061474#4061474]. But calling the random number generator more than necessary may be expensive. Its easier to call it once and scale and shift the output. There is a call to sqrt(3) but it is required only once.
srean
@srean: You're right, for uniforms. For normals, I haven't timed it, but the log, sine, and cosine calls might take more time. That's assuming time is even an issue. It might not be.
Mike Dunlavey