Difference between revisions of "830 - Haplotyper::SampleHaplotypes()"
From Genome Analysis Wiki
Jump to navigationJump to search (Created page with "<source lang="cpp"> void Haplotyper::SampleChromosomes(Random * rand) { RewindMemoryPool(); RetrieveMemoryBlock(markers - 1); int first = 0, second = 0; Sampl...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | + | float Haplotyper::SampleHaplotypes(float * vector, int & first, int & second, Random & rand) | |
{ | { | ||
− | + | float sum = 0.0; | |
− | + | float *probability = vector; | |
− | int | + | // Calculate sum over all states |
− | + | for (int i = 0; i < states; i++) | |
+ | for (int j = 0; j <= i; j++) | ||
+ | { | ||
+ | sum += *probability; | ||
+ | probability++; | ||
+ | } | ||
− | + | // Sample number and select state | |
− | + | float choice = rand->Uniform(0, sum); | |
− | |||
− | |||
− | + | sum = 0.0; | |
− | + | for (probability = vector, first = 0; first < states; first++) | |
− | + | { | |
− | + | for (second = 0; second <= first; second++) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{ | { | ||
− | + | sum += *probability; | |
− | + | probability++; | |
− | |||
− | + | if (sum >= choice) break; | |
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | + | if (second <= first) break; | |
− | |||
} | } | ||
− | + | ||
− | + | if (rand->Binary()) SWAP(first, second); | |
} | } | ||
</source> | </source> |
Latest revision as of 14:10, 2 October 2013
float Haplotyper::SampleHaplotypes(float * vector, int & first, int & second, Random & rand)
{
float sum = 0.0;
float *probability = vector;
// Calculate sum over all states
for (int i = 0; i < states; i++)
for (int j = 0; j <= i; j++)
{
sum += *probability;
probability++;
}
// Sample number and select state
float choice = rand->Uniform(0, sum);
sum = 0.0;
for (probability = vector, first = 0; first < states; first++)
{
for (second = 0; second <= first; second++)
{
sum += *probability;
probability++;
if (sum >= choice) break;
}
if (second <= first) break;
}
if (rand->Binary()) SWAP(first, second);
}