Professional Documents
Culture Documents
Lab 1 - John King
Lab 1 - John King
Lab 1 - John King
Exercise 1:
Calculations are above for part 1. Code below for part b:
i.
var d1 = function(){
flip() ? flip(.7) : flip(.1)
viz(repeat(1000,d1))
ii.
var d1 = function(){
flip(flip() ? .7 : .1)
viz(repeat(1000,d1))
iii.
var d1 = function(){
flip(.4)
viz(repeat(1000,d1))
part c)
flip(flip() ? .2 : .6)
Exercise 2:
a) In the first example, the function is evaluated before it is put into an array, while the
second is evaluated for each entry in the array
b) -
var foo = mem(function() {return flip()});
display([foo(), foo(), foo()]);
c) -
var foo = mem(function(x) {return flip()});
display([foo(0), foo(0), foo(1)]);
Exercise 3:
B is the answer. See below
Exercise 4:
a)
b)
Infer({method: "forward", samples: 1000}, function() {
var allergies = flip(0.3);
var cold = flip(0.2);
return [ sneeze('bob'),fever('bob')];
})
Exercise 5:
a)
b)
var makeCoin = function(weight) {
return function() {
return flip(weight) ? 'h' : 't'
}
}
var bend = function(coin) {
return function() {
return coin() == 'h' ? makeCoin(.7)() : makeCoin(.1)()
}
}
return bentCoin();
})
Exercise 6:
a) This is (1-p)^5, since it is the probability there is no true before the sixth flip
b)
Exercise 7:
a)
var a = flip(.8)
var b = a ? flip() : flip(.3)
b)
Exercise 8:
a) This squares
b) The distribution would be the same
Exercise 9:
Exercise 10:
a)
};
Above is my measure. It represents how much smaller the tower will be after falling. This is a
good measure because it shows what percentage of the stable will remain.