website/static/~brozek/index.html?courses%2Fstat381%2Frandomnumber.html

466 lines
21 KiB
HTML
Raw Permalink Normal View History

2020-01-15 23:07:02 -05:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
2022-02-15 01:14:58 -05:00
<meta name="author" content="Brandon Rozek">
2020-01-15 23:07:02 -05:00
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex" />
<title>Brandon Rozek</title>
<link rel="stylesheet" href="themes/bitsandpieces/styles/main.css" type="text/css" />
<link rel="stylesheet" href="themes/bitsandpieces/styles/highlightjs-github.css" type="text/css" />
</head>
<body>
<aside class="main-nav">
<nav>
<ul>
<li class="menuitem ">
<a href="index.html%3Findex.html" data-shortcut="">
Home
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fcourses.html" data-shortcut="">
Courses
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Flabaide.html" data-shortcut="">
Lab Aide
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fpresentations.html" data-shortcut="">
Presentations
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Fresearch.html" data-shortcut="">
Research
</a>
</li>
<li class="menuitem ">
<a href="index.html%3Ftranscript.html" data-shortcut="">
Transcript
</a>
</li>
</ul>
</nav>
</aside>
<main class="main-content">
<article class="article">
<h1>Random Number Generation</h1>
<h2>Introduction</h2>
<p>The generation of random numbers have a variety of applications including but not limited to the modeling of stochastic processes. It is important, therefore, to be able to generate any random number following a given distribution. One of the many ways to achieve this is to transform numbers sampled from a random uniform distribution. </p>
<p>In this lab, we will compare the effectiveness between the linear congruence method (LCM), <code>runif</code>, and <code>rexp</code> to generate random numbers. The programming language R will be used and different plots and summary statistics are drawn upon to analyze the effectiveness of the methods.</p>
<h2>Methods</h2>
<h3>The Linear Congruential Method</h3>
<p>The linear congruential method (LCM) is an algorithm that produces a sequence of pseudo-random numbers using the following recursive function
$$
X_{n + 1} = (aX_n + c) \mod m
$$
The R code we use has a <code>c</code> value of 0 which is a special case known as the multiplicative congruential generator (MCG).</p>
<p>There are several conditions for using a MCG. First, the seed value must be co-prime to <code>m</code>. In other words, the greatest common denominator between the two must be 1. A function was written in R to check this. Secondly, <code>m</code> must be a prime number or a power of a prime number. </p>
<h4>Periodicity</h4>
<p>An element of periodicity comes into play when dealing with pseudo-random number generators. Once the generator has produced a certain number of terms, it is shown to loop back to the first term of the sequence. It is advantageous, therefore, to keep the period high. </p>
<p>The period in a MCG is at most <code>m - 1</code>. In this lab, <code>m</code> has a value of $2^{31}$ and only 100 numbers were sampled from the LCM. This allows us to avoid the issue of periodicity entirely in our analysis.</p>
<h3>Mersenne-Twister Method</h3>
<p>The default pseudo-random number generator (pseudo RNG) in R is the Mersenne-Twister with the default seed value of the current time and the process id of the current R instance. Mersenne-Twister is part of a class of pseudo RNG called the generalized feedback register. This class of pseudo RNGs provide a very long period (VLP) or in other words, a long cycle before the values start repeating. VLPs are often useful when executing large simulations in R.</p>
<p>Since this method is already coded in the <code>base</code> R-package, we will leave the implementation for the curious to research.</p>
<h3>The Uniform Distribution</h3>
<h4>Probability Mass Function</h4>
<p>The uniform distribution function $Unif(\theta_1, \theta_2)$ has a probability mass function (PMF) of the following.
$$
f(x) = \frac{1}{\theta_2 - \theta_1}
$$
Where x is valid between [$\theta_1$, $\theta_2$].</p>
<p>In our case, we are producing numbers from [0,1]. We can therefore reduce the probability mass function to the following
$$
f(x) = 1
$$</p>
<h4>Expected Value</h4>
<p>The expected value can be defined as
$$
E(X) = \int_a^b xf(x)dx
$$
For the uniform distribution we're using that becomes
$$
E(X) = \int_0^1 xdx = \frac{1}{2}[x^2]_0^1 = \frac{1}{2}
$$
We should, therefore, expect the mean of the generated random number sequence to roughly equal $\frac{1}{2}$. </p>
<h4>Median</h4>
<p>To find the median of the distribution, we need to find the point at which the cumulative density function (CDF) is equal to .5.
$$
\int_0^{Median(X)} f(x)dx = \frac{1}{2}
$$
$$
\int_0^{Median(X)} dx = \frac{1}{2}
$$</p>
<p>$$
[X]_0^{Median(X)} = \frac{1}{2}
$$</p>
<p>$$
Median(X) = \frac{1}{2}
$$</p>
<p>This shows us that the median of the distribution should be roughly equal to $\frac{1}{2}$ as well.</p>
<h4>Analysis of Uniform Distribution Fit</h4>
<p>The histogram of a uniform distribution of data should look rectangular in shape. This means that the counts of all the sub-intervals should be about the same.</p>
<p>Another way to test whether or not the distribution is a good fit is to use what is called a Quantile-Quantile plot (Q-Q Plot). This is a probability plot that compares the quantiles from the theoretical distribution, this is the uniform distribution, to that of the sample.</p>
<p>For a precise Q-Q Plot, we need a lot of quantiles to compare. In this lab, we will compare 100 different quantiles. The quantiles from the theoretical distribution can be easily derived from the fact that all value ranges are equally probable. The theoretical quantiles in this case is 0.01, 0.02, ..., 0.98, 0.99, 1. The sample quantiles are obtianed by receiving 100 observations from <code>runif</code> or the LCM. </p>
<p>In the Q-Q plot, a good fit is shown when the points hug closely to the Q-Q line. It is also important to have symmetry in the points. This means that the points are ending on opposite sides of the Q-Q line.</p>
<p>For sake of exploration, we will use 5 different seed values for the linear congruential method (while making sure that the seed values are co-prime). We will then use the results of these and compare LCM to how the standard <code>runif</code> method generates random numbers.</p>
<h3>Exponential Distribution</h3>
<h4>Probability Mass Function and the Cumulative Density Function</h4>
<p>The exponential distribution is a type of continuous distribution that is defined by the following PMF
$$
f(x) = \lambda e^{-\lambda t}
$$
We can find the CDF by taking the integral of the PMF.
$$
F(x) = \int f(x)dx = \lambda \int e^{-\lambda x} dx = \lambda * (\frac{-1}{\lambda}) e^{-\lambda x} + C = -e^{-\lambda x} + C
$$
One of the conditions for the cumulative density function is that as $x \to \infty$, $F(x) \to 1$.
$$
1 = \lim<em>{x \to \infty} (-e^{-\lambda x} + C) = \lim</em>{x \to \infty} (-e^{-\lambda x}) + lim<em>{x \to \infty} ( C) = \lim</em>{x \to \infty}(-e^{\lambda x}) + C
$$
This shows that $C = 1$, since $lim_{x \to \infty} (-e^{-\lambda x})$ is equal to 0. </p>
<p>Substituting $C$ gives us the following.
$$
F(x) = 1 - e^{-\lambda x}
$$</p>
<h4>Expected Value</h4>
<p>We can find the expected value using the formula described in the previous Expected Value section. Reflected in the bounds of integration is the domain of the exponential distribution $[0, \infty)$.
$$
E(X) = \lim_{a \to \infty}\int_0^a x \lambda e^{-\lambda x} dx
$$
The integral above features a product of two functions. So as an aside, we will derive a formula so we can take the integral above.</p>
<p>The total derivative is defined as
$$
d(uv) = udv + vdu
$$</p>
<p>After taking the integral of both sides, we can solve for a formula that gives the following
$$
\int d(uv) = \int udv + \int vdu
$$</p>
<p>$$
\int udv = uv - \int vdu
$$</p>
<p>The formula above is called <em>Integration by Parts</em>. We will make use of this formula by defining $u = \lambda x$ and $dv = e^{-\lambda x} dx$</p>
<p>This implies that $du = \lambda dx$ and $v= -\frac{1}{\lambda}e^{-\lambda x}$<br />
$$
E(X) = -\lim_{a \to \infty}[\lambda x \frac{1}{\lambda}e^{-\lambda x}]<em>0^a - \lim</em>{b \to \infty}\int_0^b -\frac{1}{\lambda}e^{-\lambda x}\lambda dx
$$</p>
<p>$$
E(X) = -\lim_{a \to \infty} [xe^{-\lambda x}]<em>0^a - \lim</em>{b \to \infty}\int_0^b -e^{-\lambda x}dx
$$</p>
<p>$$
E(X) = -\lim<em>{a \to \infty}(ae^{-\lambda a}) - \lim</em>{b \to \infty}[\frac{1}{\lambda}e^{-\lambda x}]_0^b
$$</p>
<p>$$
E(X) = 0 - \frac{1}{\lambda}[\lim_{b \to \infty}(e^{-\lambda b}) - e^{-0\lambda}]
$$</p>
<p>$$
E(X) = -\frac{1}{\lambda}(-1) = \frac{1}{\lambda}
$$</p>
<p>For the purposes of this lab, we will make the rate ($\lambda$) equal to 3. Which means we should expect our mean to be roughly equal to $\frac{1}{3}$.</p>
<h4>Median</h4>
<p>The median can be found by setting the CDF equal to $\frac{1}{2}$
$$
1- e^{-\lambda Median(X)} = \frac{1}{2}
$$</p>
<p>$$
e^{-\lambda Median(X)} = \frac{1}{2}
$$</p>
<p>$$
-\lambda Median(X) = \ln(\frac{1}{2})
$$</p>
<p>$$
Median(X) = \frac{\ln(2)}{\lambda}
$$</p>
<p>This shows that we should expect our median to be around $\frac{\ln(2)}{3} \approx 0.231$.</p>
<h3>Inverse Transform Sampling</h3>
<p>Once we have a uniform distribution of values, we can then use these values to map to an exponential distribution. This is done through a technique called inverse transform sampling, the technique works as follows:</p>
<ol>
<li>Generate a random number u from the standard uniform distribution</li>
<li>Compute the value of X such that F(X) = u</li>
<li>The value of X belongs to the distribution of F</li>
</ol>
<p>Using these steps, we'll derive the exponential transform below.
$$
F(X) = 1 - e^{-\lambda x} = u
$$
Then proceeding to solve for X, we obtain the following.
$$
e^{-\lambda X} = 1 - u
$$</p>
<p>$$
-\lambda X = \ln(1 - u)
$$</p>
<p>$$
X = \frac{-\ln(1 - u)}{\lambda}
$$</p>
<p>With this formula, we can now find values for X which is a random variable that follows an exponential distribution given random uniform data $u$.</p>
<p>In this lab, we are looking at the exponential distribution with a rate of 3. Therefore, the probability mass function, the cumulative distribution function, and the exponential transform can be redefined as these respectively.
$$
f(x) = 3e^{-3x}
$$</p>
<p>$$
F(x) = 1 - e^{-3x}
$$</p>
<p>$$
X = \frac{-\ln(1 - u)}{3}
$$</p>
<h3>R Code</h3>
<p>The R code makes use of the concepts above. The purpose of this code is to output the summary statistics, histograms, and Q-Q plots are used to compare the different methods.</p>
<p>First the LCM is executed four times with three different seed values. The LCM is used to create a uniform distribution of data that is then compared to the standard <code>runif</code> function in the R language.</p>
<p>Then, transformations of a LCM, <code>runif</code>, are executed and compared with the standard <code>rexp</code> to create an exponential distribution of data with $\lambda = 3$. </p>
<h2>Results</h2>
<h3>Uniform Distribution</h3>
<p>For a small sample of 100 values, the data seems evenly distributed for all seeds and methods used. The peaks and troughs are more pronounced in the LCM methods suggesting that the <code>runif</code> command creates more evenly distributed data than the LCM. </p>
<p>Deviations from the mean and median are lowest for the LCM rather than the standard <code>runif</code> command with the exception of the LCM with the seed of 93463.</p>
<p>The Q-Q plots for all of the methods follow the Q-Q Line tightly and appear symmetric.</p>
<h3>Exponential Distribution</h3>
<p>A bin size of 20 is used to make the histograms easily comparable to each other. One interesting thing to note is that in Figure XI, there is an observation located far out from the rest of the data. For the purpose of a histogram, which is to show us the shape of the distribution, this one far observation skews what we can see. Therefore the next figure, Figure XII has that single outlier removed and shows the histogram of the rest of the data.</p>
<p>All of the histograms appear exponential in shape. Looking at the Q-Q plots, the LCM transformation and the <code>rexp</code> distribution of data fit closely to the QQ-Line. All of the Q-Q Plots have the points getting further away from the line as you get higher up in the percentiles. The <code>runif</code> transformation quantiles diverge from the line after about the 50th percentile.</p>
<p>Deviations from the mean and median were about the same for both transformed techniques (<code>runif</code> and LCM). The <code>rexp</code> command did better when it came to the deviation from the mean, but performed worse when it came to deviation from the median.</p>
<h2>Conclusion</h2>
<p>The linear congruential methods performed better when it came to simulating the distribution than the standard R functions. It more accurately captured the median for both the standard uniform data, and the exponential data. Against the <code>runif</code> command, it also performed better at simulating the mean.</p>
<p>This can especially be seen when comparing the two transform techniques. The transformed LCM distribution of data followed the Q-Q line more tightly than the transformed <code>runif</code>.</p>
<p>I speculate that this is due to the seed value used. The Mersenne-Twist method performs better when the seed value doesn't have many zeros in it. Since the seed value is determined by the system time and process id, we don't know for sure if it's a proper input for the Mersenne-Twist. For the LCM, seeds were properly tested to make sure that it was co-prime with one of its parameters. This condition allowed proper seeds to work well with the LCM. </p>
<p>Further research can be done on standardizing the seed values used across all the different pseudo random number generators and looking at the 6 other pseudo RNG that comes built into R. Changing the seed and random number generator can be achieved through the <code>set.seed</code> function.</p>
<h2>Appendix</h2>
<h3>Figures</h3>
<h4>Figure I, Histogram of LCM Random Data with seed 55555</h4>
<p><img src="file:///home/rozek/Pictures/stat381lab2/hist55555.png" alt="" /></p>
<h4>Figure II, Q-Q Plot of LCM Random Data with seed 55555</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/qqplot55555.png" alt="qqplot55555" /></p>
<h4>Figure III, Histogram of LCM Random Data with seed 93463</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/hist93463.png" alt="hist93463" /></p>
<h4>Figure IV, Q-Q Plot of LCM Random Data with seed 93463</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/q93463.png" alt="q93463" /></p>
<h4>Figure V, Histogram of LCM Random Data with seed 29345</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/hist29345.png" alt="hist29345" /></p>
<h4>Figure VI, Q-Q Plot of LCM Random Data with seed 29345</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/q29345.png" alt="q29345" /></p>
<h4>Figure VII, Histogram of LCM Random Data with seed 68237</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/hist68237.png" alt="hist68237" /></p>
<h4>Figure VIII, Q-Q Plot of LCM Random Data with seed 68237</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/q68237.png" alt="q68237" /></p>
<h4>Figure IX, Histogram of R Random Uniform Data</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/histunif.png" alt="histunif" /></p>
<h4>Figure X, Q-Q Plot of R Random Uniform Data</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/qunif.png" alt="qunif" /></p>
<h4>Figure XI, Histogram of Exponential Data from LCM Random</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/histexplcm.png" alt="histexplcm" /></p>
<h4>Figure XII, Histogram of Exponential Data from LCM Random without Outlier Above 2</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/histexplcm_nooutlier.png" alt="histexplcm_nooutlier" /></p>
<h4>Figure XIII, Q-Q Plot of Exponential Data from LCM Rnadom</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/qexplcm.png" alt="qexplcm" /></p>
<h4>Figure XIV, Histogram of Exponential Data from R Random Uniform</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/histexpunif.png" alt="histexpunif" /></p>
<h4>Figure XV, Q-Q Plot of Exponential Data from R Random Uniform</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/qexpunif.png" alt="qexpunif" /></p>
<h4>Figure XVI, Histogram of R Generated Exponential Data</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/histexp.png" alt="histexp" /></p>
<h4>Figure XVII, Q-Q Plot of R Generated Exponential Data</h4>
<p><img src="http://cs.umw.edu/home/rozek/Pictures/stat381lab2/qexp.png" alt="qexp" /></p>
<h3>Tables</h3>
<h4>Table I, Uniform Distribution Sample Data</h4>
<table>
<thead>
<tr>
<th>Method</th>
<th>Mean ($\bar{x}$)</th>
<th>Median ($\tilde{x}$)</th>
<th>$\mu - \bar{x}$</th>
<th>$m - \tilde{x}$</th>
</tr>
</thead>
<tbody>
<tr>
<td>LCM with seed 55555</td>
<td>0.505</td>
<td>0.521</td>
<td>-0.005</td>
<td>-0.021</td>
</tr>
<tr>
<td>LCM with seed 93463</td>
<td>0.452</td>
<td>0.402</td>
<td>0.048</td>
<td>0.098</td>
</tr>
<tr>
<td>LCM with seed 29345</td>
<td>0.520</td>
<td>0.502</td>
<td>-0.020</td>
<td>-0.002</td>
</tr>
<tr>
<td>LCM with seed 68237</td>
<td>0.489</td>
<td>0.517</td>
<td>0.011</td>
<td>-0.017</td>
</tr>
<tr>
<td>R Random Uniform</td>
<td>0.480</td>
<td>0.471</td>
<td>0.020</td>
<td>0.029</td>
</tr>
</tbody>
</table>
<h4>Table II, Exponential Distribution Sample Data</h4>
<table>
<thead>
<tr>
<th>Method</th>
<th>Mean</th>
<th>Median</th>
<th>$\mu - \bar{x}$</th>
<th>$m - \tilde{x}$</th>
</tr>
</thead>
<tbody>
<tr>
<td>LCM Transform</td>
<td>0.380</td>
<td>0.246</td>
<td>-0.047</td>
<td>-0.015</td>
</tr>
<tr>
<td>RUnif Transform</td>
<td>0.283</td>
<td>0.218</td>
<td>0.050</td>
<td>0.013</td>
</tr>
<tr>
<td>R Exponential</td>
<td>0.363</td>
<td>0.278</td>
<td>-0.030</td>
<td>-0.047</td>
</tr>
</tbody>
</table>
<h3>R Code</h3>
<pre><code class="language-R">library(ggplot2)
linear_congruential = function(seed) {
a = 69069
c = 0
m = 2^31
x = numeric(100)
x[1] = seed
for (i in 2:100) {
x[i] = (a * x[i-1] + c) %% m
}
xnew = x / (max(x) + 1)
xnew
}
gcd = function(x,y) {
r = x %% y;
return(ifelse(r, gcd(y, r), y))
}
check_seed = function(seed) {
if (gcd(seed, 2^31) == 1) {
print(paste("The seed value of", seed, "is co-prime"))
} else {
print(paste("The seed value of", seed, "is NOT co-prime"))
}
}
check_data = function(data, distribution, title) {
print(paste("Currently looking at", title))
print(summary(data))
print(ggplot(data.frame(data), aes(x = data)) +
geom_histogram(fill = 'white', color = 'black', bins = 10) +
xlab("Date") +
ylab("Frequency") +
ggtitle(paste("Histogram of", title)) +
theme_bw())
uqs = (1:100) / 100
if (distribution == "Uniform") {
qqplot(data, uqs, pch = 1, col = '#001155', main = paste("QQ Plot of", title), xlab = "Sample Data", ylab = "Theoretical Data")
qqline(uqs, distribution = qunif, col="red", lty=2)
} else if (distribution == "Exponential") {
eqs = -log(1-uqs) / 3
qqplot(data, eqs, pch = 1, col = '#001155', main = paste("QQ Plot of", title), xlab = "Sample Data", ylab = "Theoretical Data")
qqline(eqs, distribution=function(p) { qexp(p, rate = 3)}, col="#AA0000", lty=2)
}
}
seed1 = 55555
seed2 = 93463
seed3 = 29345
seed4 = 68237
check_seed(seed1)
lin_cong = linear_congruential(seed1)
check_data(lin_cong, "Uniform", paste("LCM Random Data with seed", seed1))
check_seed(seed2)
lin_cong2 = linear_congruential(seed2)
check_data(lin_cong2, "Uniform", paste("LCM Random Data with seed", seed2))
check_seed(seed3)
lin_cong3 = linear_congruential(seed3)
check_data(lin_cong3, "Uniform", paste("LCM Random Data with seed", seed3))
check_seed(seed4)
lin_cong4 = linear_congruential(seed4)
check_data(lin_cong4, "Uniform", paste("LCM Random Data with seed", seed4))
# Using the standard built-in R function
unif = runif(100, 0, 1)
check_data(unif, "Uniform", "R Random Uniform Data")
# Building exponential from linear congruence method
exp1 = -log(1 - lin_cong) / 3
check_data(exp1, "Exponential", "Exponential Data from LCM Random")
# Building exponential from runif
exp2 = -log(1 - unif) / 3
check_data(exp2, "Exponential", "Exponential Data from R Random Uniform")
# Building exponential from rexp
exp3 = rexp(100, rate = 3)
check_data(exp3, "Exponential", "R Generated Exponential Data")</code></pre>
</article>
</main>
<script src="themes/bitsandpieces/scripts/highlight.js"></script>
<script src="themes/bitsandpieces/scripts/mousetrap.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
hljs.initHighlightingOnLoad();
document.querySelectorAll('.menuitem a').forEach(function(el) {
if (el.getAttribute('data-shortcut').length > 0) {
Mousetrap.bind(el.getAttribute('data-shortcut'), function() {
location.assign(el.getAttribute('href'));
});
}
});
</script>
</body>
</html>