# Create a table with calculations of binomial and poisson distribution

Well, I was looking for some tables on the internet about the binomial and poisson distribution (not the cumulative ones) in LaTeX format. But I've only found the code of the cumulative distributions, which I don't want. Then I looked around for other formats (not LaTeX), but the only I've found are large tables (3–4 pages).

So I'm thinking if it's possible to make LaTeX do the calculations and then give me the table, based on a few variables. Is this possible?

The final idea is to get this, which is the horrible table the teacher gave us.

EDIT: Here is my original idea (the only way I know to do it):

\documentclass[tikz]{standalone}
\standaloneconfig{border=2mm}
\begin{document}
\begin{tikzpicture}
\foreach \c in {2,...,7}{
\def\n{0.6*\c}
\draw (-1,-\n^2/2 -\n/2) node {$\c$};
\draw (-1.5,-\n^2/2 -\n/2 + 0.3) -- (22,-\n^2/2 -\n/2 + 0.3);
\foreach \k in {0,...,\c}{
\draw (0, -\n^2/2 -\n/2 -0.6*\k) node {$\k$};
\foreach \p/\h in {0.01/1,0.05/2,0.1/3,0.15/4,0.2/5,0.25/6,0.3/7,0.3333333333/8,0.35/9,0.4/10,0.45/11,0.49/12,0.5/13}{
\draw (1.6*\h, -\n^2/2 -\n/2 -0.6*\k) node {\pgfmathparse{\n!/(\k!*(\n-\k)!)*\p^(\k)*(1-\p)^(\n-\k)}\pgfmathresult};
}
}
}
\end{tikzpicture}
\end{document}


It needs some tweaking, but this is the original idea. The problem is that I can't make \c go to 10, I get an Arithmetic Overflow (which I don't know what is it), and, of course, this is not a real table.

EDIT2: Here it is my second edition. Now with some minor problems, but almost solved by @Qrrbrbirlbel. There are still some minor problems which I have ignored, because it's not necessary to me, but may be future people will ask about it:

• First, it doesn't work well if you try to write \myHeadList{0.1,0.5,0.10,...,0.30} with ..., the PGF has some problems here (with non integers numbers). This is partially solved in Qrrbrbirlbel's answer.

• Second, to alternate color in rows, we can use \usepackage[table]{xcolor} in the preamble and \rowcolors{2}{gray!10}{white} inside the document. But with booktabs this doesn't work quite well.

-
This is essentially a simple numerical computation, so LuaTeX is ideally suited for it. See ConTeXt wiki for an example of a much simpler calculation (the lualatex solution will be similar). On a different note, why do you consider the table given by your teacher to be horrible. To me, it looks like a very well typeset table. – Aditya Nov 25 '12 at 20:26
I've never used nothing different than LaTeX, so I hope there are other options, but I will look for ConTeXt. About the document, well, is horrible because this is the document he gave us, this .pdf. And I prefer a clean table, with no noise and black points around the document. – Manuel Nov 25 '12 at 20:31
The same solution is possible in LuaLaTeX as well. The important thing is to use LuaTeX to do the calculations rather than doing them in TeX. You can also consider using your favorite programming language to do the calculation and generate the relevant LaTeX code. – Aditya Nov 25 '12 at 20:33
With the pgf math library this should be doable, but you should compose a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. And where you want the calculations show the formula you want applied. Or, see for example a similar question: Mathematical functions inside LaTeX tables. – Peter Grill Nov 25 '12 at 21:44
@Manuel: Create a few rows of the table, and for the column where the values are computed show the equation that is used to compute that column. That would be a MWE, then we just need to complete that entry to solve your problem, not try to set up the table, or try to determine the formula you want to compute that column with. – Peter Grill Nov 26 '12 at 5:18

The following code shows a solution without the use of external applications.

# Binomial distribution

I now have included PGF's own fpu library. Hopefully its setup is correct.
Even n > 8 will compile now (before: ! Dimension too large., this was due to the calculation of the binomial coefficient.)

### How to use.

There exists one user macro: \binomTable[<zero>]{<start n>}{<end n>}.

The optional <zero> argument decides whether zero cells (0.0000) should be typeset at all (0: 0.0000 is typeset, everything else: 0.0000 is not typeset).
[This is hard-coded, any change to the /pgf/number format/precision key will have to be shadowed there manually.]

The second and third argument define the start and the end n, respectively.

### Binomial coefficient

For the bionomial coefficient I used the following formula with i = \j

### pgfplotstable?

I don't know how to use pgfplotstable.

