Simulations for Dirichlet-multinomial variant wurwur model (Frank, Goodman, & Tenenbaum, 2009)


In [1]:
# setup stuff
% load_ext autoreload
% autoreload 2

import numpy as np
from random import *
from dmww_classes import *
from sampling_helper import *

seed(1) # for debugging

(1) Mutual Exclusivity


In [2]:
# define inference parameters to be used in all ME simulations
p = Params(n_samps=20,
           n_particles=1,
           alpha_r=.0001,
           alpha_nr=.001,
           empty_intent=.1,
           n_hypermoves=5)

[A] Toy corpus with familar word

Create toy corpus.


In [35]:
# build world
w = World(n_words=5, n_objs=5)
w.show()

# build corpus
c = Corpus(world=w, n_sents=20, n_per_sent=1)
c.show()


n_objs = 5
n_words = 5
o: [1] w: [1]
o: [4] w: [4]
o: [0] w: [0]
o: [2] w: [2]
o: [2] w: [2]
o: [0] w: [0]
o: [1] w: [1]
o: [3] w: [3]
o: [4] w: [4]
o: [0] w: [0]
o: [2] w: [2]
o: [0] w: [0]
o: [4] w: [4]
o: [0] w: [0]
o: [0] w: [0]
o: [1] w: [1]
o: [3] w: [3]
o: [1] w: [1]
o: [0] w: [0]
o: [3] w: [3]

Add novel word to the world and then add additional ME observation to corpus.


In [36]:
w.update(1, labels = ["fep"])
w.show()

c.sents.append([array([1,5]),array([5])])
c.update(w)

c.show()


new object indices: 5
new word indices: 5
n_objs = 6
n_words = 6
o: [1] w: [1]
o: [4] w: [4]
o: [0] w: [0]
o: [2] w: [2]
o: [2] w: [2]
o: [0] w: [0]
o: [1] w: [1]
o: [3] w: [3]
o: [4] w: [4]
o: [0] w: [0]
o: [2] w: [2]
o: [0] w: [0]
o: [4] w: [4]
o: [0] w: [0]
o: [0] w: [0]
o: [1] w: [1]
o: [3] w: [3]
o: [1] w: [1]
o: [0] w: [0]
o: [3] w: [3]
o: [1 5] w: [5]

Now, do inference with the ME observation in the corpus.


In [37]:
m = Lexicon(c, p,
            verbose=0,
            hyper_inf=True)

m.learn_lex_gibbs(c, p)
m.show()
[m.ref_score]



...................
--- score full lex ---
[[ 7.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.]
 [ 0.  0.  3.  0.  0.  0.]
 [ 0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  3.  0.]
 [ 0.  0.  0.  0.  0.  1.]]
 [ 0.  1.  0.  0.  0.  0.]
counts: 21
    intent obj: [0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -2.3 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.8]
full score: r -10.8, nr -1.8, i -5.1, p 0.2,  total: -17.5

 *** average sample time: 0.021 sec
[[ 7.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.]
 [ 0.  0.  3.  0.  0.  0.]
 [ 0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  3.  0.]
 [ 0.  0.  0.  0.  0.  1.]]
nr: [ 0.  1.  0.  0.  0.  0.]
Out[37]:
[array([-1.79196363, -1.79188446, -1.79188446, -1.79188446, -1.79188446,
       -1.79175947])]

In [39]:
m.plot_lex(w)


The novel word ("fep") gets mapped to the novel object (fep) in this small corpus.

[B] Large corpus with familiar word

Set up world and corpus.


In [40]:
corpusfile = 'corpora/corpus.csv'

w = World(corpus=corpusfile)
w.show()

c = Corpus(world=w, corpus=corpusfile)


n_objs = 23
n_words = 420

Add novel word and object to world, then add ME observation.


In [42]:
# BIRD = 22
# FEP = 23
# fep = 420

w.update(1, labels = ["fep"])
w.show()

c.sents.append([array([22,23]),array([420])])

c.update(w)


new object indices: 23
new word indices: 420
n_objs = 24
n_words = 421

Now, do inference with ME observation.


In [44]:
l = Lexicon(c, p,
            verbose=0,
            hyper_inf=True);

l.learn_lex_gibbs(c, p);



