Fitting a truncated Pareto in stan

I am trying to fit a truncated Pareto model, but am unsure how to do that in Stan. I have data that look like this:

``````library(VGAM)

N = 1000
x_min = 1
x_max = 10
x = rtruncpareto(n = N, lower = x_min, upper = x_max, shape = 3)
``````

I am able to fit these with the built-in Pareto in stan:

``````data {
int<lower=0> N;
vector<lower=0>[N] x;
real<lower=0> x_min;
}

parameters {
real<lower=0> alpha;
}

model {

alpha ~ lognormal(1, 1) T[0, 5];
x ~ pareto(x_min, alpha);
}

generated quantities {
vector[N] log_lik;

for (i in 1:N) {
log_lik[i] = pareto_lpdf(x[i] | x_min, alpha);
}
}
``````

But I am unsure how to modify the model above so that it will fit the truncated Pareto (i.e., incorporate x_max). I assume it would involve writing a custom pdf in the functions{}, but that is beyond my capability with stan.

Thanks for any help!

I was able to figure this out using guidance here: stan-book/truncation-censoring.Rmd at master · malcolmbarrett/stan-book · GitHub

The working version is:

``````functions{
real paretobinned_lpdf(real xk, real alpha, real xmin, real xmax){

return(log(((alpha*xmin^alpha)*xk^(-alpha-1))/(1 - (xmin/xmax)^alpha)));
}
}

data {
int<lower=0> N;
real<lower=0> xmin;
real<lower=0> xmax;
vector<lower=0, upper= xmax>[N] xk;
}

parameters {
real<lower=0> alpha;
}

model {
alpha ~ lognormal(1, 1);
for (i in 1:N){
xk[i] ~ paretobinned(alpha, xmin, xmax);
}
}
``````