{"id":289,"date":"2013-07-09T14:14:02","date_gmt":"2013-07-09T10:44:02","guid":{"rendered":"http:\/\/vua.nadiran.com\/?p=289"},"modified":"2013-07-09T15:12:45","modified_gmt":"2013-07-09T11:42:45","slug":"%d9%85%d8%ab%d8%a7%d9%84-%d9%87%d8%a7%db%8c-%d8%b1%da%af%d8%b1%d8%b3%db%8c%d9%88%d9%86-%d9%87%d8%a7%db%8c-lasso","status":"publish","type":"post","link":"https:\/\/vua.nadiran.com\/?p=289","title":{"rendered":"\u0645\u062b\u0627\u0644 \u0647\u0627\u06cc \u0631\u06af\u0631\u0633\u06cc\u0648\u0646 \u0647\u0627\u06cc Lasso"},"content":{"rendered":"<p dir=\"ltr\" style=\"direction: ltr;\"><a href=\"http:\/\/www.stanford.edu\/~boyd\/papers\/admm\/lasso\/lasso_example.html\">http:\/\/www.stanford.edu\/~boyd\/papers\/admm\/lasso\/lasso_example.html<\/a><\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\"><a href=\"http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node35.html\">http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node35.html<\/a><\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\"><a href=\"http:\/\/www-stat.stanford.edu\/~susan\/courses\/s227\/node5.html\">http:\/\/www-stat.stanford.edu\/~susan\/courses\/s227\/node5.html<\/a><\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\"><a href=\"http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node29.html\">http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node29.html<\/a><\/p>\n<section dir=\"ltr\" id=\"doc_center_content\"><a name=\"bs25w54-1\"><\/a><\/section>\n<div dir=\"ltr\" itemprop=\"content\">\n<h1 itemprop=\"title\" style=\"direction: ltr;\">lasso<\/h1>\n<p><!-- SYNCHTO: _bqzigzm-1.html --><\/p>\n<div>\n<p itemprop=\"purpose\" style=\"direction: ltr;\">Regularized least-squares regression using lasso or elastic net algorithms<\/p>\n<\/div>\n<\/div>\n<h2 dir=\"ltr\" id=\"syntax\" style=\"direction: ltr;\">Syntax<\/h2>\n<div dir=\"ltr\">\n<p style=\"direction: ltr;\">B = lasso(X,Y)<\/p>\n<p style=\"direction: ltr;\"><span style=\"font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;\">[B,FitInfo] = lasso(X,Y)<\/span><\/p>\n<p style=\"direction: ltr;\"><span style=\"font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;\">[B,FitInfo] = lasso(X,Y,Name,Value)<\/span><\/p>\n<\/div>\n<div dir=\"ltr\" style=\"direction: ltr;\"><\/div>\n<div dir=\"ltr\" itemprop=\"content\"><a name=\"bs25w54-2\"><\/a><\/div>\n<h2 style=\"direction: ltr;\">Description<\/h2>\n<p style=\"direction: ltr;\"><tt><a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#outputarg_B\"><tt>B<\/tt><\/a> = lasso(<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_X\"><tt>X<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_Y\"><tt>Y<\/tt><\/a>)<\/tt> returns fitted least-squares regression coefficients for a set of regularization coefficients <a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_Lambda\"><tt>Lambda<\/tt><\/a>.<\/p>\n<p style=\"direction: ltr;\"><tt>[<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#outputarg_B\"><tt>B<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#outputarg_FitInfo\"><tt>FitInfo<\/tt><\/a>] = lasso(<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_X\"><tt>X<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_Y\"><tt>Y<\/tt><\/a>)<\/tt> returns a structure containing information about the fits.<\/p>\n<p style=\"direction: ltr;\"><tt>[<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#outputarg_B\"><tt>B<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#outputarg_FitInfo\"><tt>FitInfo<\/tt><\/a>] = lasso(<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_X\"><tt>X<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#inputarg_Y\"><tt>Y<\/tt><\/a>,<a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#namevaluepairarguments\"><tt>Name,Value<\/tt><\/a>)<\/tt> fits regularized regressions with additional options specified by one or more <tt>Name,Value<\/tt> pair arguments.<\/p>\n<div dir=\"ltr\" itemprop=\"content\"><a name=\"bs25w54-4\"><\/a><\/p>\n<h2 id=\"inputs\" style=\"direction: ltr;\">Input Arguments<\/h2>\n<table style=\"direction: ltr;\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">\n<tbody>\n<tr valign=\"top\">\n<td width=\"150\"><a name=\"inputarg_X\"><\/a><tt>X<\/tt><\/td>\n<td>Numeric matrix with <tt>n<\/tt> rows and <tt>p<\/tt> columns. Each row represents one observation, and each column represents one predictor (variable).<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_Y\"><\/a><tt>Y<\/tt><\/td>\n<td>Numeric vector of length <tt>n<\/tt>, where <tt>n<\/tt> is the number of rows of <tt>X<\/tt>. <tt>Y(i)<\/tt> is the response to row <tt>i<\/tt> of <tt>X<\/tt>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a name=\"namevaluepairarguments\"><\/a><\/p>\n<h3 id=\"namevaluepairs\" style=\"direction: ltr;\">Name-Value Pair Arguments<\/h3>\n<p><a name=\"bts6w6z-1\"><\/a><\/p>\n<div>\n<p style=\"direction: ltr;\">Specify optional comma-separated pairs of <tt>Name,Value<\/tt> arguments. <tt>Name<\/tt> is the argument name and <tt>Value<\/tt> is the corresponding value. <tt>Name<\/tt> must appear inside single quotes (<tt>' '<\/tt>). You can specify several name and value pair arguments in any order as <tt>Name1,Value1,...,NameN,ValueN<\/tt>.<\/p>\n<\/div>\n<table style=\"direction: ltr;\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">\n<tbody>\n<tr valign=\"top\">\n<td width=\"150\"><a name=\"inputarg_Alpha\"><\/a><tt>'Alpha'<\/tt><\/td>\n<td>Scalar value from <tt>0<\/tt> to <tt>1<\/tt> (excluding <tt>0<\/tt>) representing the weight of lasso (<i>L<\/i><sup>1<\/sup>) versus ridge (<i>L<\/i><sup>2<\/sup>) optimization. <tt>Alpha\u00a0=\u00a0\u06f1<\/tt> represents lasso regression, <tt>Alpha<\/tt> close to <tt>0<\/tt> approaches ridge regression, and other values represent elastic net optimization. See <a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#bs25w54-6\">Definitions<\/a>.<b>Default: <\/b><tt>1<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_CV\"><\/a><tt>'CV'<\/tt><\/td>\n<td>Method <tt>lasso<\/tt> uses to estimate mean squared error:<\/p>\n<ul type=\"disc\">\n<li><tt>K<\/tt>, a positive integer \u2014 <tt>lasso<\/tt> uses <tt>K<\/tt>-fold cross validation.<\/li>\n<li><tt>cvp<\/tt>, a <a href=\"http:\/\/www.mathworks.com\/help\/stats\/cvpartitionclass.html\"><tt>cvpartition<\/tt><\/a> object \u2014 <tt>lasso<\/tt> uses the cross-validation method expressed in <tt>cvp<\/tt>. You cannot use a <tt>'leaveout'<\/tt> partition with <tt>lasso<\/tt>.<\/li>\n<li><tt>'resubstitution'<\/tt> \u2014 <tt>lasso<\/tt> uses <tt>X<\/tt> and <tt>Y<\/tt> to fit the model and to estimate the mean squared error, without cross validation.<\/li>\n<\/ul>\n<p><b>Default: <\/b><tt>'resubstitution'<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_DFmax\"><\/a><tt>'DFmax'<\/tt><\/td>\n<td>Maximum number of nonzero coefficients in the model. <tt>lasso<\/tt> returns results only for <tt>Lambda<\/tt> values that satisfy this criterion.<b>Default: <\/b><tt>Inf<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_Lambda\"><\/a><tt>'Lambda'<\/tt><\/td>\n<td>Vector of nonnegative <tt>Lambda<\/tt> values. See <a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#bs25w54-6\">Definitions<\/a>.<\/p>\n<ul type=\"disc\">\n<li>If you do not supply <tt>Lambda<\/tt>, <tt>lasso<\/tt> calculates the largest value of <tt>Lambda<\/tt> that gives a nonnull model. In this case, <tt>LambdaRatio<\/tt> gives the ratio of the smallest to the largest value of the sequence, and <tt>NumLambda<\/tt> gives the length of the vector.<\/li>\n<li>If you supply <tt>Lambda<\/tt>, <tt>lasso<\/tt> ignores <tt>LambdaRatio<\/tt> and <tt>NumLambda<\/tt>.<\/li>\n<\/ul>\n<p><b>Default: <\/b>Geometric sequence of <tt>NumLambda<\/tt> values, the largest just sufficient to produce <tt>B<\/tt>\u00a0=\u00a0<tt>\u06f0<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_LambdaRatio\"><\/a><tt>'LambdaRatio'<\/tt><\/td>\n<td>Positive scalar, the ratio of the smallest to the largest <tt>Lambda<\/tt> value when you do not set <tt>Lambda<\/tt>.If you set <tt>LambdaRatio\u00a0=\u00a0\u06f0<\/tt>, <tt>lasso<\/tt> generates a default sequence of <tt>Lambda<\/tt> values, and replaces the smallest one with <tt>0<\/tt>.<b>Default: <\/b><tt>1e-4<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_MCReps\"><\/a><tt>'MCReps'<\/tt><\/td>\n<td>Positive integer, the number of Monte Carlo repetitions for cross validation.<\/p>\n<ul type=\"disc\">\n<li>If <tt>CV<\/tt> is <tt>'resubstitution'<\/tt> or a <tt>cvpartition<\/tt> of type <tt>'resubstitution'<\/tt>, <tt>MCReps<\/tt> must be <tt>1<\/tt>.<\/li>\n<li>If <tt>CV<\/tt> is a <tt>cvpartition<\/tt> of type <tt>'holdout'<\/tt>, <tt>MCReps<\/tt> must be greater than <tt>1<\/tt>.<\/li>\n<\/ul>\n<p><b>Default: <\/b><tt>1<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_NumLambda\"><\/a><tt>'NumLambda'<\/tt><\/td>\n<td>Positive integer, the number of <tt>Lambda<\/tt> values <tt>lasso<\/tt> uses when you do not set <tt>Lambda<\/tt>. <tt>lasso<\/tt> can return fewer than <tt>NumLambda<\/tt> fits if the if the residual error of the fits drops below a threshold fraction of the variance of <tt>Y<\/tt>.<b>Default: <\/b><tt>100<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_Options\"><\/a><tt>'Options'<\/tt><\/td>\n<td>Structure that specifies whether to cross validate in parallel, and specifies the random stream or streams. Create the <tt>Options<\/tt> structure with <a href=\"http:\/\/www.mathworks.com\/help\/stats\/statset.html\"><tt>statset<\/tt><\/a>. Option fields:<\/p>\n<ul type=\"disc\">\n<li><tt>UseParallel<\/tt> \u2014 Set to <tt>true<\/tt> to compute in parallel. Default is <tt>false<\/tt>.<\/li>\n<li><tt>UseSubstreams<\/tt> \u2014 Set to <tt>true<\/tt> to compute in parallel in a reproducible fashion. To compute reproducibly, set <tt>Streams<\/tt> to a type allowing substreams: <tt>'mlfg6331_64'<\/tt> or <tt>'mrg32k3a'<\/tt>. Default is <tt>false<\/tt>.<\/li>\n<li><tt>Streams<\/tt> \u2014 A <a href=\"http:\/\/www.mathworks.com\/help\/matlab\/ref\/randstream.html\"><tt>RandStream<\/tt><\/a> object or cell array consisting of one such object. If you do not specify <tt>Streams<\/tt>, <tt>lasso<\/tt> uses the default stream.<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_PredictorNames\"><\/a><tt>'PredictorNames'<\/tt><\/td>\n<td>Cell array of strings representing names of the predictor variables, in the order in which they appear in <tt>X<\/tt>.<b>Default: <\/b><tt>{}<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_RelTol\"><\/a><tt>'RelTol'<\/tt><\/td>\n<td>Convergence threshold for the coordinate descent algorithm (see Friedman, Tibshirani, and Hastie <a href=\"http:\/\/www.mathworks.com\/help\/stats\/lasso.html#bs2_1q2\">[3]<\/a>). The algorithm terminates when successive estimates of the coefficient vector differ in the <i>L<\/i><sup>2<\/sup> norm by a relative amount less than <tt>RelTol<\/tt>.<b>Default: <\/b><tt>1e-4<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_Standardize\"><\/a><tt>'Standardize'<\/tt><\/td>\n<td>Boolean value specifying whether <tt>lasso<\/tt> scales <tt>X<\/tt> before fitting the models.<b>Default: <\/b><tt>true<\/tt><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"inputarg_Weights\"><\/a><tt>'Weights'<\/tt><\/td>\n<td>Observation weights, a nonnegative vector of length <tt>n<\/tt>, where <tt>n<\/tt> is the number of rows of <tt>X<\/tt>. <tt>lasso<\/tt> scales <tt>Weights<\/tt> to sum to <tt>1<\/tt>.<b>Default: <\/b><tt>1\/n * ones(n,1)<\/tt><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div dir=\"ltr\" itemprop=\"content\"><a name=\"bs25w54-5\"><\/a><\/p>\n<h2 style=\"direction: ltr;\">Output Arguments<\/h2>\n<table style=\"direction: ltr;\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">\n<tbody>\n<tr valign=\"top\">\n<td width=\"150\"><a name=\"outputarg_B\"><\/a><tt>B<\/tt><\/td>\n<td>Fitted coefficients, a <tt>p<\/tt>-by-<tt>L<\/tt> matrix, where <tt>p<\/tt> is the number of predictors (columns) in <tt>X<\/tt>, and <tt>L<\/tt> is the number of <tt>Lambda<\/tt> values.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><a name=\"outputarg_FitInfo\"><\/a><tt>FitInfo<\/tt><\/td>\n<td>Structure containing information about the model fits.<\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"4\">\n<colgroup>\n<col width=\"29%\" \/>\n<col width=\"71%\" \/><\/colgroup>\n<thead>\n<tr valign=\"top\">\n<th valign=\"top\" bgcolor=\"#b2b2b2\">Field in FitInfo<\/th>\n<th valign=\"top\" bgcolor=\"#b2b2b2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>Intercept<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">Intercept term <i>\u03b2<\/i><sub>\u06f0<\/sub> for each linear model, a <tt>1<\/tt>-by-<tt>L<\/tt> vector<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>Lambda<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">Lambda parameters in ascending order, a <tt>1<\/tt>-by-<tt>L<\/tt> vector<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>Alpha<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">Value of <tt>Alpha<\/tt> parameter, a scalar<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>DF<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">Number of nonzero coefficients in <tt>B<\/tt> for each value of <tt>Lambda<\/tt>, a <tt>1<\/tt>-by-<tt>L<\/tt> vector<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>MSE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">Mean squared error (MSE), a <tt>1<\/tt>-by-<tt>L<\/tt> vector<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>If you set the <tt>CV<\/tt> name-value pair to cross validate, the <tt>FitInfo<\/tt> structure contains additional fields.<\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"4\">\n<colgroup>\n<col width=\"29%\" \/>\n<col width=\"71%\" \/><\/colgroup>\n<thead>\n<tr valign=\"top\">\n<th valign=\"top\" bgcolor=\"#b2b2b2\">Field in FitInfo<\/th>\n<th valign=\"top\" bgcolor=\"#b2b2b2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>SE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">The standard error of MSE for each <tt>Lambda<\/tt>, as calculated during cross validation, a <tt>1<\/tt>-by-<tt>L<\/tt> vector<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>LambdaMinMSE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">The <tt>Lambda<\/tt> value with minimum MSE, a scalar<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>Lambda1SE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">The largest <tt>Lambda<\/tt> such that MSE is within one standard error of the minimum, a scalar<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>IndexMinMSE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">The index of <tt>Lambda<\/tt> with value <tt>LambdaMinMSE<\/tt>, a scalar<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td bgcolor=\"#f2f2f2\"><tt>Index1SE<\/tt><\/td>\n<td bgcolor=\"#f2f2f2\">The index of <tt>Lambda<\/tt> with value <tt>Lambda1SE<\/tt>, a scalar<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<pre dir=\"ltr\"><\/pre>\n<p dir=\"ltr\" style=\"direction: ltr;\">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">%<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% File: Lasso.m<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% Author: Jinzhu Jia<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">%<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% One simple (Gradiant Descent) implementation of the Lasso<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% The objective function is:<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% \u06f1\/\u06f2*sum((Y &#8211; X * beta -beta0).^2) + lambda * sum(abs(beta))<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% Comparison with CVX code is done<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% Reference: To be uploaded<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">%<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">% Version 4.23.2010<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">function [beta0,beta] = Lasso(X,Y,lambda)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">n = size(X,1);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">p = size(X,2);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">if(size(Y,1) ~= n)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">fprintf( &#8216;Error: dim of X and dim of Y are not match!!\\n&#8217;)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">quit cancel<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">end<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta0 = 0;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta= zeros(p,1);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">crit = 1;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">step = 0;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">while(crit &gt; 1E-5)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">step = step + 1;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">obj_ini = 1\/2*sum((Y &#8211; X * beta -beta0).^2) + lambda * sum(abs(beta));<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta0 = mean(Y &#8211; X*beta);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">for(j = 1:p)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">a = sum(X(:,j).^2);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">b = sum((Y &#8211; X*beta).* X(:,j)) + beta(j) * a;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">if lambda &gt;= abs(b)<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta(j) = 0;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">else<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">if b&gt;0<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta(j) = (b &#8211; lambda) \/ a;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">else<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">beta(j) = (b+lambda) \/a;<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">end<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">end<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">end<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">obj_now = 1\/2*sum((Y &#8211; X * beta -beta0).^2) + lambda * sum(abs(beta));<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">crit = abs(obj_now &#8211; obj_ini) \/ abs(obj_ini);<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">end<\/p>\n<p dir=\"ltr\" style=\"direction: ltr;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"direction: ltr;\">%% Lasso regularization example<\/p>\n<p style=\"direction: ltr;\">% Copyright (c) 2011, The MathWorks, Inc.<\/p>\n<p style=\"direction: ltr;\">%% Introduction to using LASSO<\/p>\n<p style=\"direction: ltr;\">using the lasso functionality introduced<br \/>\n% in R2011b. It is motivated by<\/p>\n<p style=\"direction: ltr;\">% This demo explains how to star<\/p>\n<p style=\"direction: ltr;\">tan example in Tibshiranis original paper<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% on the lasso.<\/pre>\n<p style=\"direction: ltr;\">via the lasso.<br \/>\n% J. Royal. Statist. Soc B., Vol. 58, No. 1, pages 267-288)<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% Tibshirani, R. (1996). Regression shrinkage and selection\r\n .\r\n\r\n% The data set that were working with in this demo is a wide<\/pre>\n<p style=\"direction: ltr;\">ferent<br \/>\n% variables and only 20 observations. 5 out of the 8 variables h<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% dataset with correlated variables. This data set includes 8 di\r\nfave\r\n% coefficients of zero. These variables have zero impact on the model. The<\/pre>\n<p style=\"direction: ltr;\">up workspace and set random seed<\/p>\n<p>clear all<br \/>\nclc<\/p>\n<p>% Set the random num<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% other three variables have non negative values and impact the model\r\n\r\n%% Clean\r\n ber stream\r\nrng(1981);\r\n\r\n%% Creating data set with specific characteristics\r\n\r\n% Create eight X variables<\/pre>\n<p style=\"direction: ltr;\">with one another<br \/>\n% The covariance matrix is spec<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% The mean of each variable will be equal to zero\r\nmu = [0 0 0 0 0 0 0 0];\r\n\r\n% The variable are correlate\r\ndified as\r\ni = 1:8;\r\nmatrix = abs(bsxfun(@minus,i',i));\r\ncovariance = repmat(.5,8,8).^matrix;<\/pre>\n<p style=\"direction: ltr;\">, covariance, 20);<\/p>\n<p>% Create a hyperplane that describes Y = f(X)<br \/>\nBeta = [3; 1<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">% Use these parameters to generate a set of multivariate normal random numbers\r\nX = mvnrnd(m\r\nu.5; 0; 0; 2; 0; 0; 0];\r\nds = dataset(Beta);\r\n\r\n% Add in a noise vector\r\nY = X * Beta + 3 * randn(20,1);\r\n\r\n%% Use linear regression to fit the model \r\n\r\nb = regress(Y,X);<\/pre>\n<p style=\"direction: ltr;\">s, &#8216;PlotType&#8217;,<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">ds.Linear = b;\r\n\r\n%% Use a lasso to fit the model\r\n\r\n[B Stats] = lasso(X,Y, 'CV', 5);\r\n\r\ndisp(B)\r\ndisp(Stats)\r\n\r\n%% Create a plot showing MSE versus lamba\r\n\r\nlassoPlot(B, Sta\r\nt 'CV')\r\n\r\n%% Identify a reasonable set of lasso coefficients\r\n\r\n% View the regression coefficients associated with Index1SE\r\n\r\nds.Lasso = B(:,Stats.Index1SE);\r\ndisp(ds)<\/pre>\n<p style=\"direction: ltr;\">(\u06f1\u06f0\u06f0,\u06f1);<br \/>\nCoeff_Num = zeros(100,1);<br \/>\nBetas = zeros(8,100);<br \/>\ncv<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">%%  Create a plot showing coefficient values versus L1 norm\r\n\r\nlassoPlot(B, Stats)\r\n\r\n%%  Run a Simulation\r\n\r\n% Preallocate some variables\r\nMSE = zeros(100,1);\r\nmse = zero\r\ns_Reg_MSE = zeros(1,100);\r\n\r\nfor i = 1 : 100\r\n\r\n    X = mvnrnd(mu, covariance, 20);\r\n    Y = X * Beta + randn(20,1);\r\n\r\n    [B Stats] = lasso(X,Y, 'CV', 5);<\/pre>\n<p style=\"direction: ltr;\">E(i) = Stats.MSE(:, Shrink);<\/p>\n<p>regf = @(XTRAIN, ytrain, XTEST)(XTEST*<\/p>\n<pre dir=\"ltr\" style=\"direction: ltr;\">    Shrink = Stats.Index1SE -  ceil((Stats.Index1SE - Stats.IndexMinMSE)\/2);\r\n    Betas(:,i) = B(:,Shrink) &gt; 0;\r\n    Coeff_Num(i) = sum(B(:,Shrink) &gt; 0);\r\n    M\r\nSregress(ytrain,XTRAIN));\r\n    cv_Reg_MSE(i) = crossval('mse',X,Y,'predfun',regf, 'kfold', 5);\r\n\r\nend\r\n\r\nNumber_Lasso_Coefficients = mean(Coeff_Num);\r\ndisp(Number_Lasso_Coefficients)\r\n\r\nMSE_Ratio = median(cv_Reg_MSE)\/median(MSE);<\/pre>\n<p style=\"direction: ltr;\">disp(MSE_Ratio)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.stanford.edu\/~boyd\/papers\/admm\/lasso\/lasso_example.html http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node35.html http:\/\/www-stat.stanford.edu\/~susan\/courses\/s227\/node5.html http:\/\/www-stat.stanford.edu\/~susan\/courses\/b494\/index\/node29.html lasso Regularized least-squares regression using lasso or elastic net algorithms Syntax B = lasso(X,Y) [B,FitInfo] = lasso(X,Y) [B,FitInfo] = lasso(X,Y,Name,Value) Description B = lasso(X,Y) returns fitted least-squares regression coefficients for a set of regularization coefficients Lambda. [B,FitInfo] = lasso(X,Y) returns a structure containing information about the fits. [B,FitInfo] = lasso(X,Y,Name,Value) fits <a href='https:\/\/vua.nadiran.com\/?p=289' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-19","category-19-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/posts\/289"}],"collection":[{"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":5,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions\/291"}],"wp:attachment":[{"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vua.nadiran.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}