63 minute read
Notice a tyop typo? Please submit an issue or open a PR.
In this lesson, we will learn about the composition method. This technique only works for random variables that are themselves combinations or mixtures of random variables, but it works well when we can use it.
We use composition when working with a random variable that comes from a combination of two random variables and can be decomposed into its constituent parts.
Let's consider a concrete example. An airplane might be late leaving the gate for two reasons, air traffic delays and maintenance delays, and these two delays compose the overall delay time. Most of the time, the total delay is due to a small air traffic delay, while, less often, the total delay is due to a larger maintenance delay.
If we can break down the total delay into the component delays - air traffic and maintenance - and those components are easy to generate, we can produce an expression for the total delay that combines the individual pieces.
The goal is to generate a random variable with a cdf, , of the following form:
In plain English, we want to generate a random variable whose cdf is a linear combination of other, "easier" cdf's, the 's. Note that and the sum of all 's equals one.
Again, the main idea here is that, instead of trying to generate one complicated random variable, we can generate a collection of simpler random variables, weight them accordingly, and then sum them to realize the single random variable.
Don't fixate on the upper limit being infinity in the summation expression. Infinity is just the theoretical upper limit. In our airplane example, the actual limit was 2.
Here's the algorithm that we will use. First, we generate a positive integer such that for all . Then, we return from cdf .
Let's look at a proof that has cdf . First, we know that the cdf of , by definition, is . Additionally, by the law of total probability:
Now, we expressed as , and we know that is equal to . Therefore:
Consider the Laplace distribution, with the following pdf and cdf:
Note that the pdf and cdf look similar to those for exponential random variables, except that is allowed to be less than zero and the exponential has a coefficient of one-half. We can think of this distribution as the exponential distribution reflected off the -axis.
Let's decompose this into a "negative exponential" and regular exponential distributions:
If we multiply each CDF here by one-half and sum them together, we get a Laplace random variable:
Let's look at both cases. When :
When :
As we can see, the composed cdf matches the expected cdf, both for and .
The cdf tells us that we generate from half the time and from the other half of the time. Correspondingly, we'll use inverse transform to solve for half the time and we'll solve for the other half. Consider:
Precisely, we transform our uniform into an exponential with probability one-half and a negative exponential with probability negative one-half.
In this lesson, we will talk about the Box-Muller method, which is a special-case algorithm used to generate standard normal random variables.
If are iid , then the following quantities are iid Nor(0,1):
Note that we must perform the trig calculations in radians! In degrees, is a very small quantity, and resulting and will not be iid Nor(0,1).
We've mentioned before that if we square a standard normal random variable, we get a chi-squared random variable with one degree of freedom. If we add chi-squared randoms, we get a chi-squared with degrees of freedom:
Meanwhile, let's consider the sum of and algebraically:
Remember from trig class the , so:
Remember also how we transform exponential random variables:
All this to say, we just demonstrated that . Furthermore, and more interestingly, we just proved that:
Let's look at another example. Suppose we take . One standard normal divided by another is a Cauchy random variable, and also a t(1) random variable.
As an aside, we can see how Cauchy random variables take on such a wide range of values. Suppose the normal in the denominator is very close to zero, and the normal in the numerator is further from zero. In that case, the quotient can take on very large positive and negative values.
Moreover:
Thus, we've just proven that . Likewise, we can take , which proves additionally that .
Furthermore:
We can also use the polar method to generate standard normals, and this method is slightly faster than Box-Muller.
First, we generate two uniforms, . Next, let's perform the following transformation:
Finally, we use the acceptance-rejection method. If , we reject and return to sampling uniforms. Otherwise:
We accept . As it turns out, and are iid Nor(0,1). This method is slightly faster than Box-Muller because it avoids expensive trigonometric calculations.
In this lesson, we will talk about how to generate order statistics efficiently.
Suppose that we have the iid observations, from some distribution with cdf . We are interested in generating a random variable, , such that is the minimum of the 's: . Since is itself a random variable, let's call its cdf . Furthermore, since refers to the smallest , it's called the first order statistic.
To generate , we could generate 's individually, which takes units of work. Could we generate more efficiently, using just one ?
The answer is yes! Since the 's are iid, we have:
In English, the cdf of is , which is equivalent to the complement: . Since , .
Now, since the minimum is greater than , then all of the 's must be greater than :
Since the 's are iid, . Therefore:
Finally, note that . Therefore, , so:
At this point, we can use inverse transform to express in terms of :
Suppose . Then:
So, . If we apply inverse transform we get:
We can do the same kind of thing for . Let's try it! If , then we can express as:
Now, since the maximum is less than or equal to , then all of the 's must less than or equal to :
Since the 's are iid, . Therefore:
Finally, note that . Therefore:
Suppose . Then:
Let's apply inverse transform:
If are iid Nor(0,1), then the sum of the squares of the 's is a chi-squared random variable with degrees of freedom:
If , and , and and are independent, then:
Note that t(1) is the Cauchy distribution.
If , and , and and are independent, then:
If we want to generate random variables from continuous empirical distributions, we'll have to settle for the CONT
function in Arena for now.
In this lesson, we will look at the multivariate normal distribution. This distribution is very important, and people use it all the time to model observations that are correlated with one another.
Consider a random vector . For example, we might draw from a distribution of heights and from a distribution of weights. This vector has the bivariate normal distribution with means and , variances and , and correlation if it has the following joint pdf:
Note the following definitions for and , which are standardized versions of the corresponding variables:
Let's contrast this pdf with the univariate normal pdf:
The fractions in front of the exponential looks similar in both cases. In fact, we could argue that the fraction for the univariate case might look like this:
In this case, however, , so the expression involving reduces to one. All this to say, the bivariate case expands on the univariate case by incorporating another term. Consider:
Now let's look at the expression inside the exponential. Consider again the exponential for the univariate case:
Again, we might argue that this expression contains a hidden term concerning that gets removed because the correlation of with itself is one:
If we look at the bivariate case again, we see something that looks similar. We retain the coefficient and we square standardized versions of and , although the standardization part is hidden away in and :
As we said, we can model heights and weights of people as a bivariate normal random variable because those two observations are correlated with one another. In layman's terms: shorter people tend to be lighter, and taller people tend to be heavier.
For example, consider the following observations, taken from a bivariate normal distribution where both means are zero, both variances are one, and the correlation between the two variables is 0.9.
The random vector has the multivariate normal distribution with mean vector and covariance matrix , if it has the following pdf:
Notice that the vector and the vector have the same dimensions: random variable has the expected value .
Now let's talk about the covariance matrix . This matrix has rows and columns, and the cell at the th row and th column holds the covariance between and . Of course, the cell at holds the covariance of with itself, which is just the variance of .
Now let's look at the fraction in front of the exponential expression. Remember that, in one dimension, we took . In dimensions, we take . In one dimension, we take . In dimensions, we take the square root of the determinant of , where the determinant is a generalization of the variance.
Now let's look at the exponential expression. In one dimension, we computed:
In dimensions, we compute:
Here, corresponds to , and is the inverse of the covariance matrix, which corresponds to in the univariate case.
All this to say, the multivariate normal distribution generalizes the univariate normal distribution.
The multivariate case has the following properties:
We can express a random variable coming from this distribution with the following notation: .
To generate , let's start out with a vector of iid Nor(0,1) random variables in the same dimension as : . We can express as , where is a -dimensional vector of zeroes and is the identity matrix. Note that the identity matrix makes sense here: since the values are iid, everywhere but the diagonal is zero.
Now, suppose that we can find the lower triangular Cholesky matrix , such that . We can think of this matrix, informally, as the "square root" of the covariance matrix.
It can be shown that is multivariate normal with mean and covariance matrix:
How do we find this magic matrix ? For , we can easily derive :
Again, if we multiply by , we get the covariance matrix, :
Here's how we generate . Since , if we carry the vector addition and matrix multiplication out, we have:
Here's the algorithm for computing a -dimensional Cholesky matrix, .
Once we compute , we can easily generate the multivariate normal random variable . First, we generate iid Nor(0,1) uniforms, . Next, we set equal to the following expression:
Note that the sum we take above is nothing more than the sum of the standard normals multiplied by the th row in .
Finally, we return .
In this lesson, we will talk about how we generate some easy stochastic processes: Markov chains and Poisson arrivals.
A time series is a collection of observations ordered by time: day to day or minute to minute, for instance. We will look at a time series of daily weather observations and determine the probability of moving between two different states, sunny and rainy.
It could be the case that the days are not independent of one another; in other words, if we have thunderstorms today, there is likely a higher than average probability that we will have thunderstorms tomorrow.
Suppose it turns out the probability that we have a thunderstorm tomorrow only depends on whether we had a thunderstorm today, and nothing else. In that case, the sequence of weather outcomes is a type of stochastic process called a Markov chain.
Let's let if it rains on day ; otherwise, . Note that we aren't dealing with Bernoulli observations here because the trials are not independent; that is, the weather on day depends on the weather on day .
We can denote the day-to-day transition probabilities with the following equation:
In other words, refers to the probability of experiencing state on day , given that we experienced state on day . For example, refers to the probability that it is sunny tomorrow, given that it rained today.
Suppose that we know the various transition probabilities, and we have the following probability state matrix:
Remember that state zero is rain and state one is sun. Therefore, means that the probability that we have rain on day given that we had rain on day equals 0.7. Likewise, means that the probability that we have sun on day given that we had rain on day equals 0.3.
Notice that the probabilities add up to one across the rows, signifying that, for a given state on day , the probability of moving to some state on day is one: the process must transition to a new state (of course, this new state could be equivalent to the current state).
Suppose it rains on Monday, and we want to simulate the rest of the week. We will fill out the following table:
Let's talk about the columns. Obviously, the first column refers to the day of the week. The column titled refers to the probability of rain given yesterday's weather. Note that we also represent this probability with , where the dot refers to yesterday's state.
In the third column, we sample a uniform. In the fourth column, we check whether that uniform is less than the transition probability from yesterday's state to rain. Finally, in the fifth column, we remark whether it rains, based on whether the uniform is less than or equal to the transition probability.
Let's see what happens on Tuesday:
Here, we are looking at the probability of transitioning from a rainy day to another rainy day. If we look up that particular probability in our state transition matrix, we get . Next, we sample a uniform, , and check whether that uniform is less than the transition probability. Since , we say that it will rain on Tuesday.
Let's see what happens on Wednesday:
Similarly, we are looking at the probability of again transitioning from a rainy day to another rainy day, so our transition probability is still . We draw another uniform, , and, since , we say that it will rain on Wednesday.
Let's see what happens on Thursday:
Here, we have the same transition probability as previously, but our uniform, , happens to be larger than our transition probability: . As a result, we say that it will not rain on Thursday.
Finally, let's look at Friday:
On Friday, we have a new transition probability. This time, we are looking at the probability of rain given sun, and . Again we draw our uniform, which happens to be greater than the transition probability, so we say it will be sunny on Friday.
Let's suppose we have a Poisson() process with a constant arrival rate of . The interarrival times of such a process are iid Exp(). Remember that, even though the rate is constant, the arrivals themselves are still random because the interarrival times are random.
Let's look at how we generate the arrival times. Note that we set to initialize the process at time zero. From there, we can compute the th arrival time, , as:
In other words, the next arrival time equals the previous arrival time, plus an exponential random variable referring to the interarrival time. Even more basically, the time at which the next person shows up is equal to the time at which the last person showed up, plus some randomly generated interarrival time.
Note that, even though we seem to be subtracting from , the natural log of is a negative number, so we are, in fact, adding two positive quantities here.
We refer to this iterative process as bootstrapping, whereby we build subsequent arrivals on previous arrivals.
Suppose we want to generate a fixed number, , arrivals from a Poisson() process in a fixed time interval . Of course, we cannot guarantee any number of arrivals in a fixed time interval in a Poisson process because the interarrival times are random.
It turns out that there is a theorem that states the following: the joint distribution of arrivals from the Poisson process during some interval is equivalent to the joint distribution of the order statistics of iid random variables.
Here's the algorithm. First, we generate iid uniforms, . Next, we sort them: . Finally, we transform them to lie on the interval :
In this lesson, we will look at nonhomogeneous Poisson processes (NHPPs), where the arrival rate changes over time. We have to be careful here: we can't use the algorithm we discussed last time to generate arrivals from NHPPs.
An NHPP maintains the same assumptions as a standard Poisson process, except that the arrival rate, , isn't constant, so the stationary increments assumption no longer applies.
Let's define the function , which describes the arrival rate at time . Let's also define the function , which counts the number of arrivals during .
Consider the following theorem to describe the number of arrivals from an NHPP between time and time :
Suppose that the arrival pattern to the Waffle House over a certain time period is an NHPP with . Let's find the probability that there will be exactly four arrivals between times and .
From the equation above, we can see that the number of arrivals in this time interval is:
Since the number of arrivals is , we can calculate the using the pmf, :
Now, let's look at an incorrect algorithm for generating NHPP arrivals. This algorithm is particularly bad because it can "skip" intervals if grows very quickly.
Here's the algorithm.
We initialize the algorithm with . To generate the th arrival, we first sample , and then we perform the following computation:
Note that, instead of multiplying by , we multiply by , which is the arrival rate at the time of the previous arrival. The problem arises when the arrival rate radically changes between the time we generate the new arrival and the time that arrival occurs.
Specifically, if , then the arrival rate we use to generate arrival , evaluated at time , is too small. Since we capture the arrival rate of the next arrival at the time of the current arrival, we essentially consider the rate to be fixed between that time and the time the arrival actually occurs.
Consider the following rate function, , graphed below. We can suppose, perhaps, that this function depicts the arrivals in a restaurant, and the peaks correspond to breakfast, lunch, and dinner rushes.
We are going to use the thinning algorithm to generate the th arrival. First, we assume that is finite. We can see the line drawn below.
From there, we will generate potential arrivals at that maximum rate :
We can see the potential arrivals in dotted purple below.
We will accept a potential arrival at time as a real arrival with probability . Thus, as approaches , the likelihood that we accept as a real arrival increases proportionally to . We call this algorithm the thinning algorithm because it thins out potential arrivals.
Let's look at the algorithm.
We initialize the algorithm . Next, we initialize , and then we iterate. We generate two iid Unif(0,1) random variables, and , and then we generate a potential arrival:
We keep generating and until this condition holds:
In other words, we only keep the potential arrival with probability . After we accept , we set to , and to , and repeat.
In this demo, we will look at how to implement an NHPP in Excel.
In column A, we have potential NHPP arrival times.
In column B, we have the rate function . Because the sine function is periodic, so are the arrival rates generated by . In fact, never goes below zero, and never goes above two, so .
In column D, we have a sequence of random variables, which we will use to generate interarrival times.
In column E, we generate the potential interarrival times by transforming into an Exp() random variable.
In column F, we bootstrap the arrivals, generating the th arrival by adding the th arrival and the th interarrival time. Note that these potential arrivals are the same values in column A.
In column G, we generate another sequence of random variables , which we will use to determine whether we accept the potential arrival as a real arrival.
In column H, we generate , which refers to the probability that we accept the corresponding potential arrival at time .
If , then we accept the arrival, and we show this boolean value in column I.
In column J, we copy over the real arrival times for all potential arrivals where . Otherwise, we mark the cell as "FALSE".
Finally, in column K, we update the arrival rate for the next iteration.
Here's what the plot of arrival rates over time looks like for this NHPP. Every dot represents an accepted arrival. In the space between the dots, we may have generated potential arrivals, but we didn't accept them as real.
We can see that we have many real arrivals when the is close to and few real arrivals when the two values diverge. Of course, this observation makes sense, given that we accept potential arrivals with probability .
In this lesson, we will look at various time series processes, most of which are used in forecasting. In particular, we will be looking at auto-regressive moving average processes (ARMA), which have standard-normal noise, and auto-regressive Pareto (ARP) processes, which have Pareto noise.
A first-order moving average process, or MA(1), is a popular time series for modeling and detecting trends. This process is defined as follows:
Here, is a constant, and the terms are typically iid Nor(0,1) - in practice, they can be iid anything - and they are independent of .
Notice that , and . Both and contain an term; generally, and pairs are correlated.
Let's derive the variance of . Remember, that:
Let's consider :
Now, we know that successive 's are independent, so the covariance is zero:
Furthermore, we know that , so:
Finally, since the 's are Nor(0,1), they both have variance one, leaving us with:
Now, let's look at the covariance between successive pairs. Here's a covariance property:
So:
Since the 's are independent, we know that . So:
We also know that , and, since , . So:
We can also demonstrate, using the above formula, that .
As we can see, the covariances die off pretty quickly. We probably wouldn't use an MA(1) process to model, say month-to-month unemployment, because the unemployment rate among three months is correlated, and this type of process cannot express that.
How might we generate an MA(1) process? Let's start with . Then, we generate to get , to get , and so on. Every time we generate a new , we can compute the corresponding .
Now let's look at a first-order autoregressive process, or AR(1), which is used to model many different real-world processes. The AR(1) process is defined as:
In order for this process to remain stationary, we need to ensure that , , and that the 's are iid .
Unlike the MA(1) process, non-consecutive observations in the AR(1) process have non-zero correlation. Specifically, the covariance function between and is as follows:
The correlations start out large for consecutive observations and decrease as increases, but they never quite become zero; there is always some correlation between any two observations in the series.
If is close to one, observations in the series are highly positively correlated. Alternatively, if is close to zero, observations in the series are highly negatively correlated. If is close to zero, then the 's are nearly independent.
How do we generate observations from an AR(1) process? Let's start with and to get . Then, we can generate to get , and so on.
Remember that:
So:
Here are the plots of three AR(1) processes, each parameterized with a different value of .
The ARMA(,) process is an obvious generalization of the MA(1) and AR(1) processes, which consists of a th order AR process and a th order MA process, which we will define as:
In the first sum, we can see the autoregressive components, and, in the second sum, we can see the moving average components.
We have to take care to choose the 's and the 's in such a way to ensure that the process doesn't blow up. In any event, ARMA(, ) processes are used in a variety of forecasting and modeling applications.
An exponential autoregressive process, or EAR, is an autoregressive process that has exponential, not normal, noise. Here's the definition:
Here we need to ensure that , , and the 's are iid Exp(1) random variables.
Believe it or not, the EAR(1) has the same covariance function as the AR(1), except that the bounds of are different:
Let's look at a plot of an EAR(1) process with . These observations are iid exponential, and if we were to make a histogram of these observations, it would resemble the exponential distribution.
Here's a plot of an EAR(1) process with