You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@madlib.apache.org by ri...@apache.org on 2016/12/22 00:08:53 UTC

[3/3] incubator-madlib git commit: Elastic net: Add cross validation

Elastic net: Add cross validation

JIRA: MADLIB-996

This commit adds cross validation feature to the main elastic_net
function. The CV parameters are added to 'optimizer_params' to ensure
backwards compatibility. This could potentially be moved to its own
parameter in a future version.

There is a special case in CV for elastic net: there is an ability to
compute the grid for lambda values automatically. The same procedure as
warmup lambdas is used (described in the original glmnet function in
R) to compute the lambda values.

Closes #77


Project: http://git-wip-us.apache.org/repos/asf/incubator-madlib/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-madlib/commit/6939fd63
Tree: http://git-wip-us.apache.org/repos/asf/incubator-madlib/tree/6939fd63
Diff: http://git-wip-us.apache.org/repos/asf/incubator-madlib/diff/6939fd63

Branch: refs/heads/master
Commit: 6939fd63b365e35af5610165560ed3853f4d815c
Parents: 38d1e87
Author: Rahul Iyer <ri...@apache.org>
Authored: Thu Nov 3 15:42:17 2016 -0700
Committer: Rahul Iyer <ri...@apache.org>
Committed: Wed Dec 21 16:08:22 2016 -0800

----------------------------------------------------------------------
 CMakeLists.txt                                  |   7 +-
 .../modules/elastic_net/elastic_net.py_in       | 490 +++++++++-------
 .../modules/elastic_net/elastic_net.sql_in      | 183 ++++--
 .../elastic_net_gaussian_fista.py_in            | 490 ----------------
 .../elastic_net/elastic_net_gaussian_igd.py_in  | 451 ---------------
 .../elastic_net_generate_result.py_in           | 207 ++++---
 .../elastic_net/elastic_net_models.py_in        | 133 ++---
 .../elastic_net_optimizer_fista.py_in           | 559 ++++++++-----------
 .../elastic_net/elastic_net_optimizer_igd.py_in | 462 +++++++--------
 .../modules/elastic_net/elastic_net_utils.py_in | 135 ++---
 .../test/elastic_net_install_check.sql_in       |  50 ++
 src/ports/postgres/modules/svm/svm.py_in        |   5 +-
 .../postgres/modules/utilities/control.py_in    |  50 +-
 .../utilities/in_mem_group_control.py_in        |  48 +-
 .../postgres/modules/utilities/utilities.py_in  |  27 +-
 .../validation/internal/cross_validation.py_in  |  22 +-
 16 files changed, 1191 insertions(+), 2128 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-madlib/blob/6939fd63/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d552ed..7e01b9c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -212,9 +212,10 @@ include(OSXUtils)
 # -- Add subdirectories --------------------------------------------------------
 
 add_subdirectory(src)
-if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
-    add_subdirectory(doc) #e.g., Clang/Clang++ does not work
-endif(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
+# if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
+#e.g., Clang/Clang++ does not work
+# endif(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
+add_subdirectory(doc)
 add_subdirectory(deploy)
 
 # -- Install path for specific madlib version ----------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-madlib/blob/6939fd63/src/ports/postgres/modules/elastic_net/elastic_net.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/elastic_net/elastic_net.py_in b/src/ports/postgres/modules/elastic_net/elastic_net.py_in
index 011a0ff..15c492d 100644
--- a/src/ports/postgres/modules/elastic_net/elastic_net.py_in
+++ b/src/ports/postgres/modules/elastic_net/elastic_net.py_in
@@ -1,9 +1,13 @@
 
 import plpy
-from elastic_net_models import __elastic_net_gaussian_igd_train
-from elastic_net_models import __elastic_net_gaussian_fista_train
-from elastic_net_models import __elastic_net_binomial_fista_train
-from elastic_net_models import __elastic_net_binomial_igd_train
+from elastic_net_models import _elastic_net_gaussian_igd_train
+from elastic_net_models import _elastic_net_gaussian_fista_train
+from elastic_net_models import _elastic_net_binomial_fista_train
+from elastic_net_models import _elastic_net_binomial_igd_train
+from elastic_net_utils import _generate_warmup_lambda_sequence
+
+from elastic_net_utils import BINOMIAL_FAMILIES, GAUSSIAN_FAMILIES, OPTIMIZERS
+
 from utilities.validate_args import is_col_array
 from utilities.validate_args import table_exists
 from utilities.validate_args import table_is_empty
@@ -11,21 +15,19 @@ from utilities.validate_args import columns_exist_in_table
 from utilities.validate_args import get_cols_and_types
 from utilities.validate_args import cols_in_tbl_valid
 from utilities.validate_args import explicit_bool_to_text
+from utilities.utilities import extract_keyvalue_params
 
 from utilities.control import MinWarning
 
 from utilities.utilities import _string_to_array_with_quotes
 from utilities.utilities import _string_to_array
-from utilities.utilities import set_client_min_messages
-from utilities.utilities import __mad_version
 from utilities.utilities import is_psql_numeric_type
 from utilities.utilities import _assert
 from utilities.utilities import add_postfix
 import re
 
-version_wrapper = __mad_version()
-mad_vec = version_wrapper.select_vecfunc()
-# ========================================================================
+from validation.internal.cross_validation import CrossValidator
+# ------------------------------------------------------------------------
 
 
 def elastic_net_help(schema_madlib, family_or_optimizer=None, **kwargs):
@@ -176,11 +178,11 @@ def elastic_net_help(schema_madlib, family_or_optimizer=None, **kwargs):
           );
 
     View the results:
-    SELECT  houses.id, 
-            houses.price, 
-            houses_en1_prediction.prediction, 
-            houses.price - houses_en1_prediction.prediction AS residual 
-    FROM houses_en1_prediction, houses 
+    SELECT  houses.id,
+            houses.price,
+            houses_en1_prediction.prediction,
+            houses.price - houses_en1_prediction.prediction AS residual
+    FROM houses_en1_prediction, houses
     WHERE houses.id=houses_en1_prediction.id;
 
         """
@@ -322,20 +324,23 @@ def elastic_net_help(schema_madlib, family_or_optimizer=None, **kwargs):
     modified version of IGD is actually used.
 
     Parameters --------------------------------
-    stepsize         - default is 0.01
-    threshold        - default is 1e-10. When a coefficient is really
-                       small, set it to be 0
-    warmup           - default is False
-    warmup_lambdas   - default is Null
-    warmup_lambda_no - default is 15. How many lambda's are used in
-                       warm-up, will be overridden if warmup_lambdas
-                       is not NULL
-    warmup_tolerance - default is the same as tolerance. The value
-                       of tolerance used during warmup.
-    parallel         - default is True. Run the computation on
-                       multiple segments or not.
-
-    When warmup is True and warmup_lambdas is NULL, a series
+    stepsize          - default is 0.01
+    threshold         - default is 1e-10. When a coefficient is really
+                        small, set it to be 0
+    warmup            - default is False
+    warmup_lambdas    - default is Null
+    warmup_lambda_no  - default is 15. How many lambda's are used in
+                        warm-up, will be overridden if warmup_lambdas
+                        is not NULL
+    warmup_tolerance  - default is the same as tolerance. The value
+                        of tolerance used during warmup.
+    n_folds           - default is 1, Number of cross validation folds.
+                        Set this to greater than 1 if CV over lambda is required.
+    validation_result - Name of the table to store the cross validation results.
+    parallel          - default is True. Run the computation on
+                        multiple segments or not.
+
+    When warmup is True or if n_folds > 1, and warmup_lambdas is NULL, a series
     of lambda values will be automatically generated and used.
 
     Reference --------------------------------
@@ -353,25 +358,28 @@ def elastic_net_help(schema_madlib, family_or_optimizer=None, **kwargs):
     Right now, it supports fitting both linear and logistic models.
 
     Parameters --------------------------------
-    max_stepsize     - default is 4.0
-    eta              - default is 1.2, if stepsize does not work
-                       stepsize/eta will be tried
-    warmup           - default is False
-    warmup_lambdas   - default is NULL, which means that lambda
-                       values will be automatically generated
-    warmup_lambda_no - default is 15. How many lambda's are used in
-                       warm-up, will be overridden if warmup_lambdas
-                       is not NULL
-    warmup_tolerance - default is the same as tolerance. The value
-                       of tolerance used during warmup.
-    use_active_set   - default is False. Sometimes active-set method
-                       can speed up the calculation.
+    max_stepsize        - default is 4.0
+    eta                 - default is 1.2, if stepsize does not work
+                          stepsize/eta will be tried
+    warmup              - default is False
+    warmup_lambdas      - default is NULL, which means that lambda
+                          values will be automatically generated
+    warmup_lambda_no    - default is 15. How many lambda's are used in
+                          warm-up, will be overridden if warmup_lambdas
+                          is not NULL
+    warmup_tolerance    - default is the same as tolerance. The value
+                          of tolerance used during warmup.
+    use_active_set      - default is False. Sometimes active-set method
+                          can speed up the calculation.
     activeset_tolerance - default is the same as tolerance. The
                           value of tolerance used during active set
                           calculation
-    random_stepsize - default is False. Whether add some randomness
-                      to the step size. Sometimes, this can speed
-                      up the calculation.
+    random_stepsize     - default is False. Whether add some randomness
+                         to the step size. Sometimes, this can speed
+                         up the calculation.
+    n_folds             - default is 1. Number of cross validation folds.
+                          Set this to greater than 1 if CV over lambda is required.
+    validation_result   - Name of the table to store the cross validation results.
 
     When warmup is True and warmup_lambdas is NULL, warmup_lambda_no
     of lambda values will be automatically generated and used.
@@ -391,22 +399,22 @@ def elastic_net_help(schema_madlib, family_or_optimizer=None, **kwargs):
     SELECT {schema_madlib}.elastic_net_train();
     for help
     """.format(schema_madlib=schema_madlib)
