0
votes

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). 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.

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). I'm not sure how to interpret that. All I want is to get random numbers following the given PDF; what do these hexadecimals mean?

2
You're printing an object reference. Maybe you wanted to call its _pdf method? Like return my_pdf()._pdf(a,b) or something?trincot
You're right, it should have been **, and I've moved to using my_pdf.pdf(a,b). However, I think I'm still doing this wrong. It gives me a number now - always the same for given parameters - when what I'm trying to do is generate random (x,y) coordinates using the PDF.user1209014

2 Answers

1
votes

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. If you want to access the _pdf function of your class now and calculate the value of the pdf you can use this code:

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.

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.

0
votes

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.

What you probably want to do is call the _pdf method on that instance, rather than return the class itself.

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. Alternatively, you don't need to use the get_brown function - just instantiate the my_pdf class and call the calculation method.