; David Golombek
; daveg@mit.edu
; 6.034 pset #7

1)  1%


; David Golombek
; daveg@mit.edu
; 6.034 pset #7

2)

(define (conditional-probability net var1 var2)
  (/ (infer net (append var1 var2))
     (infer net var2)))

(conditional-probability mcbn1 '((e #f)) '((b #t)))
;Value: .7527272727272727

(conditional-probability mcbn1 '((d #t) (e #t)) '((a #t) (b #f)))
;Value: .076

(conditional-probability mcbn1 '((a #t)) '((d #t) (e #t)))
;Value: .4517167381974248


; David Golombek
; daveg@mit.edu
; 6.034 pset #7

3)

(define (independent? net var1 var2)
  ; check equality of
  (= 
   ; P(A&B)
   (infer net (list (list var1 #t) (list var2 #t)))
   ; P(A) * P(B)
   (* (infer net (list (list var1 #t)))
      (infer net (list (list var2 #t)))
      )))

(independent? mcbn1 'a 'b)
;Value: ()

(independent? mcbn1 'd 'e)
;Value: ()

(independent? mcbn1 'a 'd)
;Value: ()


; David Golombek
; daveg@mit.edu
; 6.034 pset #7

4A)

(define prof
  (define-bnet 'prof
    '((rain       ()     (0.9  0.1)) ;; chance of rain is .1, random choice
      (tomas-late (rain) ((0.95 0.05) (0.5 0.5)))
      (paul-late  (rain) ((0.9  0.1) (0.5 0.5)))
      )))

4B)

If Paul is late, it is more likely Tomas is late, because Paul is much
more likely to be late on a rainy day, and on these days, Tomas is
very likely to be late too.  So yes, probability Paul is late does
depend on whether Tomas is late, as long as you know nothing about the
state of it raining.  If we know that it is raining, then Tomas's
lateness is only a function of that, because we no longer need to
infer the possibility of it raining from Paul's lateness.  I could
answer these questions using Bayes net programs by testing if 
paul-late and tomas-late were independent, given rain to be #t
and later #f.

4C)

(define (c-independent? net var1 var2 instantiations)
  ; We need to deal with scheme's poor floating point math,
  ; so just check if diff is within .000005
  (> .000005 
     (abs 
      (- 
       ; P(A&B | E)
       (conditional-probability net
				(list (list var1 #t)
				      (list var2 #t))
				instantiations)
       ; P(A|E) * P(B|E)
       (* (conditional-probability net 
				   (list (list var1 #t))
				   instantiations)
	  (conditional-probability net 
				   (list (list var2 #t))
				   instantiations))
       ))))

(c-independent? mcbn1 'a 'b '((c #t)))
;Value: ()

(c-independent? mcbn1 'd 'e '((c #t)))
;Value: #t

(c-independent? mcbn1 'a 'd '((b #f) (c #t)))
;Value: #t

(c-independent? mcbn1 'a 'd '((b #t) (c #f)))
;Value: #t


; David Golombek
; daveg@mit.edu
; 6.034 pset #7

5A)

(define Holmes
  (define-bnet 'holmes
    '((Burgled () (0.9999 0.0001))
      (Alarm (Burgled) ((0.998 0.002) (0.01 0.99)))
      (Watson (Alarm) ((0.75 0.25) (0.15 0.85)))
      (Gibbons (Alarm) ((0.90 0.10) (0.5 0.5)))
      )))

5B)

(conditional-probability Holmes '((Burgled #t)) '((Watson #t) (Gibbons #t)))
;Value: 1.6292872313417586e-3

5C)

(define quake
  (define-bnet 'quake
    '((Burgled () (.9999 .0001))
      (Earthquake () (.9999 .0001))
      (Alarm (Burgled Earthquake) (noisy-or .002 .00099 .0007))
      (RadioReport (Earthquake) ((1 0) (.03 .97)))
      (Watson (Alarm) ((.75 .25) (.15 .85)))
      (Gibbons (Alarm) ((.9 .1) (.5 .5)))
      )))

(conditional-probability quake '((Burgled #t)) '((Watson #t) (Gibbons #t)
						 (RadioReport #t)))

;Value: 1.0151418613001273e-4
