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

Description Usage Arguments Details Value 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_a_b_k(V, Y, A, B, k)

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).

B

A number or a vector of the same length Y, the b parameter(s).

k

A number, the k parameter.

Details

The i-th row of the returned matrix is the gradient of log_dhurdle1d_abk(V[i], Y[i], A[i], B[i], k) with respect to A[i], B[i], k if A and B are vectors, or with respect to A and B if they are scalars.

Value

A matrix with 3 columns and number of rows equal to the length of Y.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (!requireNamespace("numDeriv", quietly = TRUE))
   stop("Please install package \"numDeriv\".")
set.seed(1)
n <- 100
V <- rbinom(n, 1, 0.8)
Y <- rnorm(n) * V
A <- rnorm(n)
B <- rnorm(n)
k <- abs(rnorm(1))
grad_mat <- grad_a_b_k(V, Y, A, B, k)
numer_grad <- t(sapply(1:n,
   function(i){numDeriv::grad(function(x){
      log_dhurdle1d_abk(V[i], Y[i], x[1], x[2], x[3])}, c(A[i], B[i], k))}))
max(abs(grad_mat - numer_grad))

a <- rnorm(1)
b <- rnorm(1)
grad_mat <- grad_a_b_k(V, Y, a, b, k)
numer_grad <- t(sapply(1:n,
   function(i){numDeriv::grad(function(x){
      log_dhurdle1d_abk(V[i], Y[i], x[1], x[2], x[3])}, c(a, b, k))}))
max(abs(grad_mat - numer_grad))

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