home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / MCAD254.ZIP / STAT / CHISQR.MCD < prev    next >
Encoding:
Text File  |  1988-11-29  |  4.2 KB  |  165 lines

  1. .MCD 20000 0
  2. .CMD PLOTFORMAT logs=0,0 subdivs=1,1 size=5,15 type=l
  3. .CMD FORMAT  rd=d ct=10 im=i et=3 zt=15 pr=3 mass length time charge
  4. .CMD SET ORIGIN 0
  5. .CMD SET TOL 0.001000
  6. .CMD MARGIN 0
  7. .CMD LINELENGTH 78
  8. .CMD SET PRNCOLWIDTH 8
  9. .CMD SET PRNPRECISION 4
  10. .TXT 0 41 1 39 
  11. a1,38,39,37
  12. Copyright (c) 1988 by MathSoft, Inc.
  13. .TXT 1 41 1 72 
  14. a1,71,78,70
  15. /EQUATIONS FOR CALCULATING VALUES AND PERCENTAGE POINTS OF CHI-SQUARE
  16. .TXT 1 -63 1 38 
  17. a1,37,77,36
  18. CHI-SQUARE TEST FOR GOODNESS OF FIT
  19. .TXT 2 -18 2 79 
  20. a2,78,77,127
  21. The chi-square test can be used to test the fit of observations to a set of 
  22. predicted frequencies or to a given distribution.
  23. .EQN 1 81 1 22 
  24. Pchi_index_~1;10
  25. .EQN 0 26 1 31 
  26. altneg(n)~if(mod(n,2),-1,1)
  27. .TXT 2 -107 3 80 
  28. a3,79,77,224
  29. For example, suppose that an experiment can have five different outcomes, and 
  30. that the predicted frequencies of these outcomes are in the vector E while 
  31. the actual counts obtained in a set of experimental trials are in V:
  32. .EQN 0 79 10 81 
  33. Pchi_s(chi2,v)~(chi2/2)^(v/2)/Γ(v/2)*(2/v+Pchi_index_$(altneg(Pchi_index_)*(chi2/2)^Pchi_index_)/(Pchi_index_!*(v/2+Pchi_index_)))
  34. .EQN 4 -79 5 11 
  35. E~({5,1}÷.1÷.15÷.2÷.45÷.1)
  36. .EQN 0 16 5 10 
  37. V~({5,1}÷9÷18÷17÷39÷12)
  38. .EQN 3 29 5 28 
  39. chisquare~{55}((E*{55}V-V)^2/(E*{55}V)){49}
  40. .TXT 3 -45 1 43 
  41. a1,42,72,41
  42. The chi-square statistic for E and V is:
  43. .TXT 3 0 1 20 
  44. a1,19,45,18
  45. For this E and V,
  46. .EQN 0 20 1 20 
  47. chisquare={18994}?
  48. .TXT 2 60 1 15 
  49. a1,14,78,13
  50. [Ctrl][PgDn]
  51. .TXT 1 -80 1 15 
  52. a1,14,77,13
  53. [Ctrl][PgDn]
  54. .EQN 1 81 9 49 
  55. Pchi_i(chi2,v)~1/(2^(v/2)*Γ(v/2))*(2&chi2`t^(v/2-1)*exp(-t/2)&t)
  56. .TXT 1 -80 2 77 
  57. a2,76,76,116
  58. The probability of a greater value of chisquare for observations randomly 
  59. drawn from the predicted distribution is
  60. .EQN 3 6 1 20 
  61. df~length(V)-1
  62. .EQN 0 30 1 28 
  63. Qchi(chisquare,df)={18994}?
  64. .TXT 2 -36 3 79 
  65. a3,78,76,174
  66. so the fit in this case is good.  (Here Qchi gives the tail of the chi 
  67. square distribution.  The number of degrees of freedom is one less than the 
  68. number of observations.)
  69. .TXT 4 0 2 80 
  70. a2,79,77,119
  71. To test the fit at the α level, choose α and find the α percentage point for 
  72. the chi-square distribution:            
  73. .EQN 0 80 1 15 
  74. TOL~.000001
  75. .EQN 2 0 1 73 
  76. Pchi(chi2,v)~if(chi2<2,Pchi_s(chi2,v),Pchi_s(2,v)+Pchi_i(chi2,v))
  77. .EQN 1 -68 1 9 
  78. α~.01
  79. .EQN 0 28 1 19 
  80. X(α,df)={18994}?
  81. .EQN 1 40 1 33 
  82. Qchi(chi2,v)~1-Pchi(chi2,v)
  83. .TXT 1 -80 2 76 
  84. a2,75,76,119
  85. Since the calculated chisquare is less that this value, the observations 
  86. agree with the model according to this test.
  87. .EQN 1 80 1 26 
  88. x~if(df≈1,.5,1.5*df)
  89. .EQN 0 29 1 34 
  90. X(α,df)~root(Qchi(x,df)-α,x)
  91. .TXT 2 -109 1 15 
  92. a1,14,76,13
  93. [Ctrl][PgDn]
  94. .TXT 2 0 5 79 
  95. a5,78,76,342
  96. To test the fit of data to a given distribution, you can use MathCAD's 
  97. histogram function to group the data into intervals, and compare the actual 
  98. count with the expected count using the chi-square statistic.  The equations 
  99. below illustrate the process by testing a set of 200 normal(0,1) deviates 
  100. for a fit with the normal distribution.
  101. .TXT 6 0 1 24 
  102. a1,23,78,22
  103. 1. Draw the deviates:
  104. .EQN 0 24 1 13 
  105. i~0;199
  106. .EQN 1 0 3 37 
  107. d[i~\(-2*ln(rnd(1)))*cos(2*π*rnd(1))
  108. .TXT 4 -24 1 23 
  109. a1,22,76,21
  110. 2. Set up intervals:
  111. .EQN 0 24 1 11 
  112. n~0;7
  113. .EQN 2 0 2 21 
  114. bins[n~.5*n-1.75
  115. .EQN 0 24 2 13 
  116. bins[0~-10
  117. .EQN 0 16 2 12 
  118. bins[7~10
  119. .TXT 3 -64 1 15 
  120. a1,14,76,13
  121. [Ctrl][PgDn]
  122. .TXT 2 0 3 23 
  123. a3,22,20,53
  124. 3. Find the expected 
  125.    count in each 
  126.    interval:
  127. .EQN 0 24 1 11 
  128. k~0;6
  129. .EQN 2 0 2 42 
  130. N[k~200*(cnorm(bins[(k+1))-cnorm(bins[k))
  131. .TXT 3 -24 3 22 
  132. a3,21,19,54
  133. 4. Find the actual  
  134.    count in each    
  135.    interval
  136. .EQN 0 24 1 18 
  137. A~hist(bins,d)
  138. .EQN 1 23 7 16 
  139. N=?
  140. .EQN 0 18 7 12 
  141. A=?
  142. .TXT 3 -65 1 38 
  143. a1,37,76,36
  144. The expected and actual counts are:
  145. .TXT 2 0 1 41 
  146. a1,40,75,39
  147. 5. Calculate the chi square statistic:
  148. .EQN 1 4 5 25 
  149. chisquare~{55}((N-A)^2/N){49}
  150. .EQN 3 31 1 20 
  151. chisquare={18994}?
  152. .TXT 3 -35 1 15 
  153. a1,14,75,13
  154. [Ctrl][PgDn]
  155. .TXT 3 1 1 72 
  156. a1,71,74,70
  157. 6. Choose α and find the α percentage point for 6 degrees of freedom:
  158. .EQN 2 4 1 9 
  159. α~.25
  160. .EQN 0 13 1 17 
  161. X(α,6)={18994}?
  162. .TXT 2 -17 1 68 
  163. a1,67,75,66
  164. If chisquare is less than X(α,6), the fit is good at the α level.
  165.