Learn R Programming

BKP (version 0.2.3)

loss_fun: Loss Function for BKP and DKP Models

Description

Computes the loss for fitting BKP (binary) or DKP (multi-class) models. Supports Brier score (mean squared error) and log-loss (cross-entropy) under different prior specifications.

Usage

loss_fun(
  gamma,
  Xnorm,
  y = NULL,
  m = NULL,
  Y = NULL,
  model = c("BKP", "DKP"),
  prior = c("noninformative", "fixed", "adaptive"),
  r0 = 2,
  p0 = NULL,
  loss = c("brier", "log_loss"),
  kernel = c("gaussian", "matern52", "matern32")
)

Value

A single numeric value representing the total loss (to be minimized). The value corresponds to either the Brier score (squared error) or the log-loss (cross-entropy).

Arguments

gamma

A numeric vector of log10-transformed kernel hyperparameters.

Xnorm

A numeric matrix of normalized input features ([0,1]^d).

y

A numeric vector of observed successes (length n).

m

A numeric vector of total binomial trials (length n), corresponding to each y.

Y

A numeric matrix of observed class counts (n × q), required only when model = "DKP", where n is the number of observations and q the number of classes.

model

A character string specifying the model type: "BKP" (binary outcome) or "DKP" (multi-class outcome).

prior

Type of prior: "noninformative" (default), "fixed", or "adaptive".

r0

Global prior precision (used when prior = "fixed" or "adaptive").

p0

For BKP, a scalar in (0,1) specifying the prior mean of success probability when prior = "fixed". For DKP, a numeric vector of length equal to the number of classes specifying the global prior mean, which must sum to 1.

loss

Loss function for kernel hyperparameter tuning: "brier" (default) or "log_loss".

kernel

Kernel function for local weighting: "gaussian" (default), "matern52", or "matern32".

References

Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arXiv.2508.10447.

See Also

fit_BKP for fitting BKP models, fit_DKP for fitting DKP models, get_prior for constructing prior parameters, kernel_matrix for computing kernel matrices.

Examples

Run this code
# -------------------------- BKP ---------------------------
set.seed(123)
n <- 10
Xnorm <- matrix(runif(2 * n), ncol = 2)
m <- rep(10, n)
y <- rbinom(n, size = m, prob = runif(n))
loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, y = y, m = m, model = "BKP")

# -------------------------- DKP ---------------------------
set.seed(123)
n <- 10
q <- 3
Xnorm <- matrix(runif(2 * n), ncol = 2)
Y <- matrix(rmultinom(n, size = 10, prob = rep(1/q, q)), nrow = n, byrow = TRUE)
loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, Y = Y, model = "DKP")

Run the code above in your browser using DataLab