001/* An actor that outputs a random sequence with a NegativeBinomial 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.NegativeBinomial; 031import ptolemy.actor.parameters.PortParameter; 032import ptolemy.data.BooleanToken; 033import ptolemy.data.DoubleToken; 034import ptolemy.data.IntToken; 035import ptolemy.data.expr.SingletonParameter; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.CompositeEntity; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040 041/////////////////////////////////////////////////////////////////// 042//// NegativeBinomial 043 044/** 045 Produce a random sequence with a NegativeBinomial distribution. On each 046 iteration, a new random number is produced. The output port is of 047 type DoubleToken. The values that are generated are independent 048 and identically distributed with the mean and the standard 049 deviation given by parameters. In addition, the seed can be 050 specified as a parameter to control the sequence that is generated. 051 052 <p> This actor instantiates a 053 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/random/NegativeBinomial.html">cern.jet.random.NegativeBinomial</a> object with 054 n, the number of trials set to 1 and p, the probability of success, set 055 to 0.5. 056 057 A definition of NegativeBinomial by Wolfgang Hoschek can be found at 058 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/stat/Probability.html#negativeBinomial(int,%20int,%20double)"><code>http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/stat/Probability.html#negativeBinomial(int,%20int,%20double)</code></a>: 059 <blockquote> 060 <h3> 061 negativeBinomial</h3> 062 <pre>public static double <b>negativeBinomial</b>(int k, 063 int n, 064 double p)</pre> 065 066 067 Returns the sum of the terms <tt>0</tt> through <tt>k</tt> of the Negative Binomial Distribution. 068 <pre> k 069 -- ( n+j-1 ) n j 070 > ( ) p (1-p) 071 -- ( j ) 072 j=0 073 </pre> 074 In a sequence of Bernoulli trials, this is the probability 075 that <tt>k</tt> or fewer failures precede the <tt>n</tt>-th success. 076 <p> 077 078 The terms are not computed individually; instead the incomplete 079 beta integral is employed, according to the formula 080 </p><p> 081 <tt>y = negativeBinomial( k, n, p ) = Gamma.incompleteBeta( n, k+1, p )</tt>. 082 083 All arguments must be positive,</p> 084 <b>Parameters: 085 <br></b><code>k</code> - end term. 086 <br><code>n</code> - the number of trials. 087 <br><code>p</code> - the probability of success (must be in <tt>(0.0,1.0)</tt>). 088 </blockquote> 089 The above description of negativeBinomial() is 090 <a href="doc-files/colt-copyright.htm">copyrighted</a>. 091 092 @author David Bauer and Kostas Oikonomou 093 @version $Id$ 094 @since Ptolemy II 4.1 095 @Pt.ProposedRating Red (cxh) 096 @Pt.AcceptedRating Red (cxh) 097 */ 098public class ColtNegativeBinomial extends ColtRandomSource { 099 /** Construct an actor with the given container and name. 100 * @param container The container. 101 * @param name The name of this actor. 102 * @exception IllegalActionException If the actor cannot be contained 103 * by the proposed container. 104 * @exception NameDuplicationException If the container already has an 105 * actor with this name. 106 */ 107 public ColtNegativeBinomial(CompositeEntity container, String name) 108 throws NameDuplicationException, IllegalActionException { 109 super(container, name); 110 111 output.setTypeEquals(BaseType.INT); 112 113 n = new PortParameter(this, "n", new IntToken(1)); 114 n.setTypeEquals(BaseType.INT); 115 new SingletonParameter(n.getPort(), "_showName") 116 .setToken(BooleanToken.TRUE); 117 118 p = new PortParameter(this, "p", new DoubleToken(0.5)); 119 p.setTypeEquals(BaseType.DOUBLE); 120 new SingletonParameter(p.getPort(), "_showName") 121 .setToken(BooleanToken.TRUE); 122 123 p.moveToFirst(); 124 n.moveToFirst(); 125 } 126 127 /////////////////////////////////////////////////////////////////// 128 //// ports and parameters //// 129 130 /** The mean, or n. 131 * This has type int with default value 1. 132 */ 133 public PortParameter n; 134 135 /** The variance, or p. 136 * This has type double with default 0.5. 137 */ 138 public PortParameter p; 139 140 /////////////////////////////////////////////////////////////////// 141 //// public methods //// 142 143 /** Send a random number with a NegativeBinomial distribution to the output. 144 * This number is only changed in the prefire() method, so it will 145 * remain constant throughout an iteration. 146 * @exception IllegalActionException If there is no director. 147 */ 148 @Override 149 public void fire() throws IllegalActionException { 150 n.update(); 151 p.update(); 152 super.fire(); 153 output.send(0, new IntToken(_current)); 154 } 155 156 /////////////////////////////////////////////////////////////////// 157 //// protected methods //// 158 159 /** Method that is called after _randomNumberGenerator is changed. 160 */ 161 @Override 162 protected void _createdNewRandomNumberGenerator() { 163 _generator = new NegativeBinomial(1, 0.5, _randomNumberGenerator); 164 } 165 166 /** Generate a new random number. 167 * @exception IllegalActionException If parameter values are incorrect. 168 */ 169 @Override 170 protected void _generateRandomNumber() throws IllegalActionException { 171 int nValue = ((IntToken) n.getToken()).intValue(); 172 double pValue = ((DoubleToken) p.getToken()).doubleValue(); 173 174 _current = _generator.nextInt(nValue, pValue); 175 } 176 177 /////////////////////////////////////////////////////////////////// 178 //// private variables //// 179 180 /** The random number for the current iteration. */ 181 private int _current; 182 183 /** The random number generator. */ 184 private NegativeBinomial _generator; 185}