Adam Shen
October 5, 2022
Type ?
to bring up the presentation menu
Press o
for the presentation overview (and jump to a specific slide)
To export slides to PDF, press e
to activate PDF export mode, then print to PDF (note that some features may not function properly when exported to a PDF)
To copy a code block, hover over the top-right corner of the code block and click the clipboard icon
Name: Adam Shen
Email: firstnamelastname@cmail.carleton.ca
MTC Hours: Wednesdays 2pm to 4pm
If you do not feel very comfortable with the R language, I recommend working through Hands on Programming with R, a free online book written by the folks over at RStudio.
Chapters 2 through 7 for R basics, such as writing functions, R objects, subsetting
Chapter 9 for conditional logic
Chapter 11 for loops
Today, we will be working with a Pareto distribution, whose pdf is given by:
f(x) = \frac{\alpha 3^{\alpha}}{x^{\alpha + 1}}, \quad x > 3, \quad \alpha > 1,
and zero otherwise.
Task: Find the cdf and the quantile function.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
Here, I use the ifelse
function because I want dpareto
to be vectorized over x
, i.e. if the shape
criteria has been satisfied, I can supply a vector of values for x
and the function should return a vector of the same length.
You do not need a loop for this!
dpareto
Error in dpareto(x = 4, shape = 0.5): Value of `shape` must be greater than 1.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
ppareto
Error in ppareto(q = 4, shape = 0.5): Value of `shape` must be greater than 1.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
Note: shape
refers to \alpha.
Here, we need to use any
because the condition p <= 0 | p >= 1
returns a vector greater than length 1. If we supply a logical condition with length greater than 1 to if
, it will only use the first element.
any
evaluates to TRUE
if the condition inside contains at least one TRUE
. Otherwise, it returns FALSE
.
In the above, I have chosen to make p = 0
result in an error since F(q) = 0 for any q \leq 3. p = 1
will result in division by zero, and therefore should also raise an error.
Note: shape
refers to \alpha.
qpareto
Error in qpareto(p = 0.5, shape = 0.5): Value of `shape` must be greater than 1.
We would like to sample from the given Pareto distribution using the inverse-transform method.
Generate random deviates from Uniform(0,1).
Pass random deviates to quantile function. Done!
Let X_{1}, X_{2}, \ldots, X_{n} be an iid sample from the Pareto distribution (with scale parameter equal to 3). To generate this sample with the inverse-transform method:
For i=1, \ldots, n, generate U_{i} where U_{i} \sim \text{Unif}(0,1).
X_{i} \,=\, Q(U_{i}) \,=\, \frac{3}{(1-U_{i})^{1/\alpha}}
As seen in class, if U_{i} \sim \text{Unif}(0,1), then 1-U_{i} \sim \text{Unif}(0,1).
Therefore, it is also equivalent to perform:
X_{i} \,=\, Q(1-U_{i}) \,=\, \frac{3}{U_{i}^{1/\alpha}}
rpareto
Error in rpareto(n = 1, shape = 0.5): Value of `shape` must be greater than 1.
pop_mean <- function(shape, scale=3) {
if (shape <= 1) {
stop("Value of `shape` must be greater than 1 to avoid division by zero.")
}
(scale * shape) / (shape - 1)
}
pop_var <- function(shape, scale=3) {
if (shape <= 2) {
stop("Value of `shape` must be greater than 2 to avoid division by zero.")
}
(scale^2 * shape) / ((shape - 1)^2 * (shape - 2))
}