001/* An actor that outputs a random sequence with a Beta distribution. 002 003 Copyright (c) 2004-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.actor.lib.colt; 029 030import cern.jet.random.Beta; 031import ptolemy.actor.parameters.PortParameter; 032import ptolemy.data.BooleanToken; 033import ptolemy.data.DoubleToken; 034import ptolemy.data.expr.SingletonParameter; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040/////////////////////////////////////////////////////////////////// 041//// Beta 042 043/** 044 Produce a random sequence with a Beta distribution. On each 045 iteration, a new random number is produced. The output port is of 046 type DoubleToken. The values that are generated are independent 047 and identically distributed with the mean and the standard 048 deviation given by parameters. In addition, the seed can be 049 specified as a parameter to control the sequence that is generated. 050 051 <p> This actor instantiates a 052 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/random/Beta.html">cern.jet.random.Beta</a> object with 053 alpha and beta both set to 2.0. 054 055 <p>A definition of NegativeBinomial by Wolfgang Hoschek can be found at 056 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/stat/Probability.html#beta(double,%20double,%20double)"><code>http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/stat/Probability.html#beta(double,%20double,%20double)</code></a>:</p> 057 <blockquote> 058 <h3>beta</h3> 059 <pre>public static double <b>beta</b>(double a, 060 double b, 061 double x)</pre> 062 <p>Returns the area from zero to <tt>x</tt> under the beta density 063 function.</p> 064 <pre> x 065 - - 066 | (a+b) | | a-1 b-1 067 P(x) = ---------- | t (1-t) dt 068 - - | | 069 | (a) | (b) - 070 0 071 </pre> 072 073 <p>This function is identical to the incomplete beta 074 integral function <tt>Gamma.incompleteBeta(a, b, x)</tt>.</p> 075 076 <p>The complemented function is</p> 077 078 <p><tt>1 - P(1-x) = Gamma.incompleteBeta( b, a, x )</tt>;</p> 079 </blockquote> 080 081 The above description of beta() is 082 <a href="doc-files/colt-copyright.htm">copyrighted</a>. 083 <br>In this actor, <i>alpha</i> corresponds with <i>a</i> 084 <i>beta</i> corresponds with <i>b</i>. 085 086 087 @author David Bauer and Kostas Oikonomou 088 @version $Id$ 089 @since Ptolemy II 4.1 090 @Pt.ProposedRating Red (cxh) 091 @Pt.AcceptedRating Red (cxh) 092 */ 093public class ColtBeta extends ColtRandomSource { 094 /** Construct an actor with the given container and name. 095 * @param container The container. 096 * @param name The name of this actor. 097 * @exception IllegalActionException If the actor cannot be contained 098 * by the proposed container. 099 * @exception NameDuplicationException If the container already has an 100 * actor with this name. 101 */ 102 public ColtBeta(CompositeEntity container, String name) 103 throws NameDuplicationException, IllegalActionException { 104 super(container, name); 105 106 output.setTypeEquals(BaseType.DOUBLE); 107 108 alpha = new PortParameter(this, "alpha"); 109 alpha.setExpression("2.0"); 110 alpha.setTypeEquals(BaseType.DOUBLE); 111 new SingletonParameter(alpha.getPort(), "_showName") 112 .setToken(BooleanToken.TRUE); 113 114 beta = new PortParameter(this, "beta"); 115 beta.setExpression("2.0"); 116 beta.setTypeEquals(BaseType.DOUBLE); 117 new SingletonParameter(beta.getPort(), "_showName") 118 .setToken(BooleanToken.TRUE); 119 120 beta.moveToFirst(); 121 alpha.moveToFirst(); 122 } 123 124 /////////////////////////////////////////////////////////////////// 125 //// ports and parameters //// 126 127 /** Alpha. 128 * This has type double with default 2.0. 129 */ 130 public PortParameter alpha; 131 132 /** Beta. 133 * This has type double with default 2.0. 134 */ 135 public PortParameter beta; 136 137 /////////////////////////////////////////////////////////////////// 138 //// public methods //// 139 140 /** Send a random number with a Beta distribution to the output. 141 * This number is only changed in the prefire() method, so it will 142 * remain constant throughout an iteration. 143 * @exception IllegalActionException If there is no director. 144 */ 145 @Override 146 public void fire() throws IllegalActionException { 147 alpha.update(); 148 beta.update(); 149 super.fire(); 150 output.send(0, new DoubleToken(_current)); 151 } 152 153 /////////////////////////////////////////////////////////////////// 154 //// protected methods //// 155 156 /** Method that is called after _randomNumberGenerator is changed. 157 */ 158 @Override 159 protected void _createdNewRandomNumberGenerator() { 160 _generator = new Beta(2.0, 2.0, _randomNumberGenerator); 161 } 162 163 /** Generate a new random number. 164 * @exception IllegalActionException If parameter values are incorrect. 165 */ 166 @Override 167 protected void _generateRandomNumber() throws IllegalActionException { 168 double alphaValue = ((DoubleToken) alpha.getToken()).doubleValue(); 169 double betaValue = ((DoubleToken) beta.getToken()).doubleValue(); 170 _current = _generator.nextDouble(alphaValue, betaValue); 171 } 172 173 /////////////////////////////////////////////////////////////////// 174 //// private variables //// 175 176 /** The random number for the current iteration. */ 177 private double _current; 178 179 /** The random number generator. */ 180 private Beta _generator; 181}