grad_full_vec: Gradient of log_dhurdle1d_abk() (with scalar v and y) with...

Description Usage Arguments Details Examples

View source: R/zero_est_core.R

Description

Gradient of log_dhurdle1d_abk() (with scalar v and y) with respect to a, b, k.

Usage

1
grad_full_vec(V, Y, A, B, k11, Vo, Yo, minus_Y = TRUE)

Arguments

V

A logical vector, indicating if each entry in Y is non-zero, i.e. V = (Y != 0).

Y

A numerical vector of i.i.d. 1-d Hurdle random variables.

A

A number or a vector of the same length Y, the a parameter(s). Assumed to be results from sum_A_mat() run on Vo and Yo.

B

A number or a vector of the same length Y, the b parameter(s). Assumed to be results from sum_B_mat() run on Vo and Yo.

k11

A number, the k parameter.

Vo

A numerical vector of the same dimension as Yo indicating if each entry in Yo is non-zero, i.e. Vo = (Yo != 0).

Yo

A numerical vector, a sample for the parent nodes (regressors).

minus_Y

A logical, argument as in sum_B_mat().

Details

The derivative of log_dhurdle_vec_abk(V[,1], Y[,1], A, B, k) with respect to aa, bb and k, where A=sum_A_mat(aa, V[,2:p], Y[,2:p]) and B=sum_B_mat(bb, V[,2:p], Y[,2:p], minus_Y).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
set.seed(1)
n <- 100; p <- 10
V <- matrix(rbinom(n*p, 1, 0.8), nrow=n, ncol=p)
Y <- matrix(rnorm(n*p) * V, nrow=n, ncol=p)
aa <- rnorm(2*p-1)
bb <- rnorm(2*p-1)
k <- abs(rnorm(1))
grad_vec <- grad_full_vec(V[,1], Y[,1], sum_A_mat(aa, V[,2:p], Y[,2:p]),
   sum_B_mat(bb, V[,2:p], Y[,2:p], minus_Y=TRUE), k, V[,2:p], Y[,2:p], minus_Y=TRUE)
numer_grad <- numDeriv::grad(function(x){
   log_dhurdle_vec_abk(V[,1], Y[,1], sum_A_mat(x[1:(2*p-1)], V[,2:p], Y[,2:p]),
      sum_B_mat(x[(2*p):(4*p-2)], V[,2:p], Y[,2:p], minus_Y=TRUE), x[4*p-1])}, c(aa, bb, k))
max(abs(grad_vec - numer_grad))

grad_vec <- grad_full_vec(V[,1], Y[,1], sum_A_mat(aa, V[,2:p], Y[,2:p]),
   sum_B_mat(bb, V[,2:p], Y[,2:p], minus_Y=FALSE), k, V[,2:p], Y[,2:p], minus_Y=FALSE)
numer_grad <- numDeriv::grad(function(x){
   log_dhurdle_vec_abk(V[,1], Y[,1], sum_A_mat(x[1:(2*p-1)], V[,2:p], Y[,2:p]),
      sum_B_mat(x[(2*p):(4*p-2)], V[,2:p], Y[,2:p], minus_Y=FALSE), x[4*p-1])}, c(aa, bb, k))
max(abs(grad_vec - numer_grad))

sqyu/ZiDAG documentation built on Jan. 19, 2021, 4:11 p.m.