Reputation: 23
So I,m translating Matlab to python code and came across this line
Pv = chi2cdf(Devk,Md,'upper');
Where Devk is a 3D matrix and Md is df=10.
I was looking for the equivalent in Python. I've found this:
from scipy.stats import chi2
Pv = chi2.cdf(Devk,Md)
However, this does not give similar results, I guess because of the "upper" argument Matlab has. Does anyone has an idea of how to implement this in Python?
Someone asked to make some toy data to show the problem:
Matlab:
Devk = [1,2,3,4,5]
Md = 10
Pv = chi2cdf(Devk,Md,'upper')
Pv =
0.9998 0.9963 0.9814 0.9473 0.8912
Python:
from scipy.stats import chi2
Devk = np.array([1,2,3,4,5])
Md = 10
Pv = chi2.cdf(Devk,Md)
Pv
Out[125]: array([0.00017212, 0.00365985, 0.01857594, 0.05265302, 0.10882198])
Upvotes: 0
Views: 773
Reputation: 26896
I believe that the following holds:
chi2cdf(..., 'upper') == (1 .- chi2cdf(...))
So, in your Python code it is likely that you have to do:
from scipy.stats import chi2
Pv = 1 - chi2.cdf(Devk, Md)
Even better to use:
Pv = chi2.sf(Devk, Md)
because, according to the documentation, the survival function sf()
(also defined as 1 - cdf()
), is sometimes more accurate.
Upvotes: 1