Monte Carlo Integration In Python For Noobs

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 มิ.ย. 2018
  • Monte Carlo is probably one of the more straightforward methods of numerical Integration. It's not optimal if working with single-variable functions, but nonetheless is easy to use, and readily generalizes to multi-variable functions. In this video I motivate the method, then solve a one-dimensional integral using monte carlo. I then solve the integral a number of times, and histogram the results, showing the distribution of areas calculated.

ความคิดเห็น • 166

  • @victorserras
    @victorserras 6 ปีที่แล้ว +124

    This has been a fantastic introduction to Monte Carlo methods

  • @JagroopSingh-wd6kj
    @JagroopSingh-wd6kj 3 ปีที่แล้ว +40

    I'm not sure if threads exist in Python, but in C there is a concept called threading, where you can run multiple functions at the same time, and in this case you'd be able to split the N operations among M threads, where M threads are computing N/M operations at the same time, which would result in a faster runtime. After a quick google search, it seems that Python does have threads, I'd recommend learning them so that you can optimize your code runtime whenever creating a new program, especially in a field such as physics where you would have situations in which you'd have to compute 10000+ points sometimes.

    • @eblocha
      @eblocha 3 ปีที่แล้ว +13

      Threads “exist” in python, but only one thread can actually execute code at a time (because of the Global Interpreter Lock), so it’s best suited for I/O limited stuff, like networking. Using numpy array operations is the best way to speed up the code, since it calls C code under the hood, which is about 100x faster. Beyond that, there is multiprocessing, which creates multiple python processes at the same time.

    • @superuser8636
      @superuser8636 ปีที่แล้ว

      @@eblocha GIL has been updated but these two comments were spot on and are still relevant today

    • @gregorykafanelis5093
      @gregorykafanelis5093 11 หลายเดือนก่อน +1

      @@superuser8636 what do you mean GIL has been updated?

  • @pilchu7978
    @pilchu7978 4 ปีที่แล้ว +44

    Title: Monte Carlo Integration In Python For Noobs
    :
    :
    :
    First words: "What's goin' on smart people!"
    Great video, sub and respect (:

    • @shrinivasiyengar5799
      @shrinivasiyengar5799 3 ปีที่แล้ว +1

      I came to the comments section to write exactly that 😂😂

  • @lukkymaths8349
    @lukkymaths8349 4 ปีที่แล้ว +1

    @Andrew Dotson , I don't know if you read comments on older videos, but I just wanted to thank you. I missed the lecture on Monte Carlo and understood nothing of the example code we were given. I already subscribed to you for the awesome skits, but this was the perfect video for me. You explain the concept and what each part of the code does, without going too slow. (I am a noob at programming, ngl.) Thank you, and greetings from Finland.

  • @shrinivasiyengar5799
    @shrinivasiyengar5799 3 ปีที่แล้ว +13

    Great tutorial. It made a lot of theoretical concepts clear for me when implementing. I really wish that you make more videos on Monte Carlo Simulations. I read that these were first used to solve atomic simulations. I am not connected to the physics world, and it would be great to learn more about that.
    Once again, great job

  • @grjesus9979
    @grjesus9979 3 ปีที่แล้ว +8

    I've never been introduced to Monte Carlo integration this way. Thanks!

  • @_nem
    @_nem 4 ปีที่แล้ว +1

    This was so much better than my lecturer's explanation. Nice and concise and some coding thrown in too. Good job. Thanks Andrew!

  • @prashantjadhao22
    @prashantjadhao22 4 หลายเดือนก่อน

    Great tutorial. Cleared my concept on MC integration. I was looking for this topic on TH-cam and found your video very helpful .

  • @caseykijk1412
    @caseykijk1412 4 ปีที่แล้ว +2

    I've been meaning to learn this technique. Great introduction!!

  • @satellite964
    @satellite964 2 ปีที่แล้ว +1

    As a person who has trouble with math terminology and symbols, bless you for doing this in code.

  • @78anant420
    @78anant420 3 ปีที่แล้ว

    Excellent video. In 1 video i learnt the basics of how to plot a histogram, generate random numbers, write for loops and use arrays in python. Excellent. Thanks! Please make more of such videos using python

  • @DavidAspden
    @DavidAspden 5 ปีที่แล้ว +32

    Good video, straight into it, with results! Others would have dragged this out.

  • @siddarthshivkumar5780
    @siddarthshivkumar5780 3 ปีที่แล้ว

    This is my first video on Monte Carlo integration. Loved your explanation :) Thank you :)

  • @treebattery2901
    @treebattery2901 3 ปีที่แล้ว

    Thanks, Andrew. Helped me a ton with understanding my lab on Monte Carlo Integration.

  • @pubgplayer1720
    @pubgplayer1720 4 ปีที่แล้ว

    This was actually very very helpful! I mainly watch the comedic side of your channel, but this was just a great video.

  • @koenkempeneers5507
    @koenkempeneers5507 2 หลายเดือนก่อน

    Great, straight to the point wonderful tutorial!

  • @eduardopasseto2387
    @eduardopasseto2387 4 ปีที่แล้ว

    many thanks! you are helping a lot of programmers around the world... (I´m from Brazil!)

  • @Dinghly
    @Dinghly 2 หลายเดือนก่อน

    I first watched this in my 1st year in college thinking oh this is non of my business. I did not know I would need this video so bad. Thank you for your hardwork into making this video. luv it

  • @InspirationofGenius
    @InspirationofGenius 6 ปีที่แล้ว +1

    Hi! Thanks a lot for this video, this Python example helped me get a better idea of how to approach solving problems in Python. In my research, I have to calculate entrainment rates (the rate dry air mixes with clouds to dissipate them) in cloud fields and observe how these rates affect cloud size distributions. Dealing with atmospheric processes, most of my data is in 4 dimensional arrays, so I've been slowly getting acquainted with those. Though your example was general, showing how to solve that integral helped give me a couple of good ideas of how I can approach solving a couple of my unknown variables. Thanks again for the video, I wish you the best of luck in your QCD research! Keep up the awesome videos! -From an elightened Python noob 😊😜

  • @iman_s.5217
    @iman_s.5217 2 ปีที่แล้ว

    Hey Andrew! I just wanna say thank you for this video. I made a monte carlo maximum successive scattering simulator out of it and the program did work as intended.

  • @somasunders1902
    @somasunders1902 2 ปีที่แล้ว

    Nice Explanation bro, Need more stuffs like this.

  • @Harsimran_Singh_27
    @Harsimran_Singh_27 3 ปีที่แล้ว

    What a fantastic and helpful video... Thanks a lot. You really made it very simple and easy to understand.

  • @melinasandoval2688
    @melinasandoval2688 3 ปีที่แล้ว

    Hi Andrew, thank you very much for this explanation. Now I know how to propose the solution for my project.
    I feel like I'm going to have a lot of fun applying this method!

  • @qwertyzxcv6
    @qwertyzxcv6 3 ปีที่แล้ว

    This is great thank you! The way you teach is very clear!

  • @trigocuantico
    @trigocuantico 6 ปีที่แล้ว +1

    Great production in this video 👏👏 thanks for the content

  • @user-pb9cc8sv6r
    @user-pb9cc8sv6r 6 หลายเดือนก่อน

    clicked because I am a noob, suddenly called "Smart People", subscribed.

  • @stagmanson4155
    @stagmanson4155 5 ปีที่แล้ว +4

    Thanks Andrew, I'm just getting into my calculus courses at my college. I'm trying to pick up programming along with it and this video is quite helpful. I haven't taken any statistic courses as my degree plan doesn't require them and so my question is, have you taken any statistic courses and if so which ones? Keep up the great work we all appreciate it.

  • @laurasudupemedinilla759
    @laurasudupemedinilla759 3 ปีที่แล้ว

    Good introductory video for Montecarlo!

  • @birdstheword5
    @birdstheword5 5 ปีที่แล้ว +5

    Wha... thats absolutely genius! I was struggling to understand this but now its just clicked!

    • @viradeus4322
      @viradeus4322 3 ปีที่แล้ว

      Yea after I watched a couple of videos on Monte Carlo Integration, it suddenly clicked! A beautiful concept ngl

  • @randomperson5817
    @randomperson5817 5 ปีที่แล้ว +1

    Superb content sir 😎

  • @Eric-jh5mp
    @Eric-jh5mp ปีที่แล้ว

    I needed this last week and TH-cam didn't recommend it when I googled it. Unlucky

  • @atamamed2712
    @atamamed2712 2 ปีที่แล้ว

    Ahuyet! Thanks Andrew that was very cool!

  • @adrogoff
    @adrogoff 2 ปีที่แล้ว

    Thank you for this tutorial! Very understandable and easy, while looking for smth simmilar in internet is so complexed(

  • @abbasfathiazar7179
    @abbasfathiazar7179 2 ปีที่แล้ว

    Thank you for publishing such a great video.

  • @Dooyahha1212
    @Dooyahha1212 6 ปีที่แล้ว +6

    Eyyy lmao, I'm gonna take a class on Monte Carlo Methods!

  • @aswinjose1185
    @aswinjose1185 4 ปีที่แล้ว

    it was a good and simple intro to Monte Carlo Integration. helped a lot!!

  • @tommyshelby6277
    @tommyshelby6277 3 หลายเดือนก่อน

    THANKS BRO I GOT IT CLEARED IN 5 minutes IN!!!

  • @felipeignaciofigueroa1792
    @felipeignaciofigueroa1792 3 ปีที่แล้ว

    I actually started to get away from python but this video gave me life hahahaha, thanks!

  • @rfhp1710
    @rfhp1710 5 ปีที่แล้ว +1

    Hey andrew, this is really nice !

  • @YounesLab
    @YounesLab 7 หลายเดือนก่อน

    Amazing Video! Thank you

  • @scatmanthefetish7474
    @scatmanthefetish7474 3 ปีที่แล้ว +13

    Thoroughly disturbed by the way you write a.

  • @pipertripp
    @pipertripp 4 ปีที่แล้ว

    nice one. Yeah, np arrays are way faster than lists. Interesting about the convergence for single variable functions. Got any recommendations for books on numerical methods in general (and Python in particular)?

  • @calebwhales
    @calebwhales 6 ปีที่แล้ว +1

    These videos are really interesting to watch. Seeing you suss things out in real-time with a not-so-super-fast entry into python is awesome.
    Out of interest, does the script break anywhere for different A's, B's or func(x)?

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +1

      I suppose it might converge slower the wider the interval you integrate over. Meaning you'd need more points to get a solid representative random sample

  • @thekantor1964
    @thekantor1964 5 ปีที่แล้ว +15

    i love you bro. you saved my butt. i owe you.
    edit: subbed

  • @ProfessionalTycoons
    @ProfessionalTycoons 5 ปีที่แล้ว

    clear to the point very good video.

  • @therollingjonicle
    @therollingjonicle 7 หลายเดือนก่อน

    This is great, thank you very much!

  • @KM25263
    @KM25263 2 ปีที่แล้ว

    Nice and impressive demonstration...the x axis should not be labeled as expected value instead of area?

  • @mengyutu7170
    @mengyutu7170 5 ปีที่แล้ว

    Fantastic video!

  • @berkalkan3382
    @berkalkan3382 3 ปีที่แล้ว

    Great explanation, thank you

  • @schieflp9195
    @schieflp9195 2 ปีที่แล้ว

    Great video!

  • @porit1023
    @porit1023 6 ปีที่แล้ว +2

    love me some Monte Carlo!

  • @George-lt6jy
    @George-lt6jy 2 ปีที่แล้ว

    Two questions, first I notice that the histogram of the integrals looks like the normal distribution. Is this because of the central limit theorem? Also, there are two places where sampling occurs where the first is sampling for estimating the integral and the other is sampling integral calculations for taking the average. My question is about how each of these parameters impacts the result. For example what if I only used 100 samples for calculating a single integral but took the average of 10000 integrals?

  • @uchihamadara6024
    @uchihamadara6024 6 ปีที่แล้ว

    really interesting stuff!

  • @mirageneos
    @mirageneos 6 ปีที่แล้ว +6

    I'm using Monte Carlo to model electron transport through water for my thesis, nice to see some Monte-Carlo!

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว

      Sounds really cool!

    • @mirageneos
      @mirageneos 6 ปีที่แล้ว

      I'm loving it so far! Thanks for sharing your experience through honours and beyond - it's been great!

    • @amiteshsingh5033
      @amiteshsingh5033 4 ปีที่แล้ว

      Hey man I am working on a different project which is the Cryogenic Energy storage simulation by MCMC. Can you help me a little ?

  • @loganschumacher3870
    @loganschumacher3870 3 ปีที่แล้ว +1

    So is there a way to turn this into something to show growth of investment portfolios? Like with the average growth and standard deviation?

  • @betifransisca2248
    @betifransisca2248 5 ปีที่แล้ว +5

    You, sir, are a fucking gangster for this tutorial!!! Exactly what I was after. #Sauce

  • @dipburo
    @dipburo 6 ปีที่แล้ว

    Loved it!

  • @Galileosays
    @Galileosays 4 ปีที่แล้ว +2

    Great presentation. It works well. You said it will become a delta function, but you probably mean a Gaussian distribution, since it yields an average and a standard deviation.

    • @debunkthis
      @debunkthis ปีที่แล้ว

      No it’s a delta function. As you push the number of points to infinity you yield a deviation of 0 and get a delta function.

    • @Galileosays
      @Galileosays ปีที่แล้ว

      ​@@debunkthis Due to the randomness of chosen points, there will be a variation around an average value. The central limit theorem tells that this yields a Gaussian distribution. A delta function implies that all random points fall at the mean value.

    • @ludicrousfun7838
      @ludicrousfun7838 8 หลายเดือนก่อน

      @@Galileosays The delta function can be defined as the limit of the gaussian distribution when the variance goes to 0. As more samples are taken, the variance will indeed tend towards 0.

  • @maurocruz1824
    @maurocruz1824 5 ปีที่แล้ว

    Great video. Do have a tutorial for phyton installation?

  • @najwer23
    @najwer23 3 ปีที่แล้ว

    Thank You very much!

  • @mrinalrajak5526
    @mrinalrajak5526 5 ปีที่แล้ว

    Thank you Sir...from India....

  • @soyamiitb635
    @soyamiitb635 ปีที่แล้ว

    nice explanation❤❤❤❤❤❤❤❤❤❤

  • @naturfagstoff
    @naturfagstoff ปีที่แล้ว

    Nice idea with the histogam. Looks a bit low-slanted, though. Hard to include the last x-value in the x domain, as that interval is semi-open. But wonderful example, thank You!

  • @shashank8098
    @shashank8098 ปีที่แล้ว

    Nice introduction. Can you show a tutorial for monte carlo simulation of a honeycomb Ising lattice?

  • @jongcheulkim7284
    @jongcheulkim7284 2 ปีที่แล้ว

    Thank you very much.

  • @aritraray2501
    @aritraray2501 3 ปีที่แล้ว +1

    , how can I find the area inside a curve?
    I have data points for a hysteresis loop and I want to calculate the area inside the curve

  • @sjgabhane
    @sjgabhane 6 ปีที่แล้ว

    yaaaas, thank you for the video!

  • @felipebanegas
    @felipebanegas 3 ปีที่แล้ว

    Thank you bro for this video nice content

  • @aisak4753
    @aisak4753 4 ปีที่แล้ว

    thank you!!!so helpful

  • @kritikakhanwal614
    @kritikakhanwal614 3 ปีที่แล้ว

    hey andrew, can you make a vedio showcasing what all compuatational work you do in your research in theoretical nuclear physics?

  • @vladalex9556
    @vladalex9556 2 ปีที่แล้ว

    I don't understand however why it is better to use random samples (even for higher dimensions). Wouldn't a sample of uniformily spaced values be basically as good as a uniform distribution as it can get since the order doesn't matter. So for example for a one dimensional integral you can choose numbers from a and then increment by a small delta untill you get to b, and for a bidimensional integral do something similar but in 2 dimensions (of course making shure that you keep the points evenly spaced). For some integrals it might be easier to just take random samples i get that but when it is easier to just increment by a delta my question is if that would yield as good a result as with random numbers?

  • @inakidb
    @inakidb 5 ปีที่แล้ว

    Hi Andrew. if I look a mean for a cost. what formula apply?

  • @picosdrivethru
    @picosdrivethru 4 ปีที่แล้ว +2

    dude, thank you!

  • @wankmaster69
    @wankmaster69 6 ปีที่แล้ว +39

    Hey Andrew , please make a video on how to read a research paper and what research paper can an undergraduate can read.

    • @Paraboemba
      @Paraboemba 5 ปีที่แล้ว +24

      Read the title, next the abstract, then all the section-titles and subsection-titles and then the conclusion. Then go back and read the contents of the introduction and all following sections.

    • @jimmyding335
      @jimmyding335 4 ปีที่แล้ว +3

      @@Paraboemba that's actually how I approach most of the papers yeah. You concluded it pretty well hahahaha

  • @Ellasdssssss
    @Ellasdssssss 6 ปีที่แล้ว +1

    Do you think you could show to take the gradient of a scalar field in python? Recently discovered your channel and love your videos!

  • @MarcoACto
    @MarcoACto 6 ปีที่แล้ว +1

    Do you know if there's a scenario where using Monte Carlo's method would be preferable over other numerical integration methods (such as trapezoidal rule or simpson's rule)? I ask this because, like you said, seems like a lot of computational effort to generate random numbers and then feed them into a function.

    • @AndrewDotsonvideos
      @AndrewDotsonvideos  6 ปีที่แล้ว +3

      Marco Antônio if you start integrating function that depends on more than just x, it starts to be way more worth it to use monte carlo

    • @MarcoACto
      @MarcoACto 6 ปีที่แล้ว

      Oh I see. Thanks for the answer!

    • @UmurCanKaya
      @UmurCanKaya 5 ปีที่แล้ว +1

      In my graduation project I have to calculate the expectation value of some operators over many-body wavefunctions. In a 2D system and for 30 particles (my case) this means a 60 dimensional integral. I simply don't have any other choice beside Monte Carlo integration.

  • @nitishitankar4992
    @nitishitankar4992 7 หลายเดือนก่อน

    Thanks Andrew

  • @marcotanzilli6510
    @marcotanzilli6510 5 ปีที่แล้ว

    Thank you so much!

  • @ccuuttww
    @ccuuttww 3 ปีที่แล้ว

    Wolverine is teaching map its cool!

  • @tyroelohim4462
    @tyroelohim4462 2 ปีที่แล้ว

    Great explaination but I have one doubt like if they give two circles and told to find the intersection area whose radius is r1 = 2, r2 = 2. And random sampling is n = 100000. Then how to print estimated area? I am getting error while implementing this? Please someone help. Thank you.

  • @dustyMehul
    @dustyMehul 3 ปีที่แล้ว

    Great work bro...
    2 questions here... If anyone can help...
    1) when you wrote the very first expression of , I think it should mean that avg of f(x) for x in [a, b]... And not for any value of X.
    2) while we carry out the experiment of randomly generating value of x and then, finding f(x), we should not be bias and should pick any value of x in the domain [a, b]. But, I think, Random no generator will only generate numbers randomly between 0 and 1, and not 0 to pi.
    Not sure though how the result is perfect.

    • @bh-rf9dd
      @bh-rf9dd 3 ปีที่แล้ว

      @dusty mehul
      1. The definition of is the average of x in the region between a and b. You have x which is a random variable that can take on any value between a and b.
      2. When he created the random number generator he chose for the generator to choose numbers between 0 and pi. Random number generators allow you to choose what range of numbers you want to sample from.

  • @1q1q1q1q1q1q1qw
    @1q1q1q1q1q1q1qw 2 ปีที่แล้ว

    super nice video

  • @TheDavid43121
    @TheDavid43121 2 ปีที่แล้ว

    Only one thing is wrong. The distribution will never be a Dirac's delta. Because of the central limit theorem, the width of the normal distribution will grow as sqrt(N). (You can check the x tics with N=1000 and N=10000 and see the width is greater in the later).What will decrease as sqrt(N) is the standard deviation of the mean. Great video tho👍

  • @1q1q1q1q1q1q1qw
    @1q1q1q1q1q1q1qw 2 ปีที่แล้ว

    one question why do i have to use random numbers. shouldnt it work if i use equdistant values in my integralinterval as well ?

  • @samairastewart91
    @samairastewart91 5 ปีที่แล้ว +2

    Lol where was this last semester when I was working on this for my computational physics course?

  • @karannchew2534
    @karannchew2534 3 ปีที่แล้ว

    Revision notes.
    It's sometimes difficult to find the answer for the integration of a function. Because it's hard or impossible to even derive the formula of the integration, especially it's not closed form and the function is multi-dimensional (multiple variables).
    But if we are only after an answer, then we can do it in "numerical" way. I.e. By combinating different numbers to find the best approximate result.
    Here, it's shown the ∑ of many little results (divided by something) is approximately the ∫ of the functions.
    The Python code proves it.
    Monte Carlo is based on the principle.

  • @marcinkovalevskij5820
    @marcinkovalevskij5820 4 ปีที่แล้ว

    Thx, that helped me!

  • @vigneshwarankannan4999
    @vigneshwarankannan4999 5 ปีที่แล้ว +9

    can you please explain monte carlo method for multidimensional integration

    • @DmitryPesegov
      @DmitryPesegov 4 ปีที่แล้ว +2

      same way if integration happens for multidimensional function, say, f(x,y,z). Just vectorize the space and use the Euclidean norm for term (b-a) that would be then L2normDist(b,a) where b and a are multidimensional points. Or go deeper and use packed M-integrals for M dimensions. And if you've get the understanding about this - then, probably, it's time to meet the functional integral over a random pathes (with gaussian subintegral in each patch) in a space filled with positive and negative probabilities potentials (the simplified version of "Feynman's path integral" - without complex part about quantum physics, just the space and +/- potentials regions)

  • @arghyakusumdey7248
    @arghyakusumdey7248 4 ปีที่แล้ว

    How I put dots in required area.want to know the command in python for plotting

  • @khangpham268
    @khangpham268 4 ปีที่แล้ว

    How do you do it for 2 dimension or higher integral? Like let's say Sqrt(x + y)

  • @Riteshbachhar
    @Riteshbachhar 5 ปีที่แล้ว

    Hey Andrew can you tell me about your python journey?

  • @ruben9228
    @ruben9228 3 ปีที่แล้ว

    Great video! How would you solve for an integral with infinite limits though?

    • @bh-rf9dd
      @bh-rf9dd 3 ปีที่แล้ว

      It would depend on the nature of the function. For a function like the gaussian function which has bounds from -infinity to infinity, you could probably do the integration within 2 to 3 standard deviations of the mean to account for about 95-99% of the area. Moreover, if there exists any symmetry in the bounds you choose for the function, you could compute the integral on only one side of the function and then double the result. This should substantially reduce computational cost.

  • @user-zd9cz1lq6b
    @user-zd9cz1lq6b 2 ปีที่แล้ว

    Wouldn't the end distribution of areas end up approximating a normal distribution instead of the delta function as a result of the central limit theorem?

  • @miguelestradarodriguez5378
    @miguelestradarodriguez5378 2 ปีที่แล้ว

    How do you apply the trapezoidal rule to a dataframe instead of a function?

  • @Source1987
    @Source1987 3 ปีที่แล้ว

    Very nice / 10

  • @athreyakb1
    @athreyakb1 2 ปีที่แล้ว

    Could you help me with a monte carlo assignment I have for a more complex equation by any chance :(

  • @jessicachaves3666
    @jessicachaves3666 3 ปีที่แล้ว

    THANK YOU!!

  • @michaelvarney.
    @michaelvarney. 3 ปีที่แล้ว

    A true physicist there. Uses hadkerish style programming with loops rather than vectorizing things! 😜

    • @michaelvarney.
      @michaelvarney. 3 ปีที่แล้ว

      I speak as a physicist myself...

  • @darkpikachu_.
    @darkpikachu_. 4 ปีที่แล้ว

    What will the Julia equivalent of this code be?

  • @importnumpyasnp6539
    @importnumpyasnp6539 3 ปีที่แล้ว +1

    ''Very nice out of 10'' got me laughing