Hi, I have written a vectorized version of a correlated Pareto/NBD:
data{
int<lower=1> N; // number of customers
vector<lower=0>[N] x; // number of purchases per customer
vector<lower=0>[N] t_x; // recency per customer
vector<lower=0>[N] T; // observation period per customer
}
parameters{
// Individual-level correlated coefficients
matrix[2, N] eta; // normal errors
cholesky_factor_corr[2] L_Omega; // cholesky corr.
row_vector[2] chi; // mean
vector<lower=0,upper=pi()/2>[2] tau_unif; // scaled variance
}
transformed parameters{
vector[N] lambda; // poisson rate (correlated)
vector[N] mu; // death rate (correlated)
vector<lower=0>[2] tau; // variance of individual-level correlated coefficients
matrix[N,2] zeta; // individual-level correlated coefficients
for (r in 1:2)
tau[r] = 2.5 * tan(tau_unif[r]);
zeta = rep_matrix(chi,N) + (diag_pre_multiply(tau,L_Omega) * eta)';
lambda=exp(zeta[,1]);
mu=exp(zeta[,2]);
}
model{
vector[N] lam_mu=lambda+mu;
to_vector(eta) ~ std_normal();
L_Omega ~ lkj_corr_cholesky(2);
to_vector(chi) ~ normal(0,3);
target += x .* log(lambda) - log(lam_mu) - lam_mu .* t_x +
log(mu + lambda .* exp( -(T-t_x) .* (lam_mu) )) ;
}
generated quantities {
matrix[2,2] Omega;
matrix[2,2] Sigma;
vector[N] p_alive;
vector[N] cond_exp_T2;
int T2=78;
Omega = multiply_lower_tri_self_transpose(L_Omega);
Sigma = quad_form_diag(Omega, tau);
p_alive = inv( 1 + mu ./ (lambda + mu) .* (exp( (lambda+mu) .* (T-t_x) ) - 1) ) ;
cond_exp_T2 = lambda ./ mu .* ( 1 - exp(-mu .* (T2-T)) ) .* p_alive;
}
This model is based on the note from Dr Fader and Dr Hardie. I believe this is also the model by Abe (2009).
What I notice is that Stan is unable to converge when using Gamma distribution for heterogeneity on lambda and mu, and I have tried this original formulation for over a month. But it works really well when replacing it with lognormal distribution.
I hope this help.