Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mrd/jelly/distribution/Beta.java


1   /*
2    * Beta.java
3    *
4    * Created on May 27, 2002, 5:40 PM
5    */
6   
7   package org.mrd.jelly.distribution;
8   
9   import org.apache.commons.jelly.MissingAttributeException;
10  import org.apache.commons.jelly.TagSupport;
11  import org.apache.commons.jelly.expression.Expression;
12  import cern.jet.random.*;
13  
14  /** This is a random number generation class that produces pseudo-random
15   * doubles acording to a configured Beta Distribution. The Distribution is 
16   * supported by the cern.jet.random.Beta Distribution Class.
17   *
18   * The distribution can be configured by two strategies
19   *
20   * 1.) Alpha and Beta parameters can be provided for the distribution.
21   *
22   * 2.) The mean and standard devation can be provided to configure the
23   *    distribution. With this strategy alpha and beta are estimated through the
24   *    method of matching moments to be
25   *
26   *        alpha = mean{[mean(1-mean)/s^2] - 1}
27   *
28   *        beta = (1-mean) mean{[mean(1-mean)/s^2] - 1}
29   *
30   * @author Mark R. Diggory
31   */
32  public class Beta extends DistributionTagBase {
33      
34      /** Holds value of property mean. */
35      private double mean;
36      
37      /** Holds value of property std. */
38      private double std;
39  
40      /** Holds value of property alpha. */
41      private double alpha;
42      
43      /** Holds value of property beta. */
44      private double beta;
45      
46      private boolean setByMean = false;
47  
48      
49      /** Used by Ant to check if the appropriate attributes have been filled out.
50       * @throws BuildException if attribute is not correctly filled in.
51       */    
52      public void doStartTag(org.apache.commons.jelly.XMLOutput xMLOutput) throws Exception {
53  
54          if (getVar() == null) 
55              throw new MissingAttributeException("var");
56          
57          if(setByMean){
58              if(!(mean > Double.MIN_VALUE))
59                 throw new MissingAttributeException("mean");
60              
61              if(!(std > Double.MIN_VALUE))
62                throw new MissingAttributeException("std");
63              
64              // try to setup alpha and beta
65              
66              alpha = mean    * (( (mean*(1-mean))/(std*std) )-1); 
67              beta = (1-mean) * (( (mean*(1-mean))/(std*std) )-1);
68              
69          }else{
70              
71              if(!(alpha > Double.MIN_VALUE))
72                  throw new MissingAttributeException("alpha");
73              
74              if(!(beta > Double.MIN_VALUE))
75                  throw new MissingAttributeException("beta");
76          }
77      }
78      
79      /** Simply put, it is where the execution of the tag occurs.
80       * @throws BuildException (Currently does not).
81       */   
82      public void doEndTag(org.apache.commons.jelly.XMLOutput xMLOutput) throws Exception {
83          if( context.getVariable(getVar()) == null ){
84              context.setVariable(getVar(),new cern.jet.random.Beta(alpha, beta,this.getRandomElement()));
85          }else{
86              Object o = context.getVariable(getVar());
87              ((cern.jet.random.Beta)o).setState(alpha,beta);
88          }
89          
90          
91      }
92  
93      /** Getter for property mean.
94       * @return Value of property mean.
95       */
96      public double getMean() {
97          return mean;
98      }
99      
100     /** Setter for property mean.
101      * @param mean New value of property mean.
102      */
103     public void setMean(double mean) {
104         setByMean = true;
105         this.mean = mean;
106     }
107     
108     /** Getter for property std.
109      * @return Value of property std.
110      */
111     public double getStd() {
112         return std;
113     }
114     
115     /** Setter for property std.
116      * @param std New value of property std.
117      */
118     public void setStd(double std) {
119         setByMean = true;
120         this.std = std;
121     }
122 
123     /** Getter for property alpha.
124      * @return Value of property alpha.
125      */
126     public double getAlpha() {
127         return alpha;
128     }
129     
130     /** Setter for property alpha.
131      * @param alpha New value of property alpha.
132      */
133     public void setAlpha(double alpha) {
134         setByMean = false;
135         this.alpha = alpha;
136     }
137     
138     /** Getter for property beta.
139      * @return Value of property beta.
140      */
141     public double getBeta() {
142         return beta;
143     }
144     
145     /** Setter for property beta.
146      * @param beta New value of property beta.
147      */
148     public void setBeta(double beta) {
149         setByMean = false;
150         this.beta = beta;
151     }
152     
153         
154 
155     
156 }