Here you can find how to calculate and print table for the binomial distribution. I would like to get rid of those lines that are all empty due to too small probabilities (<=0.0000).

I used the answer given to the above mentioned question (see here). Then I added a new \mySAVETable. With the help of the etoolbox package (as already used in the answer) I tried to save only those lines that contain at least one probabilty. I also introduced a new counter to count the number of empty cells. Fortunately, this works all fine. However, cancelling the all empty lines fails. I get an empty table instead, only the head and the foot are printed.

Could anyone please help me to find out what I'm doing wrong here? Thanks!

Here is my code:

\usepackage[margin=.5cm, showframe=true]{geometry}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=4}

%%% Defining basic stuff
\def\myHeadList{0.05, 0.10, 1/6, 0.20, 0.25, 0.30, 1/3, 0.40, 0.45, 0.50}
\def\myFootList{0.95, 0.90, 5/6, 0.80, 0.75, 0.70, 2/3, 0.60, 0.55, 0.50}

\newcommand*{\binomTable}[3][0]{% #1  = 0 => results in the form 0.0000 are typeset
                                % #1 != 0 => results in the form 0.0000 are omitted
                                % #2 = startN
                                % #3 = endN
    %%% Building head of table
    \def\myHead{ $n$ & $k$}%
    \foreach \p in \myHeadList {%
        \xappto\myHead{& {$\p$}}%
    \appto\myHead{& $k$ & EmptyCells\\}%
    %%% Building foot of table
    \def\myFoot{ $n$ & $k$}%
    \foreach \p in \myFootList {%
        \xappto\myFoot{& {$\p$}}%
    \appto\myFoot{& $k$ & EmptyCells\\}%
    %%% Bulding body of table
    \foreach \n in \myN {%
        \foreach \k in {0,...,\n}{%
            \xappto\myTable{& \k}%
                    \foreach \j in {1,...,\k}{%
%            \xappto\myTable{& \noexpand\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,set thousands separator={\,},min exponent for 1000 sep=4]{\myTempValue}}%
            \foreach \p in \myHeadList {%
                    \xappto\myTable{& \noexpand\pgfmathprintnumber[skip 0.=true, dec sep={}]{\result}}%
                \ifdim\result pt<0.00005pt\relax%
                        \xappto\myTable{& \noexpand\pgfmathprintnumber[skip 0.=true, dec sep={}]{\result}}%
            \xappto\myTable{& \noexpand\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,set thousands separator={\,},min exponent for 1000 sep=4]{\result} & \arabic{EmptyCells}}%

  • 1
    Does the solution have to employ the pgf package, or might you be interested in an alternative, LuaLaTeX-based solution? Please advise. – Mico Jul 22 at 8:37
  • Thanks for your suggestion. I would rather like to have a pdfLaTeX solution. I guess LuaLaTeX would be faster in calculating. But I'm still using pdfLaTeX only. Sorry. – rsa-krypto Jul 22 at 12:19

Here's a LuaLaTeX-based solution. The OP has indicated that he/she would a pdfLaTeX-based solution; unfortunately, I don't know how to create such a solution. :-( Hopefully, my answer will be useful to other readers in the future.

The binomial probabilities are computed for various values of n, k(0\le k\le n), and 10 probabilities pevenly spaced between 0.05 and 0.50. The main Lua function shown below performs its work with three nested for loops: n ranges from 2 to n_max, k ranges from 0 to n, and p ranges from 0.05 to 0.50. The binomial probabilities are rounded to 4 digits after the decimal marker. To address your main formatting request, they are displayed as 0 if their values are less than 0.5e-4. (Speaking for myself, I find it easier to navigate a table with lots of 0 cells rather than one with lots of entirely blank cells.) In addition, if a probability can be displayed using fewer than 4 digits, the program leaves off the trailing zeros. E.g., for n=2 and p=0.1, the probabilities are shown as 0.81, 0.18, and 0.01 for k=0,1,2 in the first screenshot shown below. Similarly, for n=2 and p=0.5, the probabilities are shown as 0.25, 0.5, and 0.25 for k=0,1,2.

The code displayed below creates a longtable that stretches over roughly 2-1/2 pages. The screenshots are taken from the top and bottom of the longtable.

enter image description here

enter image description here

\usepackage[a4paper,margin=2.3cm]{geometry} % choose suitable page parameters

-- utility functions to round numbers to a preset number of digits
function round2int ( x )
   return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
function round ( num , digits )
   return round2int ( num * 10^digits ) / 10^digits

-- print the header row of the binomial probabilities table
function make_header_row ()
    for p=1,10 do
      tex.sprint ( "{"..string.format("%.2f",p/20) .. "}" )
      if p<10 then tex.sprint ("&") end

-- functions to compute factorials, # of permutations, and binom. probabilities
function factorial ( t )
  if t==0 or t==1 then return 1
  else return ( t * factorial(t-1) )

function perm ( n , k ) -- 0 \le k \le n
  return ( factorial(n) / (factorial(n-k)*factorial(k) ) )

function prob ( n , k , p )
  return ( p^k * (1-p)^(n-k) * perm(n,k) )

-- The main Lua function:
function create_binom_table ( nmax )
  for n = 2 , nmax do
    tex.sprint ( n )
    for k = 0 , n do
      tex.sprint ( "&" .. k .. "&" )
      for p = 1,10 do
        -- round result to four digits
        px = round ( prob ( n, k , p/20 ) , 4 )
        tex.sprint ( string.format("%.4g", px ) )
        if p < 10 then tex.sprint ( "&" ) end
      tex.sprint ( "\\\\" )
    if n < nmax then
      tex.sprint ("\\addlinespace")

\begin{longtable}{@{} rr *{10}{l} @{}}

$n$ & $k$ & \multicolumn{10}{c@{}}{$p$}\\
& & \directlua{make_header_row ()} \\ % create header row

\multicolumn{12}{r@{}}{\footnotesize(Continued on next page)}\\


% call the main Lua function, with n=2 until n=15
\directlua{create_binom_table ( 15 )} 


Addendum: I just noticed that while the OP's code is set to form a table with ten success probabilities ranging from 0.05 to 0.5, these probabilities are not evenly spaced. Adjusting the code above to handle such a case is not difficult.

  • Insert the following instruction near the top of the Lua code block:

    -- vector of success probabilities
  • In the function make_header_row, change the line

              tex.sprint ( "{"..string.format("%.2f",p/20) .. "}" )


              tex.sprint ( "{"..string.format("%.4g",pvec[p]) .. "}" )
  • In the function create_binom_table, change the line

                px = round ( prob ( n, k , p/20 ) , 4 )


                px = round ( prob ( n, k , pvec[p] ) , 4 )

The rest of can remain unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.