User Documentation
 All Files Functions Groups
Elastic Net Regularization
+ Collaboration diagram for Elastic Net Regularization:

About:

This module implements elastic net regularization for linear and logistic regression problems.

Online Help

View short help messages using the following statements:

-- Summary of Elastic Net Regularization
madlib.elastic_net_train()

-- Training function syntax and output table format
madlib.elastic_net_train('usage')

-- Prediction function syntax
madlib.elastic_net_train('predict')

-- Syntax for gaussian/linear model
madlib.elastic_net_train('gaussian')
madlib.elastic_net_train('linear')

-- Syntax for binomial/logistic model
madlib.elastic_net_train('binomial')
madlib.elastic_net_train('logistic')

-- Parameter formats for optimizers
madlib.elastic_net_train('fista')
madlib.elastic_net_train('igd')

Training Function
The training function has the following format:
madlib.elastic_net_train(
    tbl_source, tbl_result, col_dep_var, col_ind_var, 
    regress_family, alpha, lambda_value, standardize, 
    grouping_col, optimizer := NULL,  
    optimizer_params := NULL, excluded := NULL, 
    max_iter := 10000, tolerance := 1e-6)
Note
It is strongly recommended that you run elastic_net_train() on a subset of the data with a limited max_iter before applying it to the full data set with a large max_iter. In the pre-run, you can adjust the parameters to get the best performance and then apply the best set of parameters to the whole data set.
tbl_source

Text value. The name of the table containing the training data.

tbl_result

Text value. Name of the generated table containing the output model.

col_dep_var

Text value. An expression for the dependent variable. Both col_dep_var and col_ind_var can be valid Postgres expressions. For example, col_dep_var = 'log(y+1)', and col_ind_var = 'array[exp(x[1]), x[2], 1/(1+x[3])]'. In the binomial case, you can use a Boolean expression, for example, col_dep_var = 'y < 0'.

col_ind_var

Text value. An expression for the independent variables. Use '*' to specify all columns of tbl_source except those listed in the excluded string. If col_dep_var is a column name, it is automatically excluded from the independent variables. However, if col_dep_var is a valid Postgres expression, any column names used within the expression are only excluded if they are explicitly included in the excluded argument. It is a good idea to add all column names involved in the dependent variable expression to the excluded string.

regress_family

Text value. The regression type, either 'gaussian' ('linear') or 'binomial' ('logistic').

alpha

Float8 value. Elastic net control parameter, value in [0, 1].

lambda_value

Float8 value. Regularization parameter, positive.

standardize

Boolean value. Whether to normalize the data. Setting this to True usually yields better results and faster convergence. Default: True.

grouping_col

Text value. Not currently implemented. Any non-NULL value is ignored. An expression list used to group the input dataset into discrete groups, running one regression per group. Similar to the SQL GROUP BY clause. When this value is null, no grouping is used and a single result model is generated. Default value: NULL.

optimizer

Text value. Name of optimizer, either 'fista' or 'igd'. Default: 'fista'.

optimizer_params

Text value. Optimizer parameters, delimited with commas. The parameters differ depending on the value of optimizer. See the descriptions below for details. Default: NULL.

excluded

Text value. A comma-delimited list of column names excluded from features. For example, 'col1, col2'. If the col_ind_var is an array, excluded is a list of the integer array positions to exclude, for example '1,2'. If this argument is NULL or an empty string '', no columns are excluded.

max_iter

Integer value. The maximum number of iterations that are allowed. Default: 10000.

tolerance
Float value. The criteria to end iterations. Both the 'fista' and 'igd' optimizers compute the average difference between the coefficients of two consecutive iterations, and when the difference is smaller than tolerance or the iteration number is larger than max_iter, the computation stops. The default is 1e-6.

Optimizer Parameters
Optimizer parameters are supplied in a string containing a comma-delimited list of name-value pairs. All of these named parameters are optional, and their order does not matter. You must use the format "<param_name> = <value>" to specify the value of a parameter, otherwise the parameter is ignored.

When the elastic_net_train() optimizer argument value is 'fista', the optimizer_params argument has the following format:

  'max_stepsize = ..., eta = ..., warmup = ..., warmup_lambdas = ..., 
   warmup_lambda_no = ..., warmup_tolerance = ..., use_active_set = ..., 
   activeset_tolerance = ..., random_stepsize = ...'
max_stepsize
Initial backtracking step size. At each iteration, the algorithm first tries stepsize = max_stepsize, and if it does not work out, it then tries a smaller step size, stepsize = stepsize/eta, where eta must be larger than 1. At first glance, this seems to perform repeated iterations for even one step, but using a larger step size actually greatly increases the computation speed and minimizes the total number of iterations. A careful choice of max_stepsize can decrease the computation time by more than 10 times. The default is 4.0.
eta

If stepsize does not work stepsize / eta is tried. Must be greater than 1. The default is 2.

warmup

