PasteRack.org
Paste # 3720
2014-03-11 14:32:38

Forked from paste # 1963.

Fork as a new paste.

Paste viewed 154 times.


Embed:

vector field - Ultimatum game

  1. (require plot)
  2. (require math)
  3.  
  4. ;; usually in matrix calculation, there will be matrix that has only one element
  5. ;; this function to turn that matrix into number
  6. (define (numberise matrix)
  7.   (first (matrix->list matrix)))
  8.  
  9. ;; ULTIMATUM GAME
  10. ;;
  11. ;; ----------------------
  12. ;;        s2  |    t2
  13. ;; -----------+----------                                (2)   |   EU1(s1) > = EU1(t1) forall q
  14. ;; s1 |       |   1,4     -> p     -> EU1(s1) = (q  1-q) (1)   |   -> EU1 will always suggest to
  15. ;; ---+  2,2  |----------                                      |-> choose s1 or to encourage to
  16. ;; t1 |       |   0,0     -> (1-p) -> EU1(t1) = (q  1-q) (2)   |   set p=1 forall q<1
  17. ;; ----------------------                                (0)   |   at q=1, P1 is indifferent
  18. ;;        q      (1-q)
  19. ;;
  20. ;; EU2(s2) = (p 1-p) (2) = 2 forall p;  EU2(t2) = (p 1-p) (4)
  21. ;;                   (2)                                  (0)
  22. ;; -> p<1/2, EU2(s2) > EU2(t2) -> choose s2, set q=1
  23. ;;    p>1/2, vice versa
  24. ;;    p=1/2, P2 is indifferent
  25.  
  26. ;; in the dynamic (vector field), vector p will always point to the right, with the magnitude derease linearly
  27. ;; vector q will increase for p<1/2 linearly, then decreasy for p>1/2 linearly (symmetric magnitude through
  28. ;; the vertical line p=1/2, but reverse direction)
  29.  
  30. (define payoff-panel (list (list 2 2 2 4)
  31.                            (list 1 0 2 0)))
  32.  
  33. (define (get-payoff-matrix name)
  34.   (list->matrix 2 1
  35.    (list (list-ref (first payoff-panel) name)
  36.          (list-ref (last payoff-panel) name))))
  37. ;; name = {0 1 2 3} = {s1 t1 s2 t2}
  38.  
  39. (define (belief-matrix p) (matrix [[p (- 1 p)]]))
  40. ;; given p, generate the belief matrix #[#[p] [1-p]]
  41.  
  42. ;; expected payoff of player 1 given the probability distribution over P2's strategies
  43. (define (EU1 q)
  44.   (list (numberise (matrix* (belief-matrix q)
  45.                             (get-payoff-matrix 0)))     ; when P1 plays s1
  46.         (numberise (matrix* (belief-matrix q)
  47.                             (get-payoff-matrix 1)))))   ; when P1 plays t1
  48.  
  49. ;; expected payoff of P2 given P1's p
  50. (define (EU2 p)
  51.   (list (numberise (matrix* (belief-matrix p)
  52.                             (get-payoff-matrix 2)))     ; when P2 plays s2
  53.         (numberise (matrix* (belief-matrix p)
  54.                             (get-payoff-matrix 3)))))   ; when P2 plays t2
  55.  
  56. ;; given that q of P2, P1 calculates EU1 over s1 and t1 -> chooses his p
  57. (define (p->? q)
  58.   (cond [(> (first (EU1 q)) (last (EU1 q))) 1]          ; if EU1(s1) > EU1(t1), choose s1, p=1
  59.         [(< (first (EU1 q)) (last (EU1 q))) 0]          ; if EU1(s1) < EU1(t1), set p=0
  60.         [else 1/2]))                                    ; if equals, set p=1/2
  61.  
  62. ;; the same for P2 to decide his q given his belief on p
  63. (define (q->? p)
  64.   (cond [(> (first (EU2 p)) (last (EU2 p))) 1]
  65.         [(< (first (EU2 p)) (last (EU2 p))) 0]
  66.         [else 1/2]))
  67.  
  68. (plot (vector-field
  69.       (lambda (p q)
  70.         (vector (cond [(equal? (p->? q) 1) (- 1 p)]     ; if EU1 tells to set p=1, then add (1-p) to belief p
  71.                       [(equal? (p->? q) 0) (- p)]       ; if EU1 tells to set p=0, then minus p from belief p
  72.                       [else 0])                         ; if EU1 tells to set p=1/2, then keep the same p
  73.                 (cond [(and (equal? (q->? p) 1)         ; to update the vector q, for the first part of p,
  74.                             (< p 1/2)) q]               ; when EU2 tells to set q=1, add q to the belief q
  75.                       [(and (equal? (q->? p) 0)         ; if EU2 tells to set q=0, for p>1/2, minus q from belief q
  76.                             (> p 1/2)) (- q)]           ;
  77.                       [else 0])))                       ; if EU2 sets q=1/2, keep the same q
  78.       0 1 0 1)) ;; #:out-file "tp-coor.png"                ; let p & q run from 0 to 1, output the file as the given name
  79.  

=>

image