KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DistributionGenerator


1 import java.awt.*;
2 import java.awt.event.*;
3 import JSci.awt.*;
4 import JSci.maths.statistics.*;
5
6 /**
7 * DistributionGenerator generates random numbers from a probability distribution.
8 * @author Mark Hale
9 * @version 1.1
10 */

11 public final class DistributionGenerator extends Frame implements Runnable JavaDoc {
12         private final double mean;
13         private final double width;
14         private final ProbabilityDistribution dist;
15         private final HistogramModel model;
16
17         public static void main(String JavaDoc arg[]) {
18         // set distribution here
19
DistributionGenerator app=new DistributionGenerator(new CauchyDistribution(), 0.0, 10.0);
20 // DistributionGenerator app=new DistributionGenerator(new PoissonDistribution(50.0), 50.0, 100.0);
21
Thread JavaDoc thr=new Thread JavaDoc(app);
22                 thr.setPriority(Thread.MIN_PRIORITY);
23                 thr.start();
24         }
25         /**
26         * Constructs a distribution generator.
27         * @param pd a probability distribution
28         * @param m the mean of the sampling region
29         * @param w the width of the sampling region
30         */

31         public DistributionGenerator(ProbabilityDistribution pd, double m, double w) {
32                 super("Distribution Generator");
33                 dist=pd;
34                 mean=m;
35                 width=w;
36                 model=new HistogramModel();
37                 ScatterGraph graph=new ScatterGraph(model);
38                 addWindowListener(new WindowAdapter() {
39                         public void windowClosing(WindowEvent evt) {
40                                 dispose();
41                                 System.exit(0);
42                         }
43                 });
44                 add(graph);
45                 setSize(300,300);
46                 setVisible(true);
47         }
48         public void run() {
49                 for(int i=0;i<10000;i++)
50                         model.add(random());
51         }
52         /**
53         * Returns a random number from the distribution.
54         * Uses the rejection method.
55         */

56         public double random() {
57         /*
58          * We use the uniform function f(x)=1 to be totally general.
59          * For greater efficiency use a function f(x) which roughly
60          * over-approximates the required distribution.
61          */

62                 double x,y;
63                 do {
64                 /*
65                  * random x coordinate:
66                  * x is such that int(f(s),min,x,ds) = Math.random()*int(f(s),min,max,ds)
67                  */

68                 // uniform comparison function
69
x=mean+width*(Math.random()-0.5);
70                 // Lorentzian comparison function
71
// x=mean+Math.tan(Math.PI*Math.random());
72
// uncomment line below if using a discrete distribution
73
// x=Math.floor(x);
74
/*
75                  * random y coordinate:
76                  * y = Math.random()*f(x)
77                  */

78                 // uniform comparison function
79
y=Math.random();
80                 // Lorentzian comparison function
81
// y=Math.random()/(1.0+(x-mean)*(x-mean));
82
} while(y>dist.probability(x));
83                 return x;
84         }
85         private class HistogramModel extends AbstractGraphModel implements Graph2DModel {
86                 private final int histogram[]=new int[101];
87                 private final double binwidth=width/(histogram.length-1);
88
89                 public HistogramModel() {}
90                 public float getXCoord(int i) {
91                         return (float)(binwidth*(i-(histogram.length-1)/2.0)+mean);
92                 }
93                 public float getYCoord(int i) {
94                         return histogram[i];
95                 }
96                 public int seriesLength() {
97                         return histogram.length;
98                 }
99                 public void firstSeries() {}
100                 public boolean nextSeries() {
101                         return false;
102                 }
103                 public void add(double x) {
104                         int bin=(int)((x-mean)/binwidth)+(histogram.length-1)/2;
105                         if(bin>=0 && bin<histogram.length) {
106                                 histogram[bin]++;
107                                 fireGraphSeriesUpdated(0);
108                         }
109                 }
110         }
111 }
112
113
Popular Tags