Another illustration of the Use of Discrete Distributions Using Dynamics UI features

By Nasser Abbasi

Get["BarCharts`"];

imageWidth=500;
imageWidth2=1*imageWidth/2;

imageWidthPars1= 0.55*imageWidth;
sliderImage={0.40*imageWidthPars1,20};

imageWidthPars2= .97*(imageWidth-imageWidthPars1);
imageH1=110;
imageH1in=imageH1*.65;

imageH3=60;
imageH2=200;

cdft:=Style["cumulative distribution function",Small]
pmft:=Style["probability mass function",Small]
sc:=Style["Scale y-axis for PMF to be 1 always",Small]
b3:=Labeled[Checkbox[Dynamic[scale]],sc,{Right}]

scaleXq:=Style["Zoom in",Small]
scaleXql:=Labeled[Checkbox[Dynamic[scaleX]],scaleXq,{Right}]

b3Panel:=Panel[Column[{b3,scaleXql}] ,ImageSize->{imageWidthPars2,imageH1in}]

(*------------------------------------------------------*)
(*make plots*)
(*------------------------------------------------------*)
makePlots[distribution_,pdfLimits_,cdfLimits_,pdfAxesOrigion_,cdfAxesOrigin_,ticksForPdf_,ticksForCDF_]:=Module[{k,tbl},
(*tbl=Table[{k,PDF[distribution,k],1},{k,pdfLimits[[1]],pdfLimits[[2]]}];*)

pdf=GeneralizedBarChart[Table[{k,PDF[distribution,k],1},{k,pdfLimits[[1]],pdfLimits[[2]]}],BarStyle->{GrayLevel[0.8]},AxesOrigin->pdfAxesOrigion,AxesLabel->{"x",""},PlotRange->{Automatic,If[scale,{0,1},All]},PlotLabel->pmft,Ticks->ticksForPdf,ImagePadding->30,TicksStyle->Small];

cdf=Plot[CDF[distribution,k],{k,cdfLimits[[1]],cdfLimits[[2]]},AxesOrigin->cdfAxesOrigin,PlotLabel->cdft,AxesLabel->{"x",""},Ticks->ticksForCDF,ImagePadding->30,TicksStyle->Small]
]
(*------------------------------------------------------*)
(*displays final plot*)
(*------------------------------------------------------*)
set[pdf_,cdf_,label_]:=Module[{},Labeled[Grid[{{pdf,cdf}},Alignment->{{Left,Left}}],(Style[#1,FontSize->10]&)/@label,{{Bottom,Center}}]]

(*------------------------------------------------------*)
(*finds mean and var of dist*)
(*------------------------------------------------------*)
getMeanVar[p_]:=Module[{mean,var},mean=N[Mean[p],16];
mean=NumberForm[mean,{9,7},NumberPadding->{" ","0"}];
var=N[Variance[p],16];
var=NumberForm[var,{9,7},NumberPadding->{" ","0"}];
{mean,var}]

(*------------------------------------------------------*)
(*        hack to find CDF limit for plotting           *)
(*------------------------------------------------------*)
getLimit[y_,var_]:=Module[{to=5},While[(1-y/.var->to)>0.001,to=to+10];  to]

(*------------------------------------------------------*)
(*            hack to find ticks                        *)
(*------------------------------------------------------*)
getTicks[from_,to_]:=Module[{},If[to-from>10,{Range[from,to,Round[(to-from)/10]],Automatic},{Range[from,to,1],Automatic}]]

(*------------------------------------------------------*)
(*              binomial                                *)
(*------------------------------------------------------*)
bs1:=Slider[Dynamic[n],{1,100,1},ImageSize->Tiny]
lbs1:=Labeled[bs1,Style["N, Number of trials",Small],Left]
llbs1:={lbs1,Style[Dynamic[n],Small]}
bs2:=Slider[Dynamic[p],{0,1,0.05},ImageSize->Tiny]
lbs2:=Labeled[bs2,Style["p, probability of success in each trial",Small],Left]
llbs2:={lbs2,Style[Dynamic[p],Small]}
parsBinomialMain:=Labeled[Panel[ Column[{Row[llbs1],Row[llbs2]}],ImageSize->{imageWidthPars1,imageH1in}],Style["Select Distribution Parameters",Small],{{Top,Left}}]
parsBinomial:= Row[  {parsBinomialMain  , b3Panel}]

binomial[]:=Module[{mean,var,from,to,data,dist,x},
dist=BinomialDistribution[n,p];
{mean,var}=getMeanVar[dist];
If[scaleX,to=getLimit[CDF[dist,x],x],to=110];
from=0;
makePlots[dist,{from,to},{-2,to},{-0.5,0},{-1,0},Automatic,getTicks[-2,to]];
label="X: Number of successes in n independent trials. Trial has p chance of success";
data=StringJoin["\nMean: np=",ToString[mean],"\tVariance: n(1-p)p",ToString[var]];
label=Text[Style[StringJoin[label,data]]];
Column[  
{
parsBinomial,Row[{Panel[Dynamic[pdf],ImageSize->{imageWidth2,imageH2}],Panel[Dynamic[cdf],ImageSize->{imageWidth2,imageH2}]}
],
Panel[Dynamic[label],ImageSize->{imageWidth,imageH3}]
}
]
]

(*------------------------------------------------------*)
(*poisson*)
(*------------------------------------------------------*)
maxLambda=30;
sp1:=Slider[Dynamic[λ],{0.1,maxLambda,0.1},ImageSize->Tiny]
lsp1:=Labeled[sp1,Style["λ, Mean rate of arrival",Small],Left]
llsp1:={lsp1,Style[Dynamic[λ],Small]}

parsPoissonMain:=Labeled[Panel[ Column[{Row[llsp1]}],ImageSize->{imageWidthPars1,imageH1in}],Style["Select Distribution Parameters",Small],{{Top,Left}}]
parsPoisson:= Row[  {parsPoissonMain , b3Panel}]

poisson[]:=Module[{x,mean,var,from,data,to,dist},
dist=PoissonDistribution[λ];
{mean,var}=getMeanVar[dist];
from=0;
If[scaleX,to=getLimit[CDF[dist,x],x],to=50];
makePlots[dist,{from,to},{-2,to},{-0.5,0},{-1,0},Automatic,getTicks[-2,to]];
label="X: Number of arrivals when average rate of arrival is λ";
data=StringJoin["\nMean: λ=",ToString[mean],"\tVariance: λ=",ToString[var]];
label=Text[Style[StringJoin[label,data]]];
Column[
{
parsPoisson,
Row[{Panel[Dynamic[pdf],ImageSize->{imageWidth2,imageH2},ImageMargins->0],Panel[Dynamic[cdf],ImageSize->{imageWidth2,imageH2},ImageMargins->0]}
],
Panel[Dynamic[label],ImageSize->{imageWidth,imageH3}]
}
]
]

(*------------------------------------------------------*)
(*Main menu*)
(*------------------------------------------------------*)
Panel[Grid[{{ Labeled[ PopupMenu[Dynamic[proc],{binomial->"Binomial",poisson->"Poisson"}
],Style["Select Distribution"],Left]
},{Dynamic[proc[]]}}]
]