0

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!

5
  • 1
    Sounds like you need a book on numerical methods more than Python programming advice. Here is a tip -- recast the equation to something like [pdf(x,...)/pdf(x...)] - 1 = 0 and solve that by minimisation or root finding instead.
    – talonmies
    Feb 12, 2014 at 6:52
  • 1
    I think that if you write down the equation on a piece of paper, you can fairly straightforwardly solve this without numerical methods.
    – user707650
    Feb 12, 2014 at 7:00
  • Sure, but I want to implement it into Python code, since I want to plot those points directly with the accompanied graphs.
    – user2489252
    Feb 12, 2014 at 20:19
  • @talonmies Sounds good! So for the minimization would 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()
    – user2489252
    Feb 12, 2014 at 20:25
  • You are right, an analytical solution really is more appropriate here. However, I is good to know about these alternatives like minimization for different tasks. I will do both and compare the results. Thanks.
    – user2489252
    Feb 12, 2014 at 20:34

2 Answers 2

0

In general, it sounds like you need the scalar root-finding functions: http://docs.scipy.org/doc/scipy/reference/optimize.html

But as others have pointed out, it seems like there is an analytical solution.

0

Since you have a nice closed-form equation, you can solve it with SymPy.

I plugged in values for mu and sigma and entered this into Sympy Gamma:

 solve(1.0 / ( sqrt(2*pi) *(3**0.5) ) * exp( -0.5 * ( (x-10)/(3**0.5) )**2 ) /  (1.0 / ( sqrt(2*pi) *(2**0.5) ) * exp( -0.5 * ( (x-20)/(2**0.5) )**2 ))-1,x)

The result: 15.4554936768195

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.