# Python: compare 2 discrete distributions

Lets say I have a population consisting of items A, B, and C. I dont know the distribution of items in the population. I sample from this population 15 times (as an example, closer to 400 in my actual data) and count how many times I get each item, giving me something like this:

df = pd.DataFrame({'type': ['A', 'B', 'C'], 'count': [7,4,4]})

type  count
0    A      7
1    B      4
2    C      4


I repeat this to get a second sample:

df2 = pd.DataFrame({'type': ['A', 'B', 'C'], 'count': [4,10,1]})

type  count
0    A      4
1    B     10
2    C      1


I want to test whether these 2 samples come from the same population, or whether their frequency distributions are the same. There are posts on the theory behind this (e.g. Test for difference between 2 empirical discrete distributions), but I'm interested in implementation in Python

If I wanted to do a chi square test, would it be as simple as the following?

import scipy.stats
scipy.stats.chisquare(df['count'], df2['count'])


The docs for the chisquare function says the second argument should be expected frequencies of each category. In my mind, this means the distribution in the population, which I dont know. Is it OK to use another set of observed frequencies if I wanted to compare 2 samples?

• Are the A,B,C nominal categories, or ordinal? I ask (even though it sounds like nominal) because of the mention of discrete distributions (which it would make no sense to raise if the categories were nominal), leading to some ambiguity. – Glen_b May 11 at 1:55
• That would be me misunderstanding the term 'discrete' then (I thought it referred to the counts). But yes, the categories are nominal – Simon May 11 at 3:02
• Okay, then it makes little sense to look at that linked post. You're probably going to want some form of chi-squared test, as you seem to have figured out. In particular, given the phrasing "whether their frequency distributions are the same" suggests you're looking for a test of homogeneity of proportion. – Glen_b May 11 at 6:25

This means performing a test of independence with two-way contingency tables. The command you chose (scipy.stats.chisquare) "tests the null hypothesis that the categorical data has the given frequencies", so that's not what you need.
You can concatenate the samples, adding sample variable to the dataframe, write code to create a contingency table, and then use this command: https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.chi2_contingency.html