home *** CD-ROM | disk | FTP | other *** search
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//en">
- <!--Converted with LaTeX2HTML 2022 (Released January 1, 2022) -->
- <HTML lang="en">
- <HEAD>
- <TITLE>Contents of Some Dynamic Simulations</TITLE>
- <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
- <META NAME="viewport" CONTENT="width=device-width, initial-scale=1.0">
- <META NAME="Generator" CONTENT="LaTeX2HTML v2022">
- <LINK REL="STYLESHEET" HREF="techreport.css">
- <LINK REL="next" HREF="node51_mn.html">
- <LINK REL="previous" HREF="node49_mn.html">
- <LINK REL="up" HREF="node47_mn.html">
- <LINK REL="next" HREF="node51_mn.html">
- </HEAD>
- <BODY bgcolor="#ffffff" text="#000000" link="#9944EE" vlink="#0000ff" alink="#00ff00">
- <H2><A ID="SECTION000103000000000000000">
- Some Dynamic Simulations</A>
- </H2>
- In Section <A HREF="node44_ct.html#MorePlots.Dynamic"><IMG ALT="[*]" SRC="crossref.png"></A> we used a loop to control a dynamic
- simulation in which points in a histogram of one variable were selected and
- deselected in the order of a second variable. Let's look at how to run the
- same simulation using a <EM>slider</EM> to control the simulation.
- A slider is a modeless dialog box containing a scroll bar and a value
- display field. As the scroll bar is moved the displayed value is changed
- and an action is taken. The action is defined by an <EM>action function</EM>
- given to the scroll bar, a function that is called with one value, the
- current slider value, each time the value is changed by the user. There
- are two kinds of sliders, sequence sliders and interval sliders. Sequence
- sliders take a sequence (a list or a vector) and scroll up and down the
- sequence. The displayed value is either the current sequence element or
- the corresponding element of a display sequence. An interval slider
- dialog takes an interval, divides it into a grid and scrolls through the
- grid. By default a grid of around 30 points is used; the exact number and
- the interval end points are adjusted to give nice printed values. The
- current interval point is displayed in the display field.
- For our example lets use a sequence slider to scroll through the elements
- of the <TT> "2D hardness</TT> list in order and select the corresponding element
- of <TT> "2D abrasion-loss</TT>. The expression
- <PRE>
- (def h (histogram abrasion-loss))
- </PRE>
- sets up a histogram and saves its plot object in the variable <TT> "2D h</TT>.
- The function <TT> "2D sequence-slider-dialog</TT> takes a list or vector and
- opens a sequence slider to scroll through its argument. To do
- something useful with this dialog we need to give it an action function
- as the value of the keyword argument <TT> "2D :action</TT>. The function should
- take one argument, the current value of the sequence controlled by the
- slider. The expression
- <PRE>
- (sequence-slider-dialog (order hardness) :action
- #'(lambda (i)
- (send h :unselect-all-points)
- (send h :point-selected i t)))
- </PRE>
- sets up a slider for moving the selected point in the <TT> "2D abrasion-loss</TT>
- histogram along the order of the <TT> "2D hardness</TT> variable. The histogram
- and scrollbar are shown in Figure <A HREF="node50_ct.html#Dynaplot1"><IMG ALT="[*]" SRC="crossref.png"></A>.
- <DIV class="CENTER"><A ID="Dynaplot1"></A><A ID="1934"></A>
- <TABLE>
- <CAPTION class="BOTTOM"><STRONG>Figure:</STRONG>
- Slider-controlled animation of a histogram.</CAPTION>
- <TR><TD><IMG
- STYLE="" SRC="img3.png"
- ALT="\begin{figure}\centering
- \vspace{3.8in}
- \end{figure}"></TD></TR>
- </TABLE>
- </DIV>
- The action function is called every time the slider is moved. It is
- called with the current element of the sequence
- <TT> "2D (order hardness)</TT>, the index of the point to select. It clears all
- current selections and then selects the point specified in the call from
- the slider. The body of the function is almost identical to the body of
- the <TT> "2D dotimes</TT> loop used in Section <A HREF="node44_ct.html#MorePlots.Dynamic"><IMG ALT="[*]" SRC="crossref.png"></A>. The
- slider is thus an interactive, graphically controlled version of this
- loop.
- As another example, suppose we would like to examine the effect of
- changing the exponent in a Box-Cox power transformation
- <P><!-- MATH
- \begin{displaymath}
- h(x) = \left\{
- \begin{array}{cl}
- \frac{\textstyle x^{\lambda} - 1}{\textstyle \lambda} & \mbox{if $x \not= 0$}\\
- \log(x) & \mbox{otherwise}
- \end{array}
- \right.
- \end{displaymath}
- <DIV ALIGN="CENTER">
- <I>h</I>(<I>x</I>) = <IMG
- STYLE="" SRC="img8.png"
- ALT="$\displaystyle \left\{\vphantom{
- \begin{array}{cl}
- \frac{\textstyle x^{\lambda} ...
- ...& \mbox{if $x \not= 0$}\\
- \log(x) & \mbox{otherwise}
- \end{array}}\right.$"><IMG
- STYLE="" SRC="img9.png"
- ALT="$\displaystyle \begin{array}{cl}
- \frac{\textstyle x^{\lambda} - 1}{\textstyle \lambda} & \mbox{if $x \not= 0$}\\
- \log(x) & \mbox{otherwise}
- \end{array}$">
- </DIV><P></P>
- on a probability plot. As a first step we might define a function to
- compute the power transformation and normalize the result to fall
- between zero and one:
- <PRE>
- (defun bc (x p)
- (let* ((bcx (if (< (abs p) .0001) (log x) (/ (^ x p) p)))
- (min (min bcx))
- (max (max bcx)))
- (/ (- bcx min) (- max min))))
- </PRE>
- This definition uses the <TT> "2D let*</TT> form to establish some local variable
- bindings. The <TT> "2D let*</TT> form is like the <TT> "2D let</TT> form used above
- except that <TT> "2D let*</TT> defines its variables sequentially, allowing a
- variable to be defined in terms of other variables already defined in the
- <TT> "2D let*</TT> expression. In this case the variables <TT> "2D min</TT> and
- <TT> "2D max</TT> are defined in terms of the variable <TT> "2D bcx</TT>.
- Next we need a set of positive numbers to transform. Let's use a sample of
- thirty observations from a <!-- MATH
- $\chi^{2}_{4}$
- <I>χ</I><SUP>2</SUP><SUB>4</SUB> distribution and order the
- observations
- <PRE>
- (def x (sort-data (chisq-rand 30 4)))
- </PRE>
- The normal quantiles of the expected uniform order statistics are given by
- <PRE>
- (def r (normal-quant (/ (iseq 1 30) 31)))
- </PRE>
- and a probability plot of the untransformed data is constructed using
- <PRE>
- (def myplot (plot-points r (bc x 1)))
- </PRE>
- Since the power used is 1 the function <TT> "2D bc</TT> just rescales the data.
- There are several ways to set up a slider dialog to control the power
- parameter. The simplest approach is to use the function
- <TT> "2D interval-slider-dialog</TT>:
- <PRE>
- (interval-slider-dialog (list -1 2)
- :points 20
- :action #'(lambda (p)
- (send myplot :clear nil)
- (send myplot :add-points r (bc x p))))
- </PRE>
- <TT> "2D interval-slider-dialog</TT> takes a list of two numbers, the lower and
- upper bound of an interval. The action function is called with the current
- position in the interval.
- This approach works fine on a Mac II but may be a bit slow on a Mac Plus
- or a Mac SE. An alternative is to precompute the transformations for a
- list of powers and then use the precomputed values in the display. For
- example, using the powers defined in
- <PRE>
- (def powers (rseq -1 2 16))
- </PRE>
- we can compute the transformed data for each power and save the result as
- the variable <TT> "2D xlist</TT>:
- <PRE>
- (def xlist (mapcar #'(lambda (p) (bc x p)) powers))
- </PRE>
- The function <TT> "2D mapcar</TT> is one of several <EM>mapping functions</EM>
- available in Lisp. The first argument to <TT> "2D mapcar</TT> is a function.
- The second argument is a list. <TT> "2D mapcar</TT> takes the function, applies it
- to each element of the list and returns the list of the results
- <A NAME="tex2html128" HREF="footnode_mn.html#foot1432" TARGET="footer"><SUP>22</SUP></A>.
- Now we can use a sequence slider to move up and down the
- transformed values in <TT> "2D xlist</TT>:
- <PRE>
- (sequence-slider-dialog xlist
- :display powers
- :action #'(lambda (x)
- (send myplot :clear nil)
- (send myplot :add-points r x)))
- </PRE>
- Note that we are scrolling through a list of lists and the element passed
- to the action function is thus the list of current transformed values. We
- would not want to see these values in the display field on the slider, so
- I have used the keyword argument <TT> "2D :display</TT> to specify an alternative
- display sequence, the powers used in the transformation.
- </BODY>
- </HTML>
-