--- tags: Master, Simulation --- # Simulation homework 2 ## Exercise 1 After loading and plotting the dataset, we could see that a trend was clearly present in the data. Consequently, we did some experiments in order to find the polynomials that fit the best to the data. In the following plots it is evident that a degree as low as 5 fits well to the data already, while higher degrees yield no noticeable difference. | Polynomial fit of degree 3:![3](https://i.imgur.com/lQECuHw.png) | Polynomial fit of degree 4:![4](https://i.imgur.com/WyYqYFN.png) | |:-:|:-:| | **Polynomial fit of degree 5:**![5](https://i.imgur.com/n11JqeB.png) | **Polynomial fit of degree 6:**![6](https://i.imgur.com/3SXcPxz.png) | The resulting detrended data by subtraction of the best polynomial fit of degree 5 looks normally distributed. | Dataset with trend removed ![detrend with poly 5](https://i.imgur.com/e8q6BuI.png) | Probability distribution with gaussian fit ![gaussian fit](https://i.imgur.com/059byqL.png) | |:-:|:-:| Detrended data has mean $0.00$ and variance $3.99$. **QQ-plot against gaussian:** ![qq](https://i.imgur.com/rEhrBOr.png) Bootstrap prediction interval at level $0.95 = [-3.925 , \ 3.964]$ As shown previously, degrees lower than 5 do not completely remove the trend. For example, using degree 3 the data still shows a pretty strong trend. Meanwhile, using degrees higher than 5 gives no noticeable improvements. | **Detrend with degree 3** ![detrend degree 3](https://i.imgur.com/0TS8LR8.png) | **QQ-plot with degree 3** ![qq detrend 3](https://i.imgur.com/gFa5iDJ.png) | |:-:|:-:| | **Detrend with degree 5** ![detrend degree 5](https://i.imgur.com/e8q6BuI.png) | **QQ-plot with degree 5** ![qq detrend 5](https://i.imgur.com/rEhrBOr.png) | | **Detrend with degree 8** ![detrend degree 8](https://i.imgur.com/1HkYUDp.png) | **QQ-plot with degree 8** ![qq detrend 8](https://i.imgur.com/1US0XIx.png) | ## Exercise 2 Our implementation of the EM algorithm uses $\frac{1}{4}$, $\frac{2}{4}$ and $\frac{3}{4}$ quantiles as initial means and standard deviations equal to the std of the dataset. Using an uniform distribution for the prior curve probabilities ($\frac{1}{3}$, $\frac{1}{3}$, $\frac{1}{3}$), the results with the two methods are similar, given that the resulting updated probabilities are $0.32$, $0.33$, $0.36$. | With prior update | Without prior update | | :-: | :-: | | ![prior true](https://i.imgur.com/CtNF9zR.png) | ![prior false](https://i.imgur.com/395oocP.png)| The resulting values **with** prior update: | Curve | Mean | Std | Probability | | :---: | :---: | :---: | :---: | | Blue | 0.997 | 0.998 | 0.319 | | Orange | 5.903 | 2.782 | 0.325 | | Green | 10.01 | 1.38 | 0.357 | The resulting values **without** prior update: | Curve | Mean | Std | Probability | | :---: | :---: | :---: | :---: | | Blue | 1.013 | 1.013 | 0.333 | | Orange | 6.202 | 2.815 | 0.333 | | Green | 10.04 | 1.337 | 0.333 | If instead the starting probabilities are off, for instance 0.8, 0.1, 0.1, the prior update is essential in order to guarantee good results. | With prior update | Without prior update | |:-:|:-:| | ![prior true 0.8,0.1,0.1](https://i.imgur.com/EWQRKNw.png) | ![prior false 0.8,0.1,0.1](https://i.imgur.com/0uBzWho.png) | Although it requires 3 times the number of iterations compared to the $\frac{1}{3}$, $\frac{1}{3}$, $\frac{1}{3}$ start, the algorithm with prior update converges to quite similar results: | Curve | Mean | Std | Probability | | :---: | :---: | :---: | :---: | | Blue | 0.992 | 0.985 | 0.314 | | Orange | 6.099 | 2.924 | 0.353 | | Green | 10.04 | 1.325 | 0.332 | ## Exercise 3 We can see in blue the probability mass function. Comparing our sample to teorethical binomial (in black) we see that they follow the distribution almost perfectly. At this point we drew the poisson distribution with $\lambda = N \cdot p$ (red line) to see that such coefficient already approximates the binomial PMF. Different values of $\lambda = N \cdot 2p$ (purple) and $\lambda = N \cdot 4p$ (lime) move the distribution and its peak towards higher numbers, since higher probability or higher number of draws means more successes. ![original and approximated PMFs](https://i.imgur.com/dq6Ir1x.png) ## Exercise 4 Given the dataset, the probability mass function plot shows some divergence from the triangular distribution, but this could be the result of a too small number of trials. The black dots represent the sample distribution while the red dots are for the triangular one. ![sample pmf vs triangular pmf](https://i.imgur.com/N6nu4oS.png) Computing the chi-squared test gives $T = 9.679$ and $\text{p-value} = 0.4691$ (probability of $t \ge T$). Since $\text{p-value} > 0.05$, these results indicate that we can not reject the hypothesis. Therefore we can not be sure if the triangular distribution fits our data or not. ## Exercise 5 We estimated a $π$ value of $3.167$, using $6337$ points (iterations). The estimated probability that a point falls within the circle is $0.7917$, with CI at $0.95$ of size $0.02$, compared to the theoretical probability $\dfrac{\pi}{4} \approx 0.7854$. ![points plot](https://i.imgur.com/t48WiGK.png) The plot above is a visual representation of a run of our Monte Carlo algorithm using the aforementioned confidence interval level and interval size threshold as the stopping rule. ## Exercise 6 We did $N_{tr}=10000$ extractions using the inverted CDF formula $-\dfrac{\text{ln}(U)}{\lambda}$, with $\lambda = \dfrac{1}{mean} = 0.5$. The resulting distribution: ![](https://i.imgur.com/0zxwuF2.png) The QQ-plots against exponentials with different mean behave as expected, changing the slope of the points due to the change of scale. | QQ against same mean ![](https://i.imgur.com/EEwfYR3.png) | QQ against mean=5 ![](https://i.imgur.com/eABZpor.png) | QQ against mean=0.5 ![](https://i.imgur.com/Fc38ivK.png) | | :--------: | :--------: | :--------: | QQ-plots using initial mean = 2, but with more or less draws: | x2 draws ![](https://i.imgur.com/lotCiE6.png) | x4 draws ![](https://i.imgur.com/AEnn7lR.png) | | :------: | :------: | | **x20 draws** ![](https://i.imgur.com/TYy1BPR.png) | **only 100 draws** ![](https://i.imgur.com/rmjewGB.png) | Given those plots we can confirm that the CDF inversion behaves exponentially. Since our initial value of $N_{tr}$ was already high, increasing it doesn't bring any noticeable advantage, while decreasing it to $100$ would give a bad exponential representation.