## Code

\documentclass[landscape]{article}
\usepackage[landscape, margin=.5cm]{geometry}
\usepackage{booktabs}
\usepackage{pgf,pgffor}
\usepackage{etoolbox}
\usepgflibrary{fpu}
\pgfkeys{/pgf/fpu}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=4}

%%% Defining basic stuff
\def\myHeadList{0.01, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 1/3, 0.35, 0.40, 0.45, 0.49, 0.50}
%\def\startN{9}
%\def\endN{10}

\newif\ifsomanyzeroesshouldbeprinted
\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
\edef\startN{#2}%
\edef\endN{#3}%
\expandafter\ifnum#1=0\relax%
\somanyzeroesshouldbeprintedtrue%
\else%
\somanyzeroesshouldbeprintedfalse%
\fi%
%
\def\myHead{ $n$ & $k$ & $n\choose k$}%
\xappto\myHead{& {$\p$}}%
}%
%
%%% Bulding body of table
\def\myTable{}%
\def\myN{\startN,...,\endN}%
\foreach \n in \myN {%
\foreach \k in {0,...,\n}{%
\ifnum\k=0\relax%
\xappto\myTable{\noexpand\midrule\n}%
\fi%
\xappto\myTable{& \k}%
\pgfmathsetmacro{\binomProduct}{1}%
\xdef\myTempValue{\binomProduct}%
\ifnum\k=0\relax%
\xdef\oldK{\k}%
\else%
\foreach \j in {1,...,\k}{%
\pgfmathsetmacro{\binomProduct}{\myTempValue*(\n+1-\j)/\j}%
\xdef\myTempValue{\binomProduct}%
}%
\fi%
\pgfmathsetmacro{\myTempValue}{round(\myTempValue)}%
\xappto\myTable{& \noexpand\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,set thousands separator={\,},min exponent for 1000 sep=4]{\myTempValue}}%
\pgfmathsetmacro{\result}{\myTempValue*\p^(\k)*(1-\p)^(\n-\k)}%
\ifsomanyzeroesshouldbeprinted%
\xappto\myTable{& \noexpand\pgfmathprintnumber{\result}}%
\else%
\ifdim\result pt<0.00005pt\relax%
\gappto\myTable{&}%
\else%
\xappto\myTable{& \noexpand\pgfmathprintnumber{\result}}%
\fi%
\fi%
}%
\gappto\myTable{\\}%
}%
}%
}
\pagestyle{empty}

\def\formulae{%
\begin{tabular}{@{}l@{}}
$\displaystyle p(\xi=k)={{n}\choose{k}} p^k q^{n-k}$ \\
$\displaystyle {n\choose k}=\prod_{j=1}^k {n+1-j \over j}$
\end{tabular}%
}
\begin{document}

\binomTable[1]{2}{8}
\begin{tabular}{rrr*{13}{r}}
\end{tabular}
\hfill\formulae

\binomTable[1]{9}{11}
\begin{tabular}{rrr*{13}{r}}
\end{tabular}
\hfill\formulae

\binomTable[1]{12}{14}
\begin{tabular}{rrr*{13}{r}}
\end{tabular}
\hfill\formulae

\binomTable[1]{15}{16}
\begin{tabular}{rrr*{13}{r}}
\end{tabular}
\hfill\formulae
\end{document}


## Output

These are old images, the current code adds another column where n \choose k is given.

# Poisson distribution

Your code looks fine in my eyes.

PGF’s \foreach is known to have precision problems with ... and non-integer values.
My work-around: Use

