Difference Between One Sample vs Two Sample chi-squared test I'm working on a project that involves finding p-values for data using a chi-squared test. I've written up code for it. Unfortunately, I seem to be trapped in a loop: 


*

*I email in my code

*I'm told "This is code for a one sample chi-squared test, you need to do a two sample chi-squared test 

*I look up two sample chi-squared test online. It looks exactly like what I already have.

*Repeat, repeat, repeat


Here is my code (in Python). gens is a list of integers: 0, 1 or 2. phens is a list of integers: 0 or 1. The goal is to determine whether individuals with phen=0 have different gens compared to individuals with phen=1.
def pValues(gens,phens):
    observed=[0.,0.,0.,0.,0.,0.]​
    n0=0​
    n1=0​
    gen0=0​
    gen1=0​
    gen2=0​
    for i in range(len(gens)):​
        if phens[i]==0:​
            n0+=1​
            if gens[i]==0:​
                gen0+=1​
                observed[0]+=1​
            elif gens[i]==1:​
                gen1+=1​
                observed[1]+=1​
            elif gens[i]==2:​
                gen2+=1​
                observed[2]+=1​
        elif phens[i]==1:​
            n1+=1​
            if gens[i]==0:​
                gen0+=1​
                observed[3]+=1​
            elif gens[i]==1:​
                gen1+=1​
                observed[4]+=1​
            elif gens[i]==2:​
                gen2+=1​
                observed[5]+=1​
    expected=[n0*gen0,n0*gen1,n0*gen2,n1*gen0,n1*gen1,n1*gen2]​
    for i in range(6):​
        expected[i]=float(expected[i])/len(gens)​
    chisum=0​
    for k in range(6):​
        if expected[k]!=0:​
            chisum+=((observed[k]-expected[k])**2)/expected[k]​
        else:​
            chisum='Infinity'​
            break​
    if chisum!='Infinity':​
        pvalue=1-chi2.cdf(chisum,2)​
    else:​
        pvalue=0​
    return pvalue

So for each i=0,1,2 and j=0,1, the expected number of individuals with gen=i and phen=j is #(gen=i) * #(phen=j)/#(total). Isn't this what the two-sample chi squared test calls for? What's wrong?
Thank you
 A: I have altered the code some to make it clear that you are creating your expected values from the row totals times the column totals, divided by the overall count. Good luck.
def pValues(gens,phens):
        observed=[0.,0.,0.,0.,0.,0.]​
        n=len(gens)
        phen0=0​
        phen1=0​
        gen0=0​
        gen1=0​
        gen2=0​

        for i in range(len(gens)):​
            if phens[i]==0:​
                phen0+=1​
                if gens[i]==0:​
                    gen0+=1​
                    observed[0]+=1​
                elif gens[i]==1:​
                    gen1+=1​
                    observed[1]+=1​
                elif gens[i]==2:​
                    gen2+=1​
                    observed[2]+=1​
            elif phens[i]==1:​
                phen1+=1​
                if gens[i]==0:​
                    gen0+=1​
                    observed[3]+=1​
                elif gens[i]==1:​
                    gen1+=1​
                    observed[4]+=1​
                elif gens[i]==2:​
                    gen2+=1​
                    observed[5]+=1​

        expected=[phen0*gen0/n,phen0*gen1/n,phen0*gen2/n,phen1*gen0/n,phen1*gen1/n,phen1*gen2/n]​

        if 0 in expected:
            chisum = 'Infinity'
        else:
            chisum=0​
            for k in range(6):​
                chisum+=((observed[k]-expected[k])**2)/expected[k]​

        if chisum!='Infinity':​
            pvalue=1-chi2.cdf(chisum,2)​
        else:​
            pvalue=0​

        return pvalue

