python scipy.integrate inverse ppf

Viewed 265

Hello I would like what is the x value that would give me an specific numeric integral.

So far I am using scipy.integrate and works ok, but given a integral value is there a way to know what is the x that gave that result?

let say I have a function f(x) = |2-x| 1<=x<=3

I would like to know what is the x value that give me 0.25 (the first quartile).

What in scipy.stats for normal distribution is norm.ppf(), in this particular case I am using it for PDF (probability density function) but it can be any integral.

Thanks and regards

1 Answers

I use binary search to find answer in logarithmic time (very fast). You can also run code below online here.

import math, scipy.integrate, scipy.optimize

def f(x):
    return math.sin(x)

a, b = 0, 10
integral_value = 0.5

res_x = scipy.optimize.bisect(
    lambda x: scipy.integrate.quad(f, a, x)[0] - integral_value,
    a, b
)
print(
    'found point x', res_x, ', integral value at this point',
    scipy.integrate.quad(f, a, res_x)[0]
)
Related