...................
--- score full lex ---
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  1.]]
 [   6.    0.   20.    4.    1.   58.    1.    1.    1.    3.    2.    1.
    2.    1.   27.    4.    1.    1.    2.    3.    3.    1.    0.   25.
    2.    6.    4.    1.    6.    2.    2.   11.    1.    5.    5.    1.
    1.    1.    2.    1.    5.   22.    1.    1.    1.    0.    1.    0.
    2.    3.    4.    4.    7.    1.    3.   15.    8.    1.   16.    3.
    0.    2.    1.   10.    1.   28.    1.    2.    3.    5.   32.    1.
    2.    5.    2.    5.    2.    1.    1.    5.    1.    1.    1.    4.
   64.    1.    1.    2.    1.    5.    1.    1.    1.    1.    7.    2.
    2.    1.    1.    1.    1.    7.    2.    2.    1.    1.    2.    1.
    7.   15.    1.    2.    2.    2.    3.    0.    2.    1.    2.    1.
    1.    1.    5.    0.    0.    3.    3.    1.    3.    3.    3.    1.
   14.    3.    5.    2.    2.    4.    9.    5.    1.    4.    2.   13.
    1.    5.    2.   15.    2.   19.    1.    1.    3.    5.    2.    5.
    1.    2.    7.    2.    1.    1.    6.    3.    2.   55.    0.   11.
    1.    1.    2.    1.    1.    1.    1.    3.    5.    7.    1.    3.
    3.    1.    2.    1.    3.    2.    1.    1.    4.    2.    1.    1.
    6.    1.    5.    1.    1.    1.    1.    1.    1.    1.    1.   27.
    3.    2.   22.    0.    3.    2.   19.    2.    0.    0.    2.    1.
    0.    2.    1.    1.    2.    3.    1.    4.    4.    1.   26.   27.
    1.    8.    1.    7.    0.    1.    1.    2.    1.    5.   27.    1.
    1.    3.    1.    1.    1.    1.    1.    1.    1.    1.   14.    1.
    1.    1.    2.    1.    5.    3.    1.    1.    3.    2.    1.    9.
    0.    6.    0.    1.    6.    0.    5.    1.    3.    1.    2.    2.
   15.    3.    1.   25.   39.    1.    2.   23.    1.    3.    7.    1.
    0.    1.    2.   11.    1.    1.    1.    3.    1.    1.    1.    7.
    1.    2.   21.    2.    1.    1.    4.    1.    3.  105.    1.    5.
    1.   11.    3.    1.    1.   12.    2.    2.    7.    9.    2.   19.
    2.    5.   14.    1.    7.    2.    7.    6.    1.   21.    4.    3.
    1.    2.    1.    1.    2.    3.    2.    6.    5.    6.    2.    1.
    1.    9.    1.    7.    3.    8.    7.    2.    1.    1.    4.   12.
    4.   33.    2.   12.    1.    2.    1.    1.   14.    7.    1.    2.
    4.    5.   12.    4.    1.    5.   17.    4.    1.    1.    3.    3.
    4.    2.    1.   11.    3.    9.    1.    1.   12.    2.    6.    1.
    5.    7.    8.    1.    4.    8.    1.    1.    2.    4.    4.    1.
    2.    1.   82.    1.    1.    1.    3.    2.    4.    1.    5.    2.
    0.]
