1 import java.awt.*; 2 import java.awt.event.*; 3 import JSci.awt.*; 4 import JSci.maths.statistics.*; 5 6 11 public final class DistributionGenerator extends Frame implements Runnable { 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 arg[]) { 18 DistributionGenerator app=new DistributionGenerator(new CauchyDistribution(), 0.0, 10.0); 20 Thread thr=new Thread (app); 22 thr.setPriority(Thread.MIN_PRIORITY); 23 thr.start(); 24 } 25 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 56 public double random() { 57 62 double x,y; 63 do { 64 68 x=mean+width*(Math.random()-0.5); 70 78 y=Math.random(); 80 } 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 |