Vectorization of Asymmetric Laplace Likelihood

Any ideas how to vectorize this:

// function to return Asymmetric Laplace log-probability density

functions {

real asymmetric_laplace(real y, row_vector x, real b, vector w, real p) {
	real mu = sum(x' .* w) + b;
	return log(p) + log1m(p) - 2 * ((y < mu) ? (1 - p) * (mu - y) : p * (y - mu));
}

}
data {

int<lower=0> N;
int<lower=0> k; // number of predictors
real<lower=0, upper=1> p; // quantile of interest
matrix[N, k] x; // predictors
vector[N] y; // response

}
parameters {

real b; // intercept
vector[k] w; // slope(s)

}
model {

vector[N] ll; // log-likelihood

for (i in 1:N){
	ll[i] = asymmetric_laplace(y[i], x[i], b, w, p);
	target += ll[i];
}

}

https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html

functions{
real q_loss(real q, vector u){
  return 0.5 * sum(abs(u) + (2 * q - 1) * u);
}

real ald_lpdf(vector y, real q, real sigma, vector q_est){
  int N = num_elements(y);
  
  return N * (log(q) + log1m(q) - log(sigma)) - q_loss(q, y - q_est) / sigma;
}
}
data {
  int N;                   // Number of observation
  int P;                   // Number of predictors
  real<lower=0, upper=1> q;
  vector[N] y;             // Response variable sorted
  matrix[N, P] x;
}
parameters {
  vector[P] beta;
  real<lower=0> sigma;
}
model {
  beta ~ normal(0, 4);
  sigma ~ exponential(1);
  y ~ ald(q, sigma, x * beta);
}
2 Likes

You are a legend! Thanks!