# /Segment33

## To Calculate

1. How many distinct m by n contingency tables are there that have exactly N total events?

[itex] {N+m*n \choose m*n-1} [/itex]

Hmm. Close, but not right. How did you get this? Note that with N=1 we should get m*n, but your answer doesn't! Wpress 15:37, 21 April 2013 (CDT)

Thanks for pointing that out Bill. It should be this: [itex] {N+m*n-1 \choose m*n-1} [/itex] Kai also helped me on that, divide the N balls by m*n-1 dividers so that we can put them in m*n boxes. But, we have to fill a ball in each box first or there will be zero balls between two of the dividers sometime. So we divide N+m*n balls, the space between them will be N+m*n-1. We will pick m*n-1 dividers among the N+m*n-1 possible spaces, thus N+m*n-1 pick m*n-1. -- Silu 10.49, 22 April 2013 (CDT)

2. For every distinct 2 by 2 contingency table containing exactly 14 elements, compute its chi-square statistic, and also its Wald statistic. Display your results as a scatter plot of one statistic versus the other.

Here's my code, I didn't want to mess up with the zero draws as I might get zero denominators for probabilities in Wald's T, so I excluded zero counts in any cell.

```
import random
import itertools
import scipy.misc as ms
import math
import numpy as np
import matplotlib.pyplot as plt

counts=range(1,14)
tablesprep=list(itertools.permutations(counts, 4))
tables=[]

for table in tablesprep:
if (sum(table) ==14):
tables.append(table)

#now I get a list of tables each have total count of 14
wald=[]
chisquares=[]
for table in tables:
p_1=float(table)/float(table+table)
float(p_1)
p_2=float(table)/float(table+table)
p=float(table+table)/14
nt=p_1-p_2
dt_1=math.sqrt(p*(1-p))
dt_2=math.sqrt((1/float(table+table)+(1/float(table+table))))
t=float(nt/(dt_1*dt_2))
wald.append(t)

row_1=table+table
row_2=table+table
col_1=table+table
col_2=table+table
e_11=float(row_1*col_1)/14
e_12=float(row_1*col_2)/14
e_21=float(row_2*col_1)/14
e_22=float(row_2*col_2)/14
chisquare=(table-e_11)**2/e_11+(table-e_12)**2/e_12+(table-e_21)**2/e_21+(table-e_22)**2/e_22
chisquares.append(chisquare)

plt.scatter(chisquares, wald)
plt.xlabel(r'chisquared')
plt.ylabel(r'Wald T')
plt.show()

```