-# ========================================================================
+# ------------------------------------------------------------------------
 
 
-def elastic_net_train(schema_madlib, tbl_source, tbl_result, col_dep_var,
-                      col_ind_var, regress_family, alpha, lambda_value,
+def elastic_net_train(schema_madlib, source_table, model_table, dependent_varname,
+                      independent_varname, regress_family, alpha, lambda_value,
                       standardize, grouping_col, optimizer,
                       optimizer_params, excluded, max_iter, tolerance,
                       **kwargs):
     """
     A wrapper for all variants of elastic net regularization.
 
-    @param tbl_source        Name of data source table
-    @param col_ind_var       Name of independent variable column,
+    @param source_table        Name of data source table
+    @param independent_varname       Name of independent variable column,
                              independent variable is an array
-    @param col_dep_var       Name of dependent variable column
-    @param tbl_result        Name of the table to store the results,
+    @param dependent_varname       Name of dependent variable column
+    @param model_table        Name of the table to store the results,
                              will return fitting coefficients and
                              likelihood
     @param lambda_value      The regularization parameter
@@ -417,96 +425,173 @@ def elastic_net_train(schema_madlib, tbl_source, tbl_result, col_dep_var,
     @param optimizer_params  Parameters of the above optimizer, the format
                              is '{arg = value, ...}'::varchar[]
     @param excluded          Which variables are excluded when
-                             col_ind_var == "*"
+                             independent_varname == "*"
     """
-    if regress_family is None:
-        plpy.error("""
-                   Elastic Net error: Please enter a valid response family name!
-                   Run:
-                   SELECT {schema_madlib}.elastic_net_train();
-                   for supported response family.
-                   """.format(schema_madlib=schema_madlib))
-
-    if optimizer is None:
-        plpy.error("""
-                   Elastic Net error: Please enter a valid optimizer name!
-                   Run:
-                   SELECT {schema_madlib}.elastic_net_train('gaussian');
-                   for supported optimizers.
-                   """.format(schema_madlib=schema_madlib))
-    # handle all special cases of col_ind_var
-    col_ind_var, outstr_array = analyze_input_str(schema_madlib, tbl_source,
-                                                  col_ind_var, col_dep_var,
-                                                  excluded)
+    with MinWarning("warning"):
+        if regress_family is None:
+            plpy.error("""
+                       Elastic Net error: Please enter a valid response family name!
+                       Run:
+                       SELECT {schema_madlib}.elastic_net_train();
+                       for supported response family.
+                       """.format(schema_madlib=schema_madlib))
+
+        if optimizer is None:
+            plpy.error("""
+                       Elastic Net error: Please enter a valid optimizer name!
+                       Run:
+                       SELECT {schema_madlib}.elastic_net_train('gaussian');
+                       for supported optimizers.
+                       """.format(schema_madlib=schema_madlib))
+
+        regress_family = regress_family.lower()
+        optimizer = optimizer.lower()
+
+        if (regress_family not in (BINOMIAL_FAMILIES + GAUSSIAN_FAMILIES) or
+                optimizer not in OPTIMIZERS):
+            plpy.error("""
+                Elastic Net error: Not a supported response family or supported
+                optimizer of the given response family!
+
+                Run:
+                    SELECT {schema_madlib}.elastic_net_train();
+                for help.
+                """.format(schema_madlib=schema_madlib))
+
+        cv_param, optimizer_params = _get_cv_optimizer_params(
+            optimizer_params, alpha, lambda_value)
+
+        args = locals()
+        if cv_param['n_folds'] > 1:
+            args.update(cv_param)
+            _cross_validate_en(args)
+        _internal_elastic_net_train(**args)
+# ------------------------------------------------------------------------
+
+
+def _internal_elastic_net_train(
+        schema_madlib, source_table, model_table, dependent_varname,
+        independent_varname, grouping_col,
+        regress_family, alpha, lambda_value,
+        standardize, optimizer, optimizer_params, excluded,
+        max_iter, tolerance, **kwargs):
+
+    tbl_summary = add_postfix(model_table, "_summary")
+
+    # handle all special cases of independent_varname
+    independent_varname, outstr_array = analyze_input_str(
+        schema_madlib, source_table,
+        independent_varname, dependent_varname, excluded)
+
     # get the grouping info
     grouping_str, grouping_col = _get_grouping_col_str(schema_madlib,
-                                                           tbl_source, grouping_col)
-
-    # # Special case for ridge linear regression
-    # if ((regress_family.lower() == "gaussian" or regress_family.lower() == "linear") and
-    #     optimizer.lower() == "newton" and
-    #     alpha == 0):
-    #     plpy.execute("""select {schema_madlib}.ridge_newton_train(
-    #                         '{tbl_source}', '{col_ind_var}', '{col_dep_var}',
-    #                         '{tbl_result}', {lambda_value}, {standardize}
-    #     )""".format(schema_madlib=schema_madlib,
-    #                 tbl_source = tbl_source,
-    #                 col_ind_var = col_ind_var,
-    #                 col_dep_var = col_dep_var,
-    #                 tbl_result = tbl_result,
-    #                 lambda_value = lambda_value,
-    #                 standardize = standardize))
-    #     return None
-
-    not_supported_family = False
-    not_supported_opt = False
-    tbl_summary = add_postfix(tbl_result, "_summary")
-    if regress_family.lower() in ("gaussian", "linear"):
-        if optimizer.lower() == "igd":
-            __elastic_net_gaussian_igd_train(
-                schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                tbl_result, tbl_summary, lambda_value, alpha, standardize,
-                optimizer_params, max_iter, tolerance, outstr_array, 
+                                                       source_table, grouping_col)
+
+    if regress_family in GAUSSIAN_FAMILIES:
+        if optimizer == OPTIMIZERS.igd:
+            _elastic_net_gaussian_igd_train(
+                schema_madlib, source_table, independent_varname, dependent_varname,
+                model_table, tbl_summary, lambda_value, alpha, standardize,
+                optimizer_params, max_iter, tolerance, outstr_array,
                 grouping_str, grouping_col, **kwargs)
             return None
-        if optimizer.lower() == "fista":
-            __elastic_net_gaussian_fista_train(
-            	schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                tbl_result, tbl_summary, lambda_value, alpha, standardize,
-                optimizer_params, max_iter, tolerance, outstr_array, 
+        if optimizer == OPTIMIZERS.fista:
+            _elastic_net_gaussian_fista_train(
+                schema_madlib, source_table, independent_varname, dependent_varname,
+                model_table, tbl_summary, lambda_value, alpha, standardize,
+                optimizer_params, max_iter, tolerance, outstr_array,
                 grouping_str, grouping_col, **kwargs)
             return None
-        not_supported_opt = True
-    elif regress_family.lower() in ("binomial", "logistic"):
-        if optimizer.lower() == "igd":
-            col_dep_var = "(" + col_dep_var + ")::boolean"
-            __elastic_net_binomial_igd_train(
-                schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                tbl_result, tbl_summary, lambda_value, alpha, standardize,
-                optimizer_params, max_iter, tolerance, outstr_array, 
+    elif regress_family in BINOMIAL_FAMILIES:
+        if optimizer == OPTIMIZERS.igd:
+            dependent_varname = "(" + dependent_varname + ")::boolean"
+            _elastic_net_binomial_igd_train(
+                schema_madlib, source_table, independent_varname, dependent_varname,
+                model_table, tbl_summary, lambda_value, alpha, standardize,
+                optimizer_params, max_iter, tolerance, outstr_array,
                 grouping_str, grouping_col, **kwargs)
             return None
-
-        if optimizer.lower() == "fista":
-            col_dep_var = "(" + col_dep_var + ")::boolean"
-            __elastic_net_binomial_fista_train(
-                schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                tbl_result, tbl_summary, lambda_value, alpha, standardize,
+        if optimizer == OPTIMIZERS.fista:
+            dependent_varname = "(" + dependent_varname + ")::boolean"
+            _elastic_net_binomial_fista_train(
+                schema_madlib, source_table, independent_varname, dependent_varname,
+                model_table, tbl_summary, lambda_value, alpha, standardize,
                 optimizer_params, max_iter, tolerance, outstr_array,
                 grouping_str, grouping_col, **kwargs)
             return None
-        not_supported_opt = True
-    else:
-        not_supported_family = True
-    if not_supported_family or not_supported_opt:
-        plpy.error("""
-               Elastic Net error: Not a supported response family or supported optimizer of the given response family!
-               Run:
-               SELECT {schema_madlib}.elastic_net_train();
-               for help.
-               """.format(schema_madlib=schema_madlib))
     return None
-# ========================================================================
+# ----------------------------------------------------------------------
+
+
+def _get_cv_optimizer_params(param_str, alpha, smallest_lambda):
+    cv_params_defaults = {
+        "n_folds": (1, int),
+        "lambda_value": (None, list),
+        "alpha": ([alpha], list),
+        "n_lambdas": (15, int),
+        "validation_result": (None, str)
+    }
+    param_defaults = dict([(k, v[0]) for k, v in cv_params_defaults.items()])
+    param_types = dict([(k, v[1]) for k, v in cv_params_defaults.items()])
+
+    if not param_str:
+        return param_defaults, param_str
+
+    name_value = extract_keyvalue_params(param_str, param_types, param_defaults,
+                                         ignore_invalid=True)
+    if name_value['n_folds'] > 1:
+        if not name_value['lambda_value']:
+            if name_value['n_lambdas']:
+                name_value['lambda_value'] = _generate_warmup_lambda_sequence(
+                    smallest_lambda, name_value['n_lambdas'])
+                # no warmup when cross validating on lambda
+                param_str += ', warmup=False'
+            else:
+                name_value['lambda_value'] = [float(smallest_lambda)]
+        else:
+            name_value['lambda_value'] = map(float, name_value['lambda_value'])
+            # no warmup when cross validating on lambda
+            param_str += ', warmup=False'
+        name_value['alpha'] = map(float, name_value['alpha'])
+    return name_value, param_str
+# ------------------------------------------------------------------------
+
+
+def _cross_validate_en(args):
+    # updating params_dict will also update args['params_dict']
+    if args['n_folds'] > 1 and args['grouping_col']:
+        plpy.error('Elastic Net Error: cross validation with grouping is not supported!')
+
+    allowed_cv_params = ('lambda_value', 'alpha')  # keep trailing comma for single element
+    cv_params_values = {}
+
+    for param in allowed_cv_params:
+        if isinstance(args[param], list):
+            if len(args[param]) > 1:
+                cv_params_values[param] = args[param]
+            else:
+                args[param] = args[param][0]
+
+    if not cv_params_values and args['n_folds'] <= 1:
+        # no cross validation
+        return
+
+    if not cv_params_values and args['n_folds'] > 1:
+        plpy.warning('Elastic Net Warning: n_folds > 1 but no '
+                     'cross validation parameter provided')
+        return
+
+    if cv_params_values and args['n_folds'] <= 1:
+        plpy.error('Elastic Net Error: All parameters must be scalar '
+                   'when n_folds is 0 or 1')
+
+    scorer = 'classification' if args['regress_family'] in BINOMIAL_FAMILIES else 'regression'
+    cv = CrossValidator(_internal_elastic_net_train, elastic_net_predict_all, scorer, args)
+    val_res = cv.validate(cv_params_values, args['n_folds'])
+    if 'validation_result' in args:
+        val_res.output_tbl(args['validation_result'])
+    args.update(val_res.top('sub_args'))
+# ------------------------------------------------------------------------------
 
 
 def _get_grouping_col_str(schema_madlib, source_table, grouping_col):
@@ -517,7 +602,7 @@ def _get_grouping_col_str(schema_madlib, source_table, grouping_col):
         intersect = frozenset(
             _string_to_array(grouping_col)).intersection(
                 frozenset(
-                    ('regress_family', 'coef_all', 
+                    ('regress_family', 'coef_all',
                      'features_selected',
                      'coef_nonzero', 'intercept',
                      'log_likelihood', 'standardize',
@@ -541,7 +626,7 @@ def _get_grouping_col_str(schema_madlib, source_table, grouping_col):
 # ------------------------------------------------------------------------------
 
 
-def __check_args(tbl_source, col_ind_var, col_dep_var):
+def _check_args(tbl_source, col_ind_var, col_dep_var):
     """
     Check arguments before analyze_input_str
     """
@@ -555,7 +640,7 @@ def __check_args(tbl_source, col_ind_var, col_dep_var):
 
     if table_is_empty(tbl_source):
         plpy.error("Elastic Net error: Data table " + tbl_source + " is empty!")
-# ========================================================================
+# ------------------------------------------------------------------------
 
 
 def analyze_input_str(schema_madlib, tbl_source,
@@ -568,7 +653,7 @@ def analyze_input_str(schema_madlib, tbl_source,
     @param col_dep_var Dependent variables
     @param excluded Which variables are excluded when col_ind_var == "*"
     """
-    __check_args(tbl_source, col_ind_var, col_dep_var)
+    _check_args(tbl_source, col_ind_var, col_dep_var)
 
     outstr_array = []
     if col_ind_var == "*":
@@ -593,38 +678,36 @@ def analyze_input_str(schema_madlib, tbl_source,
             included_col_types = [col_types_dict[i] for i in outstr_array]
             if not all(is_psql_numeric_type(i)
                        for i in included_col_types):
-                plpy.error("""
-                           Elastic Net error: All columns to be included in the
-                           independent variables should be of the numeric type.
-                           """)
+                plpy.error("Elastic Net error: All columns to be included in the"
+                           "independent variables should be of the numeric type.")
         col_ind_var_new = "ARRAY[" + ','.join(outstr_array) + "]"
         return (col_ind_var_new, outstr_array)
 
     if columns_exist_in_table(tbl_source, [col_ind_var], schema_madlib):
-        ## if the input is a column name and not an expression
+        # if the input is a column name and not an expression
         return analyze_single_input_str(schema_madlib, tbl_source,
                                         col_ind_var, excluded)
     else:
-        ## if input is an expression resulting in an array output
+        # if input is an expression resulting in an array output
         matched = re.match(r"(?i)^array\[(.*)\]", col_ind_var)
         if matched:
-            ## array expression starts with the word "ARRAY"
+            # array expression starts with the word "ARRAY"
             outstr_array = _string_to_array(matched.group(1))
         else:
-            ## any other form of array expression
+            # any other form of array expression
             n_feat = plpy.execute(""" SELECT array_upper({indep_var}, 1) as num_feat
                                       FROM {source} LIMIT 1
                                   """.format(indep_var=col_ind_var,
                                              source=tbl_source))[0]["num_feat"]
-            outstr_array = ["[" + str(i) + "]" for i in range(1, n_feat+1)]
-        ## We allow expressions for independent variables that could start with
-        ## something other than 'array'
-        ##    Example use case: input independent variable of array column
-        ##     adding an intercept could be done as '1 || x' where 'x' is array
-        ##     of independent variables.
+            outstr_array = ["[" + str(i) + "]" for i in range(1, n_feat + 1)]
+        # We allow expressions for independent variables that could start with
+        # something other than 'array'
+        # Example use case: input independent variable of array column
+        # adding an intercept could be done as '1 || x' where 'x' is array
+        # of independent variables.
         # plpy.error("Elastic Net error: Independent variable format is not quite right!")
         return (col_ind_var, outstr_array)
-# ========================================================================
+# ------------------------------------------------------------------------
 
 
 def analyze_single_input_str(schema_madlib, tbl_source, col_ind_var,
@@ -670,7 +753,7 @@ def analyze_single_input_str(schema_madlib, tbl_source, col_ind_var,
             s = []
 
         outstr_array = ["%s[%s]" % (col_ind_var, str(i))
-                        for i in range(1, dimension+1) if i not in s]
+                        for i in range(1, dimension + 1) if i not in s]
         if s:
             col_ind_var_new = "ARRAY[" + ",".join(outstr_array) + "]"
         else:
@@ -680,7 +763,7 @@ def analyze_single_input_str(schema_madlib, tbl_source, col_ind_var,
     else:
         plpy.error("Elastic Net error: Single column name included for "
                    "independent variable is not found in source table.")
-# ========================================================================
+# ------------------------------------------------------------------------
 
 
 def elastic_net_predict_all(schema_madlib, tbl_model, tbl_new_source,
@@ -690,32 +773,52 @@ def elastic_net_predict_all(schema_madlib, tbl_model, tbl_new_source,
     """
     summary_table = add_postfix(tbl_model, "_summary")
     grouping_col = plpy.execute("SELECT grouping_col FROM {summary_table}".
-        format(summary_table=summary_table))[0]["grouping_col"]
-    old_msg_level = set_client_min_messages("error")
-    regress_family = plpy.execute("SELECT family FROM {tbl_model} ".
-                                  format(tbl_model=tbl_model))[0]["family"]
-
-    if regress_family.lower() in ("gaussian", "linear"):
-        predict_func = "elastic_net_gaussian_predict"
-    elif regress_family.lower() in ("binomial", "logistic"):
-        predict_func = "elastic_net_binomial_predict"
-    else:
-        plpy.error("Elastic Net error: Not a supported response family!")
+                                format(summary_table=summary_table))[0]["grouping_col"]
+    with MinWarning("error"):
+        regress_family = plpy.execute("SELECT family FROM {tbl_model} ".
+                                      format(tbl_model=tbl_model))[0]["family"]
+
+        if regress_family.lower() in ("gaussian", "linear"):
+            predict_func = "elastic_net_gaussian_predict"
+        elif regress_family.lower() in ("binomial", "logistic"):
+            predict_func = "elastic_net_binomial_predict"
+        else:
+            plpy.error("Elastic Net error: Not a supported response family!")
 
-    if col_id is None or col_id == '':
-        plpy.error("Elastic Net error: invalid ID column provided!")
-    if columns_exist_in_table(tbl_new_source, [col_id], schema_madlib):
-        elastic_net_predict_id = col_id
-    else:
-        elastic_net_predict_id = 'elastic_net_predict_id'
-
-    dense_vars = mad_vec(plpy.execute(""" SELECT features AS fs
-                                          FROM {tbl_model}
-                                      """.format(tbl_model=tbl_model))[0]["fs"])
-    dense_vars_str = "ARRAY[" + ", ".join(dense_vars) + "]"
-    # Must be careful to avoid possible name conflicts
-    if not grouping_col or grouping_col != 'NULL':
-        qstr = """
+        if col_id is None or col_id == '':
+            plpy.error("Elastic Net error: invalid ID column provided!")
+        if columns_exist_in_table(tbl_new_source, [col_id], schema_madlib):
+            elastic_net_predict_id = col_id
+        else:
+            elastic_net_predict_id = 'elastic_net_predict_id'
+
+        dense_vars = plpy.execute(""" SELECT features AS fs FROM {tbl_model}
+                                  """.format(tbl_model=tbl_model))[0]["fs"]
+        dense_vars_str = "ARRAY[" + ", ".join(dense_vars) + "]"
+        # Must be careful to avoid possible name conflicts
+
+        if grouping_col and grouping_col != 'NULL':
+            qstr = """
+                DROP TABLE IF EXISTS {tbl_predict};
+                CREATE TABLE {tbl_predict} AS
+                    SELECT
+                        {elastic_net_predict_id},
+                        {schema_madlib}.{predict_func}(coef_all, intercept, ind_var)
+                             AS prediction
+                    FROM
+                        {tbl_model} as tbl1
+                        JOIN
+                        (SELECT
+                            {grouping_col},
+                            {col_id} as {elastic_net_predict_id},
+                            {dense_vars_str} as ind_var
+                        FROM
+                            {tbl_new_source}) tbl2
+                        USING ({grouping_col})
+                        ORDER BY {grouping_col}, {elastic_net_predict_id}
+                """.format(**locals())
+        else:
+            qstr = """
             DROP TABLE IF EXISTS {tbl_predict};
             CREATE TABLE {tbl_predict} AS
                 SELECT
@@ -723,34 +826,15 @@ def elastic_net_predict_all(schema_madlib, tbl_model, tbl_new_source,
                     {schema_madlib}.{predict_func}(coef_all, intercept, ind_var)
                          AS prediction
                 FROM
-                    {tbl_model} as tbl1
-                    JOIN
-                    (SELECT
-                        {grouping_col},
-                        {col_id} as {elastic_net_predict_id},
-                        {dense_vars_str} as ind_var
-                    FROM
-                        {tbl_new_source}) tbl2
-                    USING ({grouping_col})
+                    {tbl_model} as tbl1,
+                    (
+                        SELECT
+                            {col_id} as {elastic_net_predict_id},
+                            {dense_vars_str} as ind_var
+                        FROM
+                            {tbl_new_source}
+                    ) tbl2
             """.format(**locals())
-    else:
-        qstr = """
-        DROP TABLE IF EXISTS {tbl_predict};
-        CREATE TABLE {tbl_predict} AS
-            SELECT
-                {elastic_net_predict_id},
-                {schema_madlib}.{predict_func}(coef_all, intercept, ind_var)
-                     AS prediction
-            FROM
-                {tbl_model} as tbl1,
-                (SELECT
-                    {col_id} as {elastic_net_predict_id},
-                    {dense_vars_str} as ind_var
-                FROM
-                    {tbl_new_source}) tbl2
-        """.format(**locals())
-    plpy.execute(qstr)
-
-    set_client_min_messages(old_msg_level)
+        plpy.execute(qstr)
     return None
-# ========================================================================
+# ------------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-madlib/blob/6939fd63/src/ports/postgres/modules/elastic_net/elastic_net.sql_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/elastic_net/elastic_net.sql_in b/src/ports/postgres/modules/elastic_net/elastic_net.sql_in
index 4677f1c..9bed5ac 100644
--- a/src/ports/postgres/modules/elastic_net/elastic_net.sql_in
+++ b/src/ports/postgres/modules/elastic_net/elastic_net.sql_in
@@ -166,34 +166,25 @@ computation stops.</DD>
 </DL>
 
 @anchor optimizer
-@par 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.
+@par Other Parameters
 
-When the \ref elastic_net_train() \e optimizer argument value is \b 'fista', the \e optimizer_params argument is a string containing name-value pairs with the following format. (Line breaks are inserted for readability.)
+Multiple other (optional) parameters are supplied in a string containing a
+comma-delimited list of name-value pairs. All of these named parameters are
+optional and use the format "<param_name> = <value>".
+
+The parameters described below are organized by their functionality.
+
+<em><b>Warmup parameters</b></em>
 <pre class="syntax">
-  'max_stepsize = &lt;value>,
-   eta = &lt;value>,
-   warmup = &lt;value>,
-   warmup_lambdas = &lt;value>,
-   warmup_lambda_no = &lt;value>,
-   warmup_tolerance = &lt;value>,
-   use_active_set = &lt;value>,
-   activeset_tolerance = &lt;value>,
-   random_stepsize = &lt;value>'
+  $$
+    warmup = &lt;value>,
+    warmup_lambdas = &lt;value>,
+    warmup_lambda_no = &lt;value>,
+    warmup_tolerance = &lt;value>
+  $$
 </pre>
-\b Parameters
-<DL class="arglist">
-<DT>max_stepsize</dt>
-<DD>Default: 4.0. Initial backtracking step size. At each iteration, the algorithm first tries
-<em>stepsize = max_stepsize</em>, and if it does not work out, it then tries a
-smaller step size, <em>stepsize = stepsize/eta</em>, where \e 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 \e max_stepsize can decrease the computation time by more than 10 times.</DD>
-<DT>eta</DT>
-<DD>Default: 2. If stepsize does not work \e stepsize / \e eta is tried. Must be greater than 1. </DD>
 
+<DL class="arglist">
 <DT>warmup</DT>
 <DD>Default: FALSE. If \e warmup is TRUE, a series of lambda values, which is
 strictly descent and ends at the lambda value that the user wants to calculate,
@@ -212,6 +203,86 @@ computation on only one lambda value.</DD>
 <DT>warmup_tolerance</DT>
 <DD>The value of tolerance used during warmup. The default is the same as the
 \e tolerance argument.</DD>
+</DL>
+
+<em><b>Cross validation parameters</b></em>
+@note Cross validation is not supported if grouping is used.
+<pre class="syntax">
+  $$
+    n_folds = &lt;value>,
+    validation_result = &lt;value>,
+    lambda_value = &lt;value>,
+    n_lambdas = &lt;value>,
+    alpha = &lt;value>
+  $$
+</pre>
+
+Hyperparameter optimization can be carried out using the built-in cross
+validation mechanism, which is activated by assigning a value greater than 1 to
+the parameter \e n_folds in \e params.  Presently, misclassification error is used
+for classification and mean squared error is used for regression.
+
+The values of a parameter to cross validate should be provided in a list. For
+example, to regularize with the L1 norm and use a lambda value
+from the set {0.3, 0.4, 0.5}, include 'lambda_value={0.3, 0.4, 0.5}' in
+\e other_params. Note that the use of '{}' and '[]' are both valid
+here.
+
+
+<DL class="arglist">
+
+<DT>n_folds</dt>
+<DD>Default: 0.
+Number of folds (k). Must be at least 2 to activate cross validation.
+If a value of k > 2 is specified, each fold is then used as a validation set once,
+while the other k - 1 folds form the training set.
+</DD>
+
+
+<DT>validation_result</dt>
+<DD>Default: NULL.
+Name of the table to store the cross validation results including the values of
+parameters and their averaged error values. The table is only created if the name is not NULL.
+</DD>
+
+<DT>lambda_value</DT>
+<DD>Regularization value. If a list is provided for cross validation, then warmup is
+disabled on each lambda for performance reasons. </DD>
+
+<DT>n_lambdas</DT>
+<DD>Number of lambdas to cross validate over. If a list of lambda values is not
+provided, this parameter can be used to autogenerate a list of lambdas (using the
+warmup procedure)
+disabled on each lambda for performance reasons. </DD>
+
+<DT>alpha</DT>
+<DD>Elastic net control parameter. Needs to be a list of values to apply
+cross validation on it.
+</DD>
+</DL>
+
+<em><b>Optimizer parameters</b></em>
+
+\b FISTA Parameters
+<pre class="syntax">
+  $$
+    max_stepsize = &lt;value>,
+    eta = &lt;value>,
+    use_active_set = &lt;value>,
+    activeset_tolerance = &lt;value>,
+    random_stepsize = &lt;value>
+  $$
+</pre>
+
+<DL class="arglist">
+<DT>max_stepsize</dt>
+<DD>Default: 4.0. Initial backtracking step size. At each iteration, the algorithm first tries
+<em>stepsize = max_stepsize</em>, and if it does not work out, it then tries a
+smaller step size, <em>stepsize = stepsize/eta</em>, where \e 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 \e max_stepsize can decrease the computation time by more than 10 times.</DD>
+
+<DT>eta</DT>
+<DD>Default: 2. If stepsize does not work \e stepsize / \e eta is tried. Must be greater than 1. </DD>
 
 <DT>use_active_set</DT>
 <DD>Default: FALSE. If \e use_active_set is TRUE, an active-set method is used to
@@ -229,25 +300,23 @@ we are done, otherwise the process is repeated.</DD>
 up the calculation.</DD>
 </DL>
 
-When the \ref elastic_net_train() \e optimizer argument value is \b 'igd', the
-\e optimizer_params argument is a string containing name-value pairs with
-the following format. (Line breaks are inserted for readability.)
+\b IGD parameters
 <pre class="syntax">
-  'stepsize = &lt;value>,
-   step_decay = &lt;value>,
-   threshold = &lt;value>,
-   warmup = &lt;value>,
-   warmup_lambdas = &lt;value>,
-   warmup_lambda_no = &lt;value>,
-   warmup_tolerance = &lt;value>,
-   parallel = &lt;value>'
+  $$
+      stepsize = &lt;value>,
+      step_decay = &lt;value>,
+      threshold = &lt;value>,
+      parallel = &lt;value>
+  $$
 </pre>
-\b Parameters
 <DL class="arglist">
+
 <DT>stepsize</DT>
 <DD>The default is 0.01.</DD>
+
 <DT>step_decay</DT>
-<DD>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.</DD>
+<DD>The actual stepsize used for current step is (previous stepsize) / exp(step_decay). The default value is 0, which means that a constant stepsize is used in IGD.</DD>
+
 <DT>threshold</DT>
 <DD>Default: 1e-10. When a coefficient is really small, set this coefficient to be 0.
 
@@ -259,31 +328,16 @@ 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
 \e threshold, set the original coefficient to zero.</DD>
-<DT>warmup</DT>
-<DD>Default: FALSE. If \e 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.</DD>
-<DT>warmup_lambdas</DT>
-<DD>Default: NULL. An array of lambda values to use for warmup.</DD>
-<DT>warmup_lambda_no</DT>
-<DD>The number of lambdas used in warm-up. The default is 15. If \e
-warmup_lambdas is not NULL, this argument is overridden by the size of the \e
-warmup_lambdas array.</DD>
-<DT>warmup_tolerance</DT>
-<DD>The value of tolerance used during warmup.The default is the same as the \e tolerance argument.</DD>
+
 <DT>parallel</DT>
 <DD>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
+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
-\e parallel option to allow you to  choose whether to do parallel computation.
+\e parallel option to allow you to choose whether to do parallel computation.
 </DD>
 </DL>
 
@@ -512,12 +566,12 @@ SELECT madlib.elastic_net_predict(
 </pre>
 -# View the results:
 <pre class="example">
-SELECT  houses.id, 
-        houses.price, 
-        houses_en1_prediction.prediction, 
-        houses.price - houses_en1_prediction.prediction AS residual 
-FROM houses_en1_prediction, houses 
-WHERE houses.id=houses_en1_prediction.id;
+SELECT  houses.id,
+        houses.price,
+        houses_en1_prediction.prediction,
+        houses.price - houses_en1_prediction.prediction AS residual
+FROM houses_en1_prediction, houses
+WHERE houses.id = houses_en1_prediction.id;
 </pre>
 
 @anchor additional_example
@@ -724,7 +778,12 @@ CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.elastic_net_train (
     max_iter            INTEGER,
     tolerance           DOUBLE PRECISION
 ) RETURNS VOID AS $$
-PythonFunction(elastic_net, elastic_net, elastic_net_train)
+    PythonFunctionBodyOnly(`elastic_net', `elastic_net')
+    return elastic_net.elastic_net_train(
+        schema_madlib, tbl_source, tbl_result, col_dep_var,
+        col_ind_var, regress_family, alpha, lambda_value,
+        standardize, grouping_col, optimizer, optimizer_params,
+        excluded, max_iter, tolerance)
 $$ LANGUAGE plpythonu
 m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `MODIFIES SQL DATA', `');
 

http://git-wip-us.apache.org/repos/asf/incubator-madlib/blob/6939fd63/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_fista.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_fista.py_in b/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_fista.py_in
deleted file mode 100644
index fd56fa0..0000000
--- a/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_fista.py_in
+++ /dev/null
@@ -1,490 +0,0 @@
-# coding=utf-8
-m4_changequote(`<!', `!>')
-
-import plpy
-import math
-from elastic_net_utils import __normalize_data
-from elastic_net_utils import __compute_data_scales
-from elastic_net_utils import __compute_means
-from elastic_net_utils import __tbl_dimension_rownum
-from utilities.utilities import unique_string
-from elastic_net_gaussian_igd import __tbl_dimension_rownum
-from utilities.control import IterationController2S
-from elastic_net_utils import IterationControllerNoTableDrop
-from elastic_net_utils import __elastic_net_validate_args
-from utilities.utilities import _array_to_string
-from elastic_net_utils import __compute_average_sq
-from elastic_net_utils import __generate_warmup_lambda_sequence
-from elastic_net_utils import __process_warmup_lambdas
-from elastic_net_generate_result import __elastic_net_generate_result
-from utilities.utilities import __mad_version
-from utilities.utilities import preprocess_keyvalue_params
-
-version_wrapper = __mad_version()
-mad_vec = version_wrapper.select_vecfunc()
-
-## ========================================================================
-
-
-def __fista_params_parser(optimizer_params, lambda_value, schema_madlib):
-    """
-    Parse fista parameters.
-    """
-    allowed_params = set(["max_stepsize", "eta", "warmup", "warmup_lambdas",
-                          "warmup_lambda_no", "use_active_set"])
-    name_value = dict()
-    # default values
-    name_value["max_stepsize"] = 2.
-    name_value["use_active_set"] = 1  # use of active set
-    name_value["eta"] = 2
-    name_value["warmup"] = False
-    name_value["warmup_lambdas"] = None
-    name_value["warmup_lambda_no"] = 15
-
-    warmup_lambdas = None
-    warmup_lambda_no = None
-
-    if optimizer_params is None:
-        return name_value
-
-    for s in preprocess_keyvalue_params(optimizer_params):
-        items = s.split("=")
-        if (len(items) != 2):
-            plpy.error("Elastic Net error: Optimizer parameter list has incorrect format!")
-        param_name = items[0].strip(" \"").lower()
-        param_value = items[1].strip(" \"").lower()
-
-        if param_name not in allowed_params:
-            plpy.error(
-                """
-                Elastic Net error: {param_name} is not a valid parameter name for the FISTA optimizer.
-                Run:
-
-                SELECT {schema_madlib}.elastic_net_train('fista');
-
-                to see the parameters for FISTA algorithm.
-                """.format(param_name=param_name,
-                           schema_madlib=schema_madlib))
-
-        if param_name == "max_stepsize":
-            try:
-                name_value["max_stepsize"] = float(param_value)
-            except:
-                plpy.error("Elastic Net error: max_stepsize must be a float number!")
-
-        if param_name == "eta":
-            try:
-                name_value["eta"] = float(param_value)
-            except:
-                plpy.error("Elastic Net error: eta must be a float number!")
-
-        if param_name == "warmup":
-            if param_value in ["true", "t", "yes", "y"]:
-                name_value["warmup"] = True
-            elif param_value in ["false", "f", "no", "n"]:
-                name_value["warmup"] = False
-            else:
-                plpy.error("Elastic Net error: Do you need warmup "
-                           "(True/False or yes/no) ?")
-
-        if param_name == "warmup_lambdas" and param_value != "null":
-            warmup_lambdas = param_value
-
-        if param_name == "warmup_lambda_no":
-            warmup_lambda_no = param_value
-
-        if param_name == "use_active_set":
-            if param_value in ["true", "t", "yes", "y"]:
-                name_value["use_active_set"] = 1
-            elif param_value in ["false", "f", "no", "n"]:
-                name_value["use_active_set"] = 0
-            else:
-                plpy.error("Elastic Net error: Do you need warmup "
-                           "(True/False or yes/no) ?")
-
-    if name_value["warmup"]:
-        if warmup_lambdas is not None:
-            # errors are handled in __process_warmup_lambdas
-            name_value["warmup_lambdas"] = __process_warmup_lambdas(warmup_lambdas, lambda_value)
-        if warmup_lambda_no is not None:
-            try:
-                name_value["warmup_lambda_no"] = int(warmup_lambda_no)
-            except:
-                plpy.error("Elastic Net error: warmup_lambda_no must be an "
-                           "integer!")
-
-    # validate the parameters
-    if name_value["max_stepsize"] <= 0:
-        plpy.error("Elastic Net error: backtracking parameter max_stepsize "
-                   "must be positive!")
-
-    if name_value["eta"] < 1:
-        plpy.error("Elastic Net error: backtracking parameter eta must be "
-                   "larger than 1!")
-
-    if (name_value["warmup"] and name_value["warmup_lambdas"] is None and
-            name_value["warmup_lambda_no"] < 1):
-        plpy.error("Elastic Net error: Number of warm-up lambdas must be a "
-                   "positive integer!")
-
-    return name_value
-## ========================================================================
-
-
-def __fista_create_tbl_args(**args):
-    """
-    create the temporary schema and argument table used in FISTA iterations
-    """
-    (xmean_str0, ymean) = __compute_means(**args)
-
-    plpy.execute("""
-                 drop table if exists {tbl_fista_args};
-                 create temp table {tbl_fista_args} (
-                     {dimension_name}       integer,
-                     {lambda_name}          double precision[],
-                     {alpha_name}           double precision,
-                     {total_rows_name}      integer,
-                     {max_iter_name}        integer,
-                     {tolerance_name}       double precision,
-                     {xmean_name}           double precision[],
-                     {ymean_name}           double precision,
-                     {max_stepsize_name}    double precision,
-                     {eta_name}             double precision,
-                     {activeset_name}       integer
-                 );
-                 """.format(**args))
-    plpy.execute("""
-                 insert into {tbl_fista_args} values
-                     ({dimension}, '{warmup_lambdas}'::double precision[],
-                     {alpha}, {row_num}, {max_iter}, {tolerance},
-                     '{xmean_str0}'::double precision[], {ymean}, {max_stepsize}, {eta},
-                     {use_active_set});
-                 """.format(xmean_str0=xmean_str0, ymean=ymean, **args))
-
-    return None
-## ========================================================================
-
-
-def __fista_construct_dict(schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                           tbl_result, dimension, row_num, lambda_value, alpha,
-                           normalization, max_iter, tolerance,
-                           outstr_array, optimizer_params_dict):
-    """
-    Construct the dict used by a series of SQL queries in FISTA optimizer.
-    """
-    args = dict(schema_madlib=schema_madlib, tbl_source=tbl_source,
-                tbl_data=tbl_source,  # argument name used in normalization
-                col_ind_var=col_ind_var, col_dep_var=col_dep_var,
-                col_ind_var_norm_new=unique_string(),  # for normalization usage
-                col_ind_var_tmp=unique_string(),
-                col_dep_var_norm_new=unique_string(),  # for normalization usage
-                col_dep_var_tmp=unique_string(),
-                tbl_result=tbl_result,
-                lambda_value=lambda_value, alpha=alpha,
-                dimension=dimension, row_num=row_num,
-                max_iter=max_iter, tolerance=tolerance,
-                outstr_array=outstr_array,
-                normalization=normalization)
-
-    # Add the optimizer parameters
-    args.update(optimizer_params_dict)
-
-    # Table names useful when normalizing the original data
-    # Note: in order to be consistent with the calling convention
-    # of the normalization functions, multiple elements of the dict
-    # actually have the same value. This is a price that one has to pay
-    # if he wants to save typing argument names by using **args as the
-    # function argument.
-    tbl_ind_scales = unique_string()
-    tbl_dep_scale = unique_string()
-    tbl_data_scaled = unique_string()
-    args.update(tbl_scale=tbl_dep_scale, tbl_dep_scale=tbl_dep_scale,
-                tbl_scales=tbl_ind_scales, tbl_ind_scales=tbl_ind_scales,
-                tbl_data_scaled=tbl_data_scaled)
-
-    # Table names used in IGD iterations
-    args.update(tbl_fista_state=unique_string(),
-                tbl_fista_args=unique_string())
-
-    # more, for args table
-    args["dimension_name"] = unique_string()
-    args["lambda_name"] = unique_string()
-    args["alpha_name"] = unique_string()
-    args["total_rows_name"] = unique_string()
-    args["max_iter_name"] = unique_string()
-    args["tolerance_name"] = unique_string()
-    args["xmean_name"] = unique_string()
-    args["ymean_name"] = unique_string()
-    args["max_stepsize_name"] = unique_string()
-    args["eta_name"] = unique_string()
-    args["activeset_name"] = unique_string()
-
-    return args
-## ========================================================================
-
-
-def __fista_cleanup_temp_tbls(**args):
-    """
-    Drop all temporary tables used by FISTA optimizer,
-    including tables used in the possible normalization
-    and FISTA iterations.
-    """
-    plpy.execute("""
-                 drop table if exists {tbl_ind_scales};
-                 drop table if exists {tbl_dep_scale};
-                 drop table if exists {tbl_data_scaled};
-                 drop table if exists {tbl_fista_args};
-                 drop table if exists pg_temp.{tbl_fista_state};
-                 """.format(**args))
-
-    return None
-## ========================================================================
-
-
-def __elastic_net_gaussian_fista_train(schema_madlib, tbl_source, col_ind_var,
-                                       col_dep_var, tbl_result, lambda_value, alpha,
-                                       normalization, optimizer_params, max_iter,
-                                       tolerance, outstr_array, **kwargs):
-    __elastic_net_validate_args(tbl_source, col_ind_var, col_dep_var, tbl_result,
-                                lambda_value, alpha, normalization, max_iter,
-                                tolerance)
-
-    return __elastic_net_gaussian_fista_train_compute(
-        schema_madlib, tbl_source, col_ind_var, col_dep_var, tbl_result,
-        lambda_value, alpha, normalization, optimizer_params, max_iter,
-        tolerance, outstr_array, **kwargs)
-## ========================================================================
-
-
-def __elastic_net_gaussian_fista_train_compute(
-        schema_madlib, tbl_source, col_ind_var, col_dep_var, tbl_result,
-        lambda_value, alpha, normalization, optimizer_params, max_iter,
-        tolerance, outstr_array, **kwargs):
-    """
-    Fit linear model with elastic net regularization using FISTA optimization.
-
-    @param tbl_source        Name of data source table
-    @param col_ind_var       Name of independent variable column,
-                             independent variable is an array
-    @param col_dep_var       Name of dependent variable column
-    @param tbl_result        Name of the table to store the results,
-                             will return fitting coefficients and
-                             likelihood
-    @param lambda_value      The regularization parameter
-    @param alpha             The elastic net parameter, [0, 1]
-    @param normalization     Whether to normalize the variables
-    @param optimizer_params  Parameters of the above optimizer, the format
-                             is '{arg = value, ...}'::varchar[]
-    """
-    old_msg_level = plpy.execute("""
-                                 select setting from pg_settings
-                                 where name='client_min_messages'
-                                 """)[0]['setting']
-    plpy.execute("set client_min_messages to error")
-
-    (dimension, row_num) = __tbl_dimension_rownum(schema_madlib, tbl_source, col_ind_var)
-
-    # generate a full dict to ease the following string format
-    # including several temporary table names
-    args = __fista_construct_dict(
-        schema_madlib, tbl_source, col_ind_var, col_dep_var, tbl_result,
-        dimension, row_num, lambda_value, alpha, normalization,
-        max_iter, tolerance, outstr_array,
-        __fista_params_parser(optimizer_params, lambda_value, schema_madlib))
-
-    # use normalized data or not
-    if normalization:
-        __normalize_data(args)
-        tbl_used = args["tbl_data_scaled"]
-        args["col_ind_var_new"] = args["col_ind_var_norm_new"]
-        args["col_dep_var_new"] = args["col_dep_var_norm_new"]
-    else:
-        __compute_data_scales(args)
-        tbl_used = tbl_source
-        args["col_ind_var_new"] = col_ind_var
-        args["col_dep_var_new"] = col_dep_var
-
-    args["tbl_used"] = tbl_used
-
-    if args["warmup_lambdas"] is not None:
-        args["warm_no"] = len(args["warmup_lambdas"])
-        args["warmup_lambdas"] = _array_to_string(args["warmup_lambdas"])
-
-    if args["warmup"] and args["warmup_lambdas"] is None:
-        # average squares of each feature
-        # used to estimate the largest lambda value
-        args["sq"] = __compute_average_sq(**args)
-        args["warmup_lambdas"] = __generate_warmup_lambda_sequence(
-            tbl_used, args["col_ind_var_new"], args["col_dep_var_new"],
-            dimension, row_num, lambda_value, alpha,
-            args["warmup_lambda_no"], args["sq"])
-        args["warm_no"] = len(args["warmup_lambdas"])
-        args["warmup_lambdas"] = _array_to_string(args["warmup_lambdas"])
-    elif args["warmup"] is False:
-        args["warm_no"] = 1
-        args["warmup_lambdas"] = _array_to_string([lambda_value])
-
-    # create the temp table that passes parameter values to FISTA optimizer
-    __fista_create_tbl_args(**args)
-
-    # perform the actual calculation
-    iteration_run = __compute_gaussian_fista(
-        schema_madlib, args["tbl_fista_args"],
-        args["tbl_fista_state"], tbl_used,
-        args["col_ind_var_new"],
-        args["col_dep_var_new"],
-        start_iter=0,
-        max_iter=args["max_iter"],
-        warm_no=args["warm_no"],
-        use_active_set=args["use_active_set"],
-        dimension_name=args["dimension_name"],
-        lambda_name=args["lambda_name"],
-        alpha_name=args["alpha_name"],
-        total_rows_name=args["total_rows_name"],
-        max_iter_name=args["max_iter_name"],
-        xmean_name=args["xmean_name"],
-        ymean_name=args["ymean_name"],
-        max_stepsize_name=args["max_stepsize_name"],
-        eta_name=args["eta_name"],
-        activeset_name=args["activeset_name"],
-        tolerance_name=args["tolerance_name"])
-
-    __elastic_net_generate_result("fista", iteration_run, **args)
-
-    # cleanup
-    __fista_cleanup_temp_tbls(**args)
-    plpy.execute("set client_min_messages to " + old_msg_level)
-    return None
-## ========================================================================
-
-
-def __compute_gaussian_fista(schema_madlib, tbl_args, tbl_state, tbl_source,
-                             col_ind_var, col_dep_var, start_iter, **kwargs):
-    """
-    Driver function for elastic net with Gaussian response using FISTA
-
-    @param schema_madlib Name of the MADlib schema, properly escaped/quoted
-    @param tbl_args Name of the (temporary) table containing all non-template
-        arguments
-    @param tbl_state Name of the (temporary) table containing the inter-iteration
-        states
-    @param rel_source Name of the relation containing input points
-    @param col_ind_var Name of the independent variables column
-    @param col_dep_var Name of the dependent variable column
-    @param drop_table Boolean, whether to use IterationController (True) or
-                      IterationControllerNoTableDrop (False)
-    @param kwargs We allow the caller to specify additional arguments (all of
-        which will be ignored though). The purpose of this is to allow the
-        caller to unpack a dictionary whose element set is a superset of
-        the required arguments by this function.
-
-    @return The iteration number (i.e., the key) with which to look up the
-        result in \c tbl_state
-    """
-    iterationCtrl = IterationController2S(  # TableAppend(
-        rel_args=tbl_args,
-        rel_state=tbl_state,
-        stateType="double precision[]",
-        truncAfterIteration=False,
-        schema_madlib=schema_madlib,  # Identifiers start here
-        rel_source=tbl_source,
-        col_ind_var=col_ind_var,
-        col_dep_var=col_dep_var,
-        lambda_count=1,
-        is_active=0,
-        **kwargs)
-
-    state_size = None
-
-    with iterationCtrl as it:
-        it.iteration = start_iter
-        while True:
-            # manually add the intercept term
-            it.update("""
-                      select
-                          {schema_madlib}.__gaussian_fista_step(
-                              ({col_ind_var})::double precision[],
-                              ({col_dep_var})::double precision,
-
-                              m4_ifdef(<!__HAWQ__!>, <!{{__state__}}!>,
-                              <!(select _state from {rel_state}
-                                  where _iteration = {iteration})!>),
-
-                              (_args.{lambda_name}[{lambda_count}])::double precision,
-                              (_args.{alpha_name})::double precision,
-                              (_args.{dimension_name})::integer,
-                              (_args.{xmean_name})::double precision[],
-                              (_args.{ymean_name})::double precision,
-                              1::double precision,
-                              (_args.{total_rows_name})::integer,
-                              (_args.{max_stepsize_name})::double precision,
-                              (_args.{eta_name})::double precision,
-                              (_args.{activeset_name})::integer,
-                              {is_active}::integer
-                          )
-                      from {rel_source} as _src, {rel_args} as _args
-                      """)
-
-            if it.kwargs["use_active_set"] == 1:
-                if state_size is None:
-                    m4_ifdef(<!__HAWQ__!>,
-                    <!state_size = it.get_state_size()!>,
-                    <!state_size = plpy.execute(
-                        """
-                        select array_upper(_state, 1) as size
-                        from {rel_state} limit 1;
-                        """.format(**it.kwargs))[0]["size"]!>)
-
-                m4_ifdef(<!__HAWQ__!>,
-                <!is_backtracking = it.get_state_value(state_size - 1)!>,
-                <!is_backtracking = plpy.execute(
-                    """
-                    select _state[{state_size}] as backtracking
-                    from {rel_state}
-                    where _iteration = {iteration}
-                    """.format(state_size = state_size,
-                               iteration = it.iteration,
-                               **it.kwargs))[0]["backtracking"]!>)
-
-                if it.test(
-                    """
-                    {iteration} >= _args.{max_iter_name}
-                    or
-                    {schema_madlib}.__gaussian_fista_state_diff(
-                        _state_previous, _state_current) < _args.{tolerance_name}
-                    """):
-                    if it.iteration < it.kwargs["max_iter"]:
-                        if it.kwargs["is_active"] == 0:
-                            if (it.kwargs["lambda_count"] < it.kwargs["warm_no"]):
-                                it.kwargs["lambda_count"] += 1
-                            else:
-                                break
-                        else:
-                            it.kwargs["is_active"] = 0
-                    else:
-                        break
-                else:
-                    # change active state only outside of backtracking
-                    if is_backtracking == 0:
-                        it.kwargs["is_active"] = 1
-            else:
-                if it.test(
-                    """
-                    {iteration} >= _args.{max_iter_name} or
-                    {schema_madlib}.__gaussian_fista_state_diff(
-                        _state_previous, _state_current
-                            ) < _args.{tolerance_name}
-                    """):
-                    if (it.iteration < it.kwargs["max_iter"] and
-                        it.kwargs["lambda_count"] < it.kwargs["warm_no"]):
-                        it.kwargs["lambda_count"] += 1
-                    else:
-                        break
-
-        if it.kwargs["lambda_count"] < it.kwargs["warm_no"]:
-            plpy.error("""
-                       Elastic Net error: The final target lambda value is not
-                       reached in warm-up iterations. You need more iterations!
-                       """)
-
-    return iterationCtrl.iteration

http://git-wip-us.apache.org/repos/asf/incubator-madlib/blob/6939fd63/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_igd.py_in
----------------------------------------------------------------------
diff --git a/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_igd.py_in b/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_igd.py_in
deleted file mode 100644
index d9f9c2d..0000000
--- a/src/ports/postgres/modules/elastic_net/elastic_net_gaussian_igd.py_in
+++ /dev/null
@@ -1,451 +0,0 @@
-
-import plpy
-import math
-from utilities.utilities import unique_string
-from utilities.control import IterationController2S
-from elastic_net_utils import IterationControllerNoTableDrop
-from elastic_net_utils import __compute_means
-from elastic_net_utils import __normalize_data
-from elastic_net_utils import __compute_data_scales
-from elastic_net_utils import __tbl_dimension_rownum
-from elastic_net_utils import __elastic_net_validate_args
-from utilities.utilities import _array_to_string
-from elastic_net_utils import __compute_average_sq
-from elastic_net_utils import __generate_warmup_lambda_sequence
-from elastic_net_utils import __process_warmup_lambdas
-from elastic_net_generate_result import __elastic_net_generate_result
-from utilities.utilities import __mad_version
-from utilities.utilities import preprocess_keyvalue_params
-
-version_wrapper = __mad_version()
-mad_vec = version_wrapper.select_vecfunc()
-
-## ========================================================================
-
-def __igd_params_parser(optimizer_params, lambda_value, schema_madlib):
-    """
-    Parse IGD parameters.
-    """
-    allowed_params = set(["stepsize", "warmup", "warmup_lambdas",
-                          "warmup_lambda_no", "threshold", "parallel"])
-    name_value = dict()
-    # default values
-    name_value["parallel"] = True
-    name_value["stepsize"] = 0.01
-    name_value["warmup"] = False
-    name_value["warmup_lambdas"] = None
-    name_value["warmup_lambda_no"] = 15
-    name_value["threshold"] = 1e-10
-
-    warmup_lambdas = None
-    warmup_lambda_no = None
-
-    if optimizer_params is None:
-        return name_value
-
-    for s in preprocess_keyvalue_params(optimizer_params):
-        items = s.split("=")
-        if (len(items) != 2):
-            plpy.error("Elastic Net error: Optimizer parameter list has incorrect format!")
-        param_name = items[0].strip(" \"").lower()
-        param_value = items[1].strip(" \"").lower()
-
-        if param_name not in allowed_params:
-            plpy.error(
-                """
-                Elastic Net error: {param_name} is not a valid parameter name for the IGD optimizer.
-                Run:
-
-                SELECT {schema_madlib}.elastic_net_train('igd');
-
-                to see the parameters for IGD algorithm.
-                """.format(param_name = param_name,
-                           schema_madlib = schema_madlib))
-
-        if param_name == "stepsize":
-            try:
-                name_value["stepsize"] = float(param_value)
-            except:
-                plpy.error("Elastic Net error: stepsize must be a float number!")
-
-        if param_name == "warmup":
-            if param_value in ["true", "t", "yes", "y"]:
-                name_value["warmup"] = True
-            elif param_value in ["false", "f", "no", "n"]:
-                name_value["warmup"] = False
-            else:
-                plpy.error("Elastic Net error: Do you need warmup (True/False or yes/no) ?")
-
-        if param_name == "warmup_lambdas" and param_value != "null":
-            warmup_lambdas = param_value
-
-        if param_name == "warmup_lambda_no":
-            warmup_lambda_no = param_value
-
-        if param_name == "threshold":
-            try:
-                name_value["threshold"] = float(param_value)
-            except:
-                plpy.error("Elastic Net error: threshold must be a float number!")
-
-        if param_name == "parallel":
-            if param_value in ["true", "t", "yes", "y"]:
-                name_value["parallel"] = True
-            elif param_value in ["false", "f", "no", "n"]:
-                name_value["parallel"] = False
-            else:
-                plpy.error("Elastic Net error: Do you need parallel (True/False or yes/no) ? IGD in parallel might be slower !")
-
-    if name_value["warmup"]:
-        if warmup_lambdas is not None:
-            # errors are handled in __process_warmup_lambdas
-            name_value["warmup_lambdas"] = __process_warmup_lambdas(warmup_lambdas, lambda_value)
-        if warmup_lambda_no is not None:
-            try:
-                name_value["warmup_lambda_no"] = int(warmup_lambda_no)
-            except:
-                plpy.error("Elastic Net error: warmup_lambda_no must be an integer!")
-
-
-    # validate the parameters
-    if name_value["stepsize"] <= 0:
-        plpy.error("Elastic Net error: step size must be positive!")
-
-    if (name_value["warmup"] and name_value["warmup_lambdas"] is None and
-        name_value["warmup_lambda_no"] < 1):
-        plpy.error("Elastic Net error: Number of warm-up lambdas must be a positive integer!")
-
-    if name_value["threshold"] < 0:
-        plpy.error("Elastic Net error: A positive threshold is needed to screen out tiny values around zero!")
-
-    return name_value
-
-## ========================================================================
-
-def __igd_create_tbl_args(**args):
-    """
-    create the temporary schema and argument table used in IGD iterations
-    """
-    (xmean_str0, ymean) = __compute_means(**args)
-
-    plpy.execute("""
-                 drop table if exists {tbl_igd_args};
-                 create temp table {tbl_igd_args} (
-                    {dimension_name}       integer,
-                    {stepsize_name}        double precision,
-                    {lambda_name}          double precision[],
-                    {alpha_name}           double precision,
-                    {total_rows_name}      integer,
-                    {max_iter_name}        integer,
-                    {tolerance_name}       double precision,
-                    {xmean_name}           double precision[],
-                    {ymean_name}           double precision
-                 );
-                 """.format(**args))
-    plpy.execute("""
-                 insert into {tbl_igd_args} values
-                    ({dimension}, {stepsize}, '{warmup_lambdas}'::double precision[],
-                     {alpha},
-                     {row_num}, {max_iter}, {tolerance},
-                     '{xmean_str0}'::double precision[], {ymean})
-                 """.format(xmean_str0 = xmean_str0, ymean = ymean,
-                            **args))
-
-    return None
-
-## ========================================================================
-
-def __igd_construct_dict(schema_madlib, tbl_source, col_ind_var, col_dep_var,
-                         tbl_result, dimension, row_num, lambda_value, alpha,
-                         normalization, max_iter, tolerance, outstr_array,
-                         optimizer_params_dict):
-    """
-    Construct the dict used by a series of SQL queries in IGD optimizer.
-    """
-    args = dict(schema_madlib = schema_madlib, tbl_source = tbl_source,
-                tbl_data = tbl_source, # argument name used in normalization
-                col_ind_var = col_ind_var, col_dep_var = col_dep_var,
-                col_ind_var_norm_new = unique_string(), # for normalization usage
-                col_ind_var_tmp = unique_string(),
-                col_dep_var_norm_new = unique_string(), # for normalization usage
-                col_dep_var_tmp = unique_string(),
-                tbl_result = tbl_result,
-                lambda_value = lambda_value, alpha = alpha,
-                dimension = dimension, row_num = row_num,
-                max_iter = max_iter, tolerance = tolerance,
-                outstr_array = outstr_array,
-                normalization = normalization)
-
-    # Add the optimizer parameters
-    args.update(optimizer_params_dict)
-
-    # Table names useful when normalizing the original data
-    # Note: in order to be consistent with the calling convention
-    # of the normalization functions, multiple elements of the dict
-    # actually have the same value. This is a price that one has to pay
-    # if he wants to save typing argument names by using **args as the
-    # function argument.
-    tbl_ind_scales = unique_string()
-    tbl_dep_scale = unique_string()
-    tbl_data_scaled = unique_string()
-    args.update(tbl_dep_scale = tbl_dep_scale,
-                tbl_ind_scales = tbl_ind_scales,
-                tbl_data_scaled = tbl_data_scaled)
-
-    # Table names used in IGD iterations
-    args.update(tbl_igd_state = unique_string(),
-                tbl_igd_args = unique_string())
-
-    # more, for args table
-    args["dimension_name"] = unique_string()
-    args["stepsize_name"] = unique_string()
-    args["lambda_name"] = unique_string()
-    args["alpha_name"] = unique_string()
-    args["total_rows_name"] = unique_string()
-    args["max_iter_name"] = unique_string()
-    args["tolerance_name"] = unique_string()
-    args["xmean_name"] = unique_string()
-    args["ymean_name"] = unique_string()
-
-    return args
-
-## ========================================================================
-
-def __igd_cleanup_temp_tbls(**args):
-    """
-    Drop all temporary tables used by IGD optimizer,
-    including tables used in the possible normalization
-    and IGD iterations.
-    """
-    plpy.execute("""
-                 drop table if exists {tbl_ind_scales};
-                 drop table if exists {tbl_dep_scale};
-                 drop table if exists {tbl_data_scaled};
-                 drop table if exists {tbl_igd_args};
-                 drop table if exists pg_temp.{tbl_igd_state};
-                 """.format(**args))
-    return None
-
-## ========================================================================
-
-def __elastic_net_gaussian_igd_train(schema_madlib, tbl_source, col_ind_var,
-                                     col_dep_var, tbl_result, lambda_value, alpha,
-                                     normalization, optimizer_params, max_iter,
-                                     tolerance, outstr_array, **kwargs):
-    __elastic_net_validate_args(tbl_source, col_ind_var, col_dep_var, tbl_result,
-                                lambda_value, alpha, normalization, max_iter, tolerance)
-
-    return __elastic_net_gaussian_igd_train_compute(schema_madlib, tbl_source, col_ind_var,
-                                                    col_dep_var, tbl_result, lambda_value, alpha,
-                                                    normalization, optimizer_params, max_iter,
-                                                    tolerance, outstr_array, **kwargs)
-
-## ========================================================================
-
-def __elastic_net_gaussian_igd_train_compute(schema_madlib, tbl_source, col_ind_var,
-                                             col_dep_var, tbl_result, lambda_value, alpha,
-                                             normalization, optimizer_params, max_iter,
-                                             tolerance, outstr_array, **kwargs):
-    """
-    Fit linear model with elastic net regularization using IGD optimization.
-
-    @param tbl_source        Name of data source table
-    @param col_ind_var       Name of independent variable column,
-                             independent variable is an array
-    @param col_dep_var       Name of dependent variable column
-    @param tbl_result        Name of the table to store the results,
-                             will return fitting coefficients and
-                             likelihood
-    @param lambda_value      The regularization parameter
-    @param alpha             The elastic net parameter, [0, 1]
-    @param normalization     Whether to normalize the variables
-    @param optimizer_params  Parameters of the above optimizer, the format
-                             is '{arg = value, ...}'::varchar[]
-    """
-    old_msg_level = plpy.execute("""
-                                 select setting from pg_settings
-                                 where name='client_min_messages'
-                                 """)[0]['setting']
-    plpy.execute("set client_min_messages to error")
-
-    (dimension, row_num) = __tbl_dimension_rownum(schema_madlib, tbl_source, col_ind_var)
-
-    # generate a full dict to ease the following string format
-    # including several temporary table names
-    args = __igd_construct_dict(schema_madlib, tbl_source, col_ind_var, col_dep_var, tbl_result,
-                                dimension, row_num, lambda_value, alpha, normalization,
-                                max_iter, tolerance, outstr_array,
-                                __igd_params_parser(optimizer_params, lambda_value, schema_madlib))
-
-    # use normalized data or not
-    if normalization:
-        __normalize_data(args)
-        args["tbl_used"] = args["tbl_data_scaled"]
-        args["col_ind_var_new"] = args["col_ind_var_norm_new"]
-        args["col_dep_var_new"] = args["col_dep_var_norm_new"]
-    else:
-        __compute_data_scales(args)
-        args["tbl_used"] = tbl_source
-        args["col_ind_var_new"] = col_ind_var
-        args["col_dep_var_new"] = col_dep_var
-
-    # average squares of each feature
-    # used to estimate the largest lambda value
-    # also used to screen out tiny values, so order is needed
-    args["sq"] = __compute_average_sq(**args)
-    args["sq_str"] = _array_to_string(args["sq"])
-
-    if args["warmup_lambdas"] is not None:
-        args["warm_no"] = len(args["warmup_lambdas"])
-        args["warmup_lambdas"] = _array_to_string(args["warmup_lambdas"])
-
-    if args["warmup"] and args["warmup_lambdas"] is None:
-        args["warmup_lambdas"] = __generate_warmup_lambda_sequence(args["tbl_used"], args["col_ind_var_new"],
-                                                                   args["col_dep_var_new"],
-                                                                   dimension, row_num, lambda_value, alpha,
-                                                                   args["warmup_lambda_no"], args["sq"])
-        args["warm_no"] = len(args["warmup_lambdas"])
-        args["warmup_lambdas"] = _array_to_string(args["warmup_lambdas"])
-    elif args["warmup"] is False:
-        args["warm_no"] = 1
-        args["warmup_lambdas"] = _array_to_string([lambda_value]) # only one value
-
-    # create the temp table that passes parameter values to IGD optimizer
-    __igd_create_tbl_args(**args)
-
-    # perform the actual calculation
-    iteration_run = __compute_gaussian_igd(schema_madlib, args["tbl_igd_args"],
-                                           args["tbl_igd_state"], args["tbl_used"],
-                                           args["col_ind_var_new"], args["col_dep_var_new"],
-                                           True,
-                                           max_iter = args["max_iter"],
-                                           warm_no = args["warm_no"],
-                                           parallel = args["parallel"],
-                                           col_ind_var_new = args["col_ind_var_new"],
-                                           col_dep_var_new = args["col_dep_var_new"],
-                                           dimension_name = args["dimension_name"],
-                                           stepsize_name = args["stepsize_name"],
-                                           lambda_name = args["lambda_name"],
-                                           alpha_name = args["alpha_name"],
-                                           total_rows_name = args["total_rows_name"],
-                                           max_iter_name = args["max_iter_name"],
-                                           tolerance_name = args["tolerance_name"],
-                                           xmean_name = args["xmean_name"],
-                                           ymean_name = args["ymean_name"])
-
-    __elastic_net_generate_result("igd", iteration_run, **args)
-
-    # cleanup
-    __igd_cleanup_temp_tbls(**args)
-    plpy.execute("set client_min_messages to " + old_msg_level)
-    return None
-
-## ========================================================================
-
-def __compute_gaussian_igd(schema_madlib, tbl_args, tbl_state, tbl_source,
-                           col_ind_var, col_dep_var, drop_table, **kwargs):
-    """
-    Driver function for elastic net with Gaussian response using IGD
-
-    @param schema_madlib Name of the MADlib schema, properly escaped/quoted
-    @param tbl_args Name of the (temporary) table containing all non-template
-        arguments
-    @param tbl_state Name of the (temporary) table containing the inter-iteration
-        states
-    @param rel_source Name of the relation containing input points
-    @param col_ind_var Name of the independent variables column
-    @param col_dep_var Name of the dependent variable column
-    @param drop_table Boolean, whether to use IterationController (True) or
-                      IterationControllerNoTableDrop (False)
-    @param kwargs We allow the caller to specify additional arguments (all of
-        which will be ignored though). The purpose of this is to allow the
-        caller to unpack a dictionary whose element set is a superset of
-        the required arguments by this function.
-
-    @return The iteration number (i.e., the key) with which to look up the
-        result in \c tbl_state
-    """
-    m4_ifdef(`__HAWQ__', `
-    iterationCtrl = IterationController2S(
-        rel_args = tbl_args,
-        rel_state = tbl_state,
-        stateType = "double precision[]",
-        truncAfterIteration = False,
-        schema_madlib = schema_madlib, # Identifiers start here
-        rel_source = tbl_source,
-        col_ind_var = col_ind_var,
-        col_dep_var = col_dep_var,
-        lambda_count = 1,
-        **kwargs)
-    ', `
-    if drop_table:
-        iterationCtrl = IterationController2S(
-            rel_args = tbl_args,
-            rel_state = tbl_state,
-            stateType = "double precision[]",
-            truncAfterIteration = False,
-            schema_madlib = schema_madlib, # Identifiers start here
-            rel_source = tbl_source,
-            col_ind_var = col_ind_var,
-            col_dep_var = col_dep_var,
-            lambda_count = 1,
-            **kwargs)
-    else:
-        iterationCtrl = IterationControllerNoTableDrop(
-            rel_args = tbl_args,
-            rel_state = tbl_state,
-            stateType = "double precision[]",
-            truncAfterIteration = False,
-            schema_madlib = schema_madlib, # Identifiers start here
-            rel_source = tbl_source,
-            col_ind_var = col_ind_var,
-            col_dep_var = col_dep_var,
-            lambda_count = 1,
-            **kwargs)
-    ')
-
-    with iterationCtrl as it:
-        it.iteration = 0
-
-        if it.kwargs["parallel"]:
-            it.kwargs["parallel_step_func"] = "__gaussian_igd_step"
-        else:
-            it.kwargs["parallel_step_func"] = "__gaussian_igd_step_single_seg"
-
-        while True:
-            # manually add the intercept term
-            it.update("""
-                      select
-                        {schema_madlib}.{parallel_step_func}(
-                            {col_ind_var}::double precision[],
-                            {col_dep_var}::double precision,
-                            m4_ifdef(`__HAWQ__', `({{__state__}})',
-                            `(select _state from {rel_state}
-                                where _iteration = {iteration})'),
-                            (_args.{lambda_name}[{lambda_count}])::double precision,
-                            (_args.{alpha_name})::double precision,
-                            (_args.{dimension_name})::integer,
-                            (_args.{stepsize_name})::double precision,
-                            (_args.{total_rows_name})::integer,
-                            (_args.{xmean_name})::double precision[],
-                            (_args.{ymean_name})::double precision)
-                      from {rel_source} as _src, {rel_args} as _args
-                      """)
-
-            if it.test("""
-                       {iteration} > _args.{max_iter_name} or
-                       {schema_madlib}.__gaussian_igd_state_diff(
-                            _state_previous, _state_current) < _args.{tolerance_name}
-                       """):
-                if (it.iteration < it.kwargs["max_iter"] and
-                    it.kwargs["lambda_count"] < it.kwargs["warm_no"]):
-                    it.kwargs["lambda_count"] += 1
-                else:
-                    break
-        if it.kwargs["lambda_count"] < it.kwargs["warm_no"]:
-            plpy.error("""
-                       Elastic Net error: The final target lambda value is not
-                       reached in warm-up iterations. You need more iterations!
-                       """)
-
-    return iterationCtrl.iteration