hakaru-CS4ZP6 / hakaru

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

Prob return types for chi distributions #43

Closed mkhattab940 closed 6 years ago

mkhattab940 commented 6 years ago

Hi @JacquesCarette

Referring to the convo here about making sure chiSq returns a prob, you said:

"Yes, please make sure all of these return prob. There should be no need to cast, as it should be obvious that they are all positive."

I'm unsure how to get any of the chi distributions to return prob values without a cast since these distributions are built up from the normal distribution (which returns real values) and their implementations use mathematical operators which results in a real when applied to reals/probs.

Is there some other way to ensure these distributions returns a prob type?

JacquesCarette commented 6 years ago

I need the exact details. The PDF for chi-squared is 'obviously' positive. And the relation to normal that I know is only asymptotic, not exact.

mkhattab940 commented 6 years ago

Well, here is our implementation of chi-squared:

def chiSq_iid(n nat, mean real, stdev prob):
    q <~ plate _ of n: normal(mean,stdev)
    return summate i from 0 to size(q): 
        ((q[i]-mean)/stdev)^2

As far as I can tell, even if normal wasn't returning reals, the term in the summate would be because of the subtraction before the division. Isn't the option to cast to prob then?

JacquesCarette commented 6 years ago

Yes. The squaring does ensure the results are positive, so this is safe.

mkhattab940 commented 6 years ago

Just to clarify, even though its output is strictly positive, it won't let me explicitly type it as prob without a real2prob. Shall I give them real return types, then?

JacquesCarette commented 6 years ago

No, use real2prob.