Re: Discrete probablitiy distributions
- To: mathgroup at smc.vnet.net
- Subject: [mg16327] Re: [mg16273] Discrete probablitiy distributions
- From: BobHanlon at aol.com
- Date: Sun, 7 Mar 1999 01:05:48 -0500
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 3/5/99 4:39:32 PM, Bjorn.Leonardz at hhs.se writes: >I have been trying to find a good way of making Mathematica help me >prepare some lecture notes in elementary probability. This is what I >want to achieve: > >Given the probability function for a discrete random variable, e.g in >the form of a list of probabilities > >1. Plot the probability function (I can do this with ListPlot or >BarChart) > >2. Plot the cumulative probability function. > >That remains to be done in some reasonably automatic way. > Bjorn, I haven't checked this very thoroughly, but I think it is the basis for what you want. Needs["Graphics`Graphics`"]; distQ[theDist_] := Module[{dim = Dimensions[theDist], transDist = Transpose[theDist]}, (Length[dim] == 2) && (dim[[2]] == 2) && ((Plus @@ (transDist[[2]])) == 1.) && (And @@ ((# >= 0)& /@ transDist[[2]]))]; Unprotect[PDF, CDF, Domain]; Clear[PDF, CDF, Domain]; Domain[dist_?distQ] := Module[{v = Transpose[dist][[1]]}, Interval[{Min[v], Max[v]}]]; PDF[dist_?distQ, x_?NumericQ] := 0 /; FreeQ[N[Transpose[dist][[1]]], N[x]]; PDF[dist_?distQ, x_?NumericQ] := (Plus @@ Transpose[Select[dist, #[[1]] == x&]][[2]]) /; MemberQ[N[Transpose[dist][[1]]], N[x]]; CDF[dist_?distQ, x_ ?NumericQ] := 0 /; x < Min[Domain[dist]]; CDF[dist_?distQ, x_?NumericQ] := (Plus @@ Transpose[Select[dist, #[[1]] <= x&]][[2]]); Protect[PDF, CDF, Domain]; Let the distribution for X be defined by the list {{x1, p1}, ..., {xn, pn}} where pi is the probability that X = xi. X = {{1, .1}, {2, .3}, {3, .1}, {4, .5}}; nbrValues = Dimensions[X][[1]]; Table[PDF[X, x], {x, 0, 5, .5}] {0, 0, 0.1, 0, 0.3, 0, 0.1, 0, 0.5, 0, 0} GeneralizedBarChart[Transpose[Join[Transpose[X], {Table[.5, {nbrValues}]}]], PlotRange -> {{0, 5}, Automatic}]; Plot[CDF[X, x], {x, 0, 5}]; Bob Hanlon