> Pasi Havia wrote:
Hi,
I am making a little program which calculates Wright's inbreeding
coefficient.
What I have a trouble with is converting the formula into a code.
The formula is:
Fx=?[(0,5) n + nŽ + 1 * (1 + FA)]
So which part are you having problems with?
double ComputeCoeeficient(int n1, int n2) {
return pow(0.5, n1+n2-1);
}
This is if you are using the author's notation for the generations.
(which is off by one for the sire and dame lines, so it ends up being
-1 rather than +1).
The formula is:
Fx=?[(0,5) n + nŽ + 1 * (1 + FA)]
So which part are you having problems with?
double ComputeCoeeficient(int n1, int n2) {
return pow(0.5, n1+n2-1);
}
This is if you are using the author's notation for the generations.
(which is off by one for the sire and dame lines, so it ends up being
-1 rather than +1).
No youŽre not right. If some of the sibling have same mother/father then
this calculation won't do. Note that the FA is calculated the same way as Fx
which will lead into recursion.
I already got two alternative options to implement this but I'm having
problems with both of them.
Option #1
Pedigree is in tree structure.
function inbreed(dog k)
inbreed = 0
(mother's_grandparent_, n1) = first_grandparent(k.mother)
(father's_grandparent, n2) = first_grandparent(k.father)
do while mother's_grandparent == null
(mother's_grandparent, n1) = next_grandparent(k.mother,
mother's_grandparent)
do while father's_grandparent == null
(father's_grandparent, n2) = next_grandparent(k.father,
father's_grandparent)
if mother's_grandparent == father's_grandparent
inbreed += (0,5^(n1 + n2 + 1)) * (1 +
inbreed(mother's_grandparent))
Option #2
Using recursion to solve the formula.
function calculate_inbreed(dog)
return inbreed1(dog.mother, 0, dog.father, 0)
function inbreed1(dog1, n1, dog2, n2)
inbreed = inbreed2(dog1, n1, dog2, n2)
if dog1.mother
inbreed += inbreed1(dog1.mother, n1+1, dog2, n2)
if dog1.father
inbreed += inbreed1(dog1.father, n1+1, dog2, n2)
return inbreed
function inbreed2(dog1, n1, dog2, n2)
inbreed = 0
if dog1 == dog2
inbreed += (0,5^(n1 + n2 + 1)) * (1 + inbreed(dog1))
if dog2.mother
inbreed += inbreed_father(dog1, n1, dog2.mother, n2+1)
if dog2.father
inbreed += inbreed_father(dog1, n1, dog2.father, n2+1)
return inbreed
In the option #2 I don't know what calculation I should perform in
inbreed_father method. So the problem goes a little more deep than you first
expected I think ;)
-Pasi