home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / STATISTI.PAK / DESCRIPT.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  11.6 KB  |  386 lines

  1.  
  2. (*:Version: Mathematica 2.0 *)
  3.  
  4. (*:Name: Statistics`DescriptiveStatistics` *)
  5.  
  6. (*:Title: Basic Descriptive Statistics *)
  7.  
  8. (*:Author:
  9.   Original version by Stephen Wolfram (Wolfram Research), April, 1989.
  10.   Revised by David Withoff (Wolfram Research), February, 1990.
  11. *)
  12.  
  13. (*:Legal: Copyright (c) 1990, Wolfram Research Inc. *)
  14.  
  15. (*:Reference: usage messages only. *)
  16.  
  17. (*:Summary:
  18.   Descriptive statistics (location and shape statistics) for
  19.   samples represented by lists.
  20. *)
  21.  
  22. (*:Keywords: maximum likelihood estimate, unbiased estimate *)
  23.  
  24. (*:Requirements: no special system requirements. *)
  25.  
  26. (*:Warning: none. *)
  27.  
  28. (*:Sources: basic statistics texts. *)
  29.  
  30. BeginPackage["Statistics`DescriptiveStatistics`"]
  31.  
  32. (* Location statistics *)
  33.  
  34. LocationReport::usage =
  35. "LocationReport[list] gives location statistics for list."
  36.  
  37. Mean::usage =
  38. "Mean[list] gives the mean of the entries in list."
  39.  
  40. GeometricMean::usage = 
  41. "GeometricMean[list] gives the geometric mean of the entries in list."
  42.  
  43. HarmonicMean::usage = 
  44. "HarmonicMean[list] gives the harmonic mean of the entries in list."
  45.  
  46. RootMeanSquare::usage = 
  47. "RootMeanSquare[list] gives the root mean square of values in list."
  48.  
  49. TrimmedMean::usage =
  50. "TrimmedMean[list, f] gives the mean of the entries in list, with a
  51. fraction f of entries at each end dropped.  TrimmedMean[list, {f1, f2}]
  52. finds the mean with fractions f1 and f2 trimmed off."
  53.  
  54. Quantile::usage =
  55. "Quantile[list, q] gives the q-th quantile of the entries in list."
  56.  
  57. InterpolatedQuantile::usage =
  58. "InterpolatedQuantile[list, q] gives the q-th quantile of the
  59. probability distribution inferred by linear interpolation of the
  60. entries in list."
  61.  
  62. Median::usage =
  63. "Median[list] gives the median of the entries in list."
  64.  
  65. If[StringPosition[Mode::usage, "Mode[list] gives the mode"] === {},
  66. Mode::usage = Mode::usage <> " " <>
  67. "Mode[list] gives the mode of the entries in list.  A list of modes
  68. is returned if the data is bimodal, trimodal, or multimodal."
  69. ]
  70.  
  71. Quartiles::usage =
  72. "Quartiles[list] gives a list of the quartiles of the entries in list."
  73.  
  74. (* Dispersion statistics *)
  75.  
  76. DispersionReport::usage =
  77. "DispersionReport[list] gives dispersion statistics for list."
  78.  
  79. SampleRange::usage =
  80. "SampleRange[list] gives the range of values in list."
  81.  
  82. Variance::usage =
  83. "Variance[list] gives the variance of the entries in list.
  84. Division by n-1 (rather than n) is used, giving an unbiased
  85. estimate of the population variance (use VarianceMLE[list]
  86. for a maximum likelihood estimate)."
  87.  
  88. VarianceMLE::usage =
  89. "VaranceMLE[list] gives the variance of the entries in list.
  90. Division by n (rather than n-1) is used, giving a maximum
  91. likelihood estimate of the population variance (use Variance[list]
  92. for an unbiased estimate)."
  93.  
  94. VarianceOfSampleMean::usage =
  95. "VarianceOfSampleMean[list] returns an unbiased estimate of the variance
  96. of the sample mean, using the entries in list as a sample from the
  97. population."
  98.  
  99. StandardDeviation::usage =
  100. "StandardDeviation[list] gives the standard deviation of the entries
  101. in list.  Division by n-1 is used."
  102.  
  103. StandardDeviationMLE::usage =
  104. "StandardDeviationMLE[list] returns the standard deviation of the entries
  105. in list.  Division by n is used, giving a miximum likelihood estimate of
  106. the population standard deviation."
  107.  
  108. StandardErrorOfSampleMean::usage =
  109. "StandardErrorOfSampleMean[list] returns an unbiased estimate of the
  110. standard error (standard deviation) of the sample mean, using the entries
  111. in list as a sample from the population."
  112.  
  113. MeanDeviation::usage =
  114. "MeanDeviation[list] gives the mean absolute deviation about the
  115. mean of the entries in list."
  116.  
  117. MedianDeviation::usage =
  118. "MedianDeviation[list] gives the median absolute deviation about
  119. the median of the entries in list."
  120.  
  121. InterquartileRange::usage =
  122. "InterquartileRange[list] gives the difference between the upper
  123. and lower quartiles for the entries in list."
  124.  
  125. QuartileDeviation::usage =
  126. "QuartileDeviation[list] gives the quartile deviation, or
  127. semi-interquartile range of the entries in list."
  128.  
  129. (* Shape statistics *)
  130.  
  131. ShapeReport::usage =
  132. "ShapeReport[list] gives shape statistics for list."
  133.  
  134. CentralMoment::usage =
  135. "CentralMoment[list, n] gives the n-th central moment of
  136. the entries in list with respect to their mean."
  137.  
  138. Skewness::usage =
  139. "Skewness[list] gives the coefficient of skewness of the
  140. entries in list."
  141.  
  142. PearsonSkewness1::usage =
  143. "PearsonSkewness1[list] gives Pearson's first coefficient of
  144. skewness of the entries in list."
  145.  
  146. PearsonSkewness2::usage = 
  147. "PearsonSkewness2[list] gives Pearson's second coefficient of 
  148. skewness of the entries in list."
  149.  
  150. QuartileSkewness::usage =  
  151. "QuartileSkewness[list] gives the quartile coefficient of 
  152. skewness of the entries in list." 
  153.  
  154. Kurtosis::usage =
  155. "Kurtosis[list] gives the kurtosis coefficient for the entries
  156. in list."
  157.  
  158. KurtosisExcess::usage =
  159. "KurtosisExcess[list] gives the kurtosis excess for the entries
  160. in list."
  161.  
  162.  
  163. Begin["`private`"]
  164.  
  165. Mean[list_] := Apply[Plus, list] / Length[list]  /; 
  166.                 VectorQ[list] && Length[list] > 0
  167.  
  168. GeometricMean[list_] := Apply[Times, list^(1/Length[list])]  /;
  169.                 VectorQ[list] && Length[list] > 0
  170.  
  171. HarmonicMean[list_] := Length[list] / Apply[Plus, 1/list] /;
  172.                 VectorQ[list] && Length[list] > 0
  173.  
  174. RootMeanSquare[list_] := Sqrt[Mean[list^2]]
  175.  
  176. TrimmedMean[list_, f_] := TrimmedMean[list, {f, f}]  /; 2 f < 1 
  177.  
  178. TrimmedMean[list_, {f1_, f2_}] :=
  179.     Block[{s, n},
  180.         s = Sort[list] ;
  181.         n = Length[list] ;
  182.         s = Take[s, { 1 + Floor[f1 n], 
  183.                 n - Floor[f2 n] } ] ;
  184.         Mean[s]
  185.     ]  /; VectorQ[list] && Length[list] > 2 && (f1 + f2) < 1
  186.  
  187. Quantile[list_, q_] := Part[ Sort[list], -Floor[-q Length[list]] ] /;
  188.             VectorQ[list] && 0 < q < 1 && Length[list] > 2
  189.  
  190. InterpolatedQuantile[list_, q_] :=
  191.     Block[{s, n},
  192.         s = Sort[list] ;
  193.         n = Length[list] ;
  194.         Which[
  195.             q < 1/(2n),  First[s],
  196.             q > 1-1/(2n),  Last[s],
  197.             True,
  198.                 Block[{f, i, u},
  199.                     u = n q + 1/2 ;
  200.                     i = Floor[u] ;
  201.                     f = u - i ;
  202.                     (1 - f) s[[i]] + f s[[i+1]]
  203.                 ]
  204.         ]
  205.     ]  /; VectorQ[list] && 0 < q < 1 && Length[list] > 2
  206.  
  207. Median[list_] := Sort[list][[(Length[list]+1)/2]] /;
  208.             VectorQ[list] && OddQ[Length[list]]
  209.  
  210. Median[list_] := 
  211.     Block[{s, n},
  212.         s = Sort[list] ;
  213.         n = Length[list] ;
  214.         (s[[n/2]] + s[[n/2 + 1]]) / 2
  215.     ] /; VectorQ[list] && EvenQ[Length[list]]
  216.  
  217. Unprotect[Mode]
  218. Mode[list_]:=Module[{u,c,ms},
  219.         u = Union[list];
  220.         c = Map[Count[list,#]&,u];
  221.         ms = Cases[Transpose[{u,c}],{val_,Max[c]}->val];
  222.         If[Max[c] > 1,
  223.             If[Length[ms] == 1,ms[[1]],ms],
  224.             {}]
  225.     ] /;VectorQ[list]&&Length[list]>0
  226.  
  227. LocationReport[list_] :=
  228.     {
  229.     Mean -> Mean[list] ,
  230.     HarmonicMean -> HarmonicMean[list] ,
  231.     Median -> Median[list]
  232.     }   /;  VectorQ[list]
  233.  
  234.  
  235. Quartiles[list_] := {InterpolatedQuantile[list, 1/4],
  236.             InterpolatedQuantile[list, 1/2],
  237.             InterpolatedQuantile[list, 3/4]}  /; VectorQ[list]
  238.  
  239. VarianceMLE[list_] := Mean[ (list - Mean[list])^2 ] /;
  240.                                 VectorQ[list] && Length[list] > 0
  241.  
  242. Variance[list_] :=
  243.     Apply[Plus, (list - Mean[list])^2]/(Length[list] - 1) /;
  244.                                 VectorQ[list] && Length[list] > 1
  245.  
  246. VarianceOfSampleMean[args___] :=
  247.     Block[{answer = iVarianceOfSampleMean[args]},
  248.         answer /; answer =!= Fail
  249.     ]   
  250.  
  251. iVarianceOfSampleMean[list_?VectorQ] :=
  252.     1/Length[list] Variance[list] /; Length[list] > 1
  253.  
  254. iVarianceOfSampleMean[bad___] :=
  255.     Block[{arglist = {bad}},
  256.         Which[
  257.             Length[arglist] =!= 1,
  258.                 Message[VarianceOfSampleMean::argct],
  259.             arglist[[1,0]] =!= List,
  260.                 Message[VarianceOfSampleMean::notlist],
  261.             Length[arglist[[1]]] <= 1,
  262.                 Message[VarianceOfSampleMean::short],
  263.             True,
  264.                 Message[VarianceOfSampleMean::err]
  265.         ];
  266.         Fail
  267.     ]   
  268.  
  269. VarianceOfSampleMean::argct = "Incorrect number of arguments;
  270. one argument is expected."
  271.  
  272. VarianceOfSampleMean::notlist = "Argument is not a list."
  273.  
  274. VarianceOfSampleMean::short = "Argument list must have length greater
  275. than one."
  276.  
  277. VarianceOfSampleMean::err = "Parameters did not match required form."
  278.  
  279.  
  280. StandardDeviation[list_] := Sqrt[Variance[list]]
  281.  
  282. StandardDeviationMLE[list_]:= Sqrt[VarianceMLE[list]]
  283.  
  284. StandardErrorOfSampleMean[list_List] := Sqrt[VarianceOfSampleMean[list]]
  285.  
  286. SampleRange[list_] := Max[list] - Min[list]  /; 
  287.                 VectorQ[list] && Length[list] > 0
  288.  
  289. MeanDeviation[list_] := Mean[ Abs[list - Mean[list]] ]  /; 
  290.                 VectorQ[list] && Length[list] > 0
  291.  
  292. MedianDeviation[list_] := Median[ Abs[list - Median[list]] ] /;
  293.                 VectorQ[list] && Length[list] > 0
  294.  
  295. InterquartileRange[list_] := 
  296.     InterpolatedQuantile[list, 3/4] - InterpolatedQuantile[list, 1/4]  /;
  297.                             VectorQ[list]
  298.  
  299. QuartileDeviation[list_] :=
  300.     InterquartileRange[list] / 2
  301.  
  302. DispersionReport[list_] :=
  303.     {
  304.     Variance -> Variance[list] ,
  305.     StandardDeviation -> StandardDeviation[list] ,
  306.     SampleRange -> SampleRange[list] ,
  307.     MeanDeviation -> MeanDeviation[list] ,
  308.     MedianDeviation -> MedianDeviation[list] ,
  309.     QuartileDeviation -> QuartileDeviation[list]
  310.     }   /; VectorQ[list]
  311.  
  312.  
  313. CentralMoment[list_, n_] := Mean[ (list - Mean[list])^n ] /; VectorQ[list]
  314.  
  315. Skewness[list_] := CentralMoment[list, 3] / StandardDeviation[list]^3
  316.  
  317. PearsonSkewness1[list_] :=
  318.     3 (Mean[list] - Mode[list]) / StandardDeviation[list]
  319.  
  320. PearsonSkewness2[list_] :=
  321.     3 (Mean[list] - Median[list]) / StandardDeviation[list]
  322.  
  323. QuartileSkewness[list_] := 
  324.     Block[{q1,q2,q3},
  325.         {q1,q2, q3} = Quartiles[list] ;
  326.         (q3 - 2 q2 + q1) / (q3 - q1)
  327.     ] /; VectorQ[list] && Length[list] > 0
  328.         
  329. Kurtosis[list_] :=    
  330.     CentralMoment[list, 4] / Variance[list]^2
  331.  
  332. KurtosisExcess[list_] := Kurtosis[list] - 3
  333.  
  334.  
  335. ShapeReport[list_] :=
  336.     {
  337.     Skewness -> Skewness[list] ,
  338.     QuartileSkewness -> QuartileSkewness[list] ,
  339.     KurtosisExcess -> KurtosisExcess[list]
  340.     }
  341.  
  342. End[ ]
  343.  
  344. SetAttributes[LocationReport , ReadProtected];
  345. SetAttributes[ GeometricMean, ReadProtected];
  346. SetAttributes[ HarmonicMean, ReadProtected];
  347. SetAttributes[ RootMeanSquare, ReadProtected];
  348. SetAttributes[ TrimmedMean, ReadProtected];
  349. SetAttributes[ InterpolatedQuantile, ReadProtected];
  350. SetAttributes[ Mean, ReadProtected];
  351. SetAttributes[ Median, ReadProtected];
  352. SetAttributes[ Mode, ReadProtected];
  353. SetAttributes[ Quartiles, ReadProtected];
  354. SetAttributes[ DispersionReport, ReadProtected];
  355. SetAttributes[ SampleRange, ReadProtected];
  356. SetAttributes[ MeanDeviation, ReadProtected];
  357. SetAttributes[ MedianDeviation, ReadProtected];
  358. SetAttributes[ InterquartileRange, ReadProtected];
  359. SetAttributes[ Variance, ReadProtected];
  360. SetAttributes[ VarianceOfSampleMean, ReadProtected];
  361. SetAttributes[ StandardErrorOfSampleMean, ReadProtected];
  362. SetAttributes[ StandardDeviation, ReadProtected];
  363. SetAttributes[ Quantile, ReadProtected];
  364. SetAttributes[ QuartileDeviation, ReadProtected];
  365. SetAttributes[ ShapeReport, ReadProtected];
  366. SetAttributes[ CentralMoment, ReadProtected];
  367. SetAttributes[ Skewness, ReadProtected];
  368. SetAttributes[ PearsonSkewness1 , ReadProtected];
  369. SetAttributes[ PearsonSkewness2, ReadProtected];
  370. SetAttributes[ QuartileSkewness, ReadProtected];
  371. SetAttributes[ Kurtosis, ReadProtected];
  372. SetAttributes[ KurtosisExcess, ReadProtected];
  373.  
  374. Protect[LocationReport, GeometricMean, HarmonicMean,RootMeanSquare, 
  375.     TrimmedMean, InterpolatedQuantile,Mean, Median,Mode,Quartiles, 
  376.     DispersionReport,SampleRange,MeanDeviation, MedianDeviation,
  377.     InterquartileRange, Variance, Quantile,StandardErrorOfSampleMean,
  378.     VarianceOfSampleMean,
  379.     QuartileDeviation, ShapeReport, CentralMoment,
  380.     Skewness, PearsonSkewness1, PearsonSkewness2,
  381.     QuartileSkewness, Kurtosis, KurtosisExcess];
  382.  
  383. EndPackage[ ]
  384.  
  385.  
  386.