Created
March 21, 2011 13:48
-
-
Save ain/879464 to your computer and use it in GitHub Desktop.
Returns the inverse of the standard normal cumulative distribution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> | |
<mx:Script> | |
<![CDATA[ | |
private function inverseCumulativeNormalDistribution(v:Number):Number{ | |
//refer http://home.online.no/~pjacklam/notes/invnorm/ for how this works | |
var a1:Number = -39.69683028665376; | |
var a2:Number = 220.9460984245205; | |
var a3:Number = -275.9285104469687; | |
var a4:Number = 138.3577518672690; | |
var a5:Number = -30.66479806614716; | |
var a6:Number = 2.506628277459239; | |
var b1:Number = -54.47609879822406; | |
var b2:Number = 161.5858368580409; | |
var b3:Number = -155.6989798598866; | |
var b4:Number = 66.80131188771972; | |
var b5:Number = -13.28068155288572; | |
var c1:Number = -0.007784894002430293; | |
var c2:Number = -0.3223964580411365; | |
var c3:Number = -2.400758277161838; | |
var c4:Number = -2.549732539343734; | |
var c5:Number = 4.374664141464968; | |
var c6:Number = 2.938163982698783; | |
var d1:Number = 0.007784695709041462; | |
var d2:Number = 0.3224671290700398; | |
var d3:Number = 2.445134137142996; | |
var d4:Number = 3.754408661907416; | |
var p_low:Number = 0.02425; | |
var p_high:Number = 1 - p_low; | |
var q:Number = 0; | |
var r:Number = 0; | |
var result:Number = 0; | |
//Rational approximation for lower region | |
if((0 < v) && (v < p_low)){ | |
q = Math.sqrt( -2 * Math.log(v)); | |
result = (((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1); | |
//Rational approximation for central region | |
}else{ | |
if((p_low <= arguments.v) && (v <= p_high )){ | |
q = v - 0.5; | |
r = q*q; | |
result = (((((a1*r+a2)*r+a3)*r+a4)*r+a5)*r+a6)*q / (((((b1*r+b2)*r+b3)*r+b4)*r+b5)*r+1); | |
//Rational approximation for upper region | |
}else{ | |
if((p_high < arguments.v) && (v < 1)){ | |
q = Math.sqrt(-2*Math.log(1-v)); | |
result = -(((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1); | |
} | |
} | |
} | |
return result; | |
} | |
private function startCalc():void{ | |
if(Number(input.text) < 1 && Number(input.text) > 0 ){ | |
invResult.text = inverseCumulativeNormalDistribution(Number(input.text)).toString(); | |
} | |
} | |
]]> | |
</mx:Script> | |
<mx:TextInput x="85" y="468" id="input"/> | |
<mx:Button x="288" y="470" label="Calculate" click="startCalc()"/> | |
<mx:Text x="371" y="470" text="" width="202" id="invResult"/> | |
</mx:Application> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment