Simulating Brownian Motion in Python

  Рет қаралды 41,775

mCoding

mCoding

Күн бұрын

Пікірлер: 77
@abdelghafourfid8216
@abdelghafourfid8216 3 жыл бұрын
I'd love to see more of stochastics from you, you have such a good ability of simplifying those hard concepts, please consider making a playlist for stochastics.
@gidi5779
@gidi5779 3 жыл бұрын
Great video! I'd definitely like to see more on Brownian motion and other stochastic processes
@mCoding
@mCoding 3 жыл бұрын
Noted!
@webdeveloper-vy7hb
@webdeveloper-vy7hb 3 жыл бұрын
That's a great video @mCoding, keep it up please. We need that kind of visualization to grasp the core.
@mCoding
@mCoding 3 жыл бұрын
You are welcome, I'm glad you appreciate it! Brownian Motion is a tricky thing, I'm always thinking of new ways to visualize it. Don't forget to check out my Brownian Motion zoom videos too! All viz no code in those though :)
@Jojo-wj8qv
@Jojo-wj8qv 3 жыл бұрын
You just helped me completing my homework exercise, I have been looking for a video like this one for two hours and then I found this one. You're blessed!!
@mCoding
@mCoding 3 жыл бұрын
Awesome! Brownian motion can be a lot of fun!
@Jojo-wj8qv
@Jojo-wj8qv 3 жыл бұрын
@@mCoding So is there a way to calculate the sample mean?
@mCoding
@mCoding 3 жыл бұрын
In numpy it's easy, just use np.mean(arr). For a standard brownian motion you should find the sample mean is close to 0.
@hemanthkotagiri8865
@hemanthkotagiri8865 3 жыл бұрын
A small correction: 0:38 - Matplotlib for plotting.
@mCoding
@mCoding 3 жыл бұрын
Yes, it irks me everytime I watch the video :(
@bp56789
@bp56789 3 жыл бұрын
@@mCoding Damn, NumPy does it all.
@mikeh7704
@mikeh7704 3 жыл бұрын
Learning Python with well-explained practical examples. Thank you.
@mCoding
@mCoding 3 жыл бұрын
You are welcome!
@bunny.bunbob
@bunny.bunbob 3 жыл бұрын
3:28 why do you multiply by the variance you desire and not specify it as a parameter of the np.random.normal function? i thought the SD of the normal distribution is specified in brackets and multiplying a normal distribution with a factor just changes its shape on the y-axis?!
@yuvanmar42
@yuvanmar42 3 жыл бұрын
You have great content lol you deserve the love from the algorithm
@mCoding
@mCoding 3 жыл бұрын
I appreciate that! Make sure to tell your friends!
@jankyjake401
@jankyjake401 5 ай бұрын
You were super helpful, thanks man
@Grersh
@Grersh 3 жыл бұрын
weren't you at like 800 subs yesterday lol
@mCoding
@mCoding 3 жыл бұрын
It seems I have been chosen by the algorithm...
@delinquenice
@delinquenice 3 жыл бұрын
@@mCoding I’m pretty happy for it in this case. Your tutorials are very clear and presented in a clear way, so I’m staying 🙂
@sneakyturtle6143
@sneakyturtle6143 2 жыл бұрын
Straight to the point me: happy
@mCoding
@mCoding 2 жыл бұрын
Glad you enjoyed!
@Patrickoliveirajf
@Patrickoliveirajf 3 ай бұрын
Very good content!
@ren200758
@ren200758 3 жыл бұрын
Great video. I've used it to simulate not only Brownian motion but also others, such as geometric and Ornstein-Uhlenbeck. even being able to compare with the theoretical predictions. one thing I do wonder is that how do we start from such models and apply them to real-world data? probabilistic programming?
@mCoding
@mCoding 3 жыл бұрын
Awesome! Be aware that this simulation method is not stable for OU processes with certain parameters. Your question is totally valid but unfortunately the answer is a whole career not just a KZbin comment!
@cdellio
@cdellio 3 жыл бұрын
Hi James! I've been watching a lot of your python videos recently. I don't use in python for work, but in my leisure it's what I always try to use. I have a question: how do you get to your level of understanding, best? You knock out hard concepts in really concise code. Is your skill just from investing time and energy in learning these topics, and enhancing your abilities along the way? If you have any secrets, please share them!
@mCoding
@mCoding 3 жыл бұрын
Hi Christian! Glad to hear you like my videos! This is just one of those things I don't have a good answer for. I try to learn as much as I can, and I watch lectures every day, which help. Experience helps a lot but I also have to purposefully invest in learning things that I don't understand well enough yet. My goal is to look back each year and feel like I knew nothing compared to what I learned this year.
@KaranGupta29111994
@KaranGupta29111994 2 жыл бұрын
@@mCoding, and I think that articulating it, making these videos also helps solidify that learning. Sometimes, just saying out loud things that we understand or rather we think we do helps us smoothen out any gaps in our concept. Learning is a continuous process after all. 😉Btw, I was this video's 1K (th) like.
@agb2557
@agb2557 3 жыл бұрын
Could you elaborate a bit more on dt being the variance and having to square root it and multiply with the Brownian motion? I got a bit lost there.. 😅 Around 3:20 Thanks
@mCoding
@mCoding 3 жыл бұрын
Brownian motion has the property that B(t_1) - B(t_2) has variance t_1-t_2. In our array, we take B(k * i) where k = delta t and i ranges from 0 to T/k. So each increment in our array has variance k (i+1) - k i = k = delta t. The parameter numpy wants though is the standard deviation, which is the sqrt of the variance.
@agb2557
@agb2557 3 жыл бұрын
@@mCoding got it, thank you. It wasn’t immediately obvious to me that the stdev was required.
@TheSchoolDestructer
@TheSchoolDestructer 2 жыл бұрын
In general when variance of a random variable X is say V, then variance of a*X, where a is some constant is equal to a*a*V. Hence, to set the variance of distribution with variance 1 to some V, we multiply the that distribution with sqrt(V).
@OscarMartinez-cf1hq
@OscarMartinez-cf1hq 3 жыл бұрын
Thanks great video! But why not concatenate first then cumsum? Would this not be more intuative and would start at 0 anyway, or whatever number you concatenated to?
@ibtissamaymen7321
@ibtissamaymen7321 Жыл бұрын
Plz ! Python has more benefits to programmate with than Matalb and R ?
@EW-mb1ih
@EW-mb1ih 3 жыл бұрын
Why do you use a cumulative sum for the brownian values?
@mCoding
@mCoding 3 жыл бұрын
The array contains values that are supposed to be differences of the brownian motion, i.e. B0-0, B1-B0, B2-B1, B3-B2. Doing the cumulative sum gives B0, B1-B0+B0 == B1, B2-B1+B1 == B2, B3-B2+B2 == B3, and so on. You can see that taking the cumulative sum of differences of a sequence creates a telescoping sum which recovers the original sequence.
@mikechen2951
@mikechen2951 3 жыл бұрын
Can you comment on the relationship between the normal distribution you used and the zero mean auto-correlation function in the definition of Langevin and Brownian dynamics?
@mCoding
@mCoding 3 жыл бұрын
Brownian motion is an example of a stationary zero-mean Gaussian process, which is characterized completely by its auto-correlation function R(t,s) = min(t,s). For Langevin or Brownian dynamics, you work with a stationary zero-mean Gaussian process whose auto-correlation is given by R(t,s) = 1 if t=s and 0 otherwise. In particular, in those dynamics you are not working with Brownian motion. However, there is a very general theory of Gaussian processes that covers those cases.
@mikechen2951
@mikechen2951 3 жыл бұрын
@@mCoding Thanks for the reply! I think it is funny that Brownian dynamics is not Brownian motion. Do you have any recommended readings if I want to learn more about the theory of the Gaussian process?
@mCoding
@mCoding 3 жыл бұрын
@@mikechen2951 Gaussian Processes for Machine Learning by Rasmussen and Williams is a popular text, though it requires some math knowledge.
@AbuTuraab72
@AbuTuraab72 3 жыл бұрын
@@mCoding do you think it is possible to integrate that with selenium to move the mouse without being detected as a bot
@renemartinez3014
@renemartinez3014 3 жыл бұрын
Excelente job, keep going!
@ibtissamaymen7321
@ibtissamaymen7321 Жыл бұрын
your video really helps me to simulate the Brownian motion, but plz I don't understand why you convert 1 and 0 to float by adding points (1. ),(0. )
@PeterZaitcev
@PeterZaitcev Жыл бұрын
Why haven't you used Manim for the visualization?
@ibtissamaymen7321
@ibtissamaymen7321 Жыл бұрын
Hi james! Plz in the definition of brownian motion, their increment must be independants, i can't see it in the code 🙏🙏
@mCoding
@mCoding Жыл бұрын
We use a multidimentional standard normal, whose components are independent, to create the increments.
@ibtissamaymen7321
@ibtissamaymen7321 Жыл бұрын
@@mCoding Thanks.
@gabrielamosqueda5391
@gabrielamosqueda5391 3 жыл бұрын
Many thanks!
@mCoding
@mCoding 3 жыл бұрын
You are welcome!
@erreddadjarfi371
@erreddadjarfi371 Жыл бұрын
Good work james , but if they ask me the theory justification of the code what can i say? because we simulate just vector
@mCoding
@mCoding Жыл бұрын
Well firstly anything on a computer is not going to be a true brownian motion, it's always just an approximation, but you can also think of the components of the vector as being samples from the brownian motion. So a true brownian motion has B(t) for all real t >=0, but the vector holds (approximations) of B(0.00), B(0.01), B(0.02), B(0.03), ... , up to B(T).
@rajivshukla7892
@rajivshukla7892 3 жыл бұрын
very interesting, thank you very much for making this video, !!! :)
@mCoding
@mCoding 3 жыл бұрын
Glad you liked it!
@philippb9602
@philippb9602 3 жыл бұрын
Instead of concatenating you could do cumsum of N values and then subtract the first value. Should be fast enough and reduce the number of lines.
@johanestanujaya1255
@johanestanujaya1255 2 жыл бұрын
Hi i am interested in this formula, can you write the syntax of the formula?
@EW-mb1ih
@EW-mb1ih 3 жыл бұрын
Why the variance is sqrt(dt)? I mean is it a reason why the variance is linked to the time interval?
@mCoding
@mCoding 3 жыл бұрын
The variance of brownian motion over an interval of length dt is dt. This is one of the defining properties of Browniam motion. The square root is to convert from variance to standard deviation.
@SB-pj3oj
@SB-pj3oj 3 жыл бұрын
Brownie emotion
@federicadedomenico257
@federicadedomenico257 2 жыл бұрын
Could please explain me also how to implement geometric Brownian motion without going through the log or exp, just with the formula dS= r S dt + sigma S dW? Cheers from Italy!
@mCoding
@mCoding 2 жыл бұрын
Hello from the US! For a single sample path you can just update S_i+1 = S_i * (r dt + sigma sqrt(dt) N_i+1) repeatedly in a loop. Here N_i+1 is a standard normal. Because the next time step depends on the value of the previous time step, this formula cannot be vectorized across multiple time steps. However, it can still be vectorized across sample paths, so if you want a million sample paths you can just make S a million length vector. This is "vertical" vectorization, as opposed to "horizontal" vectorization shown in the video.
@mountaindrew_
@mountaindrew_ 2 жыл бұрын
Why can't we simulate BM by sampling the increments Wt-Ws with N(0,t-s) and instead we must use sqrt(t-s)*N(0,1) ?
@mCoding
@mCoding 2 жыл бұрын
Some libraries take the mean and variance, but numpy takes the mean and standard deviation as parameters, so yes I could pass N(0, sqrt(t-s)) instead of sqrt(t-s)*N(0, 1), but this is a stylistic change at best. Mainly I put it out front so I don't have to talk about whether the argument is std deviation or variance, as the multiplication out front has no ambiguity.
@mountaindrew_
@mountaindrew_ 2 жыл бұрын
@@mCoding Thanks a lot! I was sampling the wrong distribution because of this issue. I was using rnorm from R and I was sure that no library took std dev as input. Thanks again!
@geniusben4503
@geniusben4503 3 жыл бұрын
Just to confirm, this is Brownian motion and not Geometric Brownian motion, right?
@mCoding
@mCoding 3 жыл бұрын
That is correct! A geometric Brownian motion would be K exp(aB + ct) where B is this Brownian motion.
@charan2446
@charan2446 3 жыл бұрын
Just learned if else functions and watching this video.......
@BillyZeKidd
@BillyZeKidd 3 жыл бұрын
What's the purpose of the if __name__ == '__main__' check?
@mCoding
@mCoding 3 жыл бұрын
The check passes if and only if the code is being executed/run as opposed to imported. For this code it is not necessary because the code is so small, but the structure of the program prevents the use of global variables, which can quickly get out of hand if you want to extend your code. That's why I put that in pretty much all my code (except for import only code).
@GuRuGeorge03
@GuRuGeorge03 3 жыл бұрын
People use brownian motion and add a "bias" (up or downward) to it to create stock models. they can be extremely accurate (mathematically speaking)
@mCoding
@mCoding 3 жыл бұрын
I am one of those people :). You are looking for geometric brownian motion. Biased brownian motion is not used for stock modeling because it will lead to negative prices, geometric brownian motion does not have this issue.
@venkateshmurugadas7481
@venkateshmurugadas7481 3 жыл бұрын
wow
@jonathanfan3709
@jonathanfan3709 3 жыл бұрын
I think you need a better microphone man!!
@mCoding
@mCoding 3 жыл бұрын
I'm trying to get this sorted out! Sorry for the inconvenience.
@Codeman4043
@Codeman4043 2 жыл бұрын
Wait you imported numpy for plotting? then why does it look like you imported pyplot instead?😂😂
@aniruddhasharma5738
@aniruddhasharma5738 3 жыл бұрын
I hate coding being used for chemical engineering in the form of “simulations”.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Conway's Game of Life in Python
19:39
NeuralNine
Рет қаралды 50 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
I'm Coding an Entire Physics Engine from Scratch
9:19
Gonkee
Рет қаралды 1,7 МЛН
Brownian Motion - The Physics of Randomness
13:37
Up and Atom
Рет қаралды 196 М.
The experiment that revealed the atomic world: Brownian Motion
12:26
Steve Mould
Рет қаралды 2,7 МЛН
How I make science animations
43:39
Artem Kirsanov
Рет қаралды 781 М.
Building Brownian Motion from a Random Walk
28:35
Maths Partner
Рет қаралды 47 М.
Python Physics Simulation. Galton Board. Pymunk Tutorial
8:13
Coder Space
Рет қаралды 15 М.
Asynchronous Web Apps in Python
15:30
mCoding
Рет қаралды 33 М.
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 65 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН