blob: 320bf190ab09d95406ffa3abaa9342b5112a75ea [file] [log] [blame]
<!-- HTML header for doxygen 1.8.4-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<meta name="keywords" content="madlib,postgres,greenplum,machine learning,data mining,deep learning,ensemble methods,data science,market basket analysis,affinity analysis,pca,lda,regression,elastic net,huber white,proportional hazards,k-means,latent dirichlet allocation,bayes,support vector machines,svm"/>
<title>MADlib: Elastic Net Regularization</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</script>
<!-- hack in the navigation tree -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="madlib_extra.css" rel="stylesheet" type="text/css"/>
<!-- google analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45382226-1', 'madlib.net');
ga('send', 'pageview');
</script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><a href="http://madlib.net"><img alt="Logo" src="madlib.png" height="50" style="padding-left:0.5em;" border="0"/ ></a></td>
<td style="padding-left: 0.5em;">
<div id="projectname">
<span id="projectnumber">1.9.1</span>
</div>
<div id="projectbrief">User Documentation for MADlib</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__grp__elasticnet.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Elastic Net Regularization<div class="ingroups"><a class="el" href="group__grp__super.html">Supervised Learning</a> &raquo; <a class="el" href="group__grp__regml.html">Regression Models</a></div></div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><b>Contents</b></p><ul>
<li class="level1">
<a href="#train">Training Function</a> </li>
<li class="level1">
<a href="#optimizer">Optimizer Parameters</a> </li>
<li class="level1">
<a href="#predict">Prediction Functions</a> </li>
<li class="level1">
<a href="#examples">Examples</a> </li>
<li class="level1">
<a href="#background">Technical Background</a> </li>
<li class="level1">
<a href="#literature">Literature</a> </li>
<li class="level1">
<a href="#related">Related Topics</a> </li>
</ul>
</div><p>This module implements elastic net regularization for linear and logistic regression problems.</p>
<p><a class="anchor" id="train"></a></p><dl class="section user"><dt>Training Function</dt><dd>The training function has the following syntax: <pre class="syntax">
elastic_net_train( 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
)
</pre></dd></dl>
<p><b>Arguments</b> </p><dl class="arglist">
<dt>tbl_source </dt>
<dd><p class="startdd">TEXT. The name of the table containing the training data.</p>
<p class="enddd"></p>
</dd>
<dt>tbl_result </dt>
<dd><p class="startdd">TEXT. Name of the generated table containing the output model. The output table produced by the <a class="el" href="elastic__net_8sql__in.html#a735038a5090c112505c740a90a203e83" title="Interface for elastic net. ">elastic_net_train()</a> function has the following columns: </p><table class="output">
<tr>
<th>regress_family </th><td>The regression type: 'gaussian' or 'binomial'. </td></tr>
<tr>
<th>features </th><td>An array of the features (independent variables) passed into the analysis. </td></tr>
<tr>
<th>features_selected </th><td>An array of the features selected by the analysis. </td></tr>
<tr>
<th>coef_nonzero </th><td>Fitting coefficients for the selected features. </td></tr>
<tr>
<th>coef_all </th><td>Coefficients for all selected and unselected features </td></tr>
<tr>
<th>intercept </th><td>Fitting intercept for the model. </td></tr>
<tr>
<th>log_likelihood </th><td>The negative value of the first equation above (up to a constant depending on the data set). </td></tr>
<tr>
<th>standardize </th><td>BOOLEAN. Whether the data was normalized (<em>standardize</em> argument was TRUE). </td></tr>
<tr>
<th>iteration_run </th><td>The number of iterations executed. </td></tr>
</table>
<p class="enddd"></p>
</dd>
<dt>col_dep_var </dt>
<dd><p class="startdd">TEXT. An expression for the dependent variable.</p>
<p>Both <em>col_dep_var</em> and <em>col_ind_var</em> can be valid Postgres expressions. For example, <code>col_dep_var = 'log(y+1)'</code>, and <code>col_ind_var = 'array[exp(x[1]), x[2], 1/(1+x[3])]'</code>. In the binomial case, you can use a Boolean expression, for example, <code>col_dep_var = 'y &lt; 0'</code>.</p>
<p class="enddd"></p>
</dd>
<dt>col_ind_var </dt>
<dd><p class="startdd">TEXT. An expression for the independent variables. Use <code>'*'</code> to specify all columns of <em>tbl_source</em> except those listed in the <em>excluded</em> string. If <em>col_dep_var</em> is a column name, it is automatically excluded from the independent variables. However, if <em>col_dep_var</em> is a valid Postgres expression, any column names used within the expression are only excluded if they are explicitly included in the <em>excluded</em> argument. It is a good idea to add all column names involved in the dependent variable expression to the <em>excluded</em> string.</p>
<p class="enddd"></p>
</dd>
<dt>regress_family </dt>
<dd><p class="startdd">TEXT. The regression type, either 'gaussian' ('linear') or 'binomial' ('logistic').</p>
<p class="enddd"></p>
</dd>
<dt>alpha </dt>
<dd><p class="startdd">FLOAT8. Elastic net control parameter, value in [0, 1], 1 for L-1 regularization, 0 for L-2.</p>
<p class="enddd"></p>
</dd>
<dt>lambda_value </dt>
<dd><p class="startdd">FLOAT8. Regularization parameter, positive.</p>
<p class="enddd"></p>
</dd>
<dt>standardize (optional) </dt>
<dd><p class="startdd">BOOLEAN, default: TRUE. Whether to normalize the data. Setting this to TRUE usually yields better results and faster convergence.</p>
<p class="enddd"></p>
</dd>
<dt>grouping_col (optional) </dt>
<dd><p class="startdd">TEXT, default: NULL.</p>
<dl class="section note"><dt>Note</dt><dd><em>Not currently implemented. Any non-NULL value is ignored. Grouping support will be added in a future release. </em> When implemented, an expression list will be used to group the input dataset into discrete groups, running one regression per group. Similar to the SQL <code>GROUP BY</code> clause. When this value is NULL, no grouping is used and a single result model is generated.</dd></dl>
</dd>
<dt>optimizer (optional) </dt>
<dd><p class="startdd">TEXT, default: 'fista'. Name of optimizer, either 'fista' or 'igd'.</p>
<p class="enddd"></p>
</dd>
<dt>optimizer_params (optional) </dt>
<dd><p class="startdd">TEXT, default: NULL. Optimizer parameters, delimited with commas. The parameters differ depending on the value of <em>optimizer</em>. See the descriptions below for details.</p>
<p class="enddd"></p>
</dd>
<dt>excluded (optional) </dt>
<dd><p class="startdd">TEXT, default: NULL. If the <em>col_ind_var</em> input is '*' then <em>excluded</em> can be provided as a comma-delimited list of column names that are to be excluded from the features. For example, <code>'col1, col2'</code>. If the <em>col_ind_var</em> is an array, <em>excluded</em> must be a list of the integer array positions to exclude, for example <code>'1,2'</code>. If this argument is NULL or an empty string <code>''</code>, no columns are excluded.</p>
<p class="enddd"></p>
</dd>
<dt>max_iter (optional) </dt>
<dd><p class="startdd">INTEGER, default: 1000. The maximum number of iterations that are allowed.</p>
<p class="enddd"></p>
</dd>
<dt>tolerance </dt>
<dd>FLOAT8, default: default is 1e-6. The criteria to end iterations. Both the 'fista' and 'igd' optimizers compute the difference between the loglikelihood of two consecutive iterations, and when the difference is smaller than <em>tolerance</em> or the iteration number is larger than <em>max_iter</em>, the computation stops. </dd>
</dl>
<p><a class="anchor" id="optimizer"></a></p><dl class="section user"><dt>Optimizer Parameters</dt><dd>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 "&lt;param_name&gt; = &lt;value&gt;" to specify the value of a parameter, otherwise the parameter is ignored.</dd></dl>
<p>When the <a class="el" href="elastic__net_8sql__in.html#a735038a5090c112505c740a90a203e83">elastic_net_train()</a> <em>optimizer</em> argument value is <b>'fista'</b>, the <em>optimizer_params</em> argument is a string containing name-value pairs with the following format. (Line breaks are inserted for readability.) </p><pre class="syntax">
'max_stepsize = &lt;value&gt;,
eta = &lt;value&gt;,
warmup = &lt;value&gt;,
warmup_lambdas = &lt;value&gt;,
warmup_lambda_no = &lt;value&gt;,
warmup_tolerance = &lt;value&gt;,
use_active_set = &lt;value&gt;,
activeset_tolerance = &lt;value&gt;,
random_stepsize = &lt;value&gt;'
</pre><p> <b>Parameters</b> </p><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 <em>eta</em> 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 <em>max_stepsize</em> can decrease the computation time by more than 10 times. </dd>
<dt>eta </dt>
<dd><p class="startdd">Default: 2. If stepsize does not work <em>stepsize</em> / <em>eta</em> is tried. Must be greater than 1. </p>
<p class="enddd"></p>
</dd>
<dt>warmup </dt>
<dd><p class="startdd">Default: FALSE. If <em>warmup</em> 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.</p>
<p class="enddd"></p>
</dd>
<dt>warmup_lambdas </dt>
<dd><p class="startdd">Default: NULL. The lambda value series to use when <em>warmup</em> is True. The default is NULL, which means that lambda values will be automatically generated.</p>
<p class="enddd"></p>
</dd>
<dt>warmup_lambda_no </dt>
<dd><p class="startdd">Default: 15. How many lambdas are used in warm-up. If <em>warmup_lambdas</em> is not NULL, this value is overridden by the number of provided lambda values.</p>
<p class="enddd"></p>
</dd>
<dt>warmup_tolerance </dt>
<dd><p class="startdd">The value of tolerance used during warmup. The default is the same as the <em>tolerance</em> argument.</p>
<p class="enddd"></p>
</dd>
<dt>use_active_set </dt>
<dd><p class="startdd">Default: FALSE. If <em>use_active_set</em> 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&mdash;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.</p>
<p class="enddd"></p>
</dd>
<dt>activeset_tolerance </dt>
<dd><p class="startdd">Default: the value of the tolerance argument. The value of tolerance used during active set calculation. </p>
<p class="enddd"></p>
</dd>
<dt>random_stepsize </dt>
<dd>Default: FALSE. Whether to add some randomness to the step size. Sometimes, this can speed up the calculation. </dd>
</dl>
<p>When the <a class="el" href="elastic__net_8sql__in.html#a735038a5090c112505c740a90a203e83">elastic_net_train()</a> <em>optimizer</em> argument value is <b>'igd'</b>, the <em>optimizer_params</em> argument is a string containing name-value pairs with the following format. (Line breaks are inserted for readability.) </p><pre class="syntax">
'stepsize = &lt;value&gt;,
step_decay = &lt;value&gt;,
threshold = &lt;value&gt;,
warmup = &lt;value&gt;,
warmup_lambdas = &lt;value&gt;,
warmup_lambda_no = &lt;value&gt;,
warmup_tolerance = &lt;value&gt;,
parallel = &lt;value&gt;'
</pre><p> <b>Parameters</b> </p><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>
<dt>threshold </dt>
<dd><p class="startdd">Default: 1e-10. When a coefficient is really small, set this coefficient to be 0.</p>
<p class="enddd">Due to the stochastic nature of SGD, we can only obtain very small values for the fitting coefficients. Therefore, <em>threshold</em> 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 <em>threshold</em>, set the original coefficient to zero. </p>
</dd>
<dt>warmup </dt>
<dd>Default: FALSE. If <em>warmup</em> 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 <em>warmup_lambdas</em> is not NULL, this argument is overridden by the size of the <em>warmup_lambdas</em> array. </dd>
<dt>warmup_tolerance </dt>
<dd>The value of tolerance used during warmup.The default is the same as the <em>tolerance</em> argument. </dd>
<dt>parallel </dt>
<dd><p class="startdd">Whether to run the computation on multiple segments. The default is True.</p>
<p class="enddd">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 <em>parallel</em> option to allow you to choose whether to do parallel computation. </p>
</dd>
</dl>
<p><a class="anchor" id="predict"></a></p><dl class="section user"><dt>Prediction Function</dt><dd></dd></dl>
<h4>Per-Tuple Prediction</h4>
<p>The prediction function returns a double value for Gaussian family and boolean value for Binomial family.</p>
<p>The predict function has the following syntax (<a class="el" href="elastic__net_8sql__in.html#a96db4ff4ba3ea363fafbf6c036c19fae" title="Prediction for linear models use learned coefficients for a given example. ">elastic_net_gaussian_predict()</a> and <a class="el" href="elastic__net_8sql__in.html#aa78cde79f1f2caa7c5b38f933001d793" title="Prediction for logistic models use learned coefficients for a given example. ">elastic_net_binomial_predict()</a>): </p><pre class="syntax">
elastic_net_&lt;family&gt;_predict(
coefficients,
intercept,
ind_var
)
</pre><p><b>Arguments</b> </p><dl class="arglist">
<dt>coefficients </dt>
<dd>DOUBLE PRECISION[]. Fitting coefficients, usually coef_all or coef_nonzero. </dd>
<dt>intercept </dt>
<dd>DOUBLE PRECISION. The intercept for the model. </dd>
<dt>ind_var </dt>
<dd>DOUBLE PRECISION[]. Independent variables that correspond to coefficients, use <em>features</em> column in <em>tbl_result</em> for coef_all, and <em>features_selected</em> for coef_nonzero. See also <a href="#additional_example">examples</a>. Note that unexpected results or errors may be returned in the case that this argument is not given properly. </dd>
</dl>
<p>For binomial family, there is a function (<a class="el" href="elastic__net_8sql__in.html#a308718fd5234bc1007b971a639aadf71" title="Compute the probability of belonging to the True class for a given observation. ">elastic_net_binomial_prob()</a>) that outputs the probability of the instance being True: </p><pre class="syntax">
elastic_net_binomial_prob(
coefficients,
intercept,
ind_var
)
</pre><h4>Per-Table Prediction</h4>
<p>Alternatively, you can use another prediction function that stores the prediction result in a table (<a class="el" href="elastic__net_8sql__in.html#a3578608204ac9b2d3442ff42977f632b" title="Prediction and put the result in a table can be used together with General-CV. ">elastic_net_predict()</a>). This is useful if you want to use elastic net together with the general cross validation function. </p><pre class="syntax">
elastic_net_predict( tbl_model,
tbl_new_sourcedata,
col_id,
tbl_predict
)
</pre><p> <b>Arguments</b> </p><dl class="arglist">
<dt>tbl_model </dt>
<dd>TEXT. The name of the table containing the output from the training function. </dd>
<dt>tbl_new_sourcedata </dt>
<dd>TEXT. The name of the table containing the new source data. </dd>
<dt>col_id </dt>
<dd>TEXT. The unique ID associated with each row. </dd>
<dt>tbl_predict </dt>
<dd>TEXT. The name of table to store the prediction result. </dd>
</dl>
<p>You do not need to specify whether the model is "linear" or "logistic" because this information is already included in the <em>tbl_model</em> table.</p>
<p><a class="anchor" id="examples"></a></p><dl class="section user"><dt>Examples</dt><dd></dd></dl>
<ol type="1">
<li>Display online help for the <a class="el" href="elastic__net_8sql__in.html#a735038a5090c112505c740a90a203e83" title="Interface for elastic net. ">elastic_net_train()</a> function. <pre class="example">
SELECT madlib.elastic_net_train();
</pre></li>
<li>Create an input data set. <pre class="example">
DROP TABLE IF EXISTS houses;
CREATE TABLE houses ( id INT,
tax INT,
bedroom INT,
bath FLOAT,
price INT,
size INT,
lot INT
);
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
\.
</pre></li>
<li>Train the model. <pre class="example">
DROP TABLE IF EXISTS houses_en;
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
);
</pre></li>
<li>View the resulting model. <pre class="example">
-- Turn on expanded display to make it easier to read results.
\x on
SELECT * FROM houses_en;
</pre> Result: <pre class="result">
-[ RECORD 1 ]-----+--------------------------------------------
family | gaussian
features | {tax,bath,size}
features_selected | {tax,bath,size}
coef_nonzero | {27.6945611671,11509.0099734,49.0945557639}
coef_all | {27.6945611671,11509.0099734,49.0945557639}
intercept | -11145.5061503
log_likelihood | -490118975.406
standardize | t
iteration_run | 322
</pre></li>
<li>Use the prediction function to evaluate residuals. <pre class="example">
\x off
SELECT id, price, predict, price - predict AS residual
FROM (
SELECT
houses.*,
madlib.elastic_net_gaussian_predict(
m.coef_all,
m.intercept,
ARRAY[tax,bath,size]
) AS predict
FROM houses, houses_en m) s
ORDER BY id;
</pre></li>
</ol>
<p><a class="anchor" id="additional_example"></a></p><h4>Additional Example (when coef_nonzero is different from coef_all)</h4>
<ol type="1">
<li>Reuse the <a href="#examples">houses</a> table above and train the model with alpha=1 (L-1) and a large lambda (30000). <pre class="example">
DROP TABLE IF EXISTS houses_en2;
SELECT madlib.elastic_net_train( 'houses',
'houses_en2',
'price',
'array[tax, bath, size]',
'gaussian',
1,
30000,
TRUE,
NULL,
'fista',
'',
NULL,
10000,
1e-6
);
</pre></li>
<li>View the resulting model and see coef_nonzero is different from coef_all. <pre class="example">
-- Turn on expanded display to make it easier to read results.
\x on
SELECT * FROM houses_en2;
</pre> Result: <pre class="result">
-[ RECORD 1 ]-----+--------------------------------------------
features | {tax,bath,size}
features_selected | {tax,size}
coef_nonzero | {13.3261747481,22.7347986045}
coef_all | {13.3261747481,0,22.7347986045}
intercept | 68877.5045405
log_likelihood | -1694746275.43
standardize | t
iteration_run | 115
</pre></li>
<li>We can still use the prediction function with coef_all to evaluate residuals. <pre class="example">
\x off
SELECT id, price, predict, price - predict AS residual
FROM (
SELECT
houses.*,
madlib.elastic_net_gaussian_predict(
m.coef_all,
m.intercept,
ARRAY[tax,bath,size]
) AS predict
FROM houses, houses_en2 m) s
ORDER BY id;
</pre></li>
<li>While we can also speed up the prediction function with coef_nonzero to evaluate residuals. This requires user to examine the feature_selected column in the result table to construct the correct independent variables. <pre class="example">
\x off
SELECT id, price, predict, price - predict AS residual
FROM (
SELECT
houses.*,
madlib.elastic_net_gaussian_predict(
m.coef_nonzero,
m.intercept,
ARRAY[tax,size]
) AS predict
FROM houses, houses_en2 m) s
ORDER BY id;
</pre> The two queries are expected to give same residuals: <pre class="result">
id | price | predict | residual
----+--------+------------------+-------------------
1 | 50000 | 94245.742567344 | -44245.742567344
3 | 22500 | 93242.914556232 | -70742.914556232
5 | 133000 | 120570.253114742 | 12429.746885258
7 | 260000 | 154482.653115284 | 105517.346884716
9 | 160000 | 127499.863983754 | 32500.136016246
11 | 87000 | 114415.797184981 | -27415.797184981
13 | 140000 | 150201.89180353 | -10201.89180353
15 | 65000 | 110504.97610329 | -45504.97610329
2 | 85000 | 114926.05405835 | -29926.05405835
4 | 90000 | 110026.514757197 | -20026.514757197
6 | 90500 | 105510.375306125 | -15010.375306125
8 | 142500 | 104539.017736473 | 37960.982263527
10 | 240000 | 181347.915720063 | 58652.084279937
12 | 118600 | 118884.405888047 | -284.405888046997
14 | 148000 | 131701.624106042 | 16298.375893958
(15 rows)
</pre></li>
</ol>
<p><a class="anchor" id="notes"></a></p><dl class="section user"><dt>Note</dt><dd>It is <b>strongly</b> <b>recommended</b> that you run <code><a class="el" href="elastic__net_8sql__in.html#a735038a5090c112505c740a90a203e83" title="Interface for elastic net. ">elastic_net_train()</a></code> on a subset of the data with a limited <em>max_iter</em> before applying it to the full data set with a large <em>max_iter</em>. 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.</dd></dl>
<p><a class="anchor" id="background"></a></p><dl class="section user"><dt>Technical Background</dt><dd></dd></dl>
<p>Elastic net regularization seeks to find a weight vector that, for any given training example set, minimizes: </p><p class="formulaDsp">
<img class="formulaDsp" alt="\[\min_{w \in R^N} L(w) + \lambda \left(\frac{(1-\alpha)}{2} \|w\|_2^2 + \alpha \|w\|_1 \right)\]" src="form_81.png"/>
</p>
<p> where <img class="formulaInl" alt="$L$" src="form_82.png"/> is the metric function that the user wants to minimize. Here <img class="formulaInl" alt="$ \alpha \in [0,1] $" src="form_83.png"/> and <img class="formulaInl" alt="$ lambda \geq 0 $" src="form_84.png"/>. If <img class="formulaInl" alt="$alpha = 0$" src="form_85.png"/>, we have the ridge regularization (known also as Tikhonov regularization), and if <img class="formulaInl" alt="$\alpha = 1$" src="form_86.png"/>, we have the LASSO regularization.</p>
<p>For the Gaussian response family (or linear model), we have </p><p class="formulaDsp">
<img class="formulaDsp" alt="\[L(\vec{w}) = \frac{1}{2}\left[\frac{1}{M} \sum_{m=1}^M (w^{t} x_m + w_{0} - y_m)^2 \right] \]" src="form_87.png"/>
</p>
<p>For the Binomial response family (or logistic model), we have </p><p class="formulaDsp">
<img class="formulaDsp" alt="\[ 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]\ , \]" src="form_88.png"/>
</p>
<p> where <img class="formulaInl" alt="$y_m \in {0,1}$" src="form_89.png"/>.</p>
<p>To get better convergence, one can rescale the value of each element of x </p><p class="formulaDsp">
<img class="formulaDsp" alt="\[ x' \leftarrow \frac{x - \bar{x}}{\sigma_x} \]" src="form_90.png"/>
</p>
<p> and for Gaussian case we also let </p><p class="formulaDsp">
<img class="formulaDsp" alt="\[y' \leftarrow y - \bar{y} \]" src="form_91.png"/>
</p>
<p> 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.</p>
<p>Note that fitting after scaling is not equivalent to directly fitting.</p>
<p><a class="anchor" id="literature"></a></p><dl class="section user"><dt>Literature</dt><dd></dd></dl>
<p>[1] Elastic net regularization. <a href="http://en.wikipedia.org/wiki/Elastic_net_regularization">http://en.wikipedia.org/wiki/Elastic_net_regularization</a></p>
<p>[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.</p>
<p>[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.</p>
<p><a class="anchor" id="related"></a></p><dl class="section user"><dt>Related Topics</dt><dd></dd></dl>
<p>File <a class="el" href="elastic__net_8sql__in.html" title="SQL functions for elastic net regularization. ">elastic_net.sql_in</a> documenting the SQL functions.</p>
<p>grp_validation </p>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Tue Sep 20 2016 11:27:01 for MADlib by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
</ul>
</div>
</body>
</html>