home *** CD-ROM | disk | FTP | other *** search
-
-
- (*:Version: Mathematica 2.0 *)
-
- (*:Name: Statistics`NormalDistribution *)
-
- (*:Title: Statistical Distributions Derived from the Normal Distribution *)
-
- (*:Author:
- David Withoff (Wolfram Research), March, 1991
- *)
-
- (*:Legal:
- Copyright (c) 1991, Wolfram Research Inc.
- *)
-
- (*:Reference: Usage messages only. *)
-
- (*:Summary:
- Properties and functionals of the four standard probability distributions
- derived from the normal (Gaussian) distribution. The four distributions
- are the normal distribution, Student's t-distribution, the chi-square
- distribution, and the F-ratio distribution.
- *)
-
- (*:Keywords: continuous distribution, normal distribution *)
-
- (*:Requirements: No special system requirements. *)
-
- (*:Warning:
- This package extends the definition of several descriptive statistics
- functions and the definition of Random. If the original usage messages
- are reloaded, this change will not be reflected in the usage message,
- although the extended functionality will remain.
- *)
-
- (*:Note: Most functions provide numerical results. *)
-
- (*:Sources:
- Norman L. Johnson and Samuel Kotz, Continuous Univariate
- Distributions, 1970
- *)
-
- BeginPackage["Statistics`NormalDistribution`",
- "Statistics`DescriptiveStatistics`",
- "Statistics`Common`DistributionsCommon`",
- "Statistics`InverseStatisticalFunctions`"]
-
- ChiSquareDistribution::usage =
- "ChiSquareDistribution[n] represents the Chi-Square distribution
- with n degrees of freedom."
-
- FRatioDistribution::usage =
- "FRatioDistribution[n1, n2] represents the F-ratio distribution with n1
- numerator degrees of freedom and n2 denominator degrees of freedom."
-
- NormalDistribution::usage =
- "NormalDistribution[mu, sigma] represents the Normal (Gaussian)
- distribution with mean mu and standard deviation sigma."
-
- StudentTDistribution::usage =
- "StudentTDistribution[n] represents Student's T distribution
- with n degrees of freedom."
-
- (* Functions *)
-
- (* Introduce usage messages for new functions *)
-
- Domain::usage =
- "Domain[distribution] gives the domain of the specified distribution."
-
- PDF::usage =
- "PDF[distribution, x] gives the probability density function
- of the specified statistical distribution evaluated at x."
-
- CDF::usage =
- "CDF[distribution, x] gives the cumulative distribution function of
- distribution at the point x, defined as the integral of the probability
- density of the the distribution from the lowest value in the domain to x."
-
- CharacteristicFunction::usage =
- "CharacteristicFunction[distribution, t] gives the characteristic
- function of the specified statistical distribution as a function of
- the variable t."
-
- PercentagePoint::usage =
- "PercentagePoint[distribution, q] gives the percentage point corresponding to
- percentage q of the specified statistical distribution. This is equivalent to
- Quantile[distribution, (q+1)/2] for symmetrical distributions."
-
- (*
- Extend usage messages for existing functions. Look for the
- indicated phrase to determine if this has already been done.
- *)
-
- If[StringPosition[Mean::usage, "specified statistical distribution"] === {},
-
- Mean::usage = Mean::usage <> " " <>
- "Mean[distribution] gives the mean of the specified statistical distribution.";
-
- Variance::usage = Variance::usage <> " " <>
- "Variance[distribution] gives the variance of the specified
- statistical distribution.";
-
- StandardDeviation::usage = StandardDeviation::usage <> " " <>
- "StandardDeviation[distribution] gives the standard deviation of the
- specified statistical distribution.";
-
- Skewness::usage = Skewness::usage <> " " <>
- "Skewness[distribution] gives the coefficient of skewness of the
- specified statistical distribution.";
-
- Kurtosis::usage = Kurtosis::usage <> " " <>
- "Kurtosis[distribution] gives the coefficient of kurtosis of the
- specified statistical distribution.";
-
- KurtosisExcess::usage = KurtosisExcess::usage <> " " <>
- "KurtosisExcess[distribution] gives the kurtosis excess for the
- specified statistical distribution.";
-
- Quantile::usage =
- "Quantile[distribution, q] gives the q-th quantile of the specified
- statistical distribution."
- ]
-
- (*
- Extend the usage message of Random. Look for the indicated phrase
- to determine if this has already been done.
- *)
-
- If[StringPosition[Random::usage, "specified statistical distribution"] === {},
-
- Random::usage = Random::usage <> " " <>
- "Random[distribution] gives a random number with the specified
- statistical distribution."
- ]
-
- Begin["`Private`"]
-
- (* Chi-square Distribution *)
-
- ChiSquareDistribution/: Domain[ChiSquareDistribution[___]] := {0, Infinity}
-
- ChiSquareDistribution/: PDF[ChiSquareDistribution[n_], x_] :=
- With[{result = x^(n/2-1) / (Exp[x/2] Sqrt[2]^n Gamma[n/2])},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: CDF[ChiSquareDistribution[n_], x_] :=
- With[{result = N[GammaRegularized[n/2, 0, x/2]]},
- If[NumberQ[result],result,GammaRegularized[n/2, 0, x/2]]]
-
- ChiSquareDistribution/: Mean[ChiSquareDistribution[n_]] := n
-
- ChiSquareDistribution/: Variance[ChiSquareDistribution[n_]] := 2 n
-
- ChiSquareDistribution/: StandardDeviation[ChiSquareDistribution[n_]] :=
- With[{result = Sqrt[2 n]},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: Skewness[ChiSquareDistribution[n_]] :=
- With[{result = 2 Sqrt[2/n]},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: Kurtosis[ChiSquareDistribution[n_]] :=
- With[{result = 3 + 12/n},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: KurtosisExcess[ChiSquareDistribution[n_]] :=
- With[{result = 12/n},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: CharacteristicFunction[
- ChiSquareDistribution[n_], t_] :=
- With[{result = (1 - 2 I t)^(-n/2)},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: Quantile[ChiSquareDistribution[n_], q_] :=
- With[{result = 2 InverseGammaRegularized[n/2, 0, q]},
- If[NumberQ[N[result]],N[result],result]]
-
- ChiSquareDistribution/: Random[ChiSquareDistribution[n_]] :=
- With[{result = 2 InverseGammaRegularized[n/2, 0, Random[]]},
- If[NumberQ[N[result]],N[result],result]]
-
- (* F-Distribution *)
-
- FRatioDistribution/: Domain[FRatioDistribution[___]] = {0, Infinity}
-
- FRatioDistribution/: PDF[FRatioDistribution[n1_, n2_], x_] :=
- With[{result = n1^(n1/2) n2^(n2/2) x^(n1/2 - 1)/
- ((n2 + n1 x)^((n1 + n2)/2) Beta[n1/2, n2/2])},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: CDF[FRatioDistribution[n1_, n2_], x_] :=
- With[{result = BetaRegularized[n2/(n2 + n1 x), 1, n2/2, n1/2]},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Mean[FRatioDistribution[n1_, n2_]] :=
- With[{result = n2/(n2 - 2)},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Variance[FRatioDistribution[n1_, n2_]] :=
- With[{result = 2 n2^2 (n1+n2-2) / (n1 (n2-2)^2 (n2-4))},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: StandardDeviation[FRatioDistribution[n1_, n2_]] :=
- With[{result = (n2/(n2-2)) Sqrt[2] Sqrt[n1+n2-2]/(Sqrt[n1] Sqrt[n2-4])},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Skewness[FRatioDistribution[n1_, n2_]] :=
- With[{result = (2 n1 + n2 - 2) Sqrt[8 (n2-4)]/
- (Sqrt[n1] (n2-6) Sqrt[n1+n2-2])},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Kurtosis[FRatioDistribution[n1_, n2_]] :=
- With[{result = 12 ((n2-2)^2 (n2-4) + n1 (n1+n2-2) (5 n2 -22))/
- (n1 (n2-6) (n2-8) (n1+n2-2)) + 3},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: KurtosisExcess[FRatioDistribution[n1_, n2_]] :=
- With[{result = 12 ((n2-2)^2 (n2-4) + n1 (n1+n2-2) (5 n2 -22))/
- (n1 (n2-6) (n2-8) (n1+n2-2))},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/:
- CharacteristicFunction[FRatioDistribution[n1_, n2_], t_] :=
- With[{result = Hypergeometric1F1[n1/2, 1 - n2/2, -I t n2/n1]},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Quantile[FRatioDistribution[n1_, n2_], q_] :=
- With[{result = n2/n1 (1/InverseBetaRegularized[1, -q, n2/2, n1/2] - 1)},
- If[NumberQ[N[result]],N[result],result]]
-
- FRatioDistribution/: Random[FRatioDistribution[n1_, n2_]] :=
- With[{result = n2/n1 (1/InverseBetaRegularized[1, -Random[],
- n2/2, n1/2] - 1)},
- If[NumberQ[N[result]],N[result],result]]
-
- (* Normal distribution *)
-
- NormalDistribution/: Domain[NormalDistribution[___]] = {-Infinity, Infinity}
-
- NormalDistribution/: PDF[NormalDistribution[mu_:0, sigma_:1], x_] :=
- With[{result = Exp[-((x-mu)/sigma)^2 / 2]/(sigma Sqrt[2Pi])},
- If[NumberQ[N[result]],N[result],result]]
-
- NormalDistribution/: CDF[NormalDistribution[mu_:0, sigma_:1], x_] :=
- With[{result = (Erf[(x-mu)/(Sqrt[2] sigma)] + 1) / 2},
- If[NumberQ[N[result]],N[result],result]]
-
- NormalDistribution/: Mean[NormalDistribution[mu_:0, sigma_:1]] := mu
-
- NormalDistribution/: StandardDeviation[
- NormalDistribution[mu_:0, sigma_:1]] := sigma
-
- NormalDistribution/: Variance[NormalDistribution[mu_:0, sigma_:1]] := sigma^2
-
- NormalDistribution/: Skewness[NormalDistribution[mu_:0, sigma_:1]] = 0
-
- NormalDistribution/: Kurtosis[NormalDistribution[mu_:0, sigma_:1]] = 3
-
- NormalDistribution/: KurtosisExcess[NormalDistribution[mu_:0, sigma_:1]] = 0
-
- NormalDistribution/: CharacteristicFunction[
- NormalDistribution[mu_:0, sigma_:1], t_] :=
- With[{result = Exp[I mu t - sigma^2 t^2/2]},
- If[NumberQ[N[result]],N[result],result]]
-
- NormalDistribution/: Quantile[NormalDistribution[mu_:0, sigma_:1], u_] :=
- With[{result = mu + Sqrt[2] sigma InverseErf[2 u - 1]},
- If[NumberQ[N[result]],N[result],result]]
-
- NormalDistribution/: Random[NormalDistribution[mu_:0, sigma_:1]] :=
- N[mu + sigma Sqrt[-2 Log[Random[]]] Cos[2Pi Random[]]]
-
- (* StudentT Distribution *)
-
- StudentTDistribution/: Domain[StudentTDistribution[___]] =
- {-Infinity, Infinity}
-
- StudentTDistribution/: PDF[StudentTDistribution[n_], x_] :=
- With[{result = 1/(Sqrt[n] Beta[n/2, 1/2]) Sqrt[n/(n+x^2)]^(n+1)},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: CDF[StudentTDistribution[n_], x_] :=
- With[{result = (1 + Sign[x] BetaRegularized[n/(n+x^2), 1, n/2, 1/2])/2},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: Mean[StudentTDistribution[_]] = 0
-
- StudentTDistribution/: Variance[StudentTDistribution[n_]] :=
- With[{result = n/(n-2)},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: StandardDeviation[StudentTDistribution[n_]] :=
- With[{result = Sqrt[n/(n-2)]},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: Skewness[StudentTDistribution[_]] = 0
-
- StudentTDistribution/: Kurtosis[StudentTDistribution[n_]] :=
- With[{result = 6/(n-4) + 3},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: KurtosisExcess[StudentTDistribution[n_]] :=
- With[{result = 6/(n-4)},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/:
- CharacteristicFunction[StudentTDistribution[n_], t_] :=
- With[{result = 2 BesselK[n/2, Abs[t] Sqrt[n]] (Abs[t] Sqrt[n]/2)^(n/2) /
- Gamma[n/2]},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: Quantile[StudentTDistribution[n_], q_] :=
- With[{result = Sign[2 q - 1] Sqrt[1/InverseBetaRegularized[
- 1, (1 - 2 q) Sign[2 q - 1], n/2, 1/2] -1] Sqrt[n]},
- If[NumberQ[N[result]],N[result],result]]
-
- (* Percentage point table in Abramowitz and Stegun *)
- StudentTDistribution/: PercentagePoint[StudentTDistribution[n_], q_] :=
- With[{result = Sqrt[n]
- Sqrt[1/InverseBetaRegularized[1, -q, n/2, 1/2] - 1]},
- If[NumberQ[N[result]],N[result],result]]
-
- StudentTDistribution/: Random[StudentTDistribution[n_]] :=
- Quantile[StudentTDistribution[n], Random[]]
-
- End[]
- SetAttributes[ ChiSquareDistribution ,ReadProtected];
- SetAttributes[ FRatioDistribution,ReadProtected];
- SetAttributes[ NormalDistribution,ReadProtected];
- SetAttributes[ StudentTDistribution,ReadProtected];
-
- Protect[ChiSquareDistribution, FRatioDistribution,
- NormalDistribution, StudentTDistribution];
-
- EndPackage[]
-