Rejection Sampling - VISUALLY EXPLAINED with EXAMPLES!

Sdílet
Vložit
  • čas přidán 28. 01. 2021
  • This tutorial explains the Rejection Sampling algorithm using a difficult to sample univariate distribution function.
    #sampling
    #montecarlo
    #statistics
    If you prefer to read, I have written an article on this algorithm here -
    towardsdatascience.com/what-i...
  • Věda a technologie

Komentáře • 106

  • @rfhp1710
    @rfhp1710 Před 2 lety +35

    Rotating the plots is a stroke of genius, things clicked instinctively. You are a gifted content maker. Keep making videos.

    • @KapilSachdeva
      @KapilSachdeva  Před 2 lety +6

      Thanks 🙏… finally someone saw the rotated plot and understood its significance … I am happy now :)

    • @partha95123
      @partha95123 Před rokem +2

      Indeed Kapil ji is an artist! I'm writing my thesis with his videos as inspiration.

    • @KapilSachdeva
      @KapilSachdeva  Před rokem +1

      You guys are so kind. Many thanks for the encouragement. @adithyanssathish5485 would love to read your thesis when it is ready.

    • @partha95123
      @partha95123 Před rokem +2

      Yes Kapil ji. I will send an invite to public part of my thesis defence for a duration of 30 minutes.

  • @chriskindler10
    @chriskindler10 Před 11 měsíci +6

    that's actually a terrific explanation

  • @drewburns7805
    @drewburns7805 Před rokem +2

    Fantastic explanation! Best video on this subject I've seen so far. Saved me lots of time searching for clarification. Thank you!

  • @yli6050
    @yli6050 Před 2 měsíci

    Watching your videos keeps reminding me of the phrase “a picture is worth a thousand words”, to which I want to add “ a great picture is worth thousands in gold”. Many times I had to freeze the video to let a particular moment sink in, because I couldn’t believe the insight that picture brings out . ❤❤❤

  • @julialikesscience
    @julialikesscience Před 9 měsíci +2

    this brilliant change of viewpoint helps to explain the method in a much simpler way!

  • @prajwalchauhan6440
    @prajwalchauhan6440 Před 7 měsíci +2

    Best explanation for this topic on CZcams. I wonder how you even came up with this. Genius

  • @sudhagarraghavan9696
    @sudhagarraghavan9696 Před 3 lety +6

    Excellent video and clearly explained ! Hope you can also do tutorials on other MCMC sampling methods such Gibbs Sampling, Metropolis-Hasting and Hamiltonian !

  • @alonsomadronal2919
    @alonsomadronal2919 Před rokem +2

    Such a Masterpiece of a video, thanks for the explanation!

  • @dhimanbhowmick9558
    @dhimanbhowmick9558 Před měsícem

    Thank you for your hard work, which enables us to understand crystal clear. Thanks a lot ❤❤❤

  • @Kn1ghtCh4ser
    @Kn1ghtCh4ser Před 3 měsíci +1

    You just saved a college student living in South Korea! Thanks for amazing visualization and explanations!

  • @MarcusRosenberger
    @MarcusRosenberger Před dnem

    Thank you so much! Your video really helped me understand the method and remember it, which is equally important. 🙂

  • @cjhhong
    @cjhhong Před 2 lety +1

    Thank you so much. I now understand the concept better.

  • @olivier306
    @olivier306 Před rokem +2

    Legendary video man thank you!!!

  • @corradoforza
    @corradoforza Před 2 lety +1

    Thank you so much for this video, it is very clear and makes rejection sampling easy to understand

  • @hiiiqwq5833
    @hiiiqwq5833 Před 5 měsíci +1

    This is sooo helpful! Totally addressed my concern!! Thank you!

  • @haihoangthanh8949
    @haihoangthanh8949 Před 4 měsíci +1

    thank you for your detail and understandable explanation. The video animations are great and impressive.

  • @user-hv9ze2qd8f
    @user-hv9ze2qd8f Před 10 měsíci +1

    Excellent explanation ever on rejection sampling

  • @korntantiwanit
    @korntantiwanit Před rokem +1

    Thank you for your explanation, nice presentation!

  • @cello9834
    @cello9834 Před rokem +1

    thank you so much , very well explained !

  • @ilke3395
    @ilke3395 Před 3 měsíci +1

    Thank you so much sir for the amazing explanation and visualization.

  • @jahanvi9429
    @jahanvi9429 Před 2 lety +1

    thank you, very well explained!

  • @ankushkothiyal5372
    @ankushkothiyal5372 Před 2 lety +1

    Thank you, I understood clearly.

  • @lawrencege6942
    @lawrencege6942 Před 2 lety +1

    Thank you so much!

  • @sujatharamakrishnan
    @sujatharamakrishnan Před 8 měsíci

    Thank you for the very clear explanation of the rejection samling method?.I want to compute the acceptance rate for a specifice probem. Could you define what is the acceptance rate?

  • @Nier914
    @Nier914 Před 2 lety +1

    Thank you, I understand now😊

  • @minma02262
    @minma02262 Před 2 lety +1

    Hi Sir. Amazing tutorial!

  • @janhavisoni1559
    @janhavisoni1559 Před 8 měsíci +1

    Thank you so much

  • @Anteater23
    @Anteater23 Před 2 lety +1

    Excellent video

  • @fatihboyar9697
    @fatihboyar9697 Před rokem +1

    great tutorial thanks a lot

  • @leilanifrost771
    @leilanifrost771 Před 10 měsíci +1

    That was a great video! Just wanted to ask how you would approach scaling the proposal distribution if you don't know the parameters of the target distribution?

    • @KapilSachdeva
      @KapilSachdeva  Před 10 měsíci +1

      In that case you would not use Rejection sampling. You would want to use an algo like metropolis Hastings, HMC etc

  • @lone_Traveller_101
    @lone_Traveller_101 Před 8 měsíci +1

    Lovely sir!

  • @MuhammadAbdullah-iv2gu
    @MuhammadAbdullah-iv2gu Před rokem +1

    Best explanation

  • @baongocsen5768
    @baongocsen5768 Před 29 dny

    Thank you very much for your video. But I have a question: when g(x) is a function U(a,b), the probability of choosing Y which obeys g(x) is the same for all Y in (a,b). But when the bounding function is Gaussian or something else (i.e. when simulating g(x) we cannot guarantee the probability of selecting each point is the same), does it affects the probability of selecting X which obeys f( x) ?
    ( Because if the simulation of Y is not equivalent then even if the next steps are effective, we cannot guarantee that we give equal chances to all points X when starting before "reject or accept")

  • @ZoeZhou-yk9sd
    @ZoeZhou-yk9sd Před rokem +1

    Thank you for the video! Whats is the software you used to create this video plz?

  • @ahnafsamin3777
    @ahnafsamin3777 Před 2 lety +1

    Excellent

  • @BilalTaskin-om6il
    @BilalTaskin-om6il Před 10 měsíci +1

    magnificent...

  • @abhijitharakali
    @abhijitharakali Před 10 dny

    Kudos for explaining very well. I wish to comment on just one thing. @12:08 I could not see the two colors (red and blue) clearly in the video. Maybe the picture can be made to have white background to make the colors more obvious.

  • @lerneninverschiedenenforme7513

    Do I understand correctly, that you need two random values?
    1st: Random value used as the actual "random variable"
    2nd: Random value to evaluate, wheather the random value from (1) is used or not.

  • @MeesumQazalbash
    @MeesumQazalbash Před 7 měsíci +1

    Wonderful tutorial no doubt Indian teachers are best. I am new to sampling and my work often involves multivariate pdfs to sample from. Is there any beginner's tutorial on it?

    • @KapilSachdeva
      @KapilSachdeva  Před 6 měsíci +1

      May be see the metropolis Hastings tutorial on my channel. I would suggest to learn pymc3 library as these sampling algos are part of it with a nice framework and api.

  • @pcaltd.1824
    @pcaltd.1824 Před 2 lety +3

    Lovely video. If you do not know what the target function is, how can you obtain C and how can you ensure the envelope function is not exceeded by the value of the target?

    • @KapilSachdeva
      @KapilSachdeva  Před 2 lety +2

      Sorry for the late reply. My understanding is that for Rejection sampling you need to know the function as this is how you will evaluate f(x) (f is the target distribution).
      Finding the good envelop - This is the challenge with this algorithm and it becomes worse as you go in higher dimensions. One remedy that is generally suggested is to use - "adaptive rejection sampling" .
      See section 11.1.3 of PRML (www.microsoft.com/en-us/research/uploads/prod/2006/01/Bishop-Pattern-Recognition-and-Machine-Learning-2006.pdf), it is quite well explained there.

    • @pcaltd.1824
      @pcaltd.1824 Před 2 lety +1

      @@KapilSachdeva Thanks for clarifying. The reference looks very useful.

    • @KapilSachdeva
      @KapilSachdeva  Před 2 lety

      🙏

  • @ananyapamde4514
    @ananyapamde4514 Před 2 lety +2

    May god bless you

  • @gwirvanmenez3470
    @gwirvanmenez3470 Před rokem

    Hello after computation I find that the integral of f(x) is ~5.73 on [-3;3]. Wich mean that f(x) is not a density fonction. Is the exemple still corect if the integral of f(x) is not equal to 1 on [-3;3] ?

    • @KapilSachdeva
      @KapilSachdeva  Před rokem +1

      Yes. Think of the function as unnormalized density function.

  • @marcogelsomini7655
    @marcogelsomini7655 Před rokem +2

    nice vid, what is the point of sampling if you know the analytical form of the function?

    • @KapilSachdeva
      @KapilSachdeva  Před rokem +2

      Good question.
      Three points to note -
      * Knowing the analytical form of the function does not mean that one can sample from it.
      * In a more sophisticated setup you do not even know the analytical form a function. A typical example is the posterior in Bayesian Statistics.
      *Sampling is necessary to use Law of large numbers to approximate a function (where its analytical form is available or not!)

    • @marcogelsomini7655
      @marcogelsomini7655 Před rokem

      @@KapilSachdeva ok thanks. But this algorithm required to know the real function as you need to compute C the intersection of the random sample and confront with uniform random sample in order to accept o reject right?

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      @@marcogelsomini7655 This is the correct understanding. This is also why it is a limited and inefficient algorithm.
      Look at the metropolis-hasting algorithm that does not have this requirement. I have a tutorial on that as well.

    • @marcogelsomini7655
      @marcogelsomini7655 Před rokem

      @@KapilSachdeva thx good work!

  • @chronicillnessrevolution5079

    Heads up: I plotted the target function according to its expression shown in the video, but the graph that results from this expression is different from the one you show.

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      Have uploaded my (very old notebook) on colab. See if it helps
      colab.research.google.com/drive/1olDMLkwPk16YBmfPdq1zXmvtIVG8kF8R?usp=sharing

    • @chronicillnessrevolution5079
      @chronicillnessrevolution5079 Před rokem +3

      @@KapilSachdeva Found your typo. In the video it says sin^2(6+x) but in your code it is sin^2(6*x). Using the latter gives the correct plot

    • @KapilSachdeva
      @KapilSachdeva  Před rokem +3

      Thank you for catching it 🙏

  • @pzakala
    @pzakala Před rokem

    Sorry for stupid question. I`m now in static algorithms. Why we can`t sample for target function in this case. For example, we can just divide over function for 4.5. The resulting function will looks like distribution function 😅.

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      Most likely I am not following your suggestion - "divide over function for 4.5". If possible elaborate on this.

  • @arpansrivastava6405
    @arpansrivastava6405 Před 3 měsíci

    1:31 how do we not know how to sample as the distribution function is already given and you also plotted it ?

    • @KapilSachdeva
      @KapilSachdeva  Před 2 měsíci

      Somehow it is a common confusion for many.
      Knowing a distribution function can only help you find the probability of a sample. Sampling from a function is a different task. Sampling means asking your computer to generate a sample.
      Sampling makes use of random number generator. Now your random number generator (algorithm) is to behave in such a way that the samples (random numbers) are generated in accordance with their prob distribution. Some samples are supposed to be more (the one with high prob) and some less.
      This is why various sampling algos/techniques are created. Your computer by default can give uniform random numbers. Most of the algorithms directly or indirectly manipulate the result of uniform random number generator.

  • @prateekcaire4193
    @prateekcaire4193 Před 7 měsíci

    Assuming the proposal function is the same as the target function, then the rejection region would be 0. However, this would result in accepting everything, which could lead to incorrect acceptance if there is nothing above the pink blinker point.

    • @prateekcaire4193
      @prateekcaire4193 Před 7 měsíci

      It was helpful to build some intuition around though. Thanks!

    • @prateekcaire4193
      @prateekcaire4193 Před 7 měsíci +1

      ok i got it now. proposal function sampling will also follow probabilistic sampling and sampling from proposal (guassian or uniform) is quite easy

  • @JoseVinicius-fe3vk
    @JoseVinicius-fe3vk Před rokem

    why Y axis goes beyond 1? there is no way something has over 100% chance of happening

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      This is an example function that does not represent pdf but you still want to obtain samples in accordance with this function. Or, think of it as unnormalized PDF.

  • @ddiq47
    @ddiq47 Před rokem

    How do you get the values of f to determine rejection or acceptance?
    It’s confusing to me as the reason for using this method was based on being unable to sample from the original distribution f. What am I missing here

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      In many situations you have an analytical form of the distribution function available but this does not mean you can sample from that function. This is where this technique is used.

    • @ddiq47
      @ddiq47 Před rokem

      @@KapilSachdeva when you say an analytical form of the distribution, what does that mean? Would it be a function like in the video, or a dataset of known function values?

    • @KapilSachdeva
      @KapilSachdeva  Před rokem

      A function. Using this function for a give sample you can compute it’s prob/score/value.