def get_pfix_transformed(p0, x4Ns, h): """ Try to get the same result as the function in the kimura module. Change of variables according to eqn 3 of Chen et al. When I type (integral from 0 to p of exp(b*s*(x-a)**2) ) / (integral from 0 to 1 of exp(b*s*(x-a)**2) ) I get ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-p)) ) / ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-1)) ) @param p0: proportion of mutant alleles in the population @param x4Ns: 4Ns @return: fixation probability """ if not x4Ns: # This is the neutral case. return p0 if h == 0.5: # This is the genic case. # Checking for exact equality of 0.5 is OK. return math.expm1(-x4Ns * p0) / math.expm1(-x4Ns) b = 2.0 * h - 1.0 a = h / (2.0 * h - 1.0) q = cmath.sqrt(b) * cmath.sqrt(x4Ns) # top = kimura.erfi(q * a) - kimura.erfi(q * (a - p0)) bot = kimura.erfi(q * a) - kimura.erfi(q * (a - 1)) return top / bot
def get_pfix_transformed(p0, x4Ns, h): """ Try to get the same result as the function in the kimura module. Change of variables according to eqn 3 of Chen et al. When I type (integral from 0 to p of exp(b*s*(x-a)**2) ) / (integral from 0 to 1 of exp(b*s*(x-a)**2) ) I get ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-p)) ) / ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-1)) ) @param p0: proportion of mutant alleles in the population @param x4Ns: 4Ns @return: fixation probability """ if not x4Ns: # This is the neutral case. return p0 if h == 0.5: # This is the genic case. # Checking for exact equality of 0.5 is OK. return math.expm1(-x4Ns*p0) / math.expm1(-x4Ns) b = 2.0 * h - 1.0 a = h / (2.0 * h - 1.0) q = cmath.sqrt(b) * cmath.sqrt(x4Ns) # top = kimura.erfi(q*a) - kimura.erfi(q*(a-p0)) bot = kimura.erfi(q*a) - kimura.erfi(q*(a-1)) return top / bot
def get_pfix_transformed(p0, s_in, h): """ Try to get the same result as the function in the kimura module. Change of variables according to eqn 3 of Chen et al. When I type (integral from 0 to p of exp(b*s*(x-a)**2) ) / (integral from 0 to 1 of exp(b*s*(x-a)**2) ) I get ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-p)) ) / ( erfi(sqrt(b)*sqrt(s)*a) - erfi(sqrt(b)*sqrt(s)*(a-1)) ) """ s = s_in * 2 b = 2.0 * h - 1.0 #XXX if not b: return float('nan') # transform h to alpha and beta, represented here by a and b a = h / (2.0 * h - 1.0) # intermediate parameter q = cmath.sqrt(b) * cmath.sqrt(s) # """ top = 2*q*cmath.exp(a*a*b*s) bot_a = kimura.erfi(q*(1-a)) bot_b = kimura.erfi(q*a) """ top = kimura.erfi(q*a) - kimura.erfi(q*(a-p0)) bot = kimura.erfi(q*a) - kimura.erfi(q*(a-1)) return top / bot
def get_pfix_transformed_limit(x4Ns, h): """ This is an approximation analogous to an approximation by Kimura. @param x4Ns: 4Ns @param h: dominance parameter @return: 2N times fixation probability """ if not x4Ns: # This is the neutral case. return 1.0 if h == 0.5: # This is the genic case. # Checking for exact equality of 0.5 is OK. return -x4Ns / math.expm1(-x4Ns) a = h / (2.0 * h - 1.0) b = 2.0 * h - 1.0 q = cmath.sqrt(b) * cmath.sqrt(x4Ns) # top = 2 * q * cmath.exp((q * a)**2) bot_a = kimura.erfi(q * a) bot_b = kimura.erfi(q * (1 - a)) return top / (cmath.sqrt(math.pi) * (bot_a + bot_b))
def get_pfix_transformed_limit(x4Ns, h): """ This is an approximation analogous to an approximation by Kimura. @param x4Ns: 4Ns @param h: dominance parameter @return: 2N times fixation probability """ if not x4Ns: # This is the neutral case. return 1.0 if h == 0.5: # This is the genic case. # Checking for exact equality of 0.5 is OK. return - x4Ns / math.expm1(-x4Ns) a = h / (2.0 * h - 1.0) b = 2.0 * h - 1.0 q = cmath.sqrt(b) * cmath.sqrt(x4Ns) # top = 2*q*cmath.exp((q*a)**2) bot_a = kimura.erfi(q*a) bot_b = kimura.erfi(q*(1-a)) return top / ( cmath.sqrt(math.pi) * (bot_a + bot_b) )
def get_pfix_transformed_limit(s_in, h): s = s_in * 2 b = 2.0 * h - 1.0 # if not s: #XXX is this right # limit s->0 of # (1/sqrt(pi)) * ( 2*sqrt(b)*sqrt(s)*exp(a*a*b*s) ) / # ( erfi(sqrt(b)*sqrt(s)*(1-a)) + erfi(sqrt(b)*sqrt(s)*a) ) return 1.0 if not b: #XXX is this right # limit as p->0 of (1/p) * ( expm1(-s*p) / expm1(-s) ) return s*math.exp(s) / math.expm1(s) # transform h to alpha and beta, represented here by a and b a = h / (2.0 * h - 1.0) # intermediate parameter q = cmath.sqrt(b) * cmath.sqrt(s) # top = 2*q*cmath.exp((q*a)**2) bot_a = kimura.erfi(q*a) bot_b = kimura.erfi(q*(1-a)) return top / ( cmath.sqrt(math.pi) * (bot_a + bot_b) )