001/* An actor that outputs a random sequence with a ChiSquare 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.ChiSquare; 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//// ChiSquare 042 043/** 044 Produce a random sequence with a ChiSquare 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 freedom 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/ChiSquare.html">cern.jet.random.ChiSquare</a> object with 053 freedom argument set to 1.0. 054 055 The Chi Square Distribution is 056 <blockquote> 057 A special case of the Gamma distribution. 058 <p> 059 <tt>p(x) = (1/g(f/2)) * (x/2)^(f/2-1) * exp(-x/2)</tt> with <tt>g(a)</tt> being the gamma function and <tt>f</tt> being the degrees of freedom. 060 </p><p> 061 Valid parameter ranges: <tt>freedom > 0</tt>. 062 </blockquote> 063 064 The above description of Chi Square is 065 <a href="doc-files/colt-copyright.htm">copyrighted</a>. 066 067 <p>A definition of the ChiSquare distribution can be found at 068 <a href="http://www.cern.ch/RD11/rkb/AN16pp/node31.html#SECTION000310000000000000000"><code>http://www.cern.ch/RD11/rkb/AN16pp/node31.html#SECTION000310000000000000000</code></a> 069 070 @author David Bauer and Kostas Oikonomou 071 @version $Id$ 072 @since Ptolemy II 4.1 073 @Pt.ProposedRating Red (cxh) 074 @Pt.AcceptedRating Red (cxh) 075 */ 076public class ColtChiSquare extends ColtRandomSource { 077 /** Construct an actor with the given container and name. 078 * @param container The container. 079 * @param name The name of this actor. 080 * @exception IllegalActionException If the actor cannot be contained 081 * by the proposed container. 082 * @exception NameDuplicationException If the container already has an 083 * actor with this name. 084 */ 085 public ColtChiSquare(CompositeEntity container, String name) 086 throws NameDuplicationException, IllegalActionException { 087 super(container, name); 088 089 output.setTypeEquals(BaseType.DOUBLE); 090 091 freedom = new PortParameter(this, "freedom", new DoubleToken(1.0)); 092 freedom.setTypeEquals(BaseType.DOUBLE); 093 new SingletonParameter(freedom.getPort(), "_showName") 094 .setToken(BooleanToken.TRUE); 095 096 freedom.moveToFirst(); 097 } 098 099 /////////////////////////////////////////////////////////////////// 100 //// ports and parameters //// 101 102 /** freedom. 103 * This has type double with default value 1.0. 104 */ 105 public PortParameter freedom; 106 107 /////////////////////////////////////////////////////////////////// 108 //// public methods //// 109 110 /** Send a random number with a ChiSquare distribution to the output. 111 * This number is only changed in the prefire() method, so it will 112 * remain constant throughout an iteration. 113 * @exception IllegalActionException If there is no director. 114 */ 115 @Override 116 public void fire() throws IllegalActionException { 117 freedom.update(); 118 super.fire(); 119 output.send(0, new DoubleToken(_current)); 120 } 121 122 /////////////////////////////////////////////////////////////////// 123 //// protected methods //// 124 125 /** Method that is called after _randomNumberGenerator is changed. 126 */ 127 @Override 128 protected void _createdNewRandomNumberGenerator() { 129 _generator = new ChiSquare(1.0, _randomNumberGenerator); 130 } 131 132 /** Generate a new random number. 133 * @exception IllegalActionException If parameter values are incorrect. 134 */ 135 @Override 136 protected void _generateRandomNumber() throws IllegalActionException { 137 double freedomValue = ((DoubleToken) freedom.getToken()).doubleValue(); 138 139 _current = _generator.nextDouble(freedomValue); 140 } 141 142 /////////////////////////////////////////////////////////////////// 143 //// private variables //// 144 145 /** The random number for the current iteration. */ 146 private double _current; 147 148 /** The random number generator. */ 149 private ChiSquare _generator; 150}