Triangular distribution

33 views
Skip to first unread message

Claus Möbus

unread,
Sep 4, 2020, 4:55:12 AM9/4/20
to webppl-dev
Hi Paul,
I want to use the triangle distribution (https://en.wikipedia.org/wiki/Triangular_distribution) as a prior distribution. Unfortunately, this is not in the library. Is there a guide for a casual user to provide you with the code so that triangle could be a new component of the library ? Otherwise I have to implement triangle for myself which is of no use for others.
All the best Claus

null-a

unread,
Sep 5, 2020, 4:21:53 AM9/5/20
to webppl-dev
> Is there a guide for a casual user to provide you with the code so that triangle could be a new component of the library ?

I don't think such a guide exists. (And sadly I don't have the spare capacity to write such a thing at the moment.) There are a few pointers here [1], but I suspect they won't help much without already knowing something about WebPPL internals. I might be able to help you work on a pull request to add this if you're interested, but be aware that fully implementing a new distribution is perhaps a little more involved than one might expect. For example, we have to at least think about if/how variational inference should be performed for the new distribution. Perhaps take a look at [2], in which a contributor added the student's t distribution, for a sense of this.

And don't forget, you might not necessarily have to add triangular as a primitive, you might be able to define it in WebPPL itself by building on the uniform distribution. (Similar to the inverse Gamma example we chatted about recently.)

[1] https://github.com/probmods/webppl/issues/910#issuecomment-426926853
[2] https://github.com/probmods/webppl/issues/929

Claus Möbus

unread,
Sep 6, 2020, 5:54:19 AM9/6/20
to webppl-dev
Thank you for your hints. 
the code for sampling from 'triangle' is rather simple when following Wikipedia:

var oneSampleOfTriangle = function(a, b, c) { 
  var u  = sample(Uniform({a:0, b:1}))
  var ba = b - a
  var bc = b - c
  var ca = c - a
  var Fc = ca / ba
  var x  = (0 < u) && (u < Fc) ? 
      (a + Math.sqrt(u * ba * ca)) :
      (b - Math.sqrt((1 - u) * ba * bc))
  return x
}

I'd like to dive deeper in WebPPL, but that takes some because I'm only a casual user of JS. 
All the best, Claus
Reply all
Reply to author
Forward
0 new messages