/* @(#)SuccessRunsExperiment.java Copyright (C) 2005 Kyle Siegrist Department of Mathematical Sciences University of Alabama in Huntsville This program is part of Virtual Laboratories in Probability and Statistics, http://www.math.uah.edu/stat/, a project partially supported by the National Science Foundation under grant number DUE-0089377. This program is licensed under a Creative Commons License. Basically, you are free to copy, distribute, and modify this program, and to make commercial use of the program. However you must give proper attribution. See http://creativecommons.org/licenses/by/2.0/ for more information. */ package edu.uah.math.experiments; import java.awt.FlowLayout; import java.awt.event.MouseEvent; import java.awt.Color; import java.io.Serializable; import javax.swing.JToolBar; import javax.swing.JComboBox; import javax.swing.event.ChangeEvent; import java.awt.event.ItemEvent; import java.awt.Dimension; import edu.uah.math.distributions.Domain; import edu.uah.math.distributions.GeometricDistribution; import edu.uah.math.distributions.RandomVariable; import edu.uah.math.devices.RecordTable; import edu.uah.math.devices.Timeline; import edu.uah.math.devices.RandomVariableGraph; import edu.uah.math.devices.RandomVariableTable; import edu.uah.math.devices.Parameter; /** * This class models the sucess-runs Markov chain. A sequence of Bernoulli trials is * performed. The state at any time is the length of the current run of successes. * The limiting distribution is geometric. * @author Kyle Siegrist * @version December, 2005 */ public class SuccessRunsExperiment extends Experiment{ //Variables private int initialState = 0, currentState = 0; private double prob = 0.5; //Objects private RecordTable recordTable = new RecordTable(new String[] {"Time", "X"}); private JToolBar toolBar = new JToolBar("Parameter Toolbar"); private GeometricDistribution limitDistribution = new GeometricDistribution(prob, 1); private Timeline chain = new Timeline(limitDistribution.getDomain(), "x"); private RandomVariable state = new RandomVariable(limitDistribution, "X"); private RandomVariableGraph stateGraph = new RandomVariableGraph(state); private RandomVariableTable stateTable = new RandomVariableTable(state); private JComboBox modelChoice = new JComboBox(); //Scrollbars and labels private Parameter probScroll = new Parameter(0.1, 0.95, 0.01, prob, "Probability of success", "p"); /** * This method initialize the experiment, including the toolbar, record table, Markov chain, * histogram, and data table. */ public void init(){ super.init(); setName("Ehrenfest Experiment"); //Scrollbar probScroll.getSlider().addChangeListener(this); probScroll.applyDecimalPattern("0.00"); //Toolbar toolBar.setLayout(new FlowLayout(FlowLayout.LEFT)); toolBar.add(probScroll); addToolBar(toolBar); //Urn Chain chain.setToolTipText("Markov chain: at time 0, click to set the initial state"); chain.setPointSize(6); chain.setMargins(35, 20, 20, 20); chain.setMinimumSize(new Dimension(100, 30)); chain.addMouseListener(this); chain.setCurrentTimeColor(Color.red); addComponent(chain, 0, 0, 2, 1, 10, 0); //Random variable graph addComponent(stateGraph, 0, 1, 2, 1); //Record Table recordTable.setDescription("X: state"); addComponent(recordTable, 0, 2, 1, 1); //Data Table addComponent(stateTable, 1, 2, 1, 1); //Final actions validate(); reset(); } /** * This method returns basic information about the applet, including copyright * information, descriptive information, and instructions. * @return applet infomration */ public String getAppletInfo(){ return super.getAppletInfo() + "\n\n" + "Visit http://www.math.uah.edu/stat/applets/SuccessRunsExperiment.xhtml for more information\n" + "about the applet and for a mathematical discussion of the success-runs experiment."; } /** * This method handles the scrollbar events for changing the number of states. * @param e the change event */ public void stateChanged(ChangeEvent e){ if (e.getSource() == probScroll.getSlider()){ prob = probScroll.getValue(); limitDistribution.setProbability(1 - prob); chain.setDomain(limitDistribution.getDomain()); reset(); } } /** * This method sets the initial state when the user clicks on a ball. * @param e the mouse event */ public void mouseClicked(MouseEvent e){ if (e.getSource() == chain && getTime() == 0){ int x = (int)Math.rint(chain.getXScale(e.getX())); int u = (int)chain.getDomain().getUpperBound(); if (x < 0) x = 0; else if (x > u) x = u; initialState = x; reset(); } } /** * This method performs the next step in the process. A note is played, depending on * the state. */ public void step(){ doExperiment(); playNote(currentState); update(); } /** * This method resets the experiment, including the Markov chain, the record table * the histogram and data table. */ public void reset(){ super.reset(); currentState = initialState; chain.resetData(); chain.addTime(currentState, Color.red); chain.setCurrentTime(currentState); recordTable.reset(); recordTable.addRecord(new double[]{getTime(), initialState}); state.reset(); stateGraph.reset(); stateTable.reset(); } /** * This method defines the experiment. The Markov chain moves to the next state. */ public void doExperiment(){ super.doExperiment(); double p = Math.random(); if (p <= prob) currentState++; else currentState = 0; state.setValue(currentState); } /** * This method updates the experiment, including the record table, Markov chain, * histogram, and data table. */ public void update(){ super.update(); recordTable.addRecord(new double[]{getTime(), currentState}); chain.resetData(); chain.addTime(initialState, Color.blue); chain.addTime(currentState, Color.red); chain.setCurrentTime(currentState); stateGraph.repaint(); stateTable.repaint(); } }