I have a lognorm distribution from scipy and its parameters are known.
import scipy
log_norm_obj = scipy.stats.lognorm([log_mu], shape=sigma)
I need to solve for a x which satisfies the following equation:
x = (1 - log_norm_obj.cdf(x)) / log_norm_obj.pdf(x)
How could I do this using numpy/scipy? Thanks!
You use scipy.optimize. From scipy 0.11 and later, you can use the new functions
minimizeorminimize_scalar. Assuming your x is a scalar, here’s some example code on how to do it:The above uses Brent’s method, the default. You can also the Golden method, or a bounded version of Brent’s method. The latter could be useful if your function is only defined in a given domain or you want a solution in a specific interval. An example of this:
If your function takes a vector instead of a scalar, a similar approach can be taken using
minimize. If your scipy is older than version 0.11, just use a flavour offmin.