KADD 2022 Laboratorium 7 ENFrom Łukasz Graczykowski
ExercisePart one: estimation of the Pi (1 pkt.) Please write a function which estimates the number Pi with the von Neumann (it is arguably one of the most simple examples of the use-case of a Monte Carlo method). In order to calculate the Pi, we randomly generate two numbers x and y from the uniform distribution [0,1] and we check whether the pair falls within the circle of the radius equal to 1. Next, by calculating the ratio of accepted pairs to all of them, you approximate the ratio of areas of the circle to the rectangle. By performing such a random generation, calculate the Pi number. In addition, draw the graph of the resulted points inside and outside the circle:
Part two: generation of pseudo random numbers from a any probability distribution using the acceptance rejection method of von Neumann (3 pkt.) The transformation method used a week ago has limited applications. You can do this when the analytical form of the cumulative function and its inverse function are known. The von Neumann method of generation of pseudo random numbers will work when you know only the g(y) distribution, but the drawback is that you have to generate two numbers, now one. This method will also work if the g(y) function is not a proper probability distribution (it even does not need to be normalized - every function you can always normalize). This allows for a very broad application of the von Neumann method - also to calculate definite integrals, when their analytical form does not exist. This is a typical example of a Monte Carlo method. Należy stworzyć trzy bardzo podobne funkcje przyjmujące obiekt typu
Część trzecia: metoda akceptacji i odrzucania von Neumanna z funkcją pomocniczą (1 pkt.) W celu zwiększenia wydajności metody von Neumanna, można posłużyć się ograniczeniem przedziału losowania w postacji zadanej funkcji pomocniczej. W tej części zaimplementować funkcję, która oblicza całkę z funkcji g(y) metodą akceptacji i odrzucania von Neumanna z funkcją pomocniczą s(y):
Funkcję s(y) dobieramy tak, by zawsze znajdowała się powyżej funkcji g(y) i można z niej było łatwo wygenerować liczbę pseudolosową (na przykład metodą transformacji rozkładu jednorodnego z wykorzystaniem odwrotności dystrybuanty; na wykładzie przykład z funkcją liniową). Wszystkie obliczenia należy wykonać dla funkcji typu:
Uwagi
WynikCzęść pierwsza Output: Liczba Pi wynosi: 3.14392 Części druga i trzecia Output: Calka: 0.199 Wydajnosc: 0.2072 Calka (metoda z funkcja pomocnicza): 0.198745 Wydajnosc (metoda z funkcja pomocnicza): 0.39749 Calka liczona metoda Integral: 0.198652 |