|
|
(6 intermediate revisions not shown) |
Line 19: |
Line 19: |
| 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. | | 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 <code>TF1</code>. Powinny one przyjmować funkcję g na przedziale [min,max]:
| + | Please prepare thee '''very similar''' functions taking as argument an object of type <code>TF1</code>. The arguments should be the function g and the range [min,max]: |
- | * <code>double losujVonNeumann(TF1 *g, double min, double max)</code> - funkcja zwraca ''jedną'' liczbę pseudolosową z funkcji g(y), | + | * <code>double generateVonNeumann(TF1 *g, double min, double max)</code> - function returning a generated pseudo random number from distribution g(y), |
- | * <code>double wydajnoscVonNeumann(TF1 *g, double min, double max, int n)</code> - funkcja zwraca wydajność metody akceptacji i odrzucania von Neumanna dla danej funkcji g(y) oraz zadanej liczby losowań n, | + | * <code>double efficiencyVonNeumann(TF1 *g, double min, double max, int n)</code> - function returning the efficiency of the von Neumann method for a given g(y) function and the number of generations n, |
- | * <code>double calkaVonNeumann(TF1 *g, double min, double max, int n)</code> - funkcja zwraca całkę oznaczoną (pole powierzchni pod krzywą) z funkcji g(y) na przedzialne [min,max] przy liczbie losowań n. | + | * <code>double integra;VonNeumann(TF1 *g, double min, double max, int n)</code> - function returning the definite integral (area under the curve) from function g(y) in the range [min,max] with the number of generations n. |
| | | |
- | ''Część trzecia:'' '''metoda akceptacji i odrzucania von Neumanna z funkcją pomocniczą''' (1 pkt.) | + | ''Part three:'' '''acceptance rejection method of von Neumann with the helper function''' (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):
| + | In order to improve the efficiency of the von Neumann function, we can limit the area of random points generation with the use of a helper function. In that case we can generate one number from the pair from the s(y) helper function, instead of a uniform distribution: |
- | * <code>double calkaVonNeumannZPomoc(TF1 *g, TF1 *s, double min, double max, int n, double &wydajnosc)</code> - zwraca całkę na przedziale [min,max] oraz dla zadanej liczby losowań n. Ponadto, funkcja powinna zwrócić '''przez referencję''' (ponieważ w języku C/C++ funkcja nie może zwrócić dwóch wartości) wydajność tej metody. | + | * <code>double integralVonNeumannWithHelper(TF1 *g, TF1 *s, double min, double max, int n, double &wydajnosc)</code> - returns the definite integral of the function g(y) in the range [min,max] for a given number of generations n. In addition, the function should take as an argument through a reference (because in C/C++ any function can return only one quantity). |
| | | |
- | 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ą).
| + | The function s(y) we choose '''always''' in a way it is above the function g(y) and so that you can easily generate a random number from the s(y) distribution (i.e. by applying the transformation of the inverse cumulative distribution, like last time). |
| | | |
- | Wszystkie obliczenia należy wykonać dla funkcji typu:
| + | All calculations please fo for the following function: |
| [[File:Lab07_funkcja2.png]] | | [[File:Lab07_funkcja2.png]] |
| | | |
- | == Uwagi == | + | == Attention == |
- | * Czytamy dokładnie '''Wykład 5''' [http://www.if.pw.edu.pl/~lgraczyk/KADD2019/Wyklad5-2019.pdf link] slajdy 9-19 | + | * Read carefully '''Lecture 5''' [http://www.if.pw.edu.pl/~lgraczyk/KADD2022/Wyklad5-2022.pdf link] slides 9-17 |
- | * Do generowania liczb z rozkładu jednorodnego używamy funkcji <code>Uniform</code> z klasy <code>TRandom</code> lub '''generatora napisanego w poprzednim zadaniu''' | + | * To generate numbers from the uniform distribution, use function <code>Uniform</code> from class <code>TRandom</code> OR the '''generator written last time''' |
- | * Pamiętajmy, że zawsze akceptujemy wylosowane punkty pod zadaną krzywą w stosunku do wszystkich wylosowanych (stosunek ten ma się do siebie (dla N dążących do nieskończoności) jak stosunki powierzchni obszarów wewnątrz których zostało dokonane losowanie) | + | * Remember, always accept points below the curve to all generated points. |
- | * '''Jako funkcję pomocniczą przyjmujemy funkcję liniową.''' | + | * '''As a helper function we will use a linear function.''' |
- | * W przypadku całkowania z wykorzystaniem funkcji pomocniczej zawsze dobieramy ją w taki sposób, aby można było wygenerować liczbę z jej rozkładu w łatwy sposób (za pomocą odwracania dystrybuanty) - innymi słowy, zamieniamy prostokąt (dwa rozkłady jednorodne) na inny obszar, wewnatrz którego dokonujemy losowań (musimy umieć wylosować liczby z tego obszaru) | + | * In the case of integration using a helper function, we always choose it in such a way that it is possible to generate the number from its distribution in an easy way (by inverting the cumulative distribution function) - in other words, we convert the rectangle (two uniform distributions) to another area inside which we draw (we must be able to draw numbers from this area) |
- | | + | * Quadratic function and its inverse function: |
- | * Funkcja kwadratowa i jej odwrotność: | + | |
| | | |
| [[File:Odwracanie_kwadratowej.png]] | | [[File:Odwracanie_kwadratowej.png]] |
Line 47: |
Line 46: |
| * [http://www.if.pw.edu.pl/~lgraczyk/KADD2016/Wyklad5-2016.pdf Wykład 5] | | * [http://www.if.pw.edu.pl/~lgraczyk/KADD2016/Wyklad5-2016.pdf Wykład 5] |
| | | |
- | == Wynik == | + | == Results == |
- | ''Część pierwsza'' | + | ''Part one'' |
| | | |
| [[File:lab07_kolo2.png]] | | [[File:lab07_kolo2.png]] |
| | | |
| Output: | | Output: |
- | Liczba Pi wynosi: 3.14392 | + | The Pi number equals: 3.14392 |
| | | |
- | ''Części druga i trzecia'' | + | ''Parts two and three'' |
| | | |
| [[File:Lab07_dist.png]] | | [[File:Lab07_dist.png]] |
| | | |
| Output: | | Output: |
- | Calka: 0.199 | + | Integral: 0.199 |
- | Wydajnosc: 0.2072 | + | Efficiency: 0.2072 |
- | Calka (metoda z funkcja pomocnicza): 0.198745 | + | Integral (method with the helper function): 0.198745 |
- | Wydajnosc (metoda z funkcja pomocnicza): 0.39749 | + | Efficiency (method with the helper function): 0.39749 |
- | Calka liczona metoda Integral: 0.198652 | + | Integral calculated using the ROOT method Integral: 0.198652 |
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.
In order to improve the efficiency of the von Neumann function, we can limit the area of random points generation with the use of a helper function. In that case we can generate one number from the pair from the s(y) helper function, instead of a uniform distribution: