Get startedGet started for free

Generating multivariate random variables

1. Generating multivariate random variables

Next stop: sampling from multivariate distributions!

2. Sampling from multivariate distributions

We'll cover two multivariate distributions: The first is the multinomial distribution, which can be used to simulate variables that each follow a binomial distribution with their probabilities summing up to one. For example, we could use the multinomial distribution to simulate the results of flipping a biased coin. The probabilities of heads appearing or not appearing will follow the binomial distribution and will add up to one. As with univariate distributions, we'll use dot-rvs for sampling.

3. Sampling from multivariate distributions

Our second multivariate distribution is the multivariate normal distribution. This distribution can be used to simulate variables that follow normal distributions, and their distributions can be correlated with each other or not. For example, we can simulate price and demand for a product, using dot-rvs for sampling. Let's take a closer look.

4. Sampling from multinomial distributions

Say we have a biased coin with a 20% probability of turning heads and an 80% probability of turning tails. We ask 500 people to flip the coin 50 times each. What will the outcome look like? We use the multinomial distribution in SciPy's multinomial module to simulate this experiment by passing these n, p, and size values as distribution parameters to the dot-rvs function. The pairplot function in Seaborn is a great way to see the results: the histograms in the upper left and lower right quadrants show the distribution of the number of heads and tails, respectively. The scatter plots in the lower left and upper right quadrant indicate that heads and tails outcomes are perfectly negatively correlated: if heads is not flipped, then the flip must have been tails.

5. Sampling from multivariate normal distributions

Let's look at a second example: say we'd like to simulate price (with a mean of two dollars) and demand (with a mean of six units) as two normally distributed variables. We pass these parameters to a dot-rvs function chained to the multivariate normal distribution in SciPy to simulate this experiment. We'll use Seaborn's pairplot to visualize the distributions of price and demand: the histograms in the upper left and lower right quadrant show the distribution of price and demand respectively; the scatter plots in the lower left and upper right quadrant demonstrate that the distributions of price and demand are not correlated because they are sampled independently.

6. Covariance matrix

In fact, price and demand are usually negatively correlated: as price goes up, demand for the product tends to be smaller. How do we reflect this in our simulation? We can use a covariance matrix to capture the variance and covariance of variables. Think of a covariance matrix as a numerical of the pairplot from the previous slide, where the histograms displayed the variance and scatter plots showed correlation. Here's what a covariance matrix would look like for two random variables, x and y. Assume we have historical price and demand data in a DataFrame called df_historical. Using the dot-cov pandas method, we can obtain the covariance matrix for these two variables. Here, the variance of price is 0-point-92, the variance of demand is 0-point-98 and the covariance of price and demand is -0-point-85.

7. Multivariate normal sampling with defined covariance

Assume we now know that price and demand are negatively correlated with variances of one for each variable and a covariance of negative point-nine. We define a covariance matrix and pass it to the cov keyword argument in multivariate_normal-dot-rvs. Visualizing the results, we now see in the scatter plots in the lower left and upper right quadrants that the simulated price and demand are negatively correlated. This makes sense: the simulation is conducted with a negative covariance between these two variables.

8. Let's practice!

Now let's get some practice!