% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/reconc_t.R
\name{multi_log_score_optimization}
\alias{multi_log_score_optimization}
\title{Optimize degrees of freedom (nu) via LOO Cross-Validation}
\usage{
multi_log_score_optimization(res, prior_mean, trim = 0.1)
}
\arguments{
\item{res}{Matrix of residuals (n_obs x n_var).}

\item{prior_mean}{The prior mean covariance matrix (n_var x n_var).}

\item{trim}{Fraction of observations to trim (0 to 1). Default is 0.1 (10\%).}
}
\value{
A list containing the optimization results:
\itemize{
\item \code{optimal_nu}: The optimal degrees of freedom found.
\item \code{min_neg_log_score}: The minimum negative log score achieved.
\item \code{convergence}: The convergence status of the optimizer.
\item \code{time_elapsed}: The time taken for the optimization.
}
}
\description{
Optimize degrees of freedom (nu) via LOO Cross-Validation
}
\details{
TODO: check these details

\strong{Leave-One-Out (LOO) Cross-Validation:}
This function estimates the optimal degrees of freedom \eqn{\nu} by maximizing the
out-of-sample predictive performance. This is achieved by computing the
log-density of each held-out observation \eqn{\mathbf{r}_i} given the remaining
data \eqn{\mathbf{R}_{-i}}. The total objective function is the sum of these
predictive log-densities:
\deqn{\mathcal{L}(\nu) = \sum_{i=1}^T \log f(\mathbf{r}_i | \mathbf{R}_{-i}, \nu)}

\strong{The Log-Density Function:}
For each LOO step, the residuals are assumed to follow a
multivariate t-distribution. The density is expressed directly as a function of the
posterior sum-of-squares matrix \eqn{\Psi}, where \eqn{\Psi} scales implicitly with \eqn{\nu}:
\deqn{f(\mathbf{r}_i | \Psi, \nu) = \frac{\Gamma(\frac{\nu + T}{2})}{\Gamma(\frac{\nu + T - p}{2}) \pi^{p/2}} |\Psi|^{-1/2} \left( 1 + \mathbf{r}_i^\top \Psi^{-1} \mathbf{r}_i \right)^{-\frac{\nu + T}{2}}}
In the code, \eqn{\Psi} is constructed as:
\deqn{\Psi = (\nu - p - 1)\bar{\Sigma}_{prior} + \mathbf{R}^\top\mathbf{R}}
By using this formulation, the standard scaling factors \eqn{1/\nu} and \eqn{\nu^{-p/2}}
are absorbed into the matrix inverse and determinant, respectively.

\strong{Efficient Computation via Sherman-Morrison:}
Rather than recomputing \eqn{\Psi_{-i}} and its inverse \eqn{T} times, the function uses
the full-sample matrix \eqn{\Psi} and adjusts it using the leverage
\eqn{h_i = \mathbf{r}_i^\top \Psi^{-1} \mathbf{r}_i}.

Through the Matrix Determinant Lemma and the Sherman-Morrison formula, the
internal term \eqn{(1 + \mathbf{r}_i^\top \Psi_{-i}^{-1} \mathbf{r}_i)} simplifies to \eqn{(1 - h_i)^{-1}}.
The final log-density contribution used in the code is:
\deqn{\log f_i \propto \textrm{const} - \frac{1}{2}\log|\Psi| + \frac{\nu + T - 1}{2} \log(1 - h_i)}
}
