I have a population size of say 5000 people. Every person belongs to either A, B, C or D category. I want to split the population as per a given fraction provided by user. for example, 99% of A, 0.4% of B, 0.3% of C and 0.3% of D ( total = 100% ). I just want to confirm whether my approach for this solution is correct or not. I use random numbers to do this. let,
fa = fraction of A, fb = fraction of B, fc = fraction of C and fd = fraction of D.
my algorithm is as follows: I write a function :-->
function( returns a category type A,B,C or D)
{
double r = drand48(); // gives me random number between 0 and 1.0 (uniform dist)
if( r < fa )
return A;
if( fa < r < (fa+fb) )
return B;
if( (fa+fb) < r < (fa+fb+fc) )
return C;
if( (fa+fb+fc) < r < (fa+fb+fc+fd) )
return D;
}
I just want to make sure, the population is distributed correctly as per the given fraction of each type provided.
I am not sure what category to tag this is.(sorry for the trouble)
In principle your code is right, except that there is a minuscle probability that
rmight equal e.g.fa+fbexactly, in which case none of theifs triggers. You can drop the<rpart of the condition (if repeated comparison is valid in your programming language of choice at all) anyway. If it is given thatfa+fb+fc+fdequals 1, you can also justreturn Dif the thirdiffails. So much for potential coding problems.For the distribution, you are aware that the resulting population will not be distributed completely according to the given proportions, but rather like a sample from an infinite population where the proportions are valid? For example with $5000$ as size and $0.003$ for
D, the actual number ofDs produced need not be $15$, but can easily turn out as e.g. $11$ or $19$.