Random numbers with Zero mean (not the basics)
Show older comments
Hello, I have been searching for an alternative for my problem, but wasn't really able to get a convenient solution.
Issue with randn:
Although randn is based on zero mean, it doesn't really produce an array with zero mean. Even if I generate 1 million random variables from the standard normal, the mean sometimes is "far" from zero (ex: 0.003). I really need zero mean generations. I can't simply deduct the mean, as I need to use the numbers generated in simulating new stock prices, this moves me to my specific issue.
Issue with my case:
To simplify it to the maximum, please consider the following algorithm:
for i=1:j
Step 1 StockPrice %Given
Step 2 %Some calculations
Step 3 StockPrice = StockPrice*randn %Generating new stock price
Step 4 %Some calculations
Step 5 StockPrice = StockPrice*randn %Generating new stock price
end
I really don't want to complicate my code with if statements to achieve the zero mean. Kindly note that generating one vector of random numbers will be much faster than my current method.
My problem will be solved if there is a way to move in a vector in every iteration.
Thanks in advance
6 Comments
A Jenkins
on 1 Sep 2013
Would you be allowed to generate n/2 random positive numbers, then use the other half (n/2) of your set as their negatives? That would create zero mean, but may not be what you want.
AND
on 1 Sep 2013
UJJWAL
on 1 Sep 2013
Thanks for your question. I did not know about this strange behavior of randn function. Thanks for enlightening me. I think that A M is reporting a very good way of solving your problem which is both fast and efficient.
Walter Roberson
on 1 Sep 2013
YourVector(Current_Start : Current_Start + Number_To_Use_Now - 1)
....
Current_Start = Current_Start + Number_To_Use_Now
would have the effect of moving in the vector.
Walter Roberson
on 1 Sep 2013
If you generate one random number at a time, and your random values are required to have a mean of 0 and not just statistically, then your one random number would be forced to be 0. Likewise, if you generate 2 at a time, the two would be forced to be negatives of each other. This does not sound realistic.
I would suggest that stock price movements are not uniformly random distributed, that there is too much of a tendency for No Change for a significant number of stocks (they might not be market movers but the stock exchange includes them anyhow.)
AND
on 1 Sep 2013
Accepted Answer
More Answers (3)
Peter Perkins
on 20 Sep 2013
AND, the answer to your latest question, if I understand it correctly) is yes, it doesn't matter if you generate one value at a time, or one million all at once, or one thousand values a thousand times. You will get the same sequence of values:
>> rng default
>> randn, randn, randn, randn
ans =
0.53767
ans =
1.8339
ans =
-2.2588
ans =
0.86217
>> rng default
>> randn(2,1), randn(2,1)
ans =
0.53767
1.8339
ans =
-2.2588
0.86217
>> rng default
>> randn(4,1)
ans =
0.53767
1.8339
-2.2588
0.86217
A Jenkins
on 1 Sep 2013
n=100; %number of random numbers you need
x=randn(n/2,1); %half random numbers
y=[x; -x]; %array of 'random' numbers with mean 0
mean(y)
the cyclist
on 1 Sep 2013
Edited: the cyclist
on 1 Sep 2013
0 votes
I believe this File Exchange submission will do close to what you need:
However, these numbers are uniformly distributed, not normally.
I'm not sure how hard it is to modify it.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!