# Constrained least squares

Hi, I would like to carry out a L1-constrained least squared estimate, something like following equation:

I’ve tried following codes, but failed to generate HMC samples.

``````functions {
}
data {
int<lower=1> N;  // total number of observations
vector[N] Y;  // response variable
int<lower=1> K;  // number of population-level effects
matrix[N,K] X;  // population-level design matrix
}
transformed data {
}
parameters {
vector[K] w_signed;  // population-level effects

}
transformed parameters {
vector[K] w;
real<lower=0, upper=1> w_limit;
for(k in 1:K) {
w[k] = fabs(w_signed[k]);
}
w_limit = sum(w);
}
model {

vector[N] squared_error;

for (n in 1:N){
squared_error[n] = (Y[n] - X[n] * w)^2 ;
}

target += -sum(squared_error);

}

generated quantities {
}
``````

I’ve got following error message
“Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: validate transformed params: w_limit is 15.1066, but must be less than or equal to 1 (in ‘model35f645629433b_synth_ConstrainedLeastSquare_woV’ at line 18)”

Any suggestions?

Posting the error message

Sure, here it is,

Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: validate transformed params: w_limit is 15.1066, but must be less than or equal to 1 (in ‘model35f645629433b_synth_ConstrainedLeastSquare_woV’ at line 18)

The message is saying that the constraints you declared in the transformed parameters block are being violated. Taking a very quick glance at the code in transformed parameters, it looks like you have a sum that is not constrained to be less than 1, but you are assigning that value to a variable with a declared upper bound of 1.