rethinkpriorities / squigglepy

Squiggle programming language for intuitive probabilistic estimation features in Python
MIT License
65 stars 8 forks source link

Is there an equivalent of Squiggle's `cdf`? #7

Closed erwald closed 1 year ago

erwald commented 1 year ago

See Squiggle docs (not much info there though).

Right now I'm doing something like this:

import squigglepy as sq
import numpy as np

samples = sq.norm(mean=0, sd=2) @ 10000
num_above_threshold = np.size(np.where(np.array(samples) >= 0.5))
print(num_above_threshold / len(samples)) # prints 0.4023

Which would be equivalent to this in Squiggle:

x = normal(0, 2)
1.0 - (x |> cdf(0.5)) // outputs 0.401

Is there already such a function in squigglepy? If not, it'd be convenient to have one built in. (I'd be happy to implement it if you want to add it, but up to you of course.)

peterhurford commented 1 year ago

I mainly implement this via:

import squigglepy as sq
import numpy as np
np.mean((sq.norm(mean=0, sd=2) >= 0.5) @ 10000)

What do you think of that?

peterhurford commented 1 year ago

BTW np.mean(sq.norm(mean=0, sd=2) @ 10000 >= 0.5) also works and saves you a few keystrokes

erwald commented 1 year ago

Oh cool that's nice enough, thanks! Closing this.