#NEWLINE# def _phi( p ) : #NEWLINE# #INDENT# "\n    Modified from the author's original perl code (original comments follow below)\n    by dfield@yahoo-inc.com.  May 3, 2004.\n\n    Lower tail quantile for standard normal distribution function.\n\n    This function returns an approximation of the inverse cumulative\n    standard normal distribution function.  I.e., given P, it returns\n    an approximation to the X satisfying P = Pr{Z <= X} where Z is a\n    random variable from the standard normal distribution.\n\n    The algorithm uses a minimax approximation by rational functions\n    and the result has a relative error whose absolute value is less\n    than 1.15e-9.\n\n    Author:      Peter John Acklam\n    Time-stamp:  2000-07-19 18:26:14\n    E-mail:      pjacklam@online.no\n    WWW URL:     http://home.online.no/~pjacklam\n    " #NEWLINE# if ( ( p <= 0 ) or ( p >= 1 ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Argument to ltqnorm %f must be in open interval (0,1)' % p ) ) #NEWLINE# #UNINDENT# a = ( ( - 39.69683028665376 ) , 220.9460984245205 , ( - 275.9285104469687 ) , 138.357751867269 , ( - 30.66479806614716 ) , 2.506628277459239 ) #NEWLINE# b = ( ( - 54.47609879822406 ) , 161.5858368580409 , ( - 155.6989798598866 ) , 66.80131188771972 , ( - 13.28068155288572 ) ) #NEWLINE# c = ( ( - 0.007784894002430293 ) , ( - 0.3223964580411365 ) , ( - 2.400758277161838 ) , ( - 2.549732539343734 ) , 4.374664141464968 , 2.938163982698783 ) #NEWLINE# d = ( 0.007784695709041462 , 0.3224671290700398 , 2.445134137142996 , 3.754408661907416 ) #NEWLINE# plow = 0.02425 #NEWLINE# phigh = ( 1 - plow ) #NEWLINE# if ( p < plow ) : #NEWLINE# #INDENT# q = math . sqrt ( ( ( - 2 ) * math . log ( p ) ) ) #NEWLINE# return ( ( - ( ( ( ( ( ( ( ( ( ( c [ 0 ] * q ) + c [ 1 ] ) * q ) + c [ 2 ] ) * q ) + c [ 3 ] ) * q ) + c [ 4 ] ) * q ) + c [ 5 ] ) ) / ( ( ( ( ( ( ( ( d [ 0 ] * q ) + d [ 1 ] ) * q ) + d [ 2 ] ) * q ) + d [ 3 ] ) * q ) + 1 ) ) #NEWLINE# #UNINDENT# if ( phigh < p ) : #NEWLINE# #INDENT# q = math . sqrt ( ( ( - 2 ) * math . log ( ( 1 - p ) ) ) ) #NEWLINE# return ( ( ( ( ( ( ( ( ( ( ( c [ 0 ] * q ) + c [ 1 ] ) * q ) + c [ 2 ] ) * q ) + c [ 3 ] ) * q ) + c [ 4 ] ) * q ) + c [ 5 ] ) / ( ( ( ( ( ( ( ( d [ 0 ] * q ) + d [ 1 ] ) * q ) + d [ 2 ] ) * q ) + d [ 3 ] ) * q ) + 1 ) ) #NEWLINE# #UNINDENT# q = ( p - 0.5 ) #NEWLINE# r = ( q * q ) #NEWLINE# return ( ( ( - ( ( ( ( ( ( ( ( ( ( a [ 0 ] * r ) + a [ 1 ] ) * r ) + a [ 2 ] ) * r ) + a [ 3 ] ) * r ) + a [ 4 ] ) * r ) + a [ 5 ] ) ) * q ) / ( ( ( ( ( ( ( ( ( ( b [ 0 ] * r ) + b [ 1 ] ) * r ) + b [ 2 ] ) * r ) + b [ 3 ] ) * r ) + b [ 4 ] ) * r ) + 1 ) )