[英]Generating random numbers for a probability density function in Python
I'm currently working on a project relating to brownian motion, and trying to simulate some of it using Python (a language I'm admittedly very new at). 我目前正在从事一个与布朗运动有关的项目,并尝试使用Python(我很陌生的一种语言)来模拟其中的一些。 Currently, my goal is to generate random numbers following a given probability density function.
目前,我的目标是按照给定的概率密度函数生成随机数。 I've been trying to use the scipy library for it.
我一直在尝试使用scipy库。
My current code looks like this: 我当前的代码如下所示:
>>> import scipy.stats as st
>>> class my_pdf(st.rv_continuous):
def _pdf(self,x,y):
return (1/math.sqrt(4*t*D*math.pi))*(math.exp(-((x^2)/(4*D*t))))*(1/math.sqrt(4*t*D*math.pi))*(math.exp(-((y^2)/(4*D*t))))
>>> def get_brown(a,b):
D,t = a,b
return my_pdf()
>>> get_brown(1,1)
<__main__.my_pdf object at 0x000000A66400A320>
All attempts at launching the get_brown function end up giving me these hexadecimals (always starting at 0x000000A66400A with only the last three digits changing, no matter what parameters I give for D and t). 所有尝试启动get_brown函数的尝试最终都给了我这些十六进制数(无论我为D和t给出什么参数,总是从0x000000A66400A开始,只有最后三位数字改变)。 I'm not sure how to interpret that.
我不确定如何解释。 All I want is to get random numbers following the given PDF;
我想要的只是获取给定PDF后面的随机数; what do these hexadecimals mean?
这些十六进制是什么意思?
The result you see is the memory address of the object you have created. 您看到的结果是您创建的对象的内存地址。 Now you might ask: which object?
现在您可能会问:哪个对象? Your method
get_brown(int, int)
calls return my_pdf()
which creates an object of the class my_pdf
and returns it. 您的方法
get_brown(int, int)
调用return my_pdf()
,它将创建类my_pdf
的对象并将其返回。 If you want to access the _pdf
function of your class now and calculate the value of the pdf
you can use this code: 如果要立即访问类的
_pdf
函数并计算pdf
的值,则可以使用以下代码:
get_brown(1,1)._pdf(x, y)
On the object you have just created you can also use all methods of the scipy.stats.rv_continous
class, which you can find here . 在刚刚创建的对象上,您还可以使用
scipy.stats.rv_continous
类的所有方法,可以在此处找到。
For your situation you could also discard your current code and just use the normal distribution included in scipy
as Brownian motion is mainly a Normal random process. 对于您的情况,您也可以丢弃当前代码,而仅使用
scipy
包含的scipy
因为布朗运动主要是正态随机过程。
As noted, this is a memory location. 如前所述,这是一个存储位置。 Your function
get_brown
gets an instance of the my_pdf
class, but doesn't evaluate the method inside that class. 您的函数
get_brown
获取my_pdf
类的实例,但不评估该类内的方法。
What you probably want to do is call the _pdf
method on that instance, rather than return the class itself. 您可能想做的是在该实例上调用
_pdf
方法,而不是返回类本身。
def get_brown(a,b):
D,t = a,b # what is D,t for?
return my_pdf()_pdf(a,b)
I expect that the code you've posted is a simplification of what you're really doing, but functions don't need to be inside classes - so the _pdf
function could live on it's own. 我希望您发布的代码可以简化您的实际工作,但是函数不必位于类内部-因此
_pdf
函数可以自己生存。 Alternatively, you don't need to use the get_brown
function - just instantiate the my_pdf
class and call the calculation method. 另外,您不需要使用
get_brown
函数-只需实例化my_pdf
类并调用计算方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.