001/* An actor that outputs a random sequence with a StudentT 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.StudentT;
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//// StudentT
042
043/**
044 Produce a random sequence with a StudentT 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/StudentT.html">cern.jet.random.StudentT</a> object with
053 a freedom of 1.0.
054
055 @author David Bauer and Kostas Oikonomou
056 @version $Id$
057 @since Ptolemy II 4.1
058 @Pt.ProposedRating Red (cxh)
059 @Pt.AcceptedRating Red (cxh)
060 */
061public class ColtStudentT extends ColtRandomSource {
062    /** Construct an actor with the given container and name.
063     *  @param container The container.
064     *  @param name The name of this actor.
065     *  @exception IllegalActionException If the actor cannot be contained
066     *   by the proposed container.
067     *  @exception NameDuplicationException If the container already has an
068     *   actor with this name.
069     */
070    public ColtStudentT(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        output.setTypeEquals(BaseType.DOUBLE);
075
076        freedom = new PortParameter(this, "freedom", new DoubleToken(1.0));
077        freedom.setTypeEquals(BaseType.DOUBLE);
078        new SingletonParameter(freedom.getPort(), "_showName")
079                .setToken(BooleanToken.TRUE);
080
081        freedom.moveToFirst();
082    }
083
084    ///////////////////////////////////////////////////////////////////
085    ////                     ports and parameters                  ////
086
087    /** freedom.
088     *  This has type double, initially with value 1.0.
089     */
090    public PortParameter freedom;
091
092    ///////////////////////////////////////////////////////////////////
093    ////                         public methods                    ////
094
095    /** Send a random number with a StudentT distribution to the output.
096     *  This number is only changed in the prefire() method, so it will
097     *  remain constant throughout an iteration.
098     *  @exception IllegalActionException If there is no director.
099     */
100    @Override
101    public void fire() throws IllegalActionException {
102        freedom.update();
103        super.fire();
104        output.send(0, new DoubleToken(_current));
105    }
106
107    ///////////////////////////////////////////////////////////////////
108    ////                         protected methods                 ////
109
110    /** Method that is called after _randomNumberGenerator is changed.
111     */
112    @Override
113    protected void _createdNewRandomNumberGenerator() {
114        _generator = new StudentT(1.0, _randomNumberGenerator);
115    }
116
117    /** Generate a new random number.
118     *  @exception IllegalActionException If parameter values are incorrect.
119     */
120    @Override
121    protected void _generateRandomNumber() throws IllegalActionException {
122        double freedomValue = ((DoubleToken) freedom.getToken()).doubleValue();
123
124        _current = _generator.nextDouble(freedomValue);
125    }
126
127    ///////////////////////////////////////////////////////////////////
128    ////                         private variables                 ////
129
130    /** The random number for the current iteration. */
131    private double _current;
132
133    /** The random number generator. */
134    private StudentT _generator;
135}