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

**URL:** <https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919>\
**Category:** Modeling\
**Created:** [December 10, 2018, 9:16pm UTC](https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919 "2018-12-10T21:16:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rui\_Cao](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@Rui\_Cao](https://discourse.mc-stan.org/u/Rui_Cao)\
**Post date:** [December 10, 2018, 9:16pm UTC](https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919/1 "2018-12-10T21:16:52Z")

</div>

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](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/00489a34358db05391665bc8765745ddfcaecbba.R) (455 Bytes)

```stan
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);
}
```

---

<div class="post-metadata">

**Author:** ![jjramsey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jjramsey/32/3319_2.png) [@jjramsey](https://discourse.mc-stan.org/u/jjramsey)\
**Post date:** [December 12, 2018, 7:54pm UTC](https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919/2 "2018-12-12T19:54:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rui\_Cao](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@Rui\_Cao](https://discourse.mc-stan.org/u/Rui_Cao)\
**Post date:** [December 12, 2018, 8:27pm UTC](https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919/3 "2018-12-12T20:27:57Z")

</div>

I can’t believe I messed the first thing up. Thank you [jjramsey](https://discourse.mc-stan.org/u/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!

---

<div class="post-metadata">

**Author:** ![maxbiostat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/maxbiostat/32/1326_2.png) [@maxbiostat](https://discourse.mc-stan.org/u/maxbiostat)\
**Post date:** [December 12, 2018, 8:29pm UTC](https://discourse.mc-stan.org/t/fail-to-sample-from-initial-value-in-a-power-law-distribution/6919/4 "2018-12-12T20:29:15Z")

</div>

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).
