Hello everyone,
I’m unsure if this is a silly question, but suppose I want to run a series of completely independent linear regressions as part of my model block. Is this paralellizable using something like reduce sum? For example, I have this in my model block:
for(i in 1:n_securities) {
target += normal_lpdf(returns[,i] | alpha[i] +
risk_factors * beta[,i], sigma[i]);
}
This is in an asset pricing context, where alpha is a vector with an intercept term for each security, beta is a matrix of parameters with columns corresponding to each security, and sigma is an independent scale term for each security.
I realize that I could just fit each regression independently, but since I want to work with the parameters in a joint fashion after estimation this is much more convenient. Since each estimate is completely independent, I thought it might be an easy use-case to learn how to use the reduce_sum
function. My attempts thus far have not worked, since I think I’m misunderstanding the syntax. I had something like
functions {
real partial_sum_lpdf(matrix returns,
int start,
int end,
matrix risk_factors,
vector alpha,
matrix beta,
vector sigma) {
return normal_lpdf(returns[,start:end] | alpha[start:end] + risk_factors * beta[,start:end], sigma[start:end];
}
}
But this failed all of the checks, since beta and returns were ill-formed. I have never worked with user-defined functions in Stan, and I’m pretty confused about how this all works, so any help would be appreciated!