;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname 22prob) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))))) ;; DEMO: probability #| DATA DEFINITION A (weighted X) is a structure - (make-weighted weight value) where `weight` is a number and `value` is an X A (dist X), aka "probability distribution of X" is a (listof (weighted X)) where the weights in the list add up to 1 |# (define-struct weighted (weight value)) ;; payoff : natural -> number ;; give the payoff in dollars for the given roll in the "straight-line" dice game (define (payoff roll) (cond [(= 0 roll) 0] [(= 1 roll) 1] [(= 2 roll) 1] [(= 3 roll) 0] [(= 4 roll) 3] [(= 5 roll) 2] [(= 6 roll) 0] [(= 7 roll) 2] [(= 8 roll) 0] [(= 9 roll) 0] [else (+ (payoff (remainder roll 10)) (payoff (quotient roll 10)))])) (check-expect (payoff 7) 2) (check-expect (payoff 18) 1) (check-expect (payoff 14) 4) ;; d : number -> (dist number) ;; compute the probability distribution of rolls for a die with N sides (define (d n) (build-list n (lambda (i) (make-weighted (/ 1 n) (add1 i))))) (check-expect (d 4) (list (make-weighted 1/4 1) (make-weighted 1/4 2) (make-weighted 1/4 3) (make-weighted 1/4 4))) ;; expectation : (dist X) (X -> number) -> number ;; compute the average result from applying `f` to values drawn from `dist` (define (expectation dist f) (foldl (lambda (wv sum) (+ sum (* (weighted-weight wv) (f (weighted-value wv))))) 0 dist)) (check-expect (expectation (d 4) payoff) 5/4) ;; probability : (dist X) (X -> boolean) -> number ;; compute the probability of `(p? x)` when `x` drawn randomly from `dist` (define (probability dist p?) ...) ;(check-expect (probability (d 6) (lambda (n) (< n 5))) ; ...) ;; Odds of dice 1 vs dice 2: P(observation|dice1) / P(observation|dice2) ;; How much *evidence* is added by an observation?