counts: 2476
    intent obj: [1 0 1 4 3 2 0 0 5 5 4 4 4 5 2 2 0 4 4 0 0 5 5 0 3 3 0 0 1 2 0 0 0 2 0 0 0
 0 0 0 0 2 2 2 0 1 1 2 1 2 0 0 1 2 0 1 1 0 1 0 0 0 1 1 1 2 2 1 1 0 1 2 0 0
 1 1 1 2 0 1 1 1 0 0 0 1 2 2 1 1 1 2 1 1 1 1 2 2 2 2 0 2 1 0 0 0 2 0 0 1 2
 0 0 2 2 2 0 0 0 2 0 0 2 0 2 2 0 0 2 0 2 0 0 0 0 0 2 2 0 1 0 0 0 1 1 1 1 1
 1 1 0 1 1 1 1 0 1 1 0 1 3 2 3 3 0 3 3 3 3 0 2 2 3 2 1 0 2 0 0 0 0 1 2 2 2
 1 0 3 0 0 3 1 2 2 2 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 0 1 1 0 1 1 0 0
 1 1 0 1 0 0 1 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 2 1 2 0 0 2 1 0 0 0 0 0 1
 0 1 2 0 2 1 1 0 2 0 3 3 0 2 3 0 0 3 0 0 0 2 0 2 0 3 1 0 1 1 0 1 1 3 1 1 3
 3 0 3 3 3 0 2 0 1 1 2 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 3 4 0 0 2
 2 1 0 2 2 0 4 0 4 4 4 4 4 0 2 5 5 5 5 5 1 1 3 1 2 1 1 1 1 0 0 1 0 1 1 1 1
 1 1 0 1 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 1
 1 1 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 1 1 1 1 1 0 2 0 0 2 2
 2 0 2 0 2 0 1 0 0 1 2 2 0 2 1 1 1 1 0 2 2 1 2 2 3 2 3 2 1 1 2 0 3 3 2 2 1
 1 2 0 2 2 2 0 0 3 0 3 2 2 1 1 1 0 2 2 2 2 2 2 2 2 1 1 1 2 1 0 1 0 0 0 0 0
 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 0
 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 2 0 0 0 0 0 1 1
 1 0 0 1 1 2 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 0 2 0 2 1 1]
    ref word: [ 0  3 12  1  4  0  0  2  0  2  2  0  1  0  0  0  0  2  3  1  0  1  3  0  0
  0  0  0  0  2  0  1  1  1  3  6  5  0  1  3  0  5  6  1  3  2  3  1  0  1
  0  5  0  1  0  0  2  4  4  0  2  3  0  0  3  6  0  2  1  0  1  1  0  1  1
  0  0  2  0  1  0  1  0  0  1  2  0  3  3  0  3  3  2  2  0  5  1  0  1  0
  0  0  4 10  4  0  2  4  0  4  3  0  3  3  0  0  3  0  0  1  0  0  1  0  0
  1  1  0  4  3  0  4  2  0  0  2  0  0  0  3  0  0  2  0  0  2  0  0  0  1
  0  2  0  0  0  0  4  0  0  2  4  0  0  2  0  0  2  0  1  0  0  4  0  0  1
  0  0  1  0  1  0  1  1  3  0  0  8  4  0  0  0  0  5  0  0  0  7  0  4  2
  1  4  8  0  3  8  0  1  2  0  0  1  1  0  0  1  7  0  1  2  6  1  0  0  0
  1  1  0 12  0  0  2  3  0  0  0  4  3  2  0  1  0  0  1  1  5  3  1  0  2
  0  0  1  1  1  7  0  0  0  5  0  1  3  6  0  2  0  2  1  0  3  0  2  4  0
  2  0  0  0  1  3  0  3  0  3  0  0  0  0  3  2  0  3  2  2  0  2  8  0  4
  3  0  2  0  1  0  0  0  0  0  0  0  3 10  2  0  0  1  1  0  1  0  0  0  1
  0  1  0  2  3  0  1  2  2  2  0  2  2  0  0  0  3  2  0  0  3  0  0  0  2
  0  0  2  0  2  4  0  0  2  1  1  3  1  1  0  1  2  4  0  0  0  1  3  0  2
  1  1  0  0  0  0  0 11  0  0  3  0  0  4  1  0  0  0  2  1  2  1  1  0  0
  1  0  1  0  1  0  0  3  0  4  0  0  1  0  0  0  2  2  2  3  2  0  1  1  0
  0  7  2  1  0  0  0  0  0  0  0  2  2  2  5  0  0  1  3  0  0  5  1  0  0
  0  2  1  1  7  0  0  1  6  1  1  4  0  0  0  2  2  1  5  6  2  3  4  3  1
  0  1  1  5  2  0  0  3  1  0  1  2  0  0  0  0  0  2  1  4  0  1  0  0  0
  0  1  1  1  0  1  2  1  1  0  1  0  4  2  0  0  0  0  0  4  0  3  0  0  1
  0  0  0  0  3  3  0  0  0  0  1  1  3  1  1  0  1  0  2  0  0  0  2  0  2
  0  0  0  1  1  0  6  1  1  3  0  0  0  2  2  2  0  8  8  0  0  4  0  0  0
  0  0  2  6  3  3  0  3  2  0  3  3  0  0  3  2  1  2 13  0  3  3  1  0  2
  2  3  3  1  5  0  4  4  1  1  1  0  0  0  4  0  0 11  1  0  0  1  0  2  0
  0]
    intent obj prob: [-1.5 -1.5 -1.7 -1.7 -1.7 -1.7 -1.7 -1.7 -2.3 -2.3 -1.7 -1.7 -1.7 -2.3 -1.7
 -1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 -0.8 -2.3 -0.8 -0.8]
full score: r -1648.6, nr -11386.7, i -774.7, p -73.6,  total: -13883.6

 *** average sample time: 5.909 sec

Plot.


In [57]:
l.plot_lex(w, certainwords = 1)


Luce choice in 2AFC novel-novel task.


In [65]:
known_word = l.ref[22, 8] #BIRD, bigbird
correct = l.ref[23, 420] #FEP, fep
incorrect = l.ref[22, 420] #BIRD, fep
lc = divide(correct, correct + incorrect)

print 'Known word, correct object: ' + str(known_word)
print 'Novel word, correct object: ' + str(correct)
print 'Novel word, incorrect object: ' + str(incorrect)
print 'Novel word, Luce Choice: ' + str(lc)


Known word, correct object: 26.0
Novel word, correct object: 1.0
Novel word, incorrect object: 0.0
Novel word, Luce Choice: 1.0

The novel word gets mapped to the novel object.

[C] Large corpus with "familiar" word learned ("novel-novel" ME)

Set up world and corpus.


In [45]:
corpusfile = 'corpora/corpus.csv'

w = World(corpus=corpusfile)
w.show()

c = Corpus(world=w,corpus=corpusfile)


n_objs = 23
n_words = 420

Add TWO novel words and novel objects, and add ME observations.


In [46]:
# FEP = 23
# fep = 420
# TOMA = 24
# toma = 421

