# -----------------------------
# Initialise Inputs
# -----------------------------
# Generating 30 data points from a simple linear-regression model
set.seed(1)
x <- runif(30, min = -5, max = 5)
vari <- rnorm(30, mean = 0, sd = 1)
y <- 0.75 - x + vari
lam0 <- matrix(c(0,0), nrow = 2)
z <- cbind(x, y)
# Specify moment condition functions for linear regression and its corresponding derivative
h <- function(zi, th) {
xi <- zi[1]
yi <- zi[2]
h_zith <- c(yi - th[1] - th[2] * xi, xi*(yi - th[1] - th[2] * xi))
matrix(h_zith, nrow = 2)
}
delthh <- function(zi, th) {
xi <- zi[1]
matrix(c(-1, -xi, -xi, -xi^2), 2, 2)
}
# Specify derivative of log prior
delth_logpi <- function(theta) { -0.0001 * mu0 }
# Specify AEL constant and Newton-Rhapson iteration
a <- 0.00001
AEL_iters <- 10
# Specify initial values for GVA mean vector and Cholesky
reslm <- lm(y ~ x)
mu0 <- matrix(unname(reslm$coefficients),2,1)
C0 <- unname(t(chol(vcov(reslm))))
# Specify details for ADADELTA (Stochastic Gradient-Descent)
SGD_iters <- 50
epsil <- 10^-5
rho <- 0.9
# -----------------------------
# Main
# -----------------------------
result <-compute_GVA(mu0, C0, h, delthh, delth_logpi, z, lam0,
rho, epsil, a, SGD_iters, AEL_iters)
Run the code above in your browser using DataLab