hakaru-CS4ZP6 / hakaru

A probabilistic programming language
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Discrete functions with infinite categories #20

Closed mkhattab940 closed 6 years ago

mkhattab940 commented 6 years ago

http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Logarithm.pdf http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Discreteweibull.pdf

A number of discrete functions like the above 2 have no upper bound on the value x can take. (The rest have further complications which we will deal with once this issue is solved)

Previously, we've been able to implement discrete distributions using the categorical function, and that's how I would begin to attack this problem, but we can't go feeding categorical() an infinite array.

Rather unsure how to proceed...

mkhattab940 commented 6 years ago

@JacquesCarette I believe we discussed this in our last meeting. You said it would take some care. Can you point us in the right direction of how to go about this?

JacquesCarette commented 6 years ago

You need to use a weighted Countable to implement these. Countable is to Nat what Lesbesgue is to Real. So you put a weight in front of it, and voila!

sohraa3 commented 6 years ago

@JacquesCarette I can't understand what Lesbesgue does. Also is there documentation on how to use Countable? I get an error saying the name is not in scope.

JacquesCarette commented 6 years ago

Lebesgue is essentially uniform on all of the real line. It might be that Countable is actually called Counting...

sohraa3 commented 6 years ago

@JacquesCarette So our problem was that, we couldn't pass an infinite array to categorical. I am not sure how counting can be used to overcome this problem. Can you give us some guidance on how to do this?

JacquesCarette commented 6 years ago

You don't want an infinite array, you want a formula which is used as a weight. The documents you linked to has the formulas you need.

sohraa3 commented 6 years ago

@JacquesCarette Is this the right way to do it? The first argument of weight() is the p.m.f formula found in the documents. The second argument of weight() is the absolute value of the counting variable to only account for positive integers.

def Discrete_Weibull(p prob, beta prob): x <~ counting weight(real2prob((real2prob(1-p) x) beta - (real2prob(1-p) (x+1)) beta) , return abs(x))

JacquesCarette commented 6 years ago

That sort-of works, but isn't really the best way to do it.

def Discrete_Weibull(p prob, beta prob):
    x <~ counting
    weight( if (x<0) 0 (real2prob((real2prob(1-p) ** x) ** beta - (real2prob(1-p) ** (x+1)) ** beta)),
        return x)

is better. [I may have some syntax error in the if, but the idea should be clear].