Simple Random variable simulation

by Nasser Abbasi
For Mathematics 502, Probability and Statistics, CSUF, Fall 2007

guide/UserInterfaceStructuringAndLayout

Remove["Global`*"];
Needs["BarCharts`"];

(*****************************************)
(*          ID's for randomvariables     *)
(*****************************************)
gBernoulli=0;
gBinomial=1;
gGeometric=2;
gNegativeBinomial=3;
gHyperGeometric=4;
gPoisson=5;

(*****************************************)
(*         getXTicks                     *)
(*****************************************)
getXTicks[from_,n_]:=Module[{q,r},
{q,r}=QuotientRemainder[n,10];
Range[from,n,q+1]
]


(*****************************************)
(*         getBinLabels                  *)
(*****************************************)
getBinLabels[from_,nBins_]:=Module[{q,r,labels,i,currentBinNumber,j},
{q,r}=QuotientRemainder[nBins,10];
labels={};
currentBinNumber=from;
i=1;
While[i≤ nBins,{
j=1;
AppendTo[labels,currentBinNumber]; i++;
While[j≤q,{j++;i++;AppendTo[labels,""]}];
currentBinNumber=currentBinNumber+q+1;
}
];
labels
]

(*****************************************)
(*           makeCDF                    *)
(*****************************************)
makeCDF[n_,y_]:=Module[{data,sum,i},
data=Table[{0,0},{n}];
data[[1,2]]=y[[1]];
data[[1,1]]=0;
For[i=2,i≤n,i++,{data[[i,2]]=data[[i-1,2]]+y[[i]]; data[[i,1]]=i}];
data
]

(*****************************************)
(*           getPlot                     *)
(*****************************************)
getPlot[data_,n_,scale_,xLabel_,processType_]:=Module[{pmf,cdfData,cdf,opt,ticks},

If[scale,opt={Automatic,{0,1}},opt=All];

Which[processType==gBernoulli||processType==gBinomial||processType==gPoisson,ticks=getBinLabels[0,n],
processType==gGeometric||processType==gNegativeBinomial||processType==gHyperGeometric,ticks=getBinLabels[1,n]];

(*pmf=BarChart[data,Frame->True,FrameTicks->{ticks,If[scale,{0,1},Automatic],None,None},FrameLabel->(Style[#,FontSize->14]&/@{xLabel,"p(x)","PMF"}),BarLabels->ticks,ImageSize->{650,350}];*)

pmf=BarChart[data,BarEdges->False,PlotRange->opt,Axes->True,BarLabels->ticks,
AxesLabel->{xLabel,"p(x)"},PlotLabel->"PMF",ImageSize->{650,250}];

cdfData=makeCDF[n,data];

If[processType≠gGeometric||processType≠gHyperGeometric,
cdf=BarChart[cdfData[[All,2]],BarSpacing->0, BarGroupSpacing->0,BarEdges->True,BarStyle->{Opacity[0.5]},AxesLabel->{xLabel,"Cummulative p(x)"},PlotLabel->"CDF",
BarLabels->ticks,PlotRange->{Automatic,{0,1}}]];

If[processType==gGeometric||processType==gHyperGeometric,
GraphicsArray[{pmf},ImageSize->{650,250}],GraphicsArray[{pmf,cdf},ImageSize->{650,250}]
]
]

(*****************************************)
(*           GEOMETIC                    *)
(*****************************************)
geometricPMF[trialNumber_,p_]:=Module[{v},
Which[p==0,v=0,
p==1,v=1,
True,v=(1-p)^(trialNumber-1) p
];
v
]

geometric[p_,scale_]:=Module[{data},
data=Table[geometricPMF[k,p],{k,1,20}];
getPlot[data,20,scale,"\nx, Number of trials needed\nto get first success",gGeometric]
];

doGeometric[scale_]:=Module[{},
Manipulate[geometric[p/100,scale],{{p,50,"% probability of success in each trial p="},0,100,1,Appearance->"Labeled"},ContinuousAction->True]
]


(*****************************************)
(*           BERNOULII                   *)
(*****************************************)
bernoulliPMF[x_,p_]:=Module[{},Which[x==1,p,x==0,1-p,True,0]]

bernoulli[p_,scale_]:=Module[{data},
data=Table[bernoulliPMF[x,p],{x,0,1}];
getPlot[data,2,scale,"x, Number of sucesses",gBernoulli]
];

doBernoulli[scale_]:=Module[{},
Manipulate[bernoulli[p/100.,scale],{{p,50,"% probability of success in each trial p="},0,100,1,Appearance->"Labeled"},ContinuousAction->True]
]

(*****************************************)
(*           BINOMAIL                    *)
(*****************************************)
bionmialPMF[k_,n_,p_]:=Module[{v},
Which[p==0,v=0,
p==1,If[k==0,v=0,
If[n==k,v=1,v=Binomial[n,k](p^k)(1-p)^(n-k)]],
True,v=Binomial[n,k](p^k)(1-p)^(n-k);
];
v
]

binomial[n_,p_,scale_]:=Module[{data},
data=Table[bionmialPMF[k,n,p],{k,0,n}];
getPlot[data,n+1,scale,"x, Number of sucesses",gBinomial]
];

doBinomial[scale_]:=Module[{},
Manipulate[binomial[n,p/100.,scale],{{n,30,"Number of trials"},1,100,1,Appearance->"Labeled"},{{p,12,"probability of success in each trial (%) p="},0,100,1,Appearance->"Labeled"},ContinuousAction->False]
]

(*****************************************)
(*           Negative BINOMAIL           *)
(*****************************************)
negativeBinomialPMF[k_,r_,p_]:=Module[{},
Binomial[k-1,r-1]p^r(1-p)^(k-r)
]

negativeBinomial[r_,p_,scale_]:=Module[{data,x,n},
n=r*10;
data=Table[negativeBinomialPMF[k,r,p],{k,1,n}];
getPlot[data,n,scale,"x, Number of trials\nto obtain r successes",gBinomial]
];

doNegativeBinomial[scale_]:=Module[{},
Manipulate[negativeBinomial[r,p/100.,scale],{{r,2,"Number of successes needed? r="},1,30,1,Appearance->"Labeled"},{{p,13,"probability of success in each trial (%) p="},0,100,1,Appearance->"Labeled"},ContinuousAction->False]
]

(*****************************************)
(*           Hypergeometric              *)
(*****************************************)
hyperGeometricPMF[m_,r_,n_,k_]:=Module[{},
(Binomial[r,k]Binomial[n-r,m-k])/Binomial[n,m]
]

hyperGeometric[m_,r_,n_,scale_]:=Module[{data},
data=Table[hyperGeometricPMF[m,r,n,k],{k,0,Min[m,r]}];
getPlot[data,Min[m,r],scale,"x, Number of black balls\nremoved from urn\nwhen removing\nm balls",gHyperGeometric]
];

doHyperGeometric[scale_]:=Module[{},
Manipulate[{If[m>n,m=n]; If[r>n,r=n];hyperGeometric[m,r,n,scale]},
{{n,100,"Total number of balls in urn? n="},0,200,1,Appearance->"Labeled"},{{m,50,"Number of balls to remove without replacement? m="},1,200,1,Appearance->"Labeled"},
{{r,30,"Number of black balls in urn? r="},0,200,1,Appearance->"Labeled"},ContinuousAction->True
]
]


(*****************************************)
(*           Poisson                      *)
(*****************************************)
poissonPMF[k_,λ_]:=Module[{},
((λ^k)/(k!))Exp[-λ]
]

poisson[λ_,scale_]:=Module[{data,n},
Which[λ≤1,n=6,λ≤10,n=20,λ≤20,n=40,λ≤50,n=80,λ≤70,n=120,True,n=150];
data=Table[poissonPMF[k,λ],{k,0,n}];
getPlot[data,n,scale,"x, Number of\ntrials untill success",gPoisson]
];

doPoisson[scale_]:=Module[{},
Manipulate[poisson[λ,scale],
{{λ,.1,"λ="},0.1,120,.1,Appearance->"Labeled"},
ContinuousAction->False
]
]

(*****************************************)
(*           Poisson                     *)
(*****************************************)

(*scale=False;
TabView[{ "Bernoulli"->doBernoulli[scale],"Binomial"->doBinomial[scale],"Geometric"->doGeometric[scale],"negativeBinomial"->doNegativeBinomial[scale],"hypergeometric"-> doHyperGeometric[scale],"Poisson"->doPoisson[scale]}]*)

Manipulate[  TabView[{ "Bernoulli"->doBernoulli[scale],"Binomial"->doBinomial[scale],"Geometric"->doGeometric[scale],"negativeBinomial"->doNegativeBinomial[scale],"hypergeometric"-> doHyperGeometric[scale],"Poisson"->doPoisson[scale]}],{{scale,False,"set the P(x) axis to be 1 always"},{True,False}},FrameLabel->"Simulation of some random variables. Mathematics 502, CSUF, Fall 2007, version 1.0, Auguest 24,2007"]