You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: CBIG_KRR_innerloop_cv.m
    path of file: ./repos/CBIG/utilities/matlab/predictive_models/KernelRidgeRegression
    
    the code of the file until where you have to start completion: function [y_p, y_t, acc, loss] = CBIG_KRR_innerloop_cv( bin_flag, num_inner_folds, ...
    kernel, y_resid, y_orig, num_valid_sub, with_bias, lambda, threshold, metric )

% [y_p, y_t, acc, acc_concat] = CBIG_KRR_innerloop_cv( bin_flag, num_inner_folds, ...
%     kernel, y_resid, y_orig, num_valid_sub, lambda, threshold )
%
% This function performs innner-loop cross-validation of kernel ridge
% regression with only one hyperparamter combination (kernel type and
% scale, regularization, threshold for binary cases (if necessary, e.g.
% sex)).
%
% Inputs:
%   - bin_flag
%     0 or 1. 1 means the target variables to be predicted (i.e. y_orig)
%     are binary. 0 means none of them are binary.
%     If the target variables consist of both binary and non-binary
%     variables, you need to call this function twice and predict them
%     separately with different "bin_flag".
%

    
function [y_p, y_t, acc, loss] = CBIG_KRR_innerloop_cv( bin_flag, num_inner_folds, ...
    kernel, y_resid, y_orig, num_valid_sub, with_bias, lambda, threshold, metric )

% [y_p, y_t, acc, acc_concat] = CBIG_KRR_innerloop_cv( bin_flag, num_inner_folds, ...
%     kernel, y_resid, y_orig, num_valid_sub, lambda, threshold )
%
% This function performs innner-loop cross-validation of kernel ridge
% regression with only one hyperparamter combination (kernel type and
% scale, regularization, threshold for binary cases (if necessary, e.g.
% sex)).
%
% Inputs:
%   - bin_flag
%     0 or 1. 1 means the target variables to be predicted (i.e. y_orig)
%     are binary. 0 means none of them are binary.
%     If the target variables consist of both binary and non-binary
%     variables, you need to call this function twice and predict them
%     separately with different "bin_flag".
%
%   - num_inner_folds
%     A scalar, the number of inner-loop cross-validation folds for
%     hyperparameter selection.
%     If there are enough data so that cross-validation is not needed, set
%     num_inner_folds = 1.
%
%   - kernel
%     An S x S kernel matrix.
%     For cross-validation case, S is the number of training subjects. The
%     ordering of training subjects needs to be matched with the ordering
%     of the target variable y.
%     If data were split into training, validation, and test sets, then S
%     is the number of training subject + the number of validation
%     subjects. The first S1 subjects of S will be the training subjects
%     and the last S2 = S - S1 subjects will be the validation subjects
%
%   - y_resid
%     An S x #VariableToPredict matrix of the target variable y after
%     regressing out nuisance regressors.
%     If cross-validation is not used, the first S1 rows of y_resid will
%     correspond to the training subjects and the last S2 = S - S1 rows of
%     y_resid will correspond to the validation subjects.
%
%   - y_orig (optional)
%     An S x #VariableToPredict vector of the target variable y before
%     regression of covariates. This vector is needed for binary (0 or 1) case (e.g. sex)
%     for accuracy calculation.
%     If y is not a binary variable, specify y_orig = [].
%     If cross-validation is not used, the first S1 rows of y_orig will
%     correspond to the training subjects and the last S2 = S - S1 rows of
%     y_orig will correspond to the validation subjects.
%
%   - num_valid_sub (optional)
%     A scalar, the number of validation subjects for non-cross-validation
%     case. It is assumed that the last "num_valid_sub" subjects in
%     "kernel", "y_resid" and "y_orig" are the validation subjects.
%     For cross-validation case, this argument is not needed, set
%     num_valid_sub = [].
%
%   - with_bias
%     A scalar (choose from 0 or 1).
%     - with_bias = 0 means the algorithm is to minimize
%     (y - K*alpha)^2 + (regularization of alpha);
%     - with_bias = 1 means the algorithm is to minimize
%     (y - K*alpha - beta)^2 + (regularization of alpha), where beta is a
%     constant bias for every subject, estimated from the data.
%
%   - lambda
%     A scalar, the l2-regularization parameter.
%
%   - threshold (optional)
%     A scalar, it is a threshold required to determine the prediction (1
%     or 0) for binary target variables (e.g. sex). Ignore this parameter
%     if y is not binary.
%
%   - metric (optional)
%     A string indicating the metric used to define prediction loss. The
%     loss is used to choose hyperparameters.
%     Choose from:
%       'corr'              - Pearson's correlation;
%       'COD'               - Coefficient of determination. Defined as
%                             1-||y_pred-y_test||^2/||mean(y_test)-y_test||^2,
%                             where y_pred is the prediction of the test data, 
%                             y_test is the groud truth of the test data, 
%                             and mean(y_test) is the mean of test data
%       'predictive_COD'    - Predictive coefficient of determination. Defined as
%                             1-||y_pred-y_test||^2/||mean(y_train)-y_test||^2,
%                             where y_pred is the prediction of the test data, 
%                             y_test is the groud truth of the test data, 
%                             and mean(y_train) is the mean of training data
%       'MAE'               - mean absolute error
%       'MAE_norm'          - mean absolute error divided by the standard
%                             derivation of the target variable of the training set
%       'MSE'               - mean squared error
%       'MSE_norm'          - mean squared error divided by the variance
%                             of the target variable of the training set
% Outputs:
%   - y_p
%     A cell array of length #TargetVariables. Each cell array contains the
%     predicted target values of all training subjects (using the
%     inner-loop CV manner) or validation subjects (using the
%     training-validation-test manner). Within each cell, the predicted
%     values follow the same ordering of y_resid.
%     For cross-validation case, each cell array is an S x 1 vector.
%     For training-validation-test case, each cell array is an S2 x 1 vector.
%
%   - y_t
%     A cell array of length #TargetVariables. Each cell array contains the
%     groud truth (actual) target values of all training subjects (using
%     the inner-loop CV manner) or validation subject (using
%     training-validation-test manner). Within each cell, the ground-truth
%     values follow the same ordering of y_resid.
%     If y is binary (e.g. sex), y_t will be the original y before
%     regression of nuisace covariates. If y is not binary, y_t will be the
%     y after regression of nuisance covariates.
%     For cross-validation case, each cell array is an S x size(y_resid, 2)
%     matrix.
%     For training-valdation-test case, each cell array is an S2 x
%     size(y_resid, 2) matrix.
%
%   - acc
%     A 1 x size(y_resid,2) vector, the accuracy of the prediction of each
%     target variable, averaged across all inner-loop folds.
%     For binary case, acc are the average of (true positive & negative) /
%     #subjects; for continuous variables, acc measures how correlated the
%     predicted and actual values are.
%
%   - acc_concat
%     A 1 x size(y_resid,2) vector, the accuracy of each variable. the
%     accuracy is computed based on the concatenated predicted scores of
%     all inner-loop folds.
%
% Written by CBIG under MIT license: https://github.com/ThomasYeoLab/CBIG/blob/master/LICENSE.md
% Author: Jingwei Li and Ru(by) Kong

%% setting up
num_sub = size(kernel, 1);

if(bin_flag==1 && (~exist('y_orig', 'var') || isempty(y_orig)) )
    error('"y_orig" variable is needed when y is binary (e.g. sex)')
end