Lab 1 - John King

You might also like

You are on page 1of 7

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);

var sneeze = cold || allergies;


var fever = cold;
return [ sneeze,fever];
})

c) This is the same as the pervious distribution

var allergies = mem(function(person) {return flip(.3)});


var cold = mem(function(person) {return flip(.2)})

var sneeze = function(person) {return cold(person) || allergies(person)}


var fever = function(person) {return cold(person)}
Infer({method: "forward", samples: 1000}, function() {

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)()
}
}

var fairCoin = makeCoin(.5)


var bentCoin = bend(fairCoin)

Infer({method: "forward", samples: 1000}, function() {

return bentCoin();
})

Exercise 6:

a) This is (1-p)^5, since it is the probability there is no true before the sixth flip
b)

var geometric = function(p) {


return flip(p) ? 0 : 1 + geometric(p)
};
Infer({method: "forward", samples: 1000}, geometric)

Exercise 7:

a)

var a = flip(.8)
var b = a ? flip() : flip(.3)

b)

Infer({method: "forward", samples: 1000}, function(){


var a = flip(.8)
var b = a ? flip() : flip(.3)
return [a,b]
})

Exercise 8:

a) This squares
b) The distribution would be the same

Exercise 9:

var ground = {shape: 'rect',


static: true,
dims: [worldWidth, 10],
x: worldWidth/2,
y: worldHeight}

var rect = {shape: 'rect',


static: false,
dims: [10, 100],
x: worldWidth/2,
y: 390}

var ball = {shape:'circle',


static: false,
dims: [20],
x: 100,
y: 200,
velocity: [1000,1000]
}

var bowlingWorld = [ground, rect,ball]


physics.animate(1000, bowlingWorld);

Exercise 10:

a)

var towerFallDegree = function(initialW, finalW) {


getTowerHeight(finalW)/getTowerHeight(initialW)

};

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.

You might also like