
Binary log loss (Geron Ch 4, morie.fn hmlogcl)
Source:R/geron_ml2_native.R
morie_geron_logistic_cost.RdComputed from the LOGITS as -y z + max(z, 0) + log1p(exp(-|z|)),
which is stable where -log(p) is not. Probabilities are delegated
to morie_geron_logistic_probability, as hmlogcl delegates to
hmlogp. The class-rate baseline is the bar to beat.
Examples
set.seed(4)
X <- cbind(1, rnorm(30))
y <- rbinom(30, 1, plogis(X %*% c(-0.5, 1.5)))
morie_geron_logistic_cost(X, y, theta = c(-0.5, 1.5))
#> $cost
#> [1] 0.7137735
#>
#> $per_instance
#> [1] 0.7843985 0.2380819 1.1965780 0.9094101 0.1325949 0.4614081 0.0850353
#> [8] 0.3650294 0.0915444 2.2738946 0.5334207 0.9594654 0.6565494 0.4490509
#> [15] 0.9423163 0.8239508 0.2524774 0.4495575 0.4198658 0.3339442 1.9626158
#> [22] 0.8272030 0.2085606 1.6464791 0.9066501 0.3341576 0.2236377 1.2162273
#> [29] 0.1404257 1.5886739
#>
#> $p_hat
#> [1] 0.45639413 0.21186187 0.69777332 0.59723824 0.87581984 0.63039533
#> [7] 0.08152013 0.30582378 0.91252080 0.89708940 0.58659497 0.38309765
#> [13] 0.51863786 0.36176640 0.38972409 0.43869504 0.77687378 0.36208963
#> [19] 0.34286501 0.28390628 0.85950956 0.43727064 0.81175183 0.80727270
#> [25] 0.59612511 0.28405906 0.79960478 0.70365393 0.13101180 0.79580378
#>
#> $logits
#> [1] -0.17486771 -1.31373886 0.83671697 0.39397087 1.95342700 0.53391316
#> [7] -2.42186995 -0.81971678 2.34480981 2.16529482 0.34990675 -0.47642082
#> [13] 0.07458601 -0.56770567 -0.44847214 -0.24645984 1.24754026 -0.56630600
#> [19] -0.65055266 -0.92516685 1.81122247 -0.25224647 1.46143354 1.43238532
#> [25] 0.38934541 -0.92441553 1.38382604 0.86475873 -1.89204216 1.36027126
#>
#> $baseline_cost
#> [1] 0.6842318
#>
#> $estimate
#> [1] 0.7137735
#>
#> $n
#> [1] 30
#>
#> $method
#> [1] "Binary log loss (cross-entropy)"
#>