User Documentation for MADlib
Cross Validation

Estimates the fit of a predictive model given a data set and specifications for the training, prediction, and error estimation functions.

Cross validation, sometimes called rotation estimation, is a technique for assessing how the results of a statistical analysis will generalize to an independent data set. It is mainly used in settings where the goal is prediction, and you want to estimate how accurately a predictive model will perform in practice.

The cross-validation function provided by this module is very flexible and can work with algorithms you want to cross validate, including algorithms you write yourself. Among the inputs to the cross-validation function are specifications of the modelling, prediction, and error metric functions. These three-part specifications include the name of the function, an array of arguments to pass to the function, and an array of the data types of the arguments. This makes it possible to use functions from other MADlib modules or user-defined functions that you supply.

Other inputs include the output table name, k value for the k-fold cross validation, and how many folds to try. For example, you can choose to run a simple validation instead of a full cross validation.

Cross-Validation Function
cross_validation_general( modelling_func,


VARCHAR. The name of the function that trains the model.
VARCHAR[]. An array of parameters to supply to the modelling function.
VARCHAR[]. An array of data type names for each of the parameters supplied to the modelling function.
VARCHAR. The name of the parameter that will be checked to find the optimum value. The name must appear in the modelling_params array.
VARCHAR. The name of the parameter whose values are to be studied.
VARCHAR. The name of the prediction function.
VARCHAR[]. An array of parameters to supply to the prediction function.
VARCHAR[]. An array of data type names for each of the parameters supplied to the prediction function.
VARCHAR. The name of the function for measuring errors.
VARCHAR[]. An array of parameters to supply to the error metric function.
VARCHAR[]. An array of data type names for each of the parameters supplied to the metric function.
VARCHAR. The name of the data table that will be split into training and validation parts.

VARCHAR. The name of the column containing a unique ID associated with each row, or NULL if the table has no such column.

Ideally, the data set has a unique ID for each row, so that it is easier to partition the data set into the training part and the validation part. Set the id_is_random argument to inform the cross-validation function whether the ID value is randomly assigned to each row. If it is not randomly assigned, the cross-validation function generates a random ID for each row.

BOOLEAN. TRUE if the provided ID is randomly assigned to each row.
VARCHAR. The name of the table to store the output of the cross-validation function. The output table has the following columns:
param_explored The name of the parameter checked to find the optimum value. This is the same name specified in the param_explored argument of the cross_validation_general() function.
average error The average of the errors computed by the error metric function.
standard deviation of error The standard deviation of the errors.

A comma-separated list of names of data columns to use in the calculation. When its value is NULL, the function will automatically figure out all the column names of the data table. This is only used if the data_id argument is NULL, otherwise it is ignored.

If the data set has no unique ID for each row, the cross-validation function copies the data set to a temporary table with a randomly assigned ID column. Setting this argument to the list of independent and dependent variables that are to be used in the calculation minimizes the copying workload by only copying the required data.

The new temporary table is dropped after the computation has finished.

INTEGER, default: 10. Value of k. How many folds validation? Each validation uses 1/fold_num fraction of the data for validation.

The parameter arrays for the modelling, prediction and metric functions can include the following special keywords:

Note: If the argument explore_values is NULL or has zero length, then the cross-validation function will only run a data folding.


This example uses cross validation with an elastic net regression to find the best value of the regularization parameter.

  1. Populate the table cvtest with 101 dimensional independent variables val, and dependent variable dep.
  2. Run the general cross-validation function.
    SELECT madlib.cross_validation_general
        ( 'madlib.elastic_net_train',
            '{%data%, %model%, dep, indep, gaussian, 1, lambda, TRUE, NULL, fista,
              "{eta = 2, max_stepsize = 2, use_active_set = t}",
              NULL, 2000, 1e-6}'::varchar[],
            '{varchar, varchar, varchar, varchar, varchar, double precision,
              double precision, boolean, varchar, varchar, varchar, varchar,
              integer, double precision}'::varchar[],
          '{0.02, 0.04, 0.06, 0.08, 0.10, 0.12, 0.14, 0.16, 0.18, 0.20,
            0.22, 0.24, 0.26, 0.28, 0.30, 0.32, 0.34, 0.36}'::varchar[],
            '{%model%, %data%, %id%, %prediction%}'::varchar[],
            '{text, text, text, text}'::varchar[],
            '{%prediction%, %data%, %id%, dep, %error%}'::varchar[],
            '{varchar, varchar, varchar, varchar, varchar}'::varchar[],
          '{indep, dep}'::varchar[],


max_locks_per_transaction, which usually is set to the default value of 64, limits the number of tables that can be dropped inside a single transaction (the cross-validation function). Thus, the number of different values of param_explored (or the length of the explored_values array) cannot be too large. For 10-fold cross validation, the limit of length(explored_values) is around 40. If the limit is exceeded, you may get an "out of shared memory" error because max_locks_per_transaction is exceeded.

One way to overcome this limitation is to run the cross-validation function multiple times, with each run covering a different region of values of the parameter.

In the future, MADlib may implement cross-validation functions for each individual applicable module, where it is possible to optimize the calculation to avoid dropping tables and prevent exceeding the max_locks_per_transaction limitation. Since module-specific cross-validation functions depend upon the implementation details of the modules to perform the optimization, they will not be as flexible as the generalized cross-validation function provided here.

Technical Background

One round of cross validation involves partitioning a sample of data into complementary subsets, performing the analysis on one subset (called the training set), and validating the analysis on the other subset (called the validation set or testing set). To reduce variability, multiple rounds of cross validation are performed using different partitions, and the validation results are averaged over the rounds.

In k-fold cross validation, the original sample is randomly partitioned into k equal sized subsamples. Of the k subsamples, a single subsample is retained as the validation data for testing the model, and the remaining k−1 subsamples are used as training data. The cross-validation process is repeated k times (the folds), with each of the k subsamples used exactly once as the validation data. The k results from the folds can be averaged (or otherwise combined) to produce a single estimation. The advantage of this method over repeated random sub-sampling is that all observations are used for both training and validation, and each observation is used for validation exactly once. 10-fold cross validation is commonly used, but in general k remains an unfixed parameter.

Related Topics

File cross_validation.sql_in documenting the SQL functions.