All posts by nouyang

Hands-on with Probabilistic Programming Languages

The past week with one of my graduate classes, we read a few papers on probabilistic programming lanugages (PPLs). A few fellow students presented on the papers, and included a little hands-on demo of PPLs. They’re much more approachable than I thought!

At least in the “hello world” cases, they’re pretty much like normal programming languages with the addition of a few primitives to allow for reasoning about probabilities. In the example of CHURCH, this is the flip primitive. CHURCH uses LISP-like syntax. [1]

Without further ado here are some of the examples given.

CHURCH

Here we use the online compiler at http://v1.probmods.org/play-space.html.

[2]

The first line, display(flip), shows one coin flip (50/50) – in this case we get a` #f, or false. If we run the program again, we might get a #t. Near the end, we see that we can also call flip 0.7 which will return true with 70% likelihood. We then run this 100 times and draw a histogram (I had to right-click and view the image in a new window to see it). Here we can see that we actually got about 78%-22% on this run. If we run the program again, we get a different histogram, and with more flips we get closer to the true 70% distribution.

Prolog

To see an example of how we can use PPLs to perform inference, let’s see an example in prolog.

Follow along here.
https://rextester.com/l/prolog_online_compiler

Here we defined some facts (amy is a parent of bob, and bob is a parent of cathy). Then we defined some rules (what does it mean to be a grandparent?). Finally, at the bottom, I typed in some queries to find out of cathy is a parent of amy (no) and if amy is a grandparent of cathy (yes). Then I clicked Run it and got the output at the bottom.

In this way we see prolog has “reasoned” and created a causal chain from our question to the axioms we have.

We can also write our queries directly into the program, and print them all out.

The code for these examples can be found at these links or the bottom of this post.

I’m not sure why prolog doesn’t have this concept of printing query results. I had a lot of issues with the program not stopping (TIMEOUT) and not being sure where I should type in the queries (marked with ?- in online tutorials), versus typing in the program itself.

Other thoughts

I also learned that they’re not just toy languages for computer science people, and there are statistical researcher who use these on a daily basis.

Footnotes

[1] The instructor said something like “Yea I feel like all these probabilistic programming languages and always in some obscure language. For instance CHURCH targets the niche that knows both LISP and the relevant statistical concepts. They’re mostly useful for the authors.”

[2] Note: Like LISP, the strange operator order is Polish notation.

Appendix

CHURCH

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(display(flip))

(display(+ 2 2))
(display(equal? #f #t))

(define x (list 1 2 3))
(define (square x)
(* x x)
(display (square 6))

(define (flip-biased-coin)
(flip 0.7))

(hist (repeat 100 flip-biased-coin))

Prolog 1

This should be input in their entirety into the coding area, and then run.

1
2
3
4
5
6
7
8
9
10
11
% Facts
parent(amy, bob).
parent(bob, cathy).

% Rules
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Z), ancestor(X, Z).

% Run program with ?- prompt
?- writeln(‘hello world’), nl.

Prolog 2

This should be input in their entirety into the coding area, and then run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
program :- write(‘Hello, world!’), nl.
parent(amy, bob).
parent(bob, cathy).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

printqueries :-
forall(query(Q), (Q -> writeln(yes:Q) ; writeln(no:Q))).

query(parent(amy, cathy)).
query(parent(bob, cathy)).
query(grandparent(amy, cathy)).
query(ancestor(amy, cathy)).

:- program.
:- printqueries.

and we’re back! now on aws lightsail

hi friends!

thanks for bearing with the site as it was intermittently down over the last 3 to 4 months. I was planning on migrating from nearlyfreespeech, where the bills were costing ~$120 a year on my dinky little website (!!!! I should have just stuck with blogspot) to something more reasonable, $42 + $12 domain. I’m now running on amazon lightsail, and not only is it cheaper, but somehow the page responsiveness is a lot better too!

And it promises to be superbly easy to launch new derpy flask apps, I have a hexo blog set up at /hexblog already (well, that’s node.js really, but tomayto tomahto).

Plus, if I’m really lazy, they have a browser-based SSH session, so I can be on a computer without anything installed. And they also have snapshots, so I can upgrade away on my wordpress without fear.

Only took 4 hours in the end! I think I can do it in an hour now. Also, I feel much better about wordpress backups and feeling like I can restore from them.

I still want to migrate my website away from wordpress, I think. I fought a lot of little “php has upgraded” errors and such, and flat file management just seems to way to go for small sites like mine that only have one author. No need to get mysql involved.

Yay! I’ll write up the steps I took to migrate sometime soon. Now I can blog again, boy, I actually really missed that.

Fixing front (v-)brake bike part (broken metal bit)

Hi blogosphere!

I fixed my bike today. Some quick documentation below.

I’ve not really had good front brakes in a year. The issue is this little metal piece, which broke. I wasn’t sure how to get a new one of. So I just sort of ziptie’d it together and it kinda worked, although I wore through brake pads real fast because

Turns out the solution is to just replace the entire v-brake assembly, and in fact you only have to take out two bolts to do so! Wow, so much easier than all my careful ziptie-ing and plier-ing open the metal piece.

20181207_224702
whoo zipties

bike3
left: broken (See piece at top which rusted in half) right: good

bike
Those squeeze in against the rim to brake

There were three settings, which kind of controlled gross-ly what the distance between the brakes was possible.

bike2

I adjusted the cable to the maximum I wanted them to be (allowing clearance for my rather wobbly rim, which is far from straight… I in fact had to tighten my spokes as far as they could go in some cases, and a few spokes have even broken off. But the rim is still clearly bent in places).

After that, I put the metal “springs” on. They provide restoring force, so that after you apply the brakes, the brakes open again by themselves. (They’re the long wire, bent and stuck behind a pin sticking out. I labelled it with an orange arrow in the pic below, with other arrow showing the direction the wire wants to go.

bike5

Finally, I used a little allen wrench to adjust the spacing between the brakes, so that it was roughly centered over the rim.

bike2Wow, it’s so much easier to understand how V-brakes work when you have reasonably working brakes! I had thought that centering the brakes and getting them to return properly was black magic, and resigned myself to having my brake pads constantly rubbing against the rim on one side or the other, in order to have a “trigger until full brake” margin I was comfortable with.

bikecenter2
I took the spring off, but the set screw to the top left, controls how strongly the spring arm pushes in,

By adjusting the relative strength of the two sides, you can control what the brakes look like when they’re opened, so one side isn’t rubbing.

I discovered that you don’t just squish the bolt down onto the cable, as you might for a derpy electronics project board. This prevents frayed cables, which are extremely sharp and poky 🙁

bike7I also discovered that it’s actually fairly easy to get the brake cable on and off the handlebars. There’s a hole underneath that makes this a lot easier than whatever I was doing.

bike6
This is an older picture from another time. Yes, my handbars are rotated so they’re upside down. The bolt at the center of the handlebar loosens sometimes, so the clamp loosens and then the handlebars start rotating…

bike8

Anyway, turns out there’s a whole drawerful of these bike parts at miters — not pictured here. Instead here’s two other drawers of parts… so many bike parts! They’re probably all from the era a decade ago when welding art bikes and making weird bikes was popular at MITERS.

bike10

also important news I got a bike bell! whee! 😀

bike11

Unfortunately I’m not really sure how it fits into my bike mittens scheme… I put it outside the mittens, since otherwise it’s muffled in the mittens. But I guess that means I will sometimes have to keep my right hand outside in case I need to ring the bell :0

Especially now, Boston is putting in a lot of really nice bike lanes which are on the *right* side of a line of parked cars. However this means that you have to watch out for bikes when getting out on the passenger side, which people aren’t used to. People will stand around with their coffee, or wait in the middle of the bike lane for their lyft… Also some of the bike lanes are on the sidewalks co-existing with pedestrians, and then you always have the toddler on the escape running across the bike lanes. It’s always an adventure biking in the city!

I probably lose 100 karma points every day I bike, weaken my heart from stress cancelling out my strengthening it with exercise. But I always feel nice when I stop for someone crossing the sidewalk and they give me a big smile.

conclusion

Look at my v-brakes actually holding onto the brake cable!

bike12Later I had to go change the brake pads, since the ones that came with these brakes were pretty useless (not sure why, they looked fine). But all-in-all, it felt like a productive few hours… I also cleaned off my chain and re-greased it, fixed my gear changing issues, and ordered handlebar tape, since right now mine are bare metal. The previous ones were getting gross and sticky, so I gave up and threw them away.

(I couldn’t resist and also got one of those 16 in 1 tools hehe. I wanted my own set of bike levers since the MITERS ones occasionally disappear, and this set had some included, albeit metal ones).

Screenshot from 2018-12-09 10-18-08

Maybe if I keep fixing my bike with nice parts, I’ll eventually have upgraded the bike ^__^ I really want to get a new wheel, so the rims are less wobbly and the rear quick-lock works again. I also worry about the internal gear hub, I want to service it. It sounds a little … crunchy … sometimes, and I wonder if I need to oil it or something.

 

Wow that was a long picture heavy post. I played around with annotating the pictures using my touchscreen… Don’t know I’ll do that again, it was time-consuming.

Happy Holidays! I’ll likely be back soon, sooo many posts to write. This was just a fun detour.