Fail to sample from initial value in a Power-law distribution

I fitting some data with power law distribution (I included the density function below as populationpow_lpdf) and was really having difficulty pass the sampling stage. I have tried to feed the initial value directly, which also failed. I have also included the R code I use to generate data and fit the model below.

I am not sure if I am missing something obvious here so I would really appreciate if someone can point me a direction!

FitpowerlawStan.R (455 Bytes)

functions{
real populationpow_lpdf(vector Mus,real xmin, real alpha){
  vector[num_elements(Mus)] prob;
  real lprob;
  for (i in 1:num_elements(Mus)){
      prob[i] = ((alpha-1)/xmin)*(Mus[i]/xmin)^(-alpha);
  }
  lprob = sum(log(prob));
  return lprob;
  }
}
data{
  int Ntotal;
  vector[Ntotal] Mus;
  real xmin;
}
parameters{
    real <lower=1.5, upper=7> alpha;
}
model{
  real c; 
  real d;
  c = 1.5;
  d = 7;
  alpha ~ uniform(c,d);
  Mus ~ populationpow(alpha, xmin);
}

Two things:

  • The arguments of populationpow appear to be switched. In the argument list of the function definition populationpow_lpdf, xmin comes before alpha, but the reverse is true when you invoke populationpow in the model block.

  • You don’t need to explicitly write the statement alpha ~ uniform(c,d). If no sampling statement for alpha is given, then alpha will have a uniform prior by default.

1 Like

I can’t believe I messed the first thing up. Thank you jjramsey sooo much for pointing it out!

Also thanks you for the second point. I am pretty new to Stan so I was just copying some examples before.

It works now!

Another thing is that you can use Stan’s implementation of the Pareto distribution. You just need to recall that if X\sim\text{Pareto}(x_\min, \beta) then X\sim\text{PowerLaw}(x_\min, \beta -1).