summaryrefslogtreecommitdiff
path: root/texmf-dist/doc/latex/bargraph-js/examples/binomial-dist/binomial-tbl-gh.tex
blob: 8325eb550173c4a4d881d887d366ad9320872896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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}