w.update(2, labels = ["fep", "toma"])
w.show()

c.sents.append([array([23]),array([420])]) # novel training
c.sents.append([array([23,24]),array([421])]) # ME test

c.update(w)


new object indices: 23, 24
new word indices: 420, 421
n_objs = 25
n_words = 422

Now, do inference with Gibbs sampler.


In [47]:
l = Lexicon(c, p,
            verbose=0,
            hyper_inf=True)

l.learn_lex_gibbs(c, p);



...................
--- score full lex ---
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  1.  0.]
 [ 0.  0.  0. ...,  0.  0.  1.]]
 [  6.   5.  20.   4.   1.  58.   1.   1.   1.   3.   2.   1.   2.   1.  28.
   4.   1.   1.   2.   3.   3.   1.   0.  21.   2.   6.   4.   1.   6.   2.
   2.  16.   1.   5.   1.   1.   1.   1.   2.   2.   5.  15.   1.   1.   1.
   2.   1.   0.   2.   3.   4.   4.   6.   1.   3.  12.   9.   1.  16.   0.
   3.   2.   1.  10.   1.  28.   1.   2.   3.   5.  35.   1.   2.   5.   2.
   5.   2.   1.   1.   5.   1.   1.   1.   4.  59.   1.   1.   2.   1.   5.
   1.   1.   1.   1.   7.   2.   2.   1.   1.   1.   1.   7.   2.   2.   1.
   1.   2.   1.   7.  20.   1.   2.   2.   2.   3.   0.   2.   1.   2.   1.
   1.   1.   5.   0.   2.  18.   4.   1.   3.   3.   3.   1.  14.   3.   5.
   2.   2.   4.   9.   5.   1.   4.   2.  12.   1.   5.   2.  15.   2.  19.
   1.   1.   3.   6.   2.   5.   1.   2.   7.   2.   1.   1.   6.   3.   2.
  29.   0.  10.   1.   1.   2.   1.   1.   1.   1.   3.  16.   7.   1.   3.
  10.   1.   2.   1.   3.   2.   1.   1.   6.   2.   1.   0.   3.   2.   5.
   1.   1.   1.   1.   1.   1.   1.   1.  27.   7.   2.  22.   3.   3.   2.
  16.   2.   3.   2.   2.   1.   3.   2.   1.   1.   2.   3.   1.   4.   2.
   1.  32.  26.   1.   6.   1.   7.   2.   1.   1.   2.   1.   6.  26.   1.
   1.   3.   1.   1.   1.   5.   1.   1.   1.   1.  14.   1.   1.   1.   2.
   1.   5.   2.   1.   2.   3.   3.   1.   4.   1.   6.   0.   1.   6.   0.
   5.   1.   0.   1.   2.   6.  11.   0.   1.  38.  48.   1.   2.  23.   1.
   3.   7.   1.   0.   1.   2.  11.   1.   1.   1.   3.   1.   1.   1.   5.
   1.   2.  22.   2.   1.   1.   0.   1.   3.  63.   1.   5.   1.  12.   3.
   1.   1.   9.   2.   2.   7.   9.   2.  16.   2.   5.  14.   1.   7.   2.
   7.   6.   1.  21.   4.   3.   1.   2.   1.   1.   2.   3.   2.   6.   5.
   6.   2.   1.   1.   9.   1.  12.   3.   8.   2.   2.   1.   1.   4.   4.
   4.  26.   0.  16.   1.   2.   1.   1.  14.   7.   1.   2.   4.   5.  12.
   4.   1.   5.  19.   1.   1.   1.   3.   3.   4.   2.   1.  11.   3.   9.
   1.   1.  12.   2.   6.   1.   5.   7.  14.   1.   4.   7.   1.   1.   2.
   4.   4.   1.   1.   1.  72.   1.   1.   1.   3.   2.   4.   1.   5.   2.
   0.   0.]
