# Chi-sqaure test in Python [closed]

I want to perform a Chi-square test on a known sample in Python. Since I come from R, to my knowledge I can do something like this:

x.poi<-rpois(n=200,lambda=2.5)
gf <-goodfit(x.poi,type= "poisson",method= "MinChisq")
summary(gf)


and then I can just call the summary function and get my results.

I know that in Python scipy provides a chi-square test, however, it is needed to provide an expected array of samples.

Is there an equivalent version of this R function for Python?

You cannot get exactly the same, without implementing an optimization for the MinChisq estimate of the mean of your poisson, $$\hat{\lambda}$$.

So below is an example using the "ML" option to estimate $$\hat{\lambda}$$, and you still get a chi-sq test in the end. This is convenient because the MLE estimator of lambda will be the mean:

set.seed(111)
x.poi<-rpois(n=200,lambda=2.5)
gf = goodfit(x.poi,type= "poisson",method= "ML")
summary(gf)

Goodness-of-fit test for poisson distribution

X^2 df  P(> X^2)
Likelihood Ratio 6.874807  7 0.4420304

gf$$par$$lambda
[1] 2.545

mean(x.poi)
[1] 2.545

write.csv(x.poi,"x.poi.csv")


We calculate the expected using the mean as $$\hat{\lambda}$$ :

import pandas as pd
import numpy as np
import scipy


scipy.stats.power_divergence(obs,expected,lambda_="log-likelihood".,ddof=1)