% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/conformal.R
\name{cross_conformal}
\alias{cross_conformal}
\title{Cross-conformal prediction intervals (pooled K-fold scores)}
\usage{
cross_conformal(
  F_hat,
  W,
  W_new,
  K,
  alpha = 0.05,
  n_folds = 5L,
  wrapper = NULL,
  grid_weights = NULL,
  dfspa_args = list(),
  weight_model_args = list(),
  seed = NULL
)
}
\arguments{
\item{F_hat}{An \code{m}-by-\code{G_grid} numeric matrix of study-level function
evaluations.}

\item{W}{An \code{m}-by-\code{p} matrix or data frame of study-level covariates.}

\item{W_new}{A matrix or data frame of new target covariates. Must
contain columns matching \code{W}.}

\item{K}{Integer number of basis functions.}

\item{alpha}{Miscoverage level; interval has nominal coverage
\eqn{1-\alpha}. Default \code{0.05}.}

\item{n_folds}{Integer number of folds (>= 2). Default \code{5}.}

\item{wrapper}{Optional reduction function (see \code{\link[=apply_wrapper]{apply_wrapper()}}). If
\code{NULL}, intervals are constructed pointwise at every grid point.}

\item{grid_weights}{Optional length-\code{G_grid} non-negative numeric vector
used for the \eqn{L^2(\mu)} norm and for the default wrapper.}

\item{dfspa_args, weight_model_args}{Named lists passed to \code{\link[=dfspa]{dfspa()}} and
\code{\link[=fit_weight_model]{fit_weight_model()}} respectively.}

\item{seed}{Optional integer seed for reproducible train/calibration
splits.}
}
\value{
An object of class \code{"metahunt_conformal"} (see
\code{\link[=split_conformal]{split_conformal()}} for fields). \code{method} is \code{"cross"} and \code{n_cal} is
the number of pooled scores.
}
\description{
Computes K-fold split conformal intervals in which the calibration scores
are pooled across folds, while point predictions for new targets are
produced from a final pipeline fit on all studies. Equivalent to running
\code{\link[=split_conformal]{split_conformal()}} \code{n_folds} times with different calibration sets and
pooling all conformity scores into a single empirical distribution.
}
\details{
For each fold, the MetaHunt pipeline is fit on the out-of-fold studies,
and conformity scores are computed on the in-fold studies. After all
folds complete, the pooled scores yield a single
\eqn{(1-\alpha)}-quantile (or one per grid point). Point predictions for
\code{W_new} use a pipeline refit on the full dataset. The interval at grid
point \code{g} for target \code{j} is
\eqn{[\tilde f^{(j)}(x_g) - q_g, \tilde f^{(j)}(x_g) + q_g]} (or the
scalar analogue when \code{wrapper} is supplied).

This differs from Vovk's original cross-conformal predictor for
classification. For regression, pooling scores across folds is a
common practical extension of split conformal and reduces the variance
due to the single calibration split. Exact finite-sample coverage is
not guaranteed; see Barber et al. (2021, Jackknife+) for more
conservative alternatives.
}
\examples{
\donttest{
set.seed(1)
G <- 30; m <- 60; K_true <- 3
x <- seq(0, 1, length.out = G)
basis <- rbind(sin(pi * x), cos(pi * x), x)
W <- data.frame(w1 = rnorm(m))
eta <- cbind(0.8 * W$w1, -0.3 * W$w1, rep(0, m))
pi_true <- exp(eta) / rowSums(exp(eta))
F_hat <- pi_true \%*\% basis + matrix(rnorm(m * G, sd = 0.05), m, G)
W_new <- data.frame(w1 = c(0, 1))

res <- cross_conformal(F_hat, W, W_new, K = 3, n_folds = 4,
                       dfspa_args = list(denoise = FALSE), seed = 1)
res
}

}
