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

  1.  
  2. (*:Version: Mathematica 2.0 *)
  3.  
  4. (*:Name: Statistics`ConfidenceIntervals *)
  5.  
  6. (*:Title: Confidence Intervals Related to the Normal Distribution *)
  7.  
  8. (*:Author:
  9.   Dave Withoff (Wolfram Research), February, 1990.
  10.   Revised by March, 1991.
  11. *)
  12.  
  13. (*:Legal:
  14.   Copyright (c) 1990-1991, Wolfram Research, Inc.
  15. *)
  16.  
  17. (*:Reference: usage messages only. *)
  18.  
  19. (*:Summary:
  20.   Confidence intervals for elementary reference distributions derived
  21.   from the normal distribution.  Distributions represented are
  22.   NormalDistribution, StudentTDistribution, ChiSquareDistribution,
  23.   and FRatioDistribution.
  24. *)
  25.  
  26. (*:Keywords: confidence interval, confidence level *)
  27.  
  28. (*:Requirements: No special system requirements. *)
  29.  
  30. (*:Warning: None. *)
  31.  
  32. (*:Sources: Basic statistics texts *)
  33.  
  34. BeginPackage["Statistics`ConfidenceIntervals`",
  35.              "Statistics`DescriptiveStatistics`",
  36.              "Statistics`NormalDistribution`",
  37.              "Statistics`Common`HypothesisCommon`"]
  38.  
  39. MeanCI::usage =
  40. "MeanCI[list, options] returns a list {min, max}
  41. representing a confidence interval for the population mean, using
  42. the entries in list as a sample drawn from the population."
  43.  
  44. Options[MeanCI] = {ConfidenceLevel -> .95,
  45.     KnownStandardDeviation -> None, KnownVariance -> None}
  46.  
  47. VarianceCI::usage =
  48. "VarianceCI[list] returns a list {min, max} representing
  49. a confidence interval for the population variance, using the entries in
  50. list as a sample drawn from the population."
  51.  
  52. Options[VarianceCI] = {ConfidenceLevel -> .95}
  53.  
  54. MeanDifferenceCI::usage =
  55. "MeanDifferenceCI[list1, list2, options] returns a list {min, max}
  56. representing a confidence interval for the difference Mean[list1]
  57. - Mean[list2], using the entries in list1 as a sample from the
  58. first population and the entries in list2 as a sample from the
  59. second population."
  60.  
  61. Options[MeanDifferenceCI] = {ConfidenceLevel -> .95,
  62.     KnownStandardDeviation -> None, KnownVariance -> None,
  63.     EqualVariances -> False}
  64.  
  65. VarianceRatioCI::usage =
  66. "VarianceRatioCI[list1, list2, options] returns a list representing
  67. a confidence interval for the ratio of estimated population variances,
  68. Variance[list1]/Variance[list2], using the entries in list1 as a
  69. sample from the first population and the entries in list2 as a
  70. sample from the second population."
  71.  
  72. Options[VarianceRatioCI] := {ConfidenceLevel -> .95}
  73.  
  74. StudentTCI::usage =
  75. "StudentTCI[mean, se, dof, ConfidenceLevel -> c] returns
  76. a list {min, max} representing a confidence interval at 
  77. confidence level c for the population mean, based on a sample mean,
  78. its standard error se and dof degrees of freedom. This function is used
  79. by MeanCI when the population variance is estimated from
  80. the sample."
  81.  
  82. Options[StudentTCI] = {ConfidenceLevel -> .95}
  83.  
  84. NormalCI::usage =
  85. "NormalCI[mean, sd, ConfidenceLevel -> c] returns a list
  86. {min, max} representing a confidence interval at confidence level
  87. c for the population mean, based on the sample mean and its standard
  88. deviation. This function is used by MeanCI when the population 
  89. variance is specified."
  90.  
  91. Options[NormalCI] = {ConfidenceLevel -> .95}
  92.  
  93. ChiSquareCI::usage =
  94. "ChiSquareCI[var, dof, ConfidenceLevel -> c] returns a list
  95. {min, max} representing a confidence interval at confidence level c
  96. for the population variance, based on a sample with dof degrees of
  97. freedom and unbiased variance estimate var.  This function is used
  98. by VarianceCI."
  99.  
  100. Options[ChiSquareCI] = {ConfidenceLevel -> .95}
  101.  
  102. FRatioCI::usage =
  103. "FRatioCI[ratio, numdof, dendof, ConfidenceLevel -> c] returns
  104. a list {min, max} representing a confidence interval at confidence
  105. level c for ratio, where ratio is a ratio of sample variances, numdof is the
  106. number of numerator degrees of freedom, and dendof is the number of
  107. denominator degrees of freedom.  This function is used by
  108. VarianceRatioCI."
  109.  
  110. Options[FRatioCI] = {ConfidenceLevel -> .95}
  111.  
  112.  
  113.     (*  Options  *)
  114.  
  115. ConfidenceLevel::usage =
  116. "ConfidenceLevel is an option used to specify the confidence level
  117. in functions that return statistical confidence intervals."
  118.  
  119. Begin["`Private`"]
  120.  
  121. (* Confidence Intervals for one Mean *)
  122.  
  123. MeanCI[list_List, options___] :=
  124.     Block[{mean, n, c, var0, sd0},
  125.         mean = Mean[list];
  126.         n = Length[list];
  127.         {c, var0, sd0} =
  128.             {ConfidenceLevel, KnownVariance, KnownStandardDeviation} /.
  129.                 {options} /. Options[MeanCI];
  130.         If[!(0 <= N[c] <= 1), Message[MeanCI::clev, c]];
  131.         If[sd0 =!= None,
  132.             If[var0 =!= None, Message[MeanCI::varsd]];
  133.             var0 = sd0^2
  134.         ];
  135.         If[var0 === None,
  136.             Return[StudentTCI[mean, StandardErrorOfSampleMean[list],
  137.                                     n-1, options]],
  138.             Return[NormalCI[mean, Sqrt[var0/n], options]]
  139.         ]
  140.     ]
  141.  
  142. MeanCI::varsd = "Warning: KnownStandardDeviation and KnownVariance
  143. have both been specified.  KnownVariance will be ignored."
  144.  
  145. MeanCI::clev = "Warning: ConfidenceLevel specification `1` is not
  146. in the expected range 0 <= ConfidenceLevel <= 1."
  147.  
  148. StudentTCI[mean_, se_, dof_, options___] :=
  149.     Block[{c = ConfidenceLevel /. {options} /. Options[StudentTCI], h},
  150.         If[N[se] <= 0, Message[StudentTCI::badse, se]];
  151.         h = se Quantile[StudentTDistribution[dof], (1+c)/2];
  152.         {mean-h, mean+h}]
  153.  
  154. StudentTCI::badse = "Warning: Standard error `1` is normally
  155. a positive quantity."
  156.  
  157. NormalCI[mean_, sd_, options___] :=
  158.     Block[{c = ConfidenceLevel /. {options} /. Options[NormalCI], h},
  159.         h = sd Quantile[NormalDistribution[0, 1], (1+c)/2];
  160.         {mean-h, mean+h}]
  161.  
  162. (* Confidence Intervals for one Variance *) 
  163.  
  164. VarianceCI[list_List, options___] :=
  165.     Block[
  166.         {dof, var,
  167.          c = ConfidenceLevel /. {options} /.
  168.              Options[VarianceCI]
  169.         },
  170.         dof = Length[list] - 1;
  171.         var = Variance[list];
  172.         ChiSquareCI[var, dof, options]
  173.     ]
  174.  
  175. ChiSquareCI[var_, dof_, options___] :=
  176.     Block[
  177.         {c = ConfidenceLevel /. {options} /. Options[ChiSquareCI],
  178.          min, max,sumsq
  179.         },
  180.     sumsq = dof var;
  181.         max = sumsq/Quantile[ChiSquareDistribution[dof], (1-c)/2];
  182.         min = sumsq/Quantile[ChiSquareDistribution[dof], (1+c)/2];
  183.         {min, max}
  184.     ]
  185.  
  186. (* Comparison of two Means *)
  187.  
  188. MeanDifferenceCI[list1_List, list2_List, options___] :=
  189.     Block[{diff, var0, sd0},
  190.         diff = Mean[list1] -  Mean[list2];
  191.         {var0, sd0} = {KnownVariance, KnownStandardDeviation} /.
  192.             {options} /. Options[MeanDifferenceCI];
  193.         If[sd0 =!= None,
  194.             If[var0 =!= None, Message[MeanDifferenceCI::varsd]];
  195.             var0 = sd0^2
  196.         ];
  197.         mdInterval[list1, list2, diff, var0, options]
  198.     ]
  199.  
  200. MeanDifferenceCI::varsd = "Warning: KnownStandardDeviation
  201. and KnownVariance have both been specified.  KnownVariance will be ignored."
  202.  
  203. mdInterval[list1_, list2_, diff_, None, options___] :=
  204.     Block[
  205.         {var1, var2, dof, stderr, n1, n2,
  206.          equalvar = EqualVariances /. {options} /.
  207.              Options[MeanDifferenceCI]
  208.         },
  209.         var1 = Variance[list1];
  210.         var2 = Variance[list2];
  211.         n1 = Length[list1];
  212.         n2 = Length[list2];
  213.         If[TrueQ[equalvar],
  214.             dof = n1 + n2 - 2;
  215.             stderr = Sqrt[(((n1-1) var1 + (n2-1) var2) / dof)
  216.                               (1/n1 + 1/n2)],
  217.         (* else *)
  218.             stderr = Sqrt[var1/n1 + var2/n2];
  219.             dof = stderr^4 /
  220.                   ((var1/n1)^2/(n1-1) + (var2/n2)^2/(n2-1))
  221.         ];
  222.         StudentTCI[diff, stderr, dof, options]
  223.     ]
  224.  
  225. mdInterval[list1_, list2_, diff_, var0_, options___] :=
  226.     Block[{var1, var2, stderr},
  227.         {var1, var2} = VariancePair[var0];
  228.         stderr = Sqrt[var1/Length[list1] + var2/Length[list2]];
  229.         NormalCI[diff, stderr, options]
  230.     ] /; var0 =!= None
  231.  
  232. VariancePair[{var0_}] := {var0, var0}
  233.  
  234. VariancePair[var0_List] := {var0[[1]], var0[[2]]} /; Length[var0] >= 2
  235.  
  236. VariancePair[var0_] := {var0, var0} /; Head[var0] =!= List
  237.  
  238. (* Ratio of two variances *)
  239.  
  240. VarianceRatioCI[numlist_List, denlist_List, options___] :=
  241.     Block[
  242.         {ratio, numdof, dendof,
  243.          c = ConfidenceLevel /. {options} /.
  244.              Options[VarianceRatioCI]
  245.         },
  246.         ratio = Variance[numlist]/Variance[denlist];
  247.         numdof = Length[numlist] - 1;
  248.         dendof = Length[denlist] - 1;
  249.         FRatioCI[ratio, numdof, dendof, options]
  250.     ]
  251.  
  252. FRatioCI[ratio_, numdof_, dendof_, options___] :=
  253.     Block[
  254.         {c = ConfidenceLevel /. {options} /. Options[FRatioCI],
  255.          min, max
  256.         },
  257.         min = 1/Quantile[FRatioDistribution[numdof, dendof], (1+c)/2];
  258.         max = 1/Quantile[FRatioDistribution[numdof, dendof], (1-c)/2];
  259.         {min, max} ratio
  260.     ]
  261.  
  262. End[]
  263. SetAttributes[MeanCI ,ReadProtected];
  264. SetAttributes[VarianceCI ,ReadProtected];
  265. SetAttributes[MeanDifferenceCI ,ReadProtected];
  266. SetAttributes[VarianceRatioCI ,ReadProtected];
  267. SetAttributes[StudentTCI ,ReadProtected];
  268. SetAttributes[NormalCI ,ReadProtected];
  269. SetAttributes[ChiSquareCI ,ReadProtected];
  270. SetAttributes[FRatioCI ,ReadProtected];
  271. SetAttributes[ConfidenceLevel ,ReadProtected];
  272. Protect[MeanCI, VarianceCI,MeanDifferenceCI,
  273.         VarianceRatioCI,StudentTCI,
  274.         NormalCI, ChiSquareCI,FRatioCI,ConfidenceLevel];
  275.  
  276.  
  277. EndPackage[]
  278.  
  279.