GU4206-GR5206 Sample Final Exam
Student
The STAT Fall 2018 GU4206-GR5206 Final Exam is open notes, open book(s), open computer and online
resources are allowed. Students are not allowed to communicate with any other people regarding the final
with the exception of the instructor and TA. This includes emailing fellow students, using WeChat and other
similar forms of communication. If there is any suspicion of students cheating, further investigation will take
place. If students do not follow the guidelines, they will receive a zero on the exam and potentially face more
severe consequences.
Part 1: Warm Up [15 pts]
Recall the strikes dataset from Week 6 lecture notes. This data set, compiled by Bruce Western has information
on 18 countries over 35 years.
strikes <- read.csv("strikes.csv", as.is = TRUE)
dim(strikes)
## [1] 625 8
head(strikes, 3)
## country year strike.volume unemployment inflation left.parliament
## 1 Australia 1951 296 1.3 19.8 43
## 2 Australia 1952 397 2.2 17.2 43
## 3 Australia 1953 360 2.5 4.3 43
## centralization density
## 1 0.3748588 NA
## 2 0.3751829 NA
## 3 0.3745076 NA
In this problem, you will use the variable unemployment. The goal of this exercise is to take the following
inelegant nested-loop and condense it to as few of lines of code as possible. For full credit, perform the same
task in no more that two lines of code.
countries <- unique(strikes$country)
unemployment_statistic <- rep(NA,length(countries))
counter <- 1
for (c in countries) {
one_country <- strikes[strikes$country==c,]
unemployment_temp <- one_country[,c("unemployment")]
stat <- 0
for (i in 1:length(unemployment_temp)){
stat <- stat + unemployment_temp[i]/length(unemployment_temp)
}
unemployment_statistic[counter] <- stat
counter <- counter+1
}
data.frame(country=countries,unemployment=unemployment_statistic)
## country unemployment
1
## 1 Australia 3.5057143
## 2 Austria 2.5400000
## 3 Belgium 3.6466667
## 4 Canada 6.0428571
## 5 Denmark 5.7114286
## 6 Finland 2.5714286
## 7 France 3.1828571
## 8 Germany 3.1171429
## 9 Ireland 7.7714286
## 10 Italy 6.7257143
## 11 Japan 1.6028571
## 12 Netherlands 3.6914286
## 13 New.Zealand 1.0028571
## 14 Norway 1.4285714
## 15 Sweden 2.1371429
## 16 Switzerland 0.3285714
## 17 UK 3.4514286
## 18 USA 5.5428571
Write your solution below.
## Solution goes here -------
Part 2: Simulation [50 pts]
The goal of this section is to study the random variable
X = U1 + U2,
where U1 and U2 are independent uniform random variables, each over the unit interval. This section of the
final exam has five major components; simulate X = U1 + U2 directly, plot the pdf of X = U1 + U2, use the
inverse-transform method to simulate X, use the accept-reject method to simulate X, and use the simulated
random variable X to compute a Monte-Carlo integral of the function b(x) = sin(sin(x)), over [0, 2].
Perform the following tasks:
Part 2.i) [5 pts]
Simulate 10,000 cases of the random variable X = U1 + U2 directly by using runif() for U1 and U2 and
then adding the resulting vectors. Display the first 10 simulated values of X and use ggplot to construct a
histogram of the distribution of X. Use a Base R plot for partial credit.
## Solution goes here -------
Part 2.ii) [10 pts]
Using ggplot, plot the pdf of X over the range [?1, 3]. The pdf is given by the following piecewise function:
For guidance on defining and plotting a piecewise function, please see Part 2.v.
2
## Solution goes here -------
Part 2.iii) [15 pts]
Use the inverse-transform method to simulate 10,000 draws of X. For full credit, perform the following
tasks:
a) Define a R function called F.cdf.inv which is the inverse of the cdf F(x). Test your function using the
points .1,.45,.7. More specifically, run the code F.cdf.inv(F.cdf(c(.1,.45,.7))), where F.cdf() is the
cdf.
b) Use the inverse-transform method to simulate 10,000 draws of X and display the first 10 simulated
draws.
c) Plot the 10,000 simulated cases using ggplot, i.e., plot a histogram. Also overlay the pdf f(x) on the
the histogram. For partial credit, use Base R graphics.
Note that the cumulative distribution function (cdf) of f(x) is
Note that the function F(x) appears to be non-invertible because of the quadratic terms. However, the
function is invertible over the domain of each piecewise interval.
## Solution goes here -------
Part 2.iii.b)
## Solution goes here -------
Part 2.iii.c)
## Solution goes here -------
Part 2.iv) [10 pts]
Use the accept-reject method to simulate 10,000 draws of X. For full credit, perform the following tasks:
a) Clearly identify an easy to simulate distribution g(x).
b) Identify a suitable envelope function e(x) that satisfies
f(x) ≤ e(x) = g(x)/α, where 0 < α < 1.
c) Simulate 10,000 draws from the target distribution f(x) using the accept-reject algorithm. Display
the first 10 simulated values.
3
d) Using ggplot or Base R, construct a histogram of the simulated distribution with the pdf f(x)
overlayed on the plot.
Part 2.vi.a)
Part 2.vi.b)
## Solution goes here -------
Part 2.iv.c)
## Solution goes here -------
Part 2.iv.d)
## Solution goes here -------
Part 2.v) [10 pts]
The goal of this section is to use the simulated cases coming from target distribution f(x) to numerically
integrate b(x) via Monte-Carlo. You must draw directly from f(x) by using any of the previous three
methods. The integral of interest is:
b(x)dx, where b(x) = (sin(sin(x)) 0 < x < 20 otherwise
To visualize b(x), see the code below.
x <- seq(-1,3,by=.01)
n.x <- length(x)
b <- function(x) {
out <- ifelse((x<=0)|(x>2),0,sin(sin(x)))
return(out)
}
plot_data <- data.frame(x=x,b=b(x))
library(ggplot2)
ggplot(data = plot_data) +
geom_abline(slope=0,intercept=0,linetype = "dashed")+
geom_line(mapping = aes(x = x, y = b),color="red")+
labs(title = expression(sin(sin(x))),y="b(x)")
## Solution goes here -----
Part 3: Cost of Gradient Descent vs. Newton’s Method [35 pts]
In this section, we will extend off of the gradient descent and Newton’s method algorithms to assess how long
each method takes overall and how long each method takes per iteration.
To investigate the two methods, consider the simulated dataset:
n <- 100
d <- 10
set.seed(0)
x <- matrix(rnorm(n * (d-1)), n, (d-1))
x <- cbind(rep(1,n),x)
b <- round(runif(d,-10,10),2)
y <- x %*% b + rnorm(n,mean=0,sd=5)
To begin, we will apply a traditional least squares scenario as our objective function. It (obviously) isn’t
necessary to use gradient-descent or Newton’s method. However, this nice objective function guarantees less
problems with convergence. The objective function (squared loss) is easily defined by:
square.loss <- function(beta) {
return(sum((y - x %*% beta)^2))
}
A very useful function in R is proc.time(). The third output of proc.time can be used to estimate how
long a program takes to run. A simple example follows:
5
start_time <- proc.time()[3]
min_procedure <- nlm(square.loss,rep(10,d))
end_time <- proc.time()[3]-start_time
# Elapsed time
end_time
## elapsed
## 0.007
# Look at solution
rbind(b,min_procedure$estimate)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## b -4.460000 -8.770000 -2.890000 1.540000 0.7000000 2.090000 -0.280000
## -4.851119 -8.670076 -4.439985 1.648804 0.3286596 2.407551 1.122194
## [,8] [,9] [,10]
## b -7.370000 9.29000 4.850000
## -7.510709 10.49301 4.103895
Perform the following tasks:
2.i) Overall runtime [15 pts]
In this section you will use the Gradient Descent and Newton’s Method functions from Homework 7
and Lab 6 respectively. In all methods, use defaults: x0 = rep(0,d), max.iter=200, step.size=0.001,
stopping.deriv=0.1
Run a simulation that performs the following tasks:
I) Create two vectors of length 29. Call the vectors runtime_gd and runtime_nm.
II) for d = 2, 3, . . . , 30
i) Simulate Y using the linear model
Y = β0 + β1x1 + · · · + βd?1xd?1 + ,
iid~ N(0, σ2 = 25)
Note this is already solved!
ii) Using the simulated dataset, apply both the gradient descent and Newton’s method algorithms
to estimate the parameters β0, . . . , βd?1. During the estimation procedure, store the amount of
elapsed time it took to run for each d.
III) You should have two vectors of length 29, one for gradient descent and one for Newton’s method. Plot
these runtime vectors (on the same graph) as a function of the dimension of the model d. Briefly
comment on the plot.
Note: I generally discourage nesting loops but it is fine for this assignment.
## Solution goes here -------------
2.ii) Runtime per iteration [20 pts]
In this section you will modify the Gradient Descent and Newton’s Method functions from Homework
7 and Lab 6 respectively. In all methods, use defaults: x0 = rep(0,d), max.iter=200, step.size=0.001,
stopping.deriv=0.1
6
In this problem, you will slightly change the Gradient Descent and Newton’s Method functions from
class so that the algorithms also store the run time of each iteration. Each function should output the average
runtime for all iterations.
Run a simulation that performs the following tasks:
I) Create two vectors of length 29. Call the vectors ** iter_time_gd** and ** iter_time_gd**.
II) for d = 2, 3, . . . , 30
i) Simulate Y using the linear model
Y = β0 + β1x1 + · · · + βd?1xd?1 + ,
iid~ N(0, σ2 = 25)
Note this is already solved!
ii) Using the simulated dataset, apply both the modified gradient descent and modified Newton’s
method algorithms to estimate the parameters β0, . . . , βd?1. For each d during the estimation
procedure, store the average elapsed time per iteration.
III) You should have two vectors of length 29, one for gradient descent and one for Newton’s method. Plot
these iteration runtimes (on the same graph) as a function of the dimension of the model d. Briefly
comment on the plot.
## Solution goes here -------------
7
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。