counts: 2474
    intent obj: [0 1 0 4 3 2 0 4 4 5 1 5 0 0 1 2 0 3 3 5 0 5 4 5 7 1 5 5 6 0 1 0 0 2 0 0 0
 0 0 0 1 2 2 2 0 1 1 0 1 0 0 0 1 2 0 1 0 0 1 0 0 0 0 0 1 2 2 1 1 2 0 2 0 0
 1 2 1 0 1 1 1 1 0 0 0 2 2 1 1 1 2 1 2 2 0 2 0 0 2 2 1 2 2 1 2 0 0 2 0 2 2
 2 2 2 0 2 1 0 1 2 0 0 2 0 2 1 1 0 1 1 2 0 2 0 0 1 0 2 0 0 0 0 0 1 0 1 0 1
 1 0 0 0 1 1 1 0 1 1 0 1 1 1 3 1 1 2 2 3 3 0 1 2 2 1 1 0 1 1 0 0 0 3 3 0 3
 0 0 3 1 0 0 1 2 1 2 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
 1 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 2 0 0 0 1 1 0 0 0 0 1
 0 0 0 0 0 0 1 1 0 0 0 0 0 2 3 0 3 0 0 3 0 0 0 1 3 0 0 1 0 0 0 1 0 3 1 1 3
 0 0 3 1 1 0 2 0 1 1 3 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 2 2 2 3 3 1 2
 2 0 0 4 4 0 4 2 2 2 1 1 2 4 3 2 3 5 5 0 4 4 2 4 0 1 1 1 1 1 1 1 0 0 0 1 1
 1 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1 0
 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 2 0 2 2 2 0
 2 0 0 0 0 0 1 0 0 1 0 2 0 0 1 1 1 0 2 2 2 1 2 2 0 2 0 0 0 0 0 0 3 3 0 0 1
 1 1 2 2 0 0 1 1 3 1 3 3 3 1 1 1 1 2 1 2 1 2 0 0 2 1 1 1 0 2 2 1 0 0 0 0 0
 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 0 0 1 0
 1 0 0 1 1 0 0 0 1 1 1 1 0 0 0 0 2 0 0 0 1 1 0 0 0 0 0 1 1 2 0 2 1 0 0 1 0
 1 2 2 1 2 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 2 2 2 1 0 1]
    ref word: [ 0  3  0  5  4  0  0  1  1  2  0  1  5  3  0  0  0  3  3  1  0  1  0  0  5
  0  0  0  2  2  0  1  1  0  3  6  5  0  1  1  0  4  8  2  3  2  3  3  0  3
  0  5  0  0  0  0  0  4  3  0  3  3  0  1  3  6  0  2  1  2  0  0  0  1  1
  0  0  0  0  1  0  1  0  0  0  5  0  3  0  1  4  4  0  3  0  3  0  0  1  0
  0  1  1 10  5  0  1  2  0  2  1  0  0  4  1  0  1  0  0  1  0  0  0  0  0
  2  1  0  3  3  0  1  1  0  0  2  0  0  0  0  0  0  3  0  0  2  1  0  0  6
  0  1  0  0  0  0  4  0  0  2  2  3  0  2  0  3  3  0  0  0  3  4  9  0  1
  0  1  1  0  1  0  1  2  1  0  3  0  1  0  0  3  4  4  4  1  0  8  0  2  1
  1  4  3  1  5  7  0  1  2  0  0  0  0  0  0  3  6  0  0  0  6  1  0  0  0
  3  1  0  4  0  0  2  3  0  0  0  4  3  2  0  1  0  0  1  0  5  3  1  0  2
  0  0  0  0  0  4  0  0  0  0  1  5  2  0  0  1  0  2  0  0  2  1  5  1  0
  0  0  3  0  3  2  0  4  0  1  1  0  1  0  0  5 16  4  1  1  0  4 10  0  3
  2  0  2  1  0  0  0  0  0  0  1  4  3  4  4  0  0  2  1  0  1  2  0  0  0
  0  2  1  1  3  4  0  1  1  1  0  5  1  0  1  1  1  2  0  1  2  2  0  1  1
  3  3  0  0  2  1  0  0  2  0  4  0  2  3  0  3  2  2  0  0  1  4  3  0  2
  1  1  1  0  1  1  0  3  0  0  0  0  6  4  1  1  0  0  1  1  0  0  0  6  0
  1  0  0  1  3  0  0  5  0  5  2  2  0  0  0  0  2  0  2  3  2  0  1  1  0
  0  6  3  2  1  1  0  0  0  0  0  2  2  4  5  1  0  1  2  1  0  2  0  0  4
  0  2  1  1  4  0  6  2  6  1  6  0  4  1  0  2  2  1  4  1  0  2  0  2  0
  2  2  0  4  1  0  0  2  1  4  2  2  0  0  0  0  0  0  2  4  0  1  0  0  0
  0  1  1  1  0  3  2  1  1  1  6  0  6  4  3  0  0  0  1  0  0  3  0  0  0
  0  0  0  0  0  0  0  0  0  1  1  1 10  1  1  0  0  0  0  0  0  0  2  0  2
  0  0  0  2  1  0 10  5  1  5  0  0  0  1  3  0  0  8  8  0  0  4  0  0  0
  0  0  2  5  1  1  0  3  2  0  3  4  0  0  3  0  1  0  8  0  0  1  1  0  0
  0  3  3  1  5  0  3  3  5  3  3  0  0  1  2  1  0  4  5  0  0  2  2  3  0
  0  0]
    intent obj prob: [-1.5 -1.5 -1.7 -1.7 -1.7 -1.7 -1.7 -1.7 -1.7 -2.3 -1.7 -2.3 -1.7 -1.7 -1.7
 -1.7 -1.7 -1.7 -1.7 -2.3 -1.7 -2.3 -1.7 -2.1 -2.3 -2.1 -2.1 -2.1 -2.1 -0.8
 -0.8 -0.8 -0.8 -2.3 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -2.3 -2.3 -2.3 -0.8
 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -2.3 -0.1 -2.3 -0.1 -0.1 -2.3 -0.1
 -0.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 -0.8 -1.2 -1.2 -1.5 -1.2 -1.2 -1.2 -0.8 -0.8 -0.8 -0.8 -2.3 -2.3 -1.2 -1.2
 -1.2 -1.2 -1.2 -1.2 -2.3 -0.8 -0.8 -0.8 -0.8 -2.3 -1.2 -2.3 -2.3 -2.3 -1.2
 -1.2 -1.2 -1.2 -1.2 -1.2 -1.2 -1.2 -1.2 -0.8 -0.8 -2.3 -0.8 -0.8 -0.8 -0.8
 -2.3 -2.3 -0.8 -0.1 -0.1 -0.1 -0.1 -0.1 -2.3 -2.3 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -2.3 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -2.3 -0.1 -2.3 -2.3 -2.3 -2.3 -0.1 -2.3 -0.1 -0.1 -2.3 -0.1
 -2.3 -0.1 -0.1 -2.3 -2.3 -0.1 -0.1 -0.1 -2.3 -2.3 -2.3 -0.8 -0.8 -0.8 -0.8
 -0.8 -2.3 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -2.3
 -0.8 -2.3 -0.8 -0.8 -0.8 -0.8 -0.8 -0.8 -2.3 -2.3 -0.8 -2.3 -0.8 -2.3 -2.3
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -2.3 -2.3 -2.3 -0.1 -0.1 -0.1 -2.3 -2.3 -2.3 -0.8 -0.1 -0.8]
full score: r -1642.7, nr -11239.0, i -723.0, p -152.0,  total: -13756.7

 *** average sample time: 5.943 sec

