MATLAB: I found a mistake in Lasso function

function mistakelasso regression

In the function of Lasso (function [B,stats] = lasso(X,Y,varargin)), X is A numeric matrix (NxP). There is an optional input parameter called 'Weights'.
% 'Weights' Observation weights. Must be a vector of non-negative
% values, of the same length as columns of X. At least
% two values must be positive. (default ones(N,1) or
% equivalently (1/N)*ones(N,1)).
Since N is the number of samples and P is the number of features, Weights should be the ones(P,1). If Weights is ones(N,1), that is the weight of samples.
If my thoughts are wrong, I hope someone helps me correct. Thanks!
function [B,stats] = lasso(X,Y,varargin)
%LASSO Perform lasso or elastic net regularization for linear regression.
% [B,STATS] = lasso(X,Y,…) Performs L1-constrained linear least
% squares fits (lasso) or L1- and L2-constrained fits (elastic net)
% relating the predictors in X to the responses in Y. The default is a
% lasso fit, or constraint on the L1-norm of the coefficients B.
%
% Positional parameters:
%
% X A numeric matrix (dimension, say, NxP)
% Y A numeric vector of length N
%
% Optional input parameters:
%
% 'Weights' Observation weights. Must be a vector of non-negative
% values, of the same length as columns of X. At least
% two values must be positive. (default ones(N,1) or
% equivalently (1/N)*ones(N,1)).

Best Answer

There is no bug, just misunderstanding of OP.
N is the number of observations, meaning the number of rows of X and length of y.
The optional parameter Weights is stated "Observation Weights", so correctly required as vector of lenght N.
Please see LASSO doc page
This doc is however incomplete as many TMW document "style", it never states how Weights are used, and subject to miss-interpretation or reverse-engineering. But I guess it changes the Lasso cost function under minimization to
1/2 sum_[i=1,...N] Weights(i) * (y_i - beta X(i,:)'*beta)^2 + Lambda * sum_{j=1,..,P} | beta(j) |.
after normalization to
sum(Weight)==1
Related Question