3

Non-statistician here trying to replicate some code in Python.

R has the function qchisq.

qchisq(c(0.5, 0.1, 0.99), df=1, lower.tail=F)
# [1] 0.4549364231 2.7055434541 0.0001570879

It can be replicated in Python like so:

from scipy.special import chdtri
import pandas as pd
chdtri(1, pd.Series([0.5, 0.1, 0.99]))
# Out[57]:
# 0    0.454936
# 1    2.705543
# 2    0.000157

However, Rs qchisq can also has the flag log.p, which allows me to do:

qchisq(-c(0.5, 0.1, 0.99) * log(10), df=1, lower.tail=F, log.p=T)
# [1] 1.00448472 0.06796154 2.66885996

I cannot find any way to do this in Python. How do I achieve the same thing?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • 1
    Please do not lazily comment about there being posts asking how to replicate the vanilla `qchisq` in Python. I know they exist, and they do not answer my question. – The Unfun Cat Nov 30 '17 at 14:00

1 Answers1

3

Perhaps it's useful to try to remake the effect of log.p without setting the argument in R:

qchisq(.5, 1, lower.tail = FALSE)
# 0.4549364
qchisq(-.5, 1, lower.tail = FALSE, log.p = TRUE)
# 0.265258
qchisq(exp(-.5), 1, lower.tail = FALSE)
# 0.265258

Which gives for the part with log(10):

qchisq(exp(-c(0.5, 0.1, 0.99)*log(10)), df=1, lower.tail=F)
# [1] 1.00448472 0.06796154 2.66885996

So it seems qchisq(exp(x)) == qchisq(x, log.p = TRUE).

Vandenman
  • 3,046
  • 20
  • 33
  • 1
    Yes, and `qchisq(log(c(0.5, 0.1, 0.99)), df=1, lower.tail=F, log.p=T)` gives the same result of `qchisq(c(0.5, 0.1, 0.99), df=1, lower.tail=F, log.p=F)` – Marco Sandri Nov 30 '17 at 14:29
  • `chdtri(1, np.exp(-pd.Series([0.5, 0.1, 0.99]) * np.log(10)))` worked :) Note to self from ten years ago: study more maths. – The Unfun Cat Nov 30 '17 at 15:27