Plot.


In [48]:
l.plot_lex(w, certainwords = 1)


Luce choice in 2AFC novel-novel task


In [49]:
correct = l.ref[24, 421]
incorrect = l.ref[23, 421]

lc = divide(correct, correct + incorrect)

print 'Novel word, correct object: ' + str(correct)
print 'Novel word, incorrect object: ' + str(incorrect)
print 'Novel word, Luce Choice: ' + str(lc)


Novel word, correct object: 1.0
Novel word, incorrect object: 0.0
Novel word, Luce Choice: 1.0

At ME test, novel object gets correctly mapped.

(2) Yu and Smith (2007): X-Sit Sim

Make corpus. The properties of this training corpus are described in the paper.


In [13]:
# set parameters 
condition = 3
num_words = 18
num_occurrences = 6
total_words = num_words * num_occurrences
num_trials = total_words / condition
items = np.ones(num_words) * num_occurrences

# make empty corpus
yucorpus =  list()
for i in range(num_trials):
    yucorpus.append([np.zeros(condition, dtype=int8), 
                     np.zeros(condition, dtype=int8)])

# generate actual corpus
try:
    for i in range(num_trials): # for each trial
        for j in range(condition): # for each word/object pairing in the trial
            item_probs = np.divide(items, total_words)
            
            yucorpus[i][0][j] = where(np.random.multinomial(1, item_probs) == 1)[0][0]
            
            # avoid repeats
            c = 1
            while sum(yucorpus[i][0][j] == yucorpus[i][0]) > 1:
              yucorpus[i][0][j] = where(np.random.multinomial(1, item_probs) == 1)[0][0]
              c = c + 1;
              if c > 1000:
                    break 
            
            yucorpus[i][1][j] = yucorpus[i][0][j]
         
            # decrement the item counts
            items[yucorpus[i][0][j]] = items[yucorpus[i][0][j]]  - 1;
            total_words = total_words - 1;
except ValueError:
      print 'failed to generate corpus, run again!'

Set up world and corpus.


In [14]:
w = World(n_words=18, n_objs=18)

c = Corpus(w,
           n_per_sent=condition,
           n_sents=num_trials)        
c.sents = yucorpus
c.update()

Define model parameters.


In [15]:
p = Params(n_samps=200,
           n_particles=1,
           alpha_r=.1,
           alpha_nr=1,
           empty_intent=.0001,
           n_hypermoves=5)

Do inference with Gibbs sampler.


In [16]:
l = Lexicon(c, p,
            verbose=0,
            hyper_inf=True)

l.learn_lex_gibbs(c, p)



...............................................................................

...............................................................................

.......................................
--- score full lex ---
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  5.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  3.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  2.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  6.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  2.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  3.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  3.]]
 [ 6.  3.  6.  1.  1.  3.  4.  4.  6.  6.  0.  3.  5.  6.  3.  6.  6.  3.]
