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

  1.  
  2.  
  3. (*:Version: Mathematica 2.0 *)
  4.  
  5. (*:Name: Statistics`NormalDistribution *)
  6.  
  7. (*:Title: Statistical Distributions Derived from the Normal Distribution *)
  8.  
  9. (*:Author:
  10.   David Withoff (Wolfram Research), March, 1991
  11. *)
  12.  
  13. (*:Legal:
  14.   Copyright (c) 1991, Wolfram Research Inc.
  15. *)
  16.  
  17. (*:Reference: Usage messages only. *)
  18.  
  19. (*:Summary:
  20.   Properties and functionals of the four standard probability distributions
  21.   derived from the normal (Gaussian) distribution.  The four distributions
  22.   are the normal distribution, Student's t-distribution, the chi-square
  23.   distribution, and the F-ratio distribution.
  24. *)
  25.  
  26. (*:Keywords: continuous distribution, normal distribution *)
  27.  
  28. (*:Requirements: No special system requirements. *)
  29.  
  30. (*:Warning:
  31.   This package extends the definition of several descriptive statistics
  32.   functions and the definition of Random.  If the original usage messages
  33.   are reloaded, this change will not be reflected in the usage message,
  34.   although the extended functionality will remain.
  35. *)
  36.  
  37. (*:Note: Most functions provide numerical results. *)
  38.  
  39. (*:Sources:
  40.   Norman L. Johnson and Samuel Kotz, Continuous Univariate
  41.     Distributions, 1970
  42. *)
  43.  
  44. BeginPackage["Statistics`NormalDistribution`",
  45.              "Statistics`DescriptiveStatistics`",
  46.              "Statistics`Common`DistributionsCommon`",
  47.              "Statistics`InverseStatisticalFunctions`"]
  48.  
  49. ChiSquareDistribution::usage =
  50. "ChiSquareDistribution[n] represents the Chi-Square distribution
  51. with n degrees of freedom."
  52.  
  53. FRatioDistribution::usage =
  54. "FRatioDistribution[n1, n2] represents the F-ratio distribution with n1
  55. numerator degrees of freedom and n2 denominator degrees of freedom."
  56.  
  57. NormalDistribution::usage =
  58. "NormalDistribution[mu, sigma] represents the Normal (Gaussian)
  59. distribution with mean mu and standard deviation sigma."
  60.  
  61. StudentTDistribution::usage =
  62. "StudentTDistribution[n] represents Student's T distribution
  63. with n degrees of freedom."
  64.  
  65.         (*   Functions  *)
  66.  
  67. (* Introduce usage messages for new functions *)
  68.  
  69. Domain::usage =
  70. "Domain[distribution] gives the domain of the specified distribution."
  71.  
  72. PDF::usage =
  73. "PDF[distribution, x] gives the probability density function
  74. of the specified statistical distribution evaluated at x."
  75.  
  76. CDF::usage =
  77. "CDF[distribution, x] gives the cumulative distribution function of 
  78. distribution at the point x, defined as the integral of the probability 
  79. density of the the distribution from the lowest value in the domain to x."
  80.  
  81. CharacteristicFunction::usage =
  82. "CharacteristicFunction[distribution, t] gives the characteristic
  83. function of the specified statistical distribution as a function of
  84. the variable t."
  85.  
  86. PercentagePoint::usage =
  87. "PercentagePoint[distribution, q] gives the percentage point corresponding to
  88. percentage q of the specified statistical distribution.  This is equivalent to
  89. Quantile[distribution, (q+1)/2] for symmetrical distributions."
  90.  
  91. (*
  92.   Extend usage messages for existing functions.  Look for the
  93.   indicated phrase to determine if this has already been done.
  94. *)
  95.  
  96. If[StringPosition[Mean::usage, "specified statistical distribution"] === {},
  97.  
  98. Mean::usage = Mean::usage <> " " <>
  99. "Mean[distribution] gives the mean of the specified statistical distribution.";
  100.  
  101. Variance::usage = Variance::usage <> " " <>
  102. "Variance[distribution] gives the variance of the specified
  103. statistical distribution.";
  104.  
  105. StandardDeviation::usage = StandardDeviation::usage <> " " <>
  106. "StandardDeviation[distribution] gives the standard deviation of the
  107. specified statistical distribution.";
  108.  
  109. Skewness::usage = Skewness::usage <> " " <>
  110. "Skewness[distribution] gives the coefficient of skewness of the
  111. specified statistical distribution.";
  112.  
  113. Kurtosis::usage = Kurtosis::usage <> " " <>
  114. "Kurtosis[distribution] gives the coefficient of kurtosis of the
  115. specified statistical distribution.";
  116.  
  117. KurtosisExcess::usage = KurtosisExcess::usage <> " " <>
  118. "KurtosisExcess[distribution] gives the kurtosis excess for the
  119. specified statistical distribution.";
  120.  
  121. Quantile::usage =
  122. "Quantile[distribution, q] gives the q-th quantile of the specified
  123. statistical distribution."
  124. ]
  125.  
  126. (*
  127.   Extend the usage message of Random.  Look for the indicated phrase
  128.   to determine if this has already been done.
  129. *)
  130.  
  131. If[StringPosition[Random::usage, "specified statistical distribution"] === {},
  132.  
  133. Random::usage = Random::usage <> " " <>
  134. "Random[distribution] gives a random number with the specified
  135. statistical distribution."
  136. ]
  137.  
  138. Begin["`Private`"]
  139.  
  140. (* Chi-square Distribution *)
  141.  
  142. ChiSquareDistribution/: Domain[ChiSquareDistribution[___]] := {0, Infinity}
  143.  
  144. ChiSquareDistribution/: PDF[ChiSquareDistribution[n_], x_] :=
  145.     With[{result = x^(n/2-1) / (Exp[x/2] Sqrt[2]^n Gamma[n/2])},
  146.      If[NumberQ[N[result]],N[result],result]]   
  147.  
  148. ChiSquareDistribution/: CDF[ChiSquareDistribution[n_], x_] :=
  149.         With[{result = N[GammaRegularized[n/2, 0, x/2]]},
  150.     If[NumberQ[result],result,GammaRegularized[n/2, 0, x/2]]]
  151.  
  152. ChiSquareDistribution/: Mean[ChiSquareDistribution[n_]] := n
  153.  
  154. ChiSquareDistribution/: Variance[ChiSquareDistribution[n_]] := 2 n
  155.  
  156. ChiSquareDistribution/: StandardDeviation[ChiSquareDistribution[n_]] :=
  157.     With[{result = Sqrt[2 n]},
  158.         If[NumberQ[N[result]],N[result],result]]
  159.  
  160. ChiSquareDistribution/: Skewness[ChiSquareDistribution[n_]] := 
  161.     With[{result = 2 Sqrt[2/n]},
  162.         If[NumberQ[N[result]],N[result],result]]
  163.  
  164. ChiSquareDistribution/: Kurtosis[ChiSquareDistribution[n_]] := 
  165.     With[{result = 3 + 12/n},
  166.         If[NumberQ[N[result]],N[result],result]]
  167.  
  168. ChiSquareDistribution/: KurtosisExcess[ChiSquareDistribution[n_]] :=
  169.     With[{result = 12/n},
  170.         If[NumberQ[N[result]],N[result],result]]
  171.  
  172. ChiSquareDistribution/: CharacteristicFunction[
  173.                 ChiSquareDistribution[n_], t_] :=
  174.         With[{result = (1 - 2 I t)^(-n/2)},
  175.      If[NumberQ[N[result]],N[result],result]]  
  176.  
  177. ChiSquareDistribution/: Quantile[ChiSquareDistribution[n_], q_] :=
  178.     With[{result = 2 InverseGammaRegularized[n/2, 0, q]},
  179.         If[NumberQ[N[result]],N[result],result]]
  180.  
  181. ChiSquareDistribution/: Random[ChiSquareDistribution[n_]] :=
  182.     With[{result = 2 InverseGammaRegularized[n/2, 0, Random[]]},
  183.         If[NumberQ[N[result]],N[result],result]]
  184.  
  185. (* F-Distribution *)
  186.  
  187. FRatioDistribution/: Domain[FRatioDistribution[___]] = {0, Infinity}
  188.  
  189. FRatioDistribution/: PDF[FRatioDistribution[n1_, n2_], x_] :=
  190.        With[{result  =  n1^(n1/2) n2^(n2/2) x^(n1/2 - 1)/
  191.             ((n2 + n1 x)^((n1 + n2)/2) Beta[n1/2, n2/2])},
  192.     If[NumberQ[N[result]],N[result],result]]
  193.  
  194. FRatioDistribution/: CDF[FRatioDistribution[n1_, n2_], x_] :=
  195.         With[{result = BetaRegularized[n2/(n2 + n1 x), 1, n2/2, n1/2]},
  196.     If[NumberQ[N[result]],N[result],result]]
  197.  
  198. FRatioDistribution/: Mean[FRatioDistribution[n1_, n2_]] :=
  199.     With[{result = n2/(n2 - 2)},
  200.         If[NumberQ[N[result]],N[result],result]]
  201.  
  202. FRatioDistribution/: Variance[FRatioDistribution[n1_, n2_]] :=
  203.     With[{result = 2 n2^2 (n1+n2-2) / (n1 (n2-2)^2 (n2-4))},
  204.         If[NumberQ[N[result]],N[result],result]]
  205.  
  206. FRatioDistribution/: StandardDeviation[FRatioDistribution[n1_, n2_]] :=
  207.     With[{result = (n2/(n2-2)) Sqrt[2] Sqrt[n1+n2-2]/(Sqrt[n1] Sqrt[n2-4])},
  208.         If[NumberQ[N[result]],N[result],result]]
  209.  
  210. FRatioDistribution/: Skewness[FRatioDistribution[n1_, n2_]] :=
  211.     With[{result = (2 n1 + n2 - 2) Sqrt[8 (n2-4)]/
  212.         (Sqrt[n1] (n2-6) Sqrt[n1+n2-2])},
  213.             If[NumberQ[N[result]],N[result],result]]
  214.  
  215. FRatioDistribution/: Kurtosis[FRatioDistribution[n1_, n2_]] :=
  216.     With[{result = 12 ((n2-2)^2 (n2-4) + n1 (n1+n2-2) (5 n2 -22))/
  217.         (n1 (n2-6) (n2-8) (n1+n2-2)) + 3},
  218.             If[NumberQ[N[result]],N[result],result]]
  219.  
  220. FRatioDistribution/: KurtosisExcess[FRatioDistribution[n1_, n2_]] :=
  221.     With[{result = 12 ((n2-2)^2 (n2-4) + n1 (n1+n2-2) (5 n2 -22))/
  222.         (n1 (n2-6) (n2-8) (n1+n2-2))},
  223.             If[NumberQ[N[result]],N[result],result]]
  224.  
  225. FRatioDistribution/:
  226.         CharacteristicFunction[FRatioDistribution[n1_, n2_], t_] :=
  227.     With[{result = Hypergeometric1F1[n1/2, 1 - n2/2, -I t n2/n1]},
  228.     If[NumberQ[N[result]],N[result],result]]
  229.  
  230. FRatioDistribution/: Quantile[FRatioDistribution[n1_, n2_], q_] :=
  231.     With[{result = n2/n1 (1/InverseBetaRegularized[1, -q, n2/2, n1/2] - 1)},
  232.         If[NumberQ[N[result]],N[result],result]]
  233.  
  234. FRatioDistribution/: Random[FRatioDistribution[n1_, n2_]] :=
  235.     With[{result = n2/n1 (1/InverseBetaRegularized[1, -Random[],
  236.         n2/2, n1/2] - 1)},
  237.         If[NumberQ[N[result]],N[result],result]]
  238.  
  239. (* Normal distribution *)
  240.  
  241. NormalDistribution/: Domain[NormalDistribution[___]] = {-Infinity, Infinity}
  242.  
  243. NormalDistribution/: PDF[NormalDistribution[mu_:0, sigma_:1], x_] :=
  244.         With[{result = Exp[-((x-mu)/sigma)^2 / 2]/(sigma Sqrt[2Pi])},
  245.     If[NumberQ[N[result]],N[result],result]]
  246.  
  247. NormalDistribution/: CDF[NormalDistribution[mu_:0, sigma_:1], x_] :=
  248.         With[{result = (Erf[(x-mu)/(Sqrt[2] sigma)] + 1) / 2},
  249.     If[NumberQ[N[result]],N[result],result]]
  250.  
  251. NormalDistribution/: Mean[NormalDistribution[mu_:0, sigma_:1]] := mu
  252.  
  253. NormalDistribution/: StandardDeviation[
  254.                 NormalDistribution[mu_:0, sigma_:1]] := sigma
  255.  
  256. NormalDistribution/: Variance[NormalDistribution[mu_:0, sigma_:1]] := sigma^2
  257.  
  258. NormalDistribution/: Skewness[NormalDistribution[mu_:0, sigma_:1]] = 0
  259.  
  260. NormalDistribution/: Kurtosis[NormalDistribution[mu_:0, sigma_:1]] = 3
  261.  
  262. NormalDistribution/: KurtosisExcess[NormalDistribution[mu_:0, sigma_:1]] = 0
  263.  
  264. NormalDistribution/: CharacteristicFunction[
  265.                 NormalDistribution[mu_:0, sigma_:1], t_] :=
  266.           With[{result =  Exp[I mu t - sigma^2 t^2/2]},
  267.     If[NumberQ[N[result]],N[result],result]]
  268.  
  269. NormalDistribution/: Quantile[NormalDistribution[mu_:0, sigma_:1], u_] :=
  270.     With[{result = mu + Sqrt[2] sigma InverseErf[2 u - 1]},
  271.         If[NumberQ[N[result]],N[result],result]]
  272.  
  273. NormalDistribution/: Random[NormalDistribution[mu_:0, sigma_:1]] :=
  274.         N[mu + sigma Sqrt[-2 Log[Random[]]] Cos[2Pi Random[]]]
  275.  
  276. (* StudentT Distribution *)
  277.  
  278. StudentTDistribution/: Domain[StudentTDistribution[___]] =
  279.         {-Infinity, Infinity}
  280.  
  281. StudentTDistribution/: PDF[StudentTDistribution[n_], x_] :=
  282.         With[{result = 1/(Sqrt[n] Beta[n/2, 1/2]) Sqrt[n/(n+x^2)]^(n+1)},
  283.     If[NumberQ[N[result]],N[result],result]]
  284.  
  285. StudentTDistribution/: CDF[StudentTDistribution[n_], x_] := 
  286.          With[{result = (1 + Sign[x] BetaRegularized[n/(n+x^2), 1, n/2, 1/2])/2},
  287.     If[NumberQ[N[result]],N[result],result]]
  288.  
  289. StudentTDistribution/: Mean[StudentTDistribution[_]] = 0
  290.  
  291. StudentTDistribution/: Variance[StudentTDistribution[n_]] :=
  292.     With[{result = n/(n-2)},
  293.         If[NumberQ[N[result]],N[result],result]]
  294.  
  295. StudentTDistribution/: StandardDeviation[StudentTDistribution[n_]] :=
  296.     With[{result = Sqrt[n/(n-2)]},
  297.         If[NumberQ[N[result]],N[result],result]]
  298.  
  299. StudentTDistribution/: Skewness[StudentTDistribution[_]] = 0
  300.  
  301. StudentTDistribution/: Kurtosis[StudentTDistribution[n_]] :=
  302.     With[{result = 6/(n-4) + 3},
  303.         If[NumberQ[N[result]],N[result],result]]
  304.  
  305. StudentTDistribution/: KurtosisExcess[StudentTDistribution[n_]] := 
  306.     With[{result = 6/(n-4)},
  307.         If[NumberQ[N[result]],N[result],result]]
  308.  
  309. StudentTDistribution/:
  310.         CharacteristicFunction[StudentTDistribution[n_], t_] :=
  311.        With[{result =  2 BesselK[n/2, Abs[t] Sqrt[n]] (Abs[t] Sqrt[n]/2)^(n/2) /
  312.         Gamma[n/2]},
  313.     If[NumberQ[N[result]],N[result],result]]
  314.  
  315. StudentTDistribution/: Quantile[StudentTDistribution[n_], q_] :=
  316.     With[{result = Sign[2 q - 1] Sqrt[1/InverseBetaRegularized[
  317.                 1, (1 - 2 q) Sign[2 q - 1], n/2, 1/2] -1] Sqrt[n]},
  318.             If[NumberQ[N[result]],N[result],result]]
  319.  
  320. (* Percentage point table in Abramowitz and Stegun *)
  321. StudentTDistribution/: PercentagePoint[StudentTDistribution[n_], q_] :=
  322.     With[{result = Sqrt[n]
  323.       Sqrt[1/InverseBetaRegularized[1, -q, n/2, 1/2] - 1]},
  324.         If[NumberQ[N[result]],N[result],result]]
  325.  
  326. StudentTDistribution/: Random[StudentTDistribution[n_]] :=
  327.         Quantile[StudentTDistribution[n], Random[]]
  328.  
  329. End[]
  330. SetAttributes[ ChiSquareDistribution ,ReadProtected];
  331. SetAttributes[ FRatioDistribution,ReadProtected];    
  332. SetAttributes[ NormalDistribution,ReadProtected];    
  333. SetAttributes[ StudentTDistribution,ReadProtected];    
  334.  
  335. Protect[ChiSquareDistribution, FRatioDistribution,
  336.         NormalDistribution, StudentTDistribution];
  337.  
  338. EndPackage[]
  339.