0

I am trying to apply the Q-function values for a problem. I don't know the function available for it in Python.

What is the python equivalent for the following code in octave?

>> f=0:0.01:1;
>> qfunc(f)
MSR
  • 77
  • 1
  • 3
  • 14

3 Answers3

2

The Q-function can be expressed in terms of the error function. Check here for more info. "scipy" has the error function, special.erf(), that can be used to calculate the Q-function.

import numpy as np
from scipy import special
f = np.linspace(0,1,101)
0.5 - 0.5*special.erf(f/np.sqrt(2)) # Q(f) = 0.5 - 0.5 erf(f/sqrt(2))
Farzad
  • 71
  • 4
  • 2
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 20 '20 at 20:01
1

Take a look at this https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.stats.norm.html Looks like the norm.sf method (survival function) might be what you're looking for.

Sergio
  • 640
  • 6
  • 23
0

I've used this Q function for my code and it worked perfectly well,

from scipy import special as sp
def qfunc(x):
    return 0.5-0.5*sp.erf(x/sqrt(2))

I'vent used this one but I think it should work,

def invQfunc(x):
    return sqrt(2)*sp.erfinv(1-2x)

references: https://mail.python.org/pipermail/scipy-dev/2016-February/021252.html Python equivalent of MATLAB's qfuncinv() Thanks @Anton for letting me know how to write a good answer

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Anton Menshov Sep 11 '20 at 00:32
  • The inverse Q function should be `return np.sqrt(2)*sp.erfinv(1-2*x)`, and require `import numpy as np`. – wimi May 18 '21 at 09:26