counts: 108
    intent obj: [1 2 0 1 0 1 1 1 0 0 1 1 0 1 2 0 2 1 2 0 2 1 0 2 1 0 0 1 1 1 2 1 1 1 2 1]
    ref word: [1 2 0 1 2 1 1 1 0 0 1 1 0 0 2 0 2 1 2 0 2 1 0 2 1 0 0 1 1 1 2 1 1 1 1 1]
    intent obj prob: [-1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1
 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1
 -1.1 -1.1 -1.1 -1.1 -1.1 -1.1]
full score: r -38.9, nr -222.9, i -39.6, p -2.2,  total: -303.6

 *** average sample time: 0.110 sec

Plot.


In [17]:
l.plot_lex(w, certainwords = 0)


Evaluate posterior using luce choice. At test, learners were given 4AFC task (1 correct referent and 3 foils).


In [18]:
epsilon = .00001
l.ref = l.ref + epsilon  
num_foils = 3
lc = zeros(num_words)
correct = zeros(num_words)
incorrect = zeros(num_words)
for i in range(num_words):
    foils = sample(xrange(num_words),num_foils)
    while i in foils:
        foils = sample(xrange(num_words),num_foils)
        
    correct[i] = l.ref[i,i]
    incorrect[i] = l.ref[foils[0],i] + l.ref[foils[1],0] + l.ref[foils[2],0] 
    lc[i] = divide(correct[i], correct[i] + incorrect[i])
    
choice_score = sum(lc)/num_words
choice_score


Out[18]:
0.66666016680347773

Plot for one run of the model


In [ ]:
from matplotlib import pyplot as plt
x = arange(3)
y = array([0.734509062,	0.601131376, 0.488957579])
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, .8, .8])
ax.bar(x, y, align='center')
ax.set_xticks(x)
ax.set_xticklabels(['2x2', '3x3', '4x4'])
plt.axhline(.25, color = "black", linestyle='dashed', linewidth=2)
plt.ylim([0,1])
ax.set_title('Gibbs sampler')
f.show()

(3) Dewar and Xu (2007)

Create toy corpus with arbitrary number of words and objects (n=8).


In [51]:
# build world
w = World(n_words=8, n_objs=8)
w.show()

# build corpus
c_base = Corpus(world=w, n_sents=20, n_per_sent=1)
c_base.show()


n_objs = 8
n_words = 8
o: [1] w: [1]
o: [5] w: [5]
o: [4] w: [4]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]
o: [5] w: [5]
o: [6] w: [6]
o: [6] w: [6]
o: [4] w: [4]
o: [1] w: [1]
o: [4] w: [4]
o: [7] w: [7]
o: [6] w: [6]
o: [6] w: [6]
o: [1] w: [1]
o: [3] w: [3]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]

Do inference over this corpus.


In [52]:
p = Params(n_samps=20,
           n_particles=1,
           alpha_r=.01,
           alpha_nr=1,
           empty_intent=.1,
           n_hypermoves=5)

#  posterior prior to seeing any experimental evidence
l = Lexicon(c_base, p,
            verbose=0,
            hyper_inf=True)
l.learn_lex_gibbs(c_base, p)
[l.sample_scores]

base_scores = l.sample_scores



...................
--- score full lex ---
[[ 2.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  2.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  3.]]
 [ 0.  0.  0.  0.  0.  0.  0.  0.]
counts: 20
    intent obj: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1]
full score: r -14.6, nr 0.0, i -2.1, p 0.1,  total: -16.6

 *** average sample time: 0.020 sec

Now, give evidence from each of the four experimental conditions and compare each to posterior prior to observing evidence.

(1) TWO words, TWO objects


In [53]:
w = World(n_words=8, n_objs=8)
w.show()

w.update(2, labels = ["fep", "toma"])

c_2w2o = copy.deepcopy(c_base)
c_2w2o.sents.append([array([8, 9]),array([8])])
c_2w2o.sents.append([array([8, 9]),array([9])])
c_2w2o.update(w)
c_2w2o.show()
l = Lexicon(c_2w2o, p,
            verbose=0,
            hyper_inf=True)
l.learn_lex_gibbs(c_2w2o, p)
print [l.sample_scores]
scores_2w2o = l.sample_scores


n_objs = 8
n_words = 8
new object indices: 8, 9
new word indices: 8, 9
o: [1] w: [1]
o: [5] w: [5]
o: [4] w: [4]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]
o: [5] w: [5]
o: [6] w: [6]
o: [6] w: [6]
o: [4] w: [4]
o: [1] w: [1]
o: [4] w: [4]
o: [7] w: [7]
o: [6] w: [6]
o: [6] w: [6]
o: [1] w: [1]
o: [3] w: [3]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]
o: [8 9] w: [8]
o: [8 9] w: [9]


