chi-helper.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/python3
  2. from scipy import misc
  3. from scipy import stats
  4. import sys
  5. from locale import atof
  6. # this is a silly program
  7. # it is originally from https://moonbooks.org/Articles/How-to-create-a-Chi-square-table-using-python-/
  8. # but the thing is....it.'s from the era where we still used human beings to lookup in tables like this
  9. # what you *really* want is to query this somehow
  10. # https://realpython.com/python-command-line-arguments/
  11. PValueList = [atof(sys.argv[1]),atof(sys.argv[2])]
  12. global pvalue, dfreedom
  13. def newtons_method(f, x, tolerance=0.000001):
  14. if tolerance == 0.0:
  15. print( "no difference" )
  16. while True:
  17. x1 = x - f(x) / misc.derivative(f, x)
  18. t = abs(x1 - x)
  19. if t < tolerance:
  20. break
  21. x = x1
  22. return x
  23. def f(x):
  24. return 1 - stats.chi2.cdf(x, dfreedom) - pvalue
  25. print( 'df\p' , ' |\t ', PValueList[0], ' |\t ', PValueList[1] )
  26. for i in range(1,2):
  27. dfreedom = i
  28. Result = []
  29. for pvalue in PValueList:
  30. x0 = dfreedom # x0 approximation
  31. x = newtons_method(f, x0)
  32. Result.append(x)
  33. for i in range( 2):
  34. Result[i] = round(Result[i],3)
  35. print( dfreedom, ' |\t ', Result[0], ' |\t ', Result[1] )