如何使用Python解1参数方程式(scipy/numpy?) [英] How to solve an 1-parameter equation using Python (scipy/numpy?)
问题描述
我希望您有一些有用的提示可以帮助我完成以下任务:
I hope you have some useful tip for me to approach the following task:
我写了一些简单的python代码段来绘制概率密度函数.在我的特殊情况下,让它们表示某些参数x
的类条件概率.
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
.
因此,我想知道Python中是否有一种巧妙的方法(即模块)(也许通过NumPy或SciPy函数或方法)来解决参数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
现在,我只能使用类似
x = np.arange(0, 50, 0.000001)
并将x值保留在可产生最接近值的向量中
计算比率pdf1/pdf2.
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.
下面是我编写的用于计算pdf并绘制比率的代码:
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)
谢谢!
推荐答案
通常,听起来您需要标量根查找函数: http://docs.scipy.org/doc/scipy/reference/optimize.html
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.
这篇关于如何使用Python解1参数方程式(scipy/numpy?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!