If warmup is True, a series of lambda values, which is strictly descent and ends at the lambda value that the user wants to calculate, is used. The larger lambda gives very sparse solution, and the sparse solution again is used as the initial guess for the next lambda's solution, which speeds up the computation for the next lambda. For larger data sets, this can sometimes accelerate the whole computation and may be faster than computation on only one lambda value. The default is False.

warmup_lambdas

The lambda value series to use when warmup is True. The default is NULL, which means that lambda values will be automatically generated.

warmup_lambda_no

How many lambdas are used in warm-up. If warmup_lambdas is not NULL, this value is overridden by the number of provided lambda values. The default is 15.

warmup_tolerance

The value of tolerance used during warmup. The default is the same as the tolerance argument.

use_active_set

If use_active_set is True, an active-set method is used to speed up the computation. Considerable speedup is obtained by organizing the iterations around the active set of features—those with nonzero coefficients. After a complete cycle through all the variables, we iterate on only the active set until convergence. If another complete cycle does not change the active set, we are done, otherwise the process is repeated. The default is False.

activeset_tolerance

The value of tolerance used during active set calculation. The default is the same as tolerance.

random_stepsize
Whether to add some randomness to the step size. Sometimes, this can speed up the calculation. The default is False.

When the elastic_net_train() optimizer argument value is 'igd', the optimizer_params argument has the following format:

'stepsize = ..., step_decay = ..., threshold = ..., warmup = ..., 
 warmup_lambdas = ..., warmup_lambda_no = ..., warmup_tolerance = ..., 
 parallel = ...' 
stepsize
The default is 0.01.
step_decay
The actual setpsize used for current step is (previous stepsize) / exp(setp_decay). The default value is 0, which means that a constant stepsize is used in IGD.
threshold
When a coefficient is really small, set this coefficient to be 0. The default is 1e-10. Due to the stochastic nature of SGD, we can only obtain very small values for the fitting coefficients. Therefore, threshold is needed at the end of the computation to screen out tiny values and hard-set them to zeros. This is accomplished as follows: (1) multiply each coefficient with the standard deviation of the corresponding feature; (2) compute the average of absolute values of re-scaled coefficients; (3) divide each rescaled coefficient with the average, and if the resulting absolute value is smaller than threshold, set the original coefficient to zero.
warmup
If warmup is True, a series of lambda values, which is strictly descent and ends at the lambda value that the user wants to calculate, is used. The larger lambda gives very sparse solution, and the sparse solution again is used as the initial guess for the next lambda's solution, which speeds up the computation for the next lambda. For larger data sets, this can sometimes accelerate the whole computation and may be faster than computation on only one lambda value. The default is False.
warmup_lambdas
An array of lambda values to use for warmup. The default is Null.
warmup_lambda_no
The number of lambdas used in warm-up. The default is 15. If warmup_lambdas is not NULL, this argument is overridden by the size of the warmup_lambdas array.
warmup_tolerance
The value of tolerance used during warmup.The default is the same as tolerance.
parallel
Whether to run the computation on multiple segments. The default is True. SGD is a sequential algorithm in nature. When running in a distributed manner, each segment of the data runs its own SGD model and then the models are averaged to get a model for each iteration. This averaging might slow down the convergence speed, although we also acquire the ability to process large datasets on multiple machines. This algorithm, therefore, provides the parallel optionto allow you to choose whether to do parallel computation.

Output Table
The output table produced by the elastic_net_train() function has the following columns:
family
The regression type: 'gaussian' or 'binomial'.
features
An array of the features (independent variables) passed into the analysis.
features_selected
An array of the features selected by the analysis.
coef_nonzero
Fitting coefficients for the selected features.
coef_all
Coefficients for all selected and unselected features
intercept
Fitting intercept for the model.
log_likelihood
The negative value of the first equation above (up to a constant depending on the data set).
standardize
Boolean value. Whether the data was normalized (standardize argument was True).
iteration_run
The number of iterations executed.

Prediction Function
The prediction function has the following format:
madlib.elastic_net_predict(                               
             '<regress_family>',
             coefficients,
             intercept,
             ind_var
         ) FROM tbl_result, tbl_new_source                            
The above function returns a double value for each data point. When predicting with binomial models, the return value is 1 if the predicted result is True, and 0 if the prediction is False.
regress_family
The type of regression, either 'gaussian' ('linear') or 'binomal' ('logistic').
coefficients
Fitting coefficients, as a DOUBLE array.
intercept
The intercept for the model.
ind_var
Independent variables, as a DOUBLE array.
tbl_result
The name of the output table from the training function.
tbl_new_source
The name of the table containing new data to predict.

There are several different formats of the prediction function:

  1. SELECT madlib.elastic_net_gaussian_predict (
    coefficients, intercept, ind_var
    ) FROM tbl_result, tbl_new_source LIMIT 10;
  2. SELECT madlib.elastic_net_binomial_predict (
    coefficients, intercept, ind_var
    ) FROM tbl_result, tbl_new_source LIMIT 10;

    This returns 10 BOOLEAN values.
  3. SELECT madlib.elastic_net_binomial_prob (
    coefficients, intercept, ind_var
    ) FROM tbl_result, tbl_new_source LIMIT 10;

    This returns 10 probability values for True class.

