I hope you have some useful tip for me to approach the following task:
I wrote some simple python snippet to plot probability density functions. In my particular case, let them represent class-conditional probabilities for some parameter x
.
So, I am wondering if there is an clever approach (i.e., module) in Python (maybe via a NumPy or SciPy function or method) to solve a simple equation for parameter x
.
E.g.,
pdf(x, mu=10, sigma=3**0.5) / pdf(x, mu=20, sigma=2**0.5) = 1
# get x
Right now, I can only thing of an brute force approach where I use something like
x = np.arange(0, 50, 0.000001)
and keep the x value in the vector that yields the closest
value for 1 when calculating the ratio pdf1/pdf2.
Below is the code I wrote to calculate the pdf and plot the ratio:
def pdf(x, mu=0, sigma=1):
"""Calculates the normal distribution's probability density
function (PDF).
"""
term1 = 1.0 / ( math.sqrt(2*np.pi) * sigma )
term2 = np.exp( -0.5 * ( (x-mu)/sigma )**2 )
return term1 * term2
x = np.arange(0, 100, 0.05)
pdf1 = pdf(x, mu=10, sigma=3**0.5)
pdf2 = pdf(x, mu=20, sigma=2**0.5)
# ...
# ratio = pdf1 / pdf2
# plt.plot(x, ratio)
Thanks!
[pdf(x,...)/pdf(x...)] - 1 = 0
and solve that by minimisation or root finding instead.scipy.optimize.minimize()
be the way to go? EDIT: Just found another one that might be even better suited for this problem:scipy.optimize.minimize_scalar()