...................
--- score full lex ---
[[ 2.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  2.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
counts: 22
    intent obj: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.8 -0.8]
full score: r -20.8, nr 0.0, i -3.7, p 0.1,  total: -24.4

 *** average sample time: 0.023 sec
[[-28.824709281644115, -28.876037352581982, -25.01726679525132, -25.879061130269569, -26.939111911256873, -24.342966021942669, -29.026962363154862, -27.06951932133623, -26.639150585617369, -28.802402608933164, -25.72169261864034, -29.985966088551091, -29.522335628570328, -24.393564447000536, -25.00255803387784, -29.282418299988834, -25.975926137122503, -24.471848740345997, -25.988801493558093, -24.329408709860513]]

(2) ONE word, ONE object


In [54]:
c_1w1o = copy.deepcopy(c_base)
c_1w1o.sents.append([array([8, 8]),array([8])])
c_1w1o.sents.append([array([8, 8]),array([8])])
c_1w1o.update(w)

l = Lexicon(c_1w1o, p,
            verbose=0,
            hyper_inf=True)
l.learn_lex_gibbs(c_1w1o, p)
scores_1w1o = l.sample_scores



...................
--- score full lex ---
[[ 2.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  2.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  2.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
counts: 22
    intent obj: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.8 -0.8]
full score: r -18.5, nr -0.0, i -3.7, p 0.2,  total: -22.1

 *** average sample time: 0.022 sec

(3) ONE word, TWO objects


In [55]:
c_1w2o = copy.deepcopy(c_base)
c_1w2o.sents.append([array([8, 9]),array([8])])
c_1w2o.sents.append([array([8, 9]),array([8])])

c_1w2o.update(w)
c_1w2o.show()
l = Lexicon(c_1w2o, p,
            verbose=0,
            hyper_inf=True)
l.learn_lex_gibbs(c_1w2o, p)
print [l.sample_scores]
scores_1w2o = l.sample_scores


o: [1] w: [1]
o: [5] w: [5]
o: [4] w: [4]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]
o: [5] w: [5]
o: [6] w: [6]
o: [6] w: [6]
o: [4] w: [4]
o: [1] w: [1]
o: [4] w: [4]
o: [7] w: [7]
o: [6] w: [6]
o: [6] w: [6]
o: [1] w: [1]
o: [3] w: [3]
o: [7] w: [7]
o: [0] w: [0]
o: [4] w: [4]
o: [8 9] w: [8]
o: [8 9] w: [8]


...................
--- score full lex ---
[[ 2.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  2.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  2.  0.]]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
counts: 22
    intent obj: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -2.3 -0.1 -0.1 -0.1 -0.8 -0.8]
full score: r -16.2, nr -2.3, i -5.9, p 0.0,  total: -24.4

 *** average sample time: 0.022 sec
[[-31.222265107575414, -25.632793979097151, -22.035753015308838, -26.531075019169482, -24.821678454749915, -24.71297919859007, -27.012569779281652, -22.515754621253965, -26.586861079487342, -22.516318190272653, -24.713542767608768, -22.85783996021237, -23.509918074741449, -23.509918074741336, -23.497756069730716, -23.288579534817202, -23.225673696787322, -23.153070730525769, -23.453587382538441, -25.382743822369491]]

(4) TWO words, ONE object


In [57]:
c_2w1o = copy.deepcopy(c_base)
c_2w1o.sents.append([array([8, 8]),array([8])])
c_2w1o.sents.append([array([8, 8]),array([9])])

c_2w1o.update(w)
l = Lexicon(c_2w1o, p,
            verbose=0,
            hyper_inf=True)
l.learn_lex_gibbs(c_2w1o, p)
scores_2w1o = l.sample_scores



...................
--- score full lex ---
[[ 2.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  3.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  2.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  4.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  3.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
counts: 22
    intent obj: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2]
    ref word: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    intent obj prob: [-0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
 -0.1 -0.1 -0.1 -0.1 -0.1 -0.8 -2.3]
full score: r -18.5, nr -2.3, i -5.2, p -0.0,  total: -26.1

 *** average sample time: 0.022 sec

Compare posterior scores across conditions.


In [89]:
# take maxiumum posterior for each experimental condition
scores = (max(scores_1w1o), 
          max(scores_1w2o), 
          max(scores_2w1o), 
          max(scores_2w2o))

dif_scores = max(base_scores) - scores
conditions = ('1w1o', '1w2o', '2w1o', '2w2o')
dif_scores
conditions


Out[89]:
('1w1o', '1w2o', '2w1o', '2w2o')

Make plot of difference scores before and after observing evidence, for each experimental condition.


In [93]:
#import matplotlib.pyplot as plt

fig, ax = plt.subplots()

index = np.arange(4)
bar_width = 0.8

rects1 = plt.bar(index + bar_width, dif_scores, bar_width,
                 alpha= .5,
                 color='r')
plt.xlabel('Condition')
plt.ylabel('Posterior difference scores')
plt.title('Posterior difference scores by condition')
plt.xticks(index + bar_width + (bar_width/2), conditions)

plt.tight_layout()