Alternatively, you can use another prediction function that stores the prediction result in a table. This is useful if you want to use elastic net together with the general cross validation function.

SELECT madlib.elastic_net_predict(
'tbl_train_result',
'tbl_data',
'col_id', -- ID associated with each row
'tbl_predict' -- Prediction result
);

You do not need to specify whether the model is "linear" or "logistic" because this information is already included in the result table.

Examples:
  1. Create an input data set.
    sql> DROP TABLE IF EXISTS houses;
    sql> CREATE TABLE houses (id INT, tax INT, bedroom INT, bath FLOAT, price INT,
                size INT, lot INT);
    sql> COPY houses FROM STDIN WITH DELIMITER '|';
      1 |  590 |       2 |    1 |  50000 |  770 | 22100
      2 | 1050 |       3 |    2 |  85000 | 1410 | 12000
      3 |   20 |       3 |    1 |  22500 | 1060 |  3500
      4 |  870 |       2 |    2 |  90000 | 1300 | 17500
      5 | 1320 |       3 |    2 | 133000 | 1500 | 30000
      6 | 1350 |       2 |    1 |  90500 |  820 | 25700
      7 | 2790 |       3 |  2.5 | 260000 | 2130 | 25000
      8 |  680 |       2 |    1 | 142500 | 1170 | 22000
      9 | 1840 |       3 |    2 | 160000 | 1500 | 19000
     10 | 3680 |       4 |    2 | 240000 | 2790 | 20000
     11 | 1660 |       3 |    1 |  87000 | 1030 | 17500
     12 | 1620 |       3 |    2 | 118600 | 1250 | 20000
     13 | 3100 |       3 |    2 | 140000 | 1760 | 38000
     14 | 2070 |       2 |    3 | 148000 | 1550 | 14000
     15 |  650 |       3 |  1.5 |  65000 | 1450 | 12000
    \.
    
  2. Train the model.
    sql> DROP TABLE IF EXISTS houses_en;
    sql> SELECT madlib.elastic_net_train(  
           'houses', 'houses_en', 'price', 'array[tax, bath, size]', 
           'gaussian', 0.5, 0.1, true, null, 'fista', 
           '', 
            null, 10000, 1e-6);
    
  3. View the resulting model.
    -- Turn on expanded display to make it easier to read results.
    sql> \x on
    sql> SELECT * from houses_en;
    
  4. Use the prediction function to evaluate residuals.
    sql> SELECT *, price - predict as residual FROM (
        SELECT houses.*, 
            madlib.elastic_net_predict(
                'gaussian', m.coef_nonzero, m.intercept, array[tax,bath,size]
            ) as predict
        FROM houses, houses_en m) s;
    

See Also
File elastic_net.sql_in documenting the SQL functions.
Cross Validation

Technical Background

Elastic net regularization seeks to find a weight vector that, for any given training example set, minimizes:

\[\min_{w \in R^N} L(w) + \lambda \left(\frac{(1-\alpha)}{2} \|w\|_2^2 + \alpha \|w\|_1 \right)\]

where \(L\) is the metric function that the user wants to minimize. Here \( \alpha \in [0,1] \) and \( lambda \geq 0 \). If \(alpha = 0\), we have the ridge regularization (known also as Tikhonov regularization), and if \(\alpha = 1\), we have the LASSO regularization.

For the Gaussian response family (or linear model), we have

\[L(\vec{w}) = \frac{1}{2}\left[\frac{1}{M} \sum_{m=1}^M (w^{t} x_m + w_{0} - y_m)^2 \right] \]

For the Binomial response family (or logistic model), we have

\[ L(\vec{w}) = \sum_{m=1}^M\left[y_m \log\left(1 + e^{-(w_0 + \vec{w}\cdot\vec{x}_m)}\right) + (1-y_m) \log\left(1 + e^{w_0 + \vec{w}\cdot\vec{x}_m}\right)\right]\ , \]

where \(y_m \in {0,1}\).

To get better convergence, one can rescale the value of each element of x

\[ x' \leftarrow \frac{x - \bar{x}}{\sigma_x} \]

and for Gaussian case we also let

\[y' \leftarrow y - \bar{y} \]

and then minimize with the regularization terms. At the end of the calculation, the orginal scales will be restored and an intercept term will be obtained at the same time as a by-product.

Note that fitting after scaling is not equivalent to directly fitting.

Literature:

[1] Elastic net regularization. http://en.wikipedia.org/wiki/Elastic_net_regularization

[2] Beck, A. and M. Teboulle (2009), A fast iterative shrinkage-thresholding algorithm for linear inverse problems. SIAM J. on Imaging Sciences 2(1), 183-202.

[3] Shai Shalev-Shwartz and Ambuj Tewari, Stochastic Methods for l1 Regularized Loss Minimization. Proceedings of the 26th International Conference on Machine Learning, Montreal, Canada, 2009.