12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/usr/bin/python3
- from scipy import misc
- from scipy import stats
- import sys
- from locale import atof
- # this is a silly program
- # it is originally from https://moonbooks.org/Articles/How-to-create-a-Chi-square-table-using-python-/
- # but the thing is....it.'s from the era where we still used human beings to lookup in tables like this
- # what you *really* want is to query this somehow
- #
- # Creative Commons Attribution-ShareAlike 4.0 International License.
- # CC Ben, the Founder of moonbooks.org
-
- # https://realpython.com/python-command-line-arguments/
- PValueList = [atof(sys.argv[1]),atof(sys.argv[2])]
- global pvalue, dfreedom
- def newtons_method(f, x, tolerance=0.000001):
- if tolerance == 0.0:
- print( "no difference" )
- while True:
- x1 = x - f(x) / misc.derivative(f, x)
- t = abs(x1 - x)
- if t < tolerance:
- break
- x = x1
- return x
- def f(x):
- return 1 - stats.chi2.cdf(x, dfreedom) - pvalue
- print( 'df\p' , ' |\t ', PValueList[0], ' |\t ', PValueList[1] )
- for i in range(1,2):
- dfreedom = i
- Result = []
- for pvalue in PValueList:
- x0 = dfreedom # x0 approximation
- x = newtons_method(f, x0)
- Result.append(x)
- for i in range( 2):
- Result[i] = round(Result[i],3)
- print( dfreedom, ' |\t ', Result[0], ' |\t ', Result[1] )
|