# Lab 1: Sampling Random Variables#

*For the class on Wednesday, January 10th*

Tip

Looking for hints?

## A. Inverse transform sampling#

Write a function to generate random variates sampled from an exponential distribution. Its probability density function is \(p(x; \lambda) = \lambda e^{-\lambda x}\), defined for all \(x \geq 0\). Use the inverse transform sampling method with the built-in standard uniform random number generator.

Generate 1,000 random variates using your function and another 1,000 using

`np.random.default_rng().exponential`

. In both cases, set \(\lambda = 2\) (note that the`scale`

parameter in the numpy function is defined as \(1/\lambda\)). Make a two-sample quantile-quantile plot (q-q plot) to check if you implement the exponential distribution random variate function correctly.

```
# Put your implementation for Part A here!
```

### Post-implementation Questions - Part A#

How would you interpret the q-q plot you made in Step 2?

If instead you only generate 20 random variates in Step 2, how would the q-q plot change? And how would your interpretion change?

*// Write your answers here*

## B. Rejection sampling#

Write a function to generate points that are sampled uniformly at random within the circle \(x^2 + y^2 = 1\) on the 2D \(xy\) plane. Use the rejection sampling method with the built-in standard uniform random number generator.

Use your function to generate 1,000 points and make a scatter plot. Do they look uniformly at random within the circle?

Write a test (testing on the points that you generated) to convince yourself that your implementation is correct.

(

*Optional*) Revise the function you wrote in Step 1 more general, so that it takes in two arguments \(a\) and \(b\), and generates points that are sampled uniformly at random within the ellipse \(\frac{x}{a}^2 + \frac{x}{b}^2 = 1\). (Make sure that when \(a=b=1\) it reverts to the original behavior!)

```
# Put your implementation for Part B here!
```

## C. Uniformly sampling random points on a 2D sphere#

Consider a 2D unit sphere (i.e., the surface of a 3D ball with a radius of 1, centered at the origin). Write a function to generate points (in Cartesian coordinates) that are sampled uniformly at random on that sphere, and generate 1,000 points with your function.

```
# Put your implementation for Part C here!
```

### Post-implementation Questions - Part C#

* After* you completed your function, take a look at
the extra materials under Part C of the hints page
and then answer the following questions:

Does your implementation use the same idea as any of the three methods in the extra materials? If so, identify which one.

Briefly explain why each of the methods (the ones in the extra materials and yours, if different) generate points that are sampled uniformly at random on a 2D unit sphere.

Briefly comment on the pros and cons of each of the methods (the ones in the extra materials and yours, if different).

*// Write your answers here*

## Final question#

Roughly how much time did you spend on this lab (*not* including the time you spent in class)?

*// Write your answers here*

Tip

**How to submit this notebook on Canvas?**

Once you complete this lab and have all your code, text answers, and all the desired results after running the notebook properly displayed in this notebook, please convert the notebook into HTML format by running the following:

```
jupyter nbconvert --to html /path/to/labs/01.ipynb
```

Then, upload the resulting HTML file to Canvas for the corresponding assignment.