\foreach \ll in \myLList {%
\pgfmathsetmacro{\l}{.1*\ll}


and then you can create your tables with

\poissonTable[1]{1,...,40}{12}


Interestingly \pgfmathsetmacor{\l}{\ll/10} won’t work.

In my given example below I used {.1*1,.1*...,.1*40} and parsing that to \l. The macro \poissonTable takes 1, .1* and 40 as separated parameters whereas the macro \poissonTableL expects the list itself.

I added an option to space every _5_th row by testing whether \l is divisible by .5. We could just use a row-counter we can check for divisibility, but that would give us an separate last row in the second example (which is like your professor's).

Testing for the last row works out of the box with \poissonTable but needs a little help for \poissonTable.

This solution is very special and probably will need extra care if the rows differ very much from this scheme.

Maybe you want a more explicit sort of giving the rows like in another answer of mine where you could say {.1,.2,.3,.4,[1].5,.6,.7,.8,.9,[1]1,…} so that rows with [<x>] get <x>ex-spacing …?

## Code (without output)

\documentclass[landscape]{article}
\usepackage[landscape,margin=.5cm]{geometry}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{pgf,pgffor}
\usepackage{etoolbox}
\usepgflibrary{fpu}
\pgfkeys{/pgf/fpu}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=4}

\edef\mysecondtest{false}%
\def\mc#1{\multicolumn{1}{c}{#1}}
\newif\ifsomanyzeroesshouldbeprinted
\newcommand*{\poissonTable}[5][0]{% #1   = 0 => results in the form 0.0000 are typeset
% #1 != 0 => results in the form 0.0000 are omitted
% #2 = startN
% #3 = midN
% #4 = endN
% #5 = endK
\edef\startN{#2}\edef\midN{#3}\edef\endN{#4}%
\edef\myLList{\midN\startN,\midN...,\midN\endN}%
\edef\lastN{\midN\endN}%
\poissonTableL[#1]{\myLList}{#5}
}
\newcommand*{\poissonTableL}[3][0]{% #1   = 0 => results in the form 0.0000 are typeset
% #1 != 0 => results in the form 0.0000 are omitted
% #2 = Liste
% #3 = endK
\edef\myLList{#2}
\edef\endK{#3}%
\expandafter\ifnum#1=0\relax%
\somanyzeroesshouldbeprintedtrue%
\else%
\somanyzeroesshouldbeprintedfalse%
\fi%
\def\myLHead{\mc{$\lambda$} }%
\foreach \k in {0,...,\endK} {%
\ifnum\k=0\relax%
\xappto\myLHead{& \noexpand\mc{\llap{$k\to{}$}$\k$}}% or \to replaced by =
\else%
\fi
}%
%%% Bulding body of table
\def\myLTable{}%
\foreach \ll in \myLList {%
\pgfmathsetmacro{\l}{\ll}%
\xappto\myLTable{ $\l$}%
\foreach \k in {0,...,\endK}{%
\pgfmathsetmacro{\Lresult}{e^(-\l)*((\l)^(\k))/((\k)!)}%
\ifsomanyzeroesshouldbeprinted%
\xappto\myLTable{& \noexpand\pgfmathprintnumber{\Lresult}}%
\else\ifdim\Lresult pt<0.00005pt\relax%
\gappto\myLTable{&}%
\else%
\xappto\myLTable{& \noexpand\pgfmathprintnumber{\Lresult}}%
\fi%
\fi%
}%
\gappto\myLTable{\\}%
\pgfkeys{/pgf/fpu=false}%
\pgfmathsetmacro{\lastRow}{\ll==\lastN?"0":"1"}%
\pgfmathsetmacro{\lastRow}{\mysecondtest?"0":"\lastRow"}%
\pgfmathparse{mod(\l,.5)==0?"[\lastRow ex]":""}%
\xappto\myLTable{\pgfmathresult}%
}%
\edef\mysecondtest{false}%
}
\pagestyle{empty}

\begin{document}
\poissonTable[1]{1}{.1*}{40}{12}
\begin{tabular}{rr*{13}{r}}
\end{tabular}

\edef\lastN{10}%
\def\mysecondtest{mod(\ll,1)==0}%
\poissonTableL[1]{.1*1,.1*...,.1*20,.1*22,.1*24,.1*...,.1*40,5,6,...,10}{12}
\begin{tabular}{rr*{13}{r}}
\end{tabular}
\end{document}

-
Wow, looks great. I guess next step is trying pgfplotstable :) – percusse Nov 26 '12 at 17:42
Really nice answer! – Gonzalo Medina Nov 26 '12 at 17:55
Haha - I feel abashed now. Clearly a perfect answer! Which part of the code produces the \hline between? I woudl be happy if could elaborate the code a little bit. What happens here? – Manuel Kuehner Nov 26 '12 at 18:11
@Manuel See my updated answer on the topic of the \foreach-... problem. Colors and lines (especially booktabs’) are usually considered not to be used together. (→ Professional-looking tables with alternating row colors?) You could also consider coloring spans of lines, using extra \midrules or adding vertical space every five or ten rows (as in your professor's table). – Qrrbrbirlbel Nov 26 '12 at 22:25
If {.1*1,.1*...,.1*40} syntax is causing trouble you can switch to [evaluate=\x as \scaledx using 0.1*\x] key and give the list as integers again. – percusse Nov 27 '12 at 1:07

It is quite simple. If you are familiar with Matlab or GNU R. I prepared a Matlab example for the first row. It is only semi-automatic -- you need to modify the result manually. But I guess it takes not more than 2 hours until you are satisfied (depending on your experience of course).

Here's the Matlab code:

%% Generate first row
% http://f.cl.ly/items/1z2p0g2H410J3K0v1z32/Binomial_y_Poisson.pdf

n = 2;
for k=0:1:2
i=1;
for p=[0.01 0.05 0.15 0.20 0.30 1/3 0.35 0.40 0.45 0.50]
Result(k+1,i) = binopdf(k,n,p)
i=i+1
end
end

%% Save to file
% Problem: Still no \\ at the end of each row
try
delete myResult1File
catch
end

dlmwrite('myResult1File.txt',Result,'delimiter','&','newline','pc','precision','%1.4f')

%% Add \\\r\n to each row

myFileIn = fopen('myResult1File.txt');
myFileOut = fopen('myResult2File.txt','w');

while ~feof(myFileIn)
s = fgetl(myFileIn); % read line by line
fprintf(myFileOut,'& & & %s\\\\\r\n',s); % write line by line and add \\ before newline
end

fclose(myFileIn)
fclose(myFileOut)


The result will be stored in myResult2File.txt and will look like this:

& & & 0.9801&0.9025&0.7225&0.6400&0.4900&0.4444&0.4225&0.3600&0.3025&0.2500\\
& & & 0.0198&0.0950&0.2550&0.3200&0.4200&0.4444&0.4550&0.4800&0.4950&0.5000\\
& & & 0.0001&0.0025&0.0225&0.0400&0.0900&0.1111&0.1225&0.1600&0.2025&0.2500\\


By the way, myResult1File.txt looks like this:

0.9801&0.9025&0.7225&0.6400&0.4900&0.4444&0.4225&0.3600&0.3025&0.2500
0.0198&0.0950&0.2550&0.3200&0.4200&0.4444&0.4550&0.4800&0.4950&0.5000
0.0001&0.0025&0.0225&0.0400&0.0900&0.1111&0.1225&0.1600&0.2025&0.2500


The LaTeX file could look like this (I recommend using the booktabs package for example):

\documentclass[a4paper]{article}
\usepackage[textwidth=170mm]{geometry}
\begin{document}

\begin{center}
{\large\textbf{\hspace*{1.0cm}Table of the Binominal Distribution}}
\end{center}

Here you go\ldots

\begin{center}
\small
\begin{tabular}{|r|r|*{11}r|} % *{11}r -> rrrrrrrrrrr
\hline
$n \downarrow$ & $k \downarrow$ & $p \to$ & 0.01 & 0.05 & 0.15 & 0.20 & 0.30 & 1/3 & 0.35 & 0.40 & 0.45 & 0.50\\
\hline
2 & 0 &  & 0.9801&0.9025&0.7225&0.6400&0.4900&0.4444&0.4225&0.3600&0.3025&0.2500\\
& 1 &  & 0.0198&0.0950&0.2550&0.3200&0.4200&0.4444&0.4550&0.4800&0.4950&0.5000\\
& 2 &  & 0.0001&0.0025&0.0225&0.0400&0.0900&0.1111&0.1225&0.1600&0.2025&0.2500\\
\hline
\end{tabular}
\end{center}

\end{document}


The PDF file will then look like this:

To compare with the original:

Damn, I now see that I forgot p=0.10 and p=0.49 but you get the idea anyway.

Remark: There seems to be a little rounding error in your professors table (compare n=2,k=0,p=0.1). Or Matlab is wrong.

Another Remark: Why do you need still this kind of table nowadays?! Isn't it outdated like a sin or log table :)?

-
I would recommend using *11r instead of rrrrrrrrrrr, easier to read and change. Also you could modify the MATLAB code to output all columns of the table and then just use \input to include it. This way it will save the trouble of copy-n-paste. – mythealias Nov 26 '12 at 16:30
@mythealias: I agree totally. I just had not enough time to do it better (I am supposed to work on my thesis right now ;)). – Manuel Kuehner Nov 26 '12 at 16:43
Thanks for the solution, I'm pretty bad at using MatLab, but I will copy your solution when I come back home (I have MatLab installed in the PC). Anyway, I would prefer a LaTeX based solution, where the calculus was made by the same program which compiles my document. Good luck with your thesis. – Manuel Nov 26 '12 at 17:01
@mythealias Actually *{11}r is needed. – Qrrbrbirlbel Nov 26 '12 at 21:08
@Qrrbrbirlbel: I edited the code accordingly. Thx. – Manuel Kuehner Nov 27 '12 at 17:30