View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.util;
25  
26  import java.util.*;
27  import java.awt.*;
28  
29  public class NormalDist {
30  
31     public static final double MTOL      	  = 1.0E-12;
32  
33     public static double normdist(double xx, double mu, double sigma) {
34        if ( Math.abs(sigma-0.0) < MTOL ) sigma = 0.001;
35  
36        double x = (xx < mu) ? 2.0*mu - xx : xx;
37        double Zx = Math.exp(-0.5*Math.pow((x-mu)/sigma,2.0))/
38                    (sigma*Math.sqrt(2.0*Math.PI));
39  
40        double p = 0.33267;
41        double a1 = 0.4361836;
42        double a2 = -0.1201676;
43        double a3 = 0.9372980;
44  
45        double t = 1.0/(1.0+p*x);
46        double Px = 1.0 - Zx*(a1*t + a2*Math.pow(t,2.0) + a3*Math.pow(t,3.0));
47        
48        return (xx < mu) ? 1 - Px : Px;
49     }
50     public static double normdist(double x) {
51        return normdist(x,0.0,1.0);
52     }
53  
54     public static double norminv(double q, double mu, double sigma) {
55        return mu + sigma*norminv(q);
56     }
57     public static double norminv(double q) {
58        if ( q == 0.50 ) return 0.0;
59  
60        q = 1.0-q;
61  
62        double p = (q > 0.0 && q < 0.5) ? q : (1.0 - q);
63        double t = Math.sqrt(Math.log(1.0/Math.pow(p,2.0)));
64  
65        double c0 = 2.515517;
66        double c1 = 0.802853;
67        double c2 = 0.010328;
68  
69        double d1 = 1.432788;
70        double d2 = 0.189269;
71        double d3 = 0.001308;
72  
73        double X = t - (c0 + c1*t + c2*Math.pow(t,2.0))/
74                   (1.0 + d1*t + d2*Math.pow(t,2.0) + d3*Math.pow(t,3.0));
75        
76        if ( q > 0.5 ) X *= -1.0;
77        return X;
78     }
79     public static void main(String[] args) {
80        for ( double i = -3.5; i < 4; i += 0.5 ) {
81           double p = normdist(i);
82           double x = norminv(p);
83           System.out.println(i+"\t"+p+"\t"+x);
84        }
85     }
86  }
87