I am trying to do some Monte Carlo Type simulations. In this, I have few choices which needs to be selected using weighted probability. To do this, I have calculated a cumulative probability (Prob_Rv) and I access the choice using something like
choice = find(Prob_Rv > rand, 1, 'first');
For a example scenario, Lets take Prob_Rv as
Prob_Rv = [0.0001;0.0001;0.9993;1.0000];
From this example, the Probability of choice 2 is zero, however rest of the 3 choices can be with certain probability. On doing something like this, I find that some of the choice occur more frequently than they should and some less frequently than they should. I tried to test it using a code like this:
Prob4 = 7.4831e-4;
totNum = 2000000;
nn = zeros(totNum,1);
storeVal = zeros(100,4);
for i = 1:100
rng shuffle;
nn = rand(totNum,1);
Prob1_1 = (sum(nn < Prob_Rv(1)))/totNum;
Prob2_1 = (sum(nn < Prob_Rv(2))/totNum) - Prob1_1;
Prob3_1 = (sum(nn < Prob_Rv(3))/totNum) - Prob2_1;
Prob4_1 = 1 - Prob1_1 - Prob2_1 - Prob3_1;
storeVal(i,:) = [Prob1_1 Prob2_1 Prob3_1 Prob4_1];
end
plot(storeVal(:,4));
hold on;
plot(1:100,Prob4*ones(100,1),'*');
In this, I create 200,000 random numbers and check the probability of a choice. In the image below, I have plotted the probability obtained for choice 4 and compared it with the probability it should be. The probability I get are nearly always lower than it should be.
Is it something I am doing wrong? Is there a way to resolve this?
Edit: #1 I have tried removing the 'rng shuffle' in every loop, and the problem remains the same.