home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1995 / ARCHIVE95.iso / discs / pipeline / 6_12 / ReadMe < prev    next >
Text File  |  1993-07-25  |  10KB  |  224 lines

  1. %OP%VS4.13 (28-Apr-92), Gerald L Fitton, R4000 5966 9904 9938 
  2. %OP%DP0
  3. %OP%IRY
  4. %OP%PL0
  5. %OP%HM0
  6. %OP%FM0
  7. %OP%BM0
  8. %OP%LM4
  9. %OP%PT1
  10. %OP%PDPipeLine
  11. %OP%WC1026,2262,648,1748,0,0,0,0
  12. %CO:A,72,72%
  13. %C%Curve Fitting
  14. %C%by Gerald L Fitton
  15. Keywords:
  16. Best Fit Fitton
  17.  
  18. Introduction
  19. For over a year now I have been asked to explain how to use the 
  20. PipeDream functions linest(y,x) and trend({c,m},x).  I kept promising 
  21. myself that I ought to get around to it but it has taken a problem sent 
  22. to me by John Nottage to finally make my mind up that now the time has 
  23. surely come.
  24.  
  25. I have decided that, in addition to describing the 'line of best fit' 
  26. functions linest(y,x) and the trend({c,m},x) function, I could use the 
  27. opportunity to show you how using Arrays and Names often make formulae 
  28. much more usable and readable.
  29.  
  30. Best Straight line
  31.  
  32. Let's start by creating the spreadsheet shown in the screen shot, 
  33. figureá1, below.
  34.  
  35. The formula for a straight line is yá=ám*xá+ác.  The parameter c is 
  36. called 'the constant term' and m is called 'the gradient'.  In the 
  37. range A12A17 I have inserted half a dozen values of x and, in the range 
  38. B12B17, the corresponding values of y have been calculated by putting c 
  39. and m (from B6 and B7) into the formula yá=ám*xá+ác.  This formula is 
  40. used in many engineering and financial applications.  An example is 
  41. "totalácostá=ámarginalácostáof a unitá*ánumberáofáunitsá+áfixedácost".  
  42. The "marginal cost of a unit" is the gradient, m, and in this example, 
  43. it is the extra cost of producing one more unit.  The "fixed cost", c, 
  44. is the cost you incur just getting ready to make some units (such as 
  45. machinery) but it doesn't include the "marginal costs" such as raw 
  46. material and labour.
  47.  
  48. Names
  49.  
  50. In slot B12 I could have typed B$7*A12+B$6 and then replicated it down 
  51. through the range B12B17 - but I haven't done that.  In my version slot 
  52. B12 contains the formula set_value(y,m*x+c).  I suggest that my version
  53. makes the spreadsheet much more readable but at the expense of a little 
  54. more effort on the part of the writer of the application.
  55.  
  56. It may help you follow the next paragraph if you refer to the screen 
  57. shot, figureá2 below.
  58.  
  59. The four letters within the set_value(y,m*x+c) function, y, m, x and c, 
  60. are all PipeDream Names.  If, in the final version of this speadsheet, 
  61. you were to place the pointer over the italic f (just to the left of 
  62. the tick and cross and to the right of the PipeDream logo - near the 
  63. formula line) and click the mouse select (left) button then a sub menu 
  64. called 'functions' will open and, at the bottom of the list, you will 
  65. see 'Edit name'.  If you were to run the pointer through the arrow to 
  66. the right of 'Edit name' you would see that there are seven names 
  67. defined.  These are x, y, y_est, m, c, c_est, m_est.  You could run the 
  68. pointer through, say, c, and you would see that the definition of the 
  69. name c is the content of the slot B6.  Similarly you could establish 
  70. that the name x is the range of slots A12A17.  This range contains the 
  71. values of x!
  72.  
  73. To define a name such as x as the range of slots A12A17 you click on 
  74. the italic f, run the pointer through 'Define name', type x in the 
  75. dialogue box alongside 'Name:', you type A12A17 into the dialogue box 
  76. alongside 'Refers to:' and finally click on the OK box.
  77.  
  78. The names I have defined are: xáasáA12A17, yáasáB12B17, 
  79. y_estáasáD12D17, máasáB7, cáasáB6, c_estáasáD6 and m_estáasáD7.
  80.  
  81. The set_value(,) function
  82.  
  83. The function set_value(,) is a simple way of evaluating a function many 
  84. times and 'poking' the answers into a range of slots.  It is 
  85. particularly simple when used with names.  Yes, I know it takes time to 
  86. define the names, but, for large ranges particularly, it is worth it.  
  87. The function in slot B12 is  set_value(y,m*x+c).  Having defined the 
  88. names as slots or ranges of slots  set_value(y,m*x+c)  calculates all 
  89. the y values from m*x+c and 'pokes' the answers into the range of slots 
  90. defined as y.  What is 'clever' about PipeDream is that each value in 
  91. the y range is individually calculated using its corresponding x and 
  92. the result 'poked' into the 'correct' y slot.
  93.  
  94. The set_value(,) formula is written only once and does not have to be 
  95. replicated (or copied) down the range of slots.  Using names in this 
  96. way reduces the size of the PipeDream file in memory (the function 
  97. appears in only one slot) and on the disc.  Because the file is smaller 
  98. the loading time and the time to recalculate is also reduced.  I 
  99. recommend using names and set_value(,) to you as a way of reducing 
  100. memory usage, disc space and speeding up loading, saving and 
  101. recalculation.
  102.  
  103. The linest(y,x) function
  104.  
  105. In slot D6 I have entered the formula  index(linest(y,x),1,1).  Let's 
  106. deal with  linest(y,x)  first.
  107.  
  108. The function  linest(y,x)  takes as its arguments only two ranges of 
  109. values, in our case y and x.  Please note that the y range is the first 
  110. argument.  In our simple example x is a single column of values, a more 
  111. complex version (which we'll deal with another day) uses one y range 
  112. and a many column x range!  When used in this simple way  linest(y,x)  
  113. returns an array of two numbers in a single slot.  The two numbers are 
  114. the row array  {c_est,m_est}  where c_est and m_est are the 'constant 
  115. term' and the 'gradient' of a straight line which is the 'best' 
  116. straight line for the set of points.  Strictly it is the 'least squares 
  117. regression line of y on x'!
  118.  
  119. On page 250 of the First Edition of the PipeDreamá4 Reference Guide the 
  120. function  linest(,,,,)  is shown with five arguments.  All too often I 
  121. have been asked how to use the last three arguments.  The answer is 
  122. that you can't because, in PipeDream version 4.13 they don't exist and, 
  123. as far as I know, they are never likely to.  The function linest(y,x) 
  124. can take only two arguments but the second argument, x, can consist of 
  125. many columns.  Let's deal with what is called 'multivariate regression' 
  126. another day.  For now let it suffice that the arguments stats, 
  127. constant_parameters and measured_errors of the First Edition do not and 
  128. will not exist.
  129.  
  130. The index(array,col,row) function
  131.  
  132. To 'split up' the array returned by linest(y,x) into its two separate 
  133. terms, {c_est,m_est}, we need to use the index(array,col,row) function.  
  134. The index function I have entered into slot D6 is 
  135. index(linest(y,x),1,1).  Since linest(y,x)á=á{c_est,m_est},
  136. array, index(linest(y,x),1,1)á=á(index({c_est,m_est},1,1).
  137.  
  138. The array  {c_est,m_est}  has only one row and so the last (third) 
  139. argument of index({c_est,m_est},1,1) must be 1.  The second argument, 
  140. 1, selects the first element of the array which is c_est.  Hence 
  141. index(linest(y,x),1,1)á=ác_est.
  142.  
  143. The value in D6 is our best estimate of the constant term c_est for the 
  144. 'least squares regression line of y on x'!
  145.  
  146. Slot D7 contains  index(linest(y,x),2,1).  The only difference is the 
  147. second argument, 2, which selects the gradient, m_est.  The value in D7 
  148. is our best estimate of the gradient, m_est.
  149.  
  150. A point you should realise here is that I've 'cheated' by using 'exact' 
  151. values of y calculated from the formula yá=ám*xá+ác so that, if I have 
  152. the 'right' formulae in column D, then my 'estimated' values for c_est 
  153. and m_est are bound to be 'exact' and equal to the values of c and m in 
  154. B6 and B7!
  155.  
  156. The trend({c,m},x) function
  157. From the screen shot, figureá1, you will see that slot D12 contains the 
  158. formula set_value(y_est,trend({c_est,m_est},x)) - and we've dealt with 
  159. set_value(,).  Note that y_est is the name of the range D12D17, so 
  160. let's have a look at trend({c_est,m_est},x).
  161.  
  162. The function trend({c_est,m_est},x) returns a column array when x is a 
  163. column (it is in this case) or a row array when x is a row.  The column 
  164. array returned by trend({c_est,m_est},x) is exactly the same array as 
  165. that given by the formula {m_est*xá+ác_est}.  We saw how {m*xá+ác} 
  166. returned an array when we looked at slot B12.  I think that, when I
  167. know the values of c and m (separately), then I would prefer to enter  
  168. m_est*xá+ác_est  (without the curly brackets) instead of  
  169. trend({c_est,m_est},x);  it's shorter and it does exactly the same 
  170. thing.  On another occasion when we have a look at the function 
  171. logest(y,x) to produce log linear and log log graphs we shall see that 
  172. this second approach has its advantages.  The function trend(,) has the 
  173. advantage if you don't want to use the values of {c,m} separately.  In 
  174. those cases you can write  trend(linest(y,x),x); and we'll find that we 
  175. can't do that with log linear and log log curve fitting.
  176.  
  177. To summarise where we are up to now.  In the spreadsheet of figureá1 
  178. there are only five slots containing formula.  These are:
  179.  
  180. B12á=áset_value(y, m*x+c)
  181. C12á=áset_value(C12C17,x)
  182. D12á=áset_value(y_est,trend({c_est,m_est},x))
  183. D6  = index(linest(y,x),1,1)
  184. D7  = index(linest(y,x),2,1)
  185.  
  186. There are seven PipeDream Names which have been defined.  These are:
  187.  
  188. xá=áA12A17
  189. yá=áb12B17
  190. y_está=áD12D17
  191. má=áB7
  192. cá=áB6
  193. m_está=áD7
  194. c_est =áD6
  195.  
  196. You can alter the values in B6 and B7 and in the range A12A17 but, for 
  197. now, you should change nothing else.
  198.  
  199. Best Straight line
  200. Up to now the y values in B12B17 have been generated from a formula 
  201. which uses the values c and m in B6 and B7.
  202.  
  203. Place the cursor in slot B12, delete the formula and replace it with 
  204. the number 1.  Nothing much should happen but the values of y are now 
  205. no longer calculated from the formula using c and m from B6 and B7.  
  206. Enter a few y values which are different from but approximately the 
  207. same as those generated by the formula and, when the sheet has 
  208. recalculated, you will find that y and y_est do not quite match.  If 
  209. you plot the values of y against x you will no longer have a straight 
  210. line (unless you're lucky) but plotting y_est against x will give you a 
  211. perfect straight line, the 'best' straight line for the x and y values.
  212.  
  213. Don't save the modified sheet unless you change the name!
  214.  
  215. Charting
  216.  
  217. To help you visualise what is going on you might like to try creating a 
  218. chart with two sets of data.  The first set should use the x and y 
  219. values in A12B17 and you can plot it in red.  The second set should 
  220. use the data in C12D17 and you can plot it in blue.
  221.  
  222. You won't see the blue points until you modify the y data as described 
  223. in the previous paragraph.
  224.