diff options
Diffstat (limited to 'macros/latex/contrib/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex')
-rw-r--r-- | macros/latex/contrib/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex | 401 |
1 files changed, 401 insertions, 0 deletions
diff --git a/macros/latex/contrib/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex b/macros/latex/contrib/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex new file mode 100644 index 0000000000..8325eb5501 --- /dev/null +++ b/macros/latex/contrib/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex @@ -0,0 +1,401 @@ +%xpmfBar.bgnoname@Pmf +\documentclass{article} +\usepackage[fleqn]{amsmath} +\usepackage[designiv,forcolorpaper,tight]{web} +\usepackage{grayhints} +\usepackage[dynamic]{bargraph-js} + + % \previewOn\pmcaOn + + +\parindent0pt + +\def\maxN{72} +\begin{insDLJS*}{binomialDistr} +\begin{newsegment}{The Binomial Distribution} +var to=app.setTimeOut('this.layout="TwoColumnLeft"',5); +var maxN=\maxN; +var pmfBinKNP=[0,0,0]; +var cdfBinKNP=[0,0,0]; +var aBinCoeff=new Array(); +aBinCoeff[1]=[1]; +var aPmfCdf=new Array(); +aBinCoeff[2]=[1,2,1]; +for (var i=3; i<= maxN; i++) { + aBinCoeff[i]=new Array(); + aBinCoeff[i][0]=1; + for (var j=1; j < i; j++) + aBinCoeff[i][j]=aBinCoeff[i-1][j-1]+aBinCoeff[i-1][j]; + aBinCoeff[i][i]=1; +} +function BinPMF(k,n,p){ + if (n==0) return null; + var q = 1 - p, r=n-k; + return aBinCoeff[n][k]*Math.pow(p,k)*Math.pow(q,r); +} +function BinCDF(k,n,p){ + var cdf=0; + for (var i=0; i <= Math.floor(k); i++ ) + cdf += (BinPMF(i,n,p)); + return cdf; +} +function updateNPValues(suffix,ext,event) { + /* suffix="pmf"|"cdf",ext="n"|"p" */ + var index=(ext=="n")?1:2; + if (suffix=="pmf") + pmfBinKNP[index]=event.value; + else + cdfBinKNP[index]=event.value; +} +function pmfValidate(suffix,event) { + var n = this.getField(suffix+"n"); + var p = this.getField(suffix+"p"); + if (n.value=="") { + app.alert("First, enter a value for \"n\" between 1 and "+maxN+", inclusive"); + event.rc=false; + return; + } else { + if (suffix=="pmf") + pmfBinKNP[1]=n.value; + else + cdfBinKNP[1]=n.value; + if (p.value=="") { + app.alert("First, enter a decimal number for \"p\" between 0 and 1, inclusive"); + event.rc=false; + return; + } else + if (suffix=="pmf") + pmfBinKNP[2]=p.value; + else + cdfBinKNP[2]=p.value; + } + var k = 1*event.value; + if ( (suffix=="pmf")&&(k < 0) ) { + event.rc=false; + app.alert("\"k\" must be a nonnegative integer"); + return; + } + if ( (suffix=="pmf")&&(k > n.value) ) { + event.rc=false; + app.alert("\"k\" must be a nonnegative integer no larger than "+ n.value); + return; + } + if ( (suffix=="pmf")&&(!Number.isInteger(k)) ) { + event.rc=false; + app.alert("\"k\" must be a nonnegative integer"); + return; + } + if (suffix=="pmf") + pmfBinKNP[0]=k; + else + cdfBinKNP[0]=k; +} +function ValidateBinParams(fld) { + var k=this.getField(fld+"k"); + var n=this.getField(fld+"n"); + var p=this.getField(fld+"p"); + var bOK=( (k.valueAsString!="") && (n.valueAsString!="") && (p.valueAsString!="") ); + return bOK; +} +function pmfCalculate(fld) { % dps + var bOk=ValidateBinParams(fld); + if (!bOk) app.alert("Some of the parameters have no value, please fix."); + if (bOk) { + if ( pmfBinKNP[0] > pmfBinKNP[1] ) { + app.alert("The \"k\" parameter is larger than the \"n\" parmater, fix it"); + } else { + var f = this.getField(fld); + var Pr=BinPMF(pmfBinKNP[0],pmfBinKNP[1],pmfBinKNP[2]); + if (Pr!=null) f.value=util.printf("\%.6f",Pr); + } + } +} +function cdfCalculate(fld) { + var bOk=ValidateBinParams(fld); + if (!bOk) app.alert("Some of the parameters have no value, please fix."); + if (bOk) { + var f = this.getField(fld); + if ( cdfBinKNP[0]> cdfBinKNP[1]) { + f.value=util.printf("\%.6f",1); + } else { + if (cdfBinKNP[0] < 0 ) { + f.value=util.printf("\%.6f",0); + } else { + var Pr=BinCDF(cdfBinKNP[0],cdfBinKNP[1],cdfBinKNP[2]); + f.value=util.printf("\%.6f",Pr); + } + } + } +} +function tableCalc(tableN,tableP,cFld,cTableN,cTableP) { + var f1=this.getField(cFld+".1"); + var f2=this.getField(cFld+".2"); + var f3=this.getField(cFld+".3"); + var N=this.getField(cTableN); + var P=this.getField(cTableP); + aPmfCdf=new Array(); // initialize array (declared above) + if ( N.valueAsString=="" || P.valueAsString=="") { + f1.value="At least one of the parameters, \"n\" or \"p\", does not have a value, please correct this." + return; + } + var pmfPr=0; cdfPr=0, rowStr="",tableStr1="",tableStr2="",tableStr3=""; + var tableHead=util.printf(" \%s ","k") + +util.printf(" \%s ","pmf") + +util.printf(" \%s","cdf")+"\r"; + if ( tableN > 24 ) var col1=24; + else var col1=tableN; + for (var k=0; k<=Math.min(24,tableN); k++) { + pmfPr=BinPMF(k,tableN,tableP); + cdfPr=BinCDF(k,tableN,tableP); + rowStr=((k<10)?util.printf("\%4d",k):util.printf("\%3d",k)) + +util.printf("\%10.6f", pmfPr) + +util.printf("\%10.6f", cdfPr) + +"\r"; + tableStr1+=rowStr; + aPmfCdf.push([k,pmfPr,cdfPr]); + } + if ( tableN <= 24 ) f1.value=tableHead+tableStr1; + else { + for (var k=25; k<=Math.min(48,tableN); k++) { + pmfPr=BinPMF(k,tableN,tableP); + cdfPr=BinCDF(k,tableN,tableP); + rowStr=((k<10)?util.printf("\%4d",k):util.printf("\%3d",k)) + +util.printf("\%10.6f", pmfPr) + +util.printf("\%10.6f", cdfPr) + +"\r"; + tableStr2+=rowStr; + aPmfCdf.push([k,pmfPr,cdfPr]); + } + if ( tableN <= 48 ) { + f1.value=tableHead+tableStr1; + f2.value=tableHead+tableStr2; + } else { + for (var k=49; k<=tableN; k++) { + pmfPr=BinPMF(k,tableN,tableP); + cdfPr=BinCDF(k,tableN,tableP); + rowStr=((k<10)?util.printf("\%4d",k):util.printf("\%3d",k)) + +util.printf("\%10.6f", pmfPr) + +util.printf("\%10.6f", cdfPr) + +"\r"; + tableStr3+=rowStr; + aPmfCdf.push([k,pmfPr,cdfPr]); + } + f1.value=tableHead+tableStr1; + f2.value=tableHead+tableStr2; + f3.value=tableHead+tableStr3; + } + } +} +\end{newsegment} +\end{insDLJS*} +% +% Special formatting macros +% +\def\binPresets{\autoCenter{y}\Q{1}\S{U}\BC{gray}\textColor{0.75 g}\textSize{6}} +\def\ValidateInt{% + if ( !Number.isInteger(1*event.value) ) {\r\t + event.rc=false;\r\t + app.alert("\\"n\\" must be an integer");\r + }% +} +% +% Begin PMF macros +% +\def\pmfk{\textField[\presets{\binPresets} + \TU{Enter the number of successes "k". Must be less than "n", the second parameter} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{k}} + \AAValidate{pmfValidate("pmf",event);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{pmfk}{10bp}{8bp}} +\def\pmfn{\textField[\presets{\binPresets} + \TU{Enter "n", the number of trials, must be no greater than \maxN} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{n}} + \AAValidate{EFRange_Validate(true,1,true,\maxN);\r % dps + if (event.rc) updateNPValues("pmf","n",event);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{pmfn}{10bp}{8bp}} +\def\pmfp{\textField[\presets{\binPresets} + \TU{Enter "p", the probability of success, a decimal number between + 0 and 1 inclusive}\AA{% + \AAKeystroke{AFNumber_Keystroke(3,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(3,1,0,0,"",true);\FmtToGray{p}} + \AAValidate{EFRange_Validate(true,0,true,1);\r % dps + if (event.rc) updateNPValues("pmf","p",event);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{pmfp}{18bp}{8bp}} +\def\pmf{\textField[\textSize{6}\S{U}\BC{gray}]{pmf}{1in}{8bp}} +\def\calcPMF#1{\pushButton[\textSize{6}\CA{Calc}\A{\JS{% + pmfCalculate("#1");}} +]{calcPMF}{}{8bp}} +% +% Begin CDF macros +% +\def\cdfk{\textField[\presets{\binPresets} + \TU{Enter any number for "k"} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{k}} + \AAValidate{pmfValidate("cdf",event);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{cdfk}{10bp}{8bp}} +\def\cdfn{\textField[\presets{\binPresets} + \TU{Enter "n", the number of trials, must be no greater than \maxN} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{n}} + \AAValidate{EFRange_Validate(true,1,true,\maxN);\r\ValidateInt\r + if (event.rc) updateNPValues("cdf","n",event);} % dps + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{cdfn}{10bp}{8bp}} +\def\cdfp{\textField[\presets{\binPresets} + \TU{Enter "p", the probability of success, a decimal number between + 0 and 1 inclusive}\AA{% + \AAKeystroke{AFNumber_Keystroke(3,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(3,1,0,0,"",true);\FmtToGray{p}} + \AAValidate{EFRange_Validate(true,0,true,1);\r % dps + if (event.rc) updateNPValues("cdf","p",event);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} +}]{cdfp}{18bp}{8bp}} +\def\cdf{\textField[\textSize{6}\S{U}\BC{gray}]{cdf}{1in}{8bp}} +\def\calcCDF#1{\pushButton[\textSize{6}\CA{Calc}\A{\JS{% + cdfCalculate("#1");}} +]{calcCDF}{}{8bp}} + + + +\begin{document} + +%\previewOff + +\section*{Tables of Binomial Probabilities} + +\def\binTablePresets{\autoCenter{y}\Q{1}\S{U}} + +\( + n=\textField[\presets{\binTablePresets}\textColor{0.75 g}\BC{gray} + \TU{Enter "n", the number of trials, must be no greater than \maxN} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{n}} + \AAValidate{AFRange_Validate(true,1,true,\maxN);\r\ValidateInt } + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} + }]{tableN}{16bp}{11bp}\cgBdry\quad + p=\textField[\presets{\binTablePresets}\textColor{0.75 g}\BC{gray} + \TU{Enter "p", the probability of success, a decimal number between 0 and 1 inclusive} + \AA{\AAKeystroke{AFNumber_Keystroke(3,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(3,1,0,0,"",true);\FmtToGray{p}} + \AAValidate{AFRange_Validate(true,0,true,1);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} + }]{tableP}{25bp}{11bp}\cgBdry\, +\pushButton[\CA{Calc}\AAmouseup{% + var tableN=this.getField("tableN").value;\r + var tableP=this.getField("tableP").value;\r + tableCalc(tableN,tableP,"TableOfBinPrs","tableN","tableP");\r + displayDyBargraph("Pmf",aPmfCdf,true,false,{bc:color.red,fc:color.blue});\r + displayDyBargraph("Cdf",aPmfCdf,false,false,{bc:color.red,fc:color.blue}); + }]{tableCalc}{}{11bp}\olBdry +\pushButton[\CA{Reset} + \TU{Press to clear the fields of this page, and shift-press to clear all fields.} + \A{\JS{% + this.calculate=true;\r + if (event.shift)\r\t + this.resetForm();\r + else\r\t + this.resetForm(["TableOfBinPrs","tableN","tableP"]);\r + this.removeField("pmfBar");\r + this.removeField("cdfBar"); +}}]{reset}{}{11bp} +\) + \vcgBdry[12pt] +In the table, $\operatorname{\textsf{pmf}}=f(k;n,p)=\operatorname{Pr}(X=k)$, +$\operatorname{\textsf{cdf}}=F(k;n,p)=\operatorname{Pr}(X\le k)$. + \vcgBdry[12pt]{\setlength{\fboxsep}{0pt}\fbox{% + \textField[\BC{}\Ff{\FfMultiline}]{TableOfBinPrs.1}{105bp}{11bp*26}\olBdry + \textField[\BC{}\Ff{\FfMultiline}]{TableOfBinPrs.2}{105bp}{11bp*26}\olBdry + \textField[\BC{}\Ff{\FfMultiline}]{TableOfBinPrs.3}{105bp}{11bp*26}}\bigskip + +\newpage + +%\setlength{\fboxsep}{3pt} + +%\previewOn +\begin{center}\bfseries + The bar graph for the probability mass function (pmf) +\end{center} +\fbox{\begin{bargraphenv}[width=\linewidth,height=2in,o=vert]{Pmf} %[width=.67\linewidth,height=2in,o=vert]{kaf} +\begin{bargraph}{pmfBar}\isdynamic %\relax +\end{bargraph} +\end{bargraphenv}}\vcgBdry[\medskipamount]\pushButton[\TU{This button re-scales the bar graph so that the tallest bar +takes the entire height of the region; shift-click reverts bar graph to its original scaling.}\CA{Optimize}\A{\JS{% +displayDyBargraph("Pmf",aPmfCdf,true,!event.shift);}}]{optimize}{}{11bp}\quad{Under normal scaling, the height of this region is 1 unit, when the bar graph is optimized, the height is the height of the tallest bar.} + +\bigskip +\begin{center}\bfseries + The bar graph for the cumulative distribution function (cdf) +\end{center} +\fbox{\begin{bargraphenv}[width=\linewidth,height=2in,o=vert]{Cdf} +\begin{bargraph}{cdfBar}\isdynamic %\relax +\end{bargraph} +\end{bargraphenv}}\vcgBdry[\medskipamount] The height of the region above is 1~unit.\vcgBdry[\bigskipamount] +\( + n=\textField[\presets{\binTablePresets}\textColor{0.75 g}\BC{gray} + \TU{Enter "n", the number of trials, must be no greater than \maxN} + \AA{\AAKeystroke{AFNumber_Keystroke(0,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(0,1,0,0,"",true);\FmtToGray{n}} + \AAValidate{AFRange_Validate(true,1,true,\maxN);\r\ValidateInt } + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} + }]{tableN}{16bp}{11bp}\cgBdry\quad + p=\textField[\presets{\binTablePresets}\textColor{0.75 g}\BC{gray} + \TU{Enter "p", the probability of success, a decimal number between 0 and 1 inclusive} + \AA{\AAKeystroke{AFNumber_Keystroke(3,1,0,0,"",true);\r\KeyToGray} + \AAFormat{AFNumber_Format(3,1,0,0,"",true);\FmtToGray{p}} + \AAValidate{AFRange_Validate(true,0,true,1);} + \AAOnFocus{\JS{\FocusToBlack}} + \AAOnBlur{\JS{\BlurToBlack}} + }]{tableP}{25bp}{11bp}\cgBdry\, +\pushButton[\CA{Calc}\AAmouseup{% + var tableN=this.getField("tableN").value;\r + var tableP=this.getField("tableP").value;\r + tableCalc(tableN,tableP,"TableOfBinPrs","tableN","tableP");\r + displayDyBargraph("Pmf",aPmfCdf,true,false,{bc:color.blue,fc:color.red});\r + displayDyBargraph("Cdf",aPmfCdf,false,false,{bc:color.blue,fc:color.red}); + }]{tableCalc}{}{11bp}\olBdry +\pushButton[\CA{Reset} + \TU{Press to clear the fields of this page, and shift-press to clear all fields.} + \A{\JS{% + this.calculate=true;\r + if (event.shift)\r\t + this.resetForm();\r + else\r\t + this.resetForm(["TableOfBinPrs","tableN","tableP"]);\r + this.removeField("Pmf@pmfBar");\r + this.removeField("Cdf@cdfBar"); +}}]{reset}{}{11bp}\cgBdry[2em] +\pushButton[\CA{Toggle Border}\A{\JS{% + var f=this.getField("Pmf@pmfBar");\r + if ( f!=null ) {\r\t + var a=f.getArray();\r\t\t + if (f.saveStrokeColor==undefined) f.saveStrokeColor=color.red;\r\t + if (!color.equal(a[0].strokeColor,color.transparent))\r\t\t + f.saveStrokeColor=a[0].strokeColor;\r\t + var g=getField("Cdf@cdfBar");\r\t + if (color.equal(a[0].strokeColor,color.transparent)) {\r\t\t + f.strokeColor=f.saveStrokeColor;\r\t\t + g.strokeColor=f.saveStrokeColor;\r\t + } else {\r\t\t + f.strokeColor=color.transparent;\r\t\t + g.strokeColor=color.transparent;\r\t + }\r + } +}}]{toggleBdry}{}{11bp} +\) + +\end{document} |