
Logistic gradient and Hessian (Geron Ch 4, morie.fn hmlogg)
Source:R/geron_ml2_native.R
morie_geron_logistic_gradient.Rd(1/m) X^T (p_hat - y) with the PSD Hessian (1/m) X^T diag(p(1-p)) X, which is what makes the cost convex.
Examples
set.seed(4)
X <- cbind(1, rnorm(30))
y <- rbinom(30, 1, plogis(X %*% c(-0.5, 1.5)))
morie_geron_logistic_gradient(X, y, theta = c(0, 0))
#> $gradient
#> [1] 0.06666667 -0.07309471
#>
#> $hessian
#> [,1] [,2]
#> [1,] 0.2500000 0.1217968
#> [2,] 0.1217968 0.2169862
#>
#> $p_hat
#> [1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
#> [20] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
#>
#> $residuals
#> [1] -0.5 0.5 0.5 0.5 -0.5 -0.5 0.5 0.5 -0.5 0.5 -0.5 -0.5 -0.5 0.5 -0.5
#> [16] -0.5 -0.5 0.5 0.5 0.5 0.5 -0.5 -0.5 0.5 0.5 0.5 -0.5 0.5 0.5 0.5
#>
#> $estimate
#> [1] 0.09893069
#>
#> $n
#> [1] 30
#>
#> $method
#> [1] "Logistic regression cost gradient"
#>