home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / applications / xlispstat / man.lzh / Manual / Mac / techreport.tex / node50_ct.html (.txt) < prev    next >
Encoding:
LaTeX Document  |  1990-10-12  |  8.4 KB  |  182 lines

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//en">
  2. <!--Converted with LaTeX2HTML 2022 (Released January 1, 2022) -->
  3. <HTML lang="en">
  4. <HEAD>
  5. <TITLE>Contents of Some Dynamic Simulations</TITLE>
  6. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
  7. <META NAME="viewport" CONTENT="width=device-width, initial-scale=1.0">
  8. <META NAME="Generator" CONTENT="LaTeX2HTML v2022">
  9. <LINK REL="STYLESHEET" HREF="techreport.css">
  10. <LINK REL="next" HREF="node51_mn.html">
  11. <LINK REL="previous" HREF="node49_mn.html">
  12. <LINK REL="up" HREF="node47_mn.html">
  13. <LINK REL="next" HREF="node51_mn.html">
  14. </HEAD>
  15. <BODY bgcolor="#ffffff" text="#000000" link="#9944EE" vlink="#0000ff" alink="#00ff00">
  16. <H2><A ID="SECTION000103000000000000000">
  17. Some Dynamic Simulations</A>
  18. </H2>
  19. In Section <A HREF="node44_ct.html#MorePlots.Dynamic"><IMG  ALT="[*]" SRC="crossref.png"></A> we used a loop to control a dynamic
  20. simulation in which points in a histogram of one variable were selected and
  21. deselected in the order of a second variable. Let's look at how to run the
  22. same simulation using a <EM>slider</EM> to control the simulation. 
  23. A slider is a modeless dialog box containing a scroll bar and a value
  24. display field. As the scroll bar is moved the displayed value is changed
  25. and an action is taken. The action is defined by an <EM>action function</EM>
  26. given to the scroll bar, a function that is called with one value, the
  27. current slider value, each time the value is changed by the user. There
  28. are two kinds of sliders, sequence sliders and interval sliders. Sequence
  29. sliders take a sequence (a list or a vector) and scroll up and down the
  30. sequence. The displayed value is either the current sequence element or
  31. the corresponding element of a display sequence. An interval slider
  32. dialog takes an interval, divides it into a grid and scrolls through the
  33. grid. By default a grid of around 30 points is used; the exact number and
  34. the interval end points are adjusted to give nice printed values. The
  35. current interval point is displayed in the display field.
  36. For our example lets use a sequence slider to scroll through the elements
  37. of the <TT> "2D hardness</TT> list in order and select the corresponding element
  38. of <TT> "2D abrasion-loss</TT>. The expression 
  39. <PRE>
  40. (def h (histogram abrasion-loss))
  41. </PRE>
  42. sets up a histogram and saves its plot object in the variable <TT> "2D h</TT>.
  43. The function <TT> "2D sequence-slider-dialog</TT> takes a list or vector and
  44. opens a sequence slider to scroll through its argument. To do
  45. something useful with this dialog we need to give it an action function 
  46. as the value of the keyword argument <TT> "2D :action</TT>. The function should
  47. take one argument, the current value of the sequence controlled by the
  48. slider. The expression
  49. <PRE> 
  50. (sequence-slider-dialog (order hardness) :action
  51.                         #'(lambda (i)
  52.                             (send h :unselect-all-points)
  53.                             (send h :point-selected i t)))
  54. </PRE>
  55. sets up a slider for moving the selected point in the <TT> "2D abrasion-loss</TT>
  56. histogram along the order of the <TT> "2D hardness</TT> variable. The histogram
  57. and scrollbar are shown in Figure <A HREF="node50_ct.html#Dynaplot1"><IMG  ALT="[*]" SRC="crossref.png"></A>.
  58. <DIV class="CENTER"><A ID="Dynaplot1"></A><A ID="1934"></A>
  59. <TABLE>
  60. <CAPTION class="BOTTOM"><STRONG>Figure:</STRONG>
  61. Slider-controlled animation of a histogram.</CAPTION>
  62. <TR><TD><IMG
  63.  STYLE="" SRC="img3.png"
  64.  ALT="\begin{figure}\centering
  65. \vspace{3.8in}
  66. \end{figure}"></TD></TR>
  67. </TABLE>
  68. </DIV>
  69. The action function is called every time the slider is moved. It is
  70. called with the current element of the sequence 
  71. <TT> "2D (order hardness)</TT>, the index of the point to select. It clears all
  72. current selections and then selects the point specified in the call from
  73. the slider. The body of the function is almost identical to the body of
  74. the <TT> "2D dotimes</TT> loop used in Section <A HREF="node44_ct.html#MorePlots.Dynamic"><IMG  ALT="[*]" SRC="crossref.png"></A>. The
  75. slider is thus an interactive, graphically controlled version of this
  76. loop.
  77. As another example, suppose we would like to examine the effect of
  78. changing the exponent in a Box-Cox power transformation
  79. <P><!-- MATH
  80.  \begin{displaymath}
  81. h(x) = \left\{
  82. \begin{array}{cl}
  83. \frac{\textstyle x^{\lambda} - 1}{\textstyle \lambda} & \mbox{if $x \not= 0$}\\
  84. \log(x)                         & \mbox{otherwise}
  85. \end{array}
  86. \right.
  87. \end{displaymath}
  88. <DIV ALIGN="CENTER">
  89. <I>h</I>(<I>x</I>) = <IMG
  90.  STYLE="" SRC="img8.png"
  91.  ALT="$\displaystyle \left\{\vphantom{
  92. \begin{array}{cl}
  93. \frac{\textstyle x^{\lambda} ...
  94. ...& \mbox{if $x \not= 0$}\\
  95. \log(x) & \mbox{otherwise}
  96. \end{array}}\right.$"><IMG
  97.  STYLE="" SRC="img9.png"
  98.  ALT="$\displaystyle \begin{array}{cl}
  99. \frac{\textstyle x^{\lambda} - 1}{\textstyle \lambda} & \mbox{if $x \not= 0$}\\
  100. \log(x) & \mbox{otherwise}
  101. \end{array}$">
  102. </DIV><P></P>
  103. on a probability plot. As a first step we might define a function to
  104. compute the power transformation and normalize the result to fall
  105. between zero and one:
  106. <PRE>
  107. (defun bc (x p)
  108.   (let* ((bcx (if (< (abs p) .0001) (log x) (/ (^ x p) p)))
  109.          (min (min bcx))
  110.          (max (max bcx)))
  111.     (/ (- bcx min) (- max min))))
  112. </PRE>
  113. This definition uses the <TT> "2D let*</TT> form to establish some local variable
  114. bindings. The <TT> "2D let*</TT> form is like the <TT> "2D let</TT> form used above
  115. except that <TT> "2D let*</TT> defines its variables sequentially, allowing a
  116. variable to be defined in terms of other variables already defined in the
  117. <TT> "2D let*</TT> expression. In this case the variables <TT> "2D min</TT> and
  118. <TT> "2D max</TT> are defined in terms of the variable <TT> "2D bcx</TT>. 
  119. Next we need a set of positive numbers to transform. Let's use a sample of
  120. thirty observations from a <!-- MATH
  121.  $\chi^{2}_{4}$
  122. <I>χ</I><SUP>2</SUP><SUB>4</SUB> distribution and order the
  123. observations
  124. <PRE>
  125. (def x (sort-data (chisq-rand 30 4)))
  126. </PRE>
  127. The normal quantiles of the expected uniform order statistics are given by
  128. <PRE>
  129. (def r (normal-quant (/ (iseq 1 30) 31)))
  130. </PRE>
  131. and a probability plot of the untransformed data is constructed using
  132. <PRE>
  133. (def myplot (plot-points r (bc x 1)))
  134. </PRE>
  135. Since the power used is 1 the function <TT> "2D bc</TT> just rescales the data.
  136. There are several ways to set up a slider dialog to control the power
  137. parameter. The simplest approach is to use the function
  138. <TT> "2D interval-slider-dialog</TT>:
  139. <PRE>
  140. (interval-slider-dialog (list -1 2)
  141.                         :points 20
  142.                         :action #'(lambda (p)
  143.                                    (send myplot :clear nil)
  144.                                    (send myplot :add-points r (bc x p))))
  145. </PRE>
  146. <TT> "2D interval-slider-dialog</TT> takes a list of two numbers, the lower and
  147. upper bound of an interval. The action function is called with the current
  148. position in the interval.
  149. This approach works fine on a Mac II but may be a bit slow on a Mac Plus
  150. or a Mac SE. An alternative is to precompute the transformations for a
  151. list of powers and then use the precomputed values in the display. For
  152. example, using the powers defined in
  153. <PRE>
  154. (def powers (rseq -1 2 16))
  155. </PRE>
  156. we can compute the transformed data for each power and save the result as
  157. the variable <TT> "2D xlist</TT>:
  158. <PRE>
  159. (def xlist (mapcar #'(lambda (p) (bc x p)) powers))
  160. </PRE>
  161. The function <TT> "2D mapcar</TT> is one of several <EM>mapping functions</EM>
  162. available in Lisp. The first argument to <TT> "2D mapcar</TT> is a function.
  163. The second argument is a list. <TT> "2D mapcar</TT> takes the function, applies it
  164. to each element of the list and returns the list of the results
  165. <A NAME="tex2html128" HREF="footnode_mn.html#foot1432" TARGET="footer"><SUP>22</SUP></A>. 
  166. Now we can use a sequence slider to move up and down the
  167. transformed values in <TT> "2D xlist</TT>:
  168. <PRE>
  169. (sequence-slider-dialog xlist
  170.                         :display powers
  171.                         :action #'(lambda (x)
  172.                                    (send myplot :clear nil)
  173.                                    (send myplot :add-points r x)))
  174. </PRE>
  175. Note that we are scrolling through a list of lists and the element passed
  176. to the action function is thus the list of current transformed values. We
  177. would not want to see these values in the display field on the slider, so
  178. I have used the keyword argument <TT> "2D :display</TT> to specify an alternative
  179. display sequence, the powers used in the transformation.
  180. </BODY>
  181. </HTML>
  182.