Esempio n. 1
0
def compositional_disjunction():
    lexica = Lexica(baselexicon={
        p: [w1, w2],
        q: [w1, w3],
        pandq: [w1]
    },
                    costs={
                        p: 0.0,
                        q: 0.0,
                        pandq: 1.0
                    },
                    join_closure=True,
                    nullsem=True,
                    nullcost=5.0,
                    disjunction_cost=1.0)
    lexica.display()
    mod = Pragmod(lexica=lexica.lexica2matrices(),
                  messages=lexica.messages,
                  states=lexica.states,
                  costs=lexica.cost_vector(),
                  prior=np.repeat(1.0 / len(lexica.states),
                                  len(lexica.states)),
                  lexprior=np.repeat(1.0 / len(lexica), len(lexica)),
                  temperature=1.0,
                  alpha=1.0,
                  beta=1.0)
    mod.run_expertise_model(n=2, display=True, digits=2)
Esempio n. 2
0
 def explore_listener_parameters(self):
     """Explore a large parameter space, classifying the parameter vectors
     based on the max listener <world,lex> inference given self.msg""" 
     results = defaultdict(list)
     for dcost, alpha, beta, depth in product(self.dcosts,
                                              self.alphas,
                                              self.betas,
                                              self.depths):
         params = {'alpha': alpha,
                   'beta': beta,
                   'depth': depth,
                   'disjunction_cost': dcost}
         lexica = Lexica(
             baselexicon=self.baselexicon, 
             costs=self.lexical_costs,
             join_closure=True,
             nullsem=True,
             nullcost=5.0,
             disjunction_cost=dcost,
             unknown_word=self.unknown_word)
         lexmats = lexica.lexica2matrices()
         mod = Pragmod(
             lexica=lexmats,
             messages=lexica.messages,
             states=lexica.states,
             costs=lexica.cost_vector(),
             prior=np.repeat(1.0/len(lexica.states), len(lexica.states)),
             lexprior=np.repeat(1.0/len(lexmats), len(lexmats)),
             temperature=1.0,
             alpha=alpha,
             beta=beta)
         # Run the model:
         langs = mod.run_expertise_model(n=depth, display=False)
         # Get the listener's joint probability table for this message:
         msg_index = mod.messages.index(self.msg)
         prob_table = langs[-1][msg_index]        
         sorted_probs = sorted(prob_table.flatten())
         max_pair = None
         max_prob = sorted_probs[-1]
         # No ties allowed!
         if max_prob != sorted_probs[-2]:
             for i, j in product(list(range(prob_table.shape[0])),
                                 list(range(prob_table.shape[1]))):
                 if prob_table[i, j] == max_prob:
                     max_pair = (i, mod.states[j])
         # Add the target probability:
         params['prob'] = max_prob
         # Print to show progress:
         print(max_pair, params)
         # Store this dictionary of results -- parameters plus the predicted probability
         # max_pair is a lexicon index and state name.
         results[max_pair].append(params)
     return results
Esempio n. 3
0
 def explore_listener_parameters(self):
     """Explore a large parameter space, classifying the parameter vectors
     based on the max listener <world,lex> inference given self.msg"""
     results = defaultdict(list)
     for dcost, alpha, beta, depth in product(self.dcosts, self.alphas,
                                              self.betas, self.depths):
         params = {
             'alpha': alpha,
             'beta': beta,
             'depth': depth,
             'disjunction_cost': dcost
         }
         lexica = Lexica(baselexicon=self.baselexicon,
                         costs=self.lexical_costs,
                         join_closure=True,
                         nullsem=True,
                         nullcost=5.0,
                         disjunction_cost=dcost,
                         unknown_word=self.unknown_word)
         lexmats = lexica.lexica2matrices()
         mod = Pragmod(lexica=lexmats,
                       messages=lexica.messages,
                       states=lexica.states,
                       costs=lexica.cost_vector(),
                       prior=np.repeat(1.0 / len(lexica.states),
                                       len(lexica.states)),
                       lexprior=np.repeat(1.0 / len(lexmats), len(lexmats)),
                       temperature=1.0,
                       alpha=alpha,
                       beta=beta)
         # Run the model:
         langs = mod.run_expertise_model(n=depth, display=False)
         # Get the listener's joint probability table for this message:
         msg_index = mod.messages.index(self.msg)
         prob_table = langs[-1][msg_index]
         sorted_probs = sorted(prob_table.flatten())
         max_pair = None
         max_prob = sorted_probs[-1]
         # No ties allowed!
         if max_prob != sorted_probs[-2]:
             for i, j in product(list(range(prob_table.shape[0])),
                                 list(range(prob_table.shape[1]))):
                 if prob_table[i, j] == max_prob:
                     max_pair = (i, mod.states[j])
         # Add the target probability:
         params['prob'] = max_prob
         # Print to show progress:
         print(max_pair, params)
         # Store this dictionary of results -- parameters plus the predicted probability
         # max_pair is a lexicon index and state name.
         results[max_pair].append(params)
     return results
Esempio n. 4
0
def simple_disjunction():
    lexicon = np.array([[1.0, 1.0, 0.0], [1.0, 0.0, 1.0], [1.0, 0.0, 0.0],
                        [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
    mod = Pragmod(lexica=None,
                  messages=[p, q, pandq, porq, NULL_MSG],
                  states=[w1, w2, w3],
                  costs=np.array([0.0, 0.0, 1.0, 1.0, 5.0]),
                  prior=np.repeat(1.0 / 4.0, 3),
                  lexprior=None,
                  temperature=1.0,
                  alpha=1.0,
                  beta=0.0)
    mod.run_base_model(lexicon, n=2, display=True, digits=2)
Esempio n. 5
0
def simple_scalar_inference_example():

    def lexicon_iterator():
        mats = [
            np.array([[0., 1., 1.], [0., 0., 1.], [1., 1., 1.]]),
            np.array([[0., 1., 0.], [0., 0., 1.], [1., 1., 1.]]),
            np.array([[0., 0., 1.], [0., 0., 1.], [1., 1., 1.]])]
        for mat in mats:
            yield mat

    mod = Pragmod(
        lexica=lexicon_iterator,
        messages=['A scored', 'A aced', 'NULL'],
        states=['N', 'S', 'A'],
        temperature=1.0,
        nullmsg=True,
        nullcost=5.0)

    mod.stream_lexical_uncertainty(n=0)

    for lex in lexicon_iterator():
        print("=" * 70)
        print("Messages")
        display_matrix(lex, rnames=mod.messages, cnames=mod.states, digits=2)
        print('l0')
        display_matrix(mod.l0(lex), rnames=mod.messages, cnames=mod.states, digits=2)
        print('s1')
        display_matrix(mod.s1(lex), rnames=mod.states, cnames=mod.messages, digits=2)
        print('l1')
        display_matrix(mod.L(mod.S(lex)), rnames=mod.messages, cnames=mod.states, digits=2)        

    display_matrix(mod.final_listener,  rnames=mod.messages, cnames=mod.states, digits=2)
Esempio n. 6
0
def simple_disjunction():    
    lexicon = np.array([[1.0, 1.0, 0.0],
                        [1.0, 0.0, 1.0],
                        [1.0, 0.0, 0.0],
                        [1.0, 1.0, 1.0],
                        [1.0, 1.0, 1.0]])
    mod = Pragmod(
        lexica=None,
        messages=[p, q, pandq, porq, NULL_MSG],
        states=[w1, w2, w3],
        costs=np.array([0.0, 0.0, 1.0, 1.0, 5.0]),
        prior=np.repeat(1.0/4.0, 3),
        lexprior=None,
        temperature=1.0,
        alpha=1.0,
        beta=0.0)
    mod.run_base_model(lexicon, n=2, display=True, digits=2)
Esempio n. 7
0
def generic_disjunction_example(alpha=1.0,
                                beta=1.0,
                                disjunction_cost=1.0,
                                n=2,
                                fulldisplay=False,
                                unknown_word=None):
    """Common code for our two illustrative examples, which
    differ only in the above keyword parameters. Increase n to see
    greater depths of recursion. use fulldisplay=True to see more
    details."""
    # Use the lexicon generation convenience function to
    # generate all the join-closure lexica and calculate
    # the necessary message costs:
    lexica = Lexica(baselexicon={
        'A': ['1'],
        'B': ['2'],
        'X': ['1', '2']
    },
                    costs={
                        'A': 0.0,
                        'B': 0.0,
                        'X': 0.0
                    },
                    join_closure=True,
                    nullsem=True,
                    nullcost=5.0,
                    disjunction_cost=disjunction_cost,
                    unknown_word=unknown_word)
    # Lexical matrices:
    lexmats = lexica.lexica2matrices()
    # Pragmatic models for the above lexical space.
    mod = Pragmod(lexica=lexmats,
                  messages=lexica.messages,
                  states=lexica.states,
                  costs=lexica.cost_vector(),
                  prior=np.repeat(1.0 / len(lexica.states),
                                  len(lexica.states)),
                  lexprior=np.repeat(1.0 / len(lexmats), len(lexmats)),
                  temperature=1.0,
                  alpha=alpha,
                  beta=beta)
    if fulldisplay:
        lexica.display()
        # Run the base model on the individual lexica so we can show
        # those lower steps:
        for lex in lexmats:
            print("=" * 70)
            print(mod.lex2str(lex))
            mod.run_base_model(lex, n=2, display=True, digits=2)
    ## Run the anxious experts model - fulldisplay=True for a fuller picture:
    langs = mod.run_expertise_model(n=n, display=fulldisplay, digits=2)
    # Look at the specific table we care about:
    msg_index = mod.messages.index('A v X')
    final_listener = langs[-1]
    mod.display_joint_listener(final_listener[msg_index], digits=2)
    return langs
Esempio n. 8
0
 def I_implicature_simulation_datapoint(common_ref_prob,
                                        dcost=1.0,
                                        alpha=2.0):
     # Values to obtain:
     is_max = False
     listener_val = None
     speaker_val = None
     # Set-up:
     lexica = Lexica(baselexicon=BASELEXICON,
                     costs=LEXICAL_COSTS,
                     join_closure=True,
                     nullsem=True,
                     nullcost=5.0,
                     disjunction_cost=dcost)
     ref_probs = np.array([
         common_ref_prob, (1.0 - common_ref_prob) / 2.0,
         (1.0 - common_ref_prob) / 2.0
     ])
     lexprior = np.repeat(1.0 / len(lexica.lexica2matrices()),
                          len(lexica.lexica2matrices()))
     # Run the model:
     mod = Pragmod(lexica=lexica.lexica2matrices(),
                   messages=lexica.messages,
                   states=lexica.states,
                   costs=lexica.cost_vector(),
                   lexprior=lexprior,
                   prior=ref_probs,
                   alpha=alpha)
     langs = mod.run_expertise_model(n=3, display=False, digits=2)
     # Get the values we need:
     speaker = mod.speaker_lexical_marginalization(langs[-2])
     listener = mod.listener_lexical_marginalization(langs[-3])
     superkind_term_index = mod.messages.index(SUPERKIND_MSG)
     common_state_index = mod.states.index(COMMON_REF)
     disj_term_index = mod.messages.index(DISJ_MSG)
     disj_state_index = mod.states.index(DISJ_REF)
     # Fill in listener_val and speaker_val:
     listener_val = listener[superkind_term_index, common_state_index]
     speaker_val = speaker[disj_state_index, disj_term_index]
     # Determine whether max, with a bit of rounding to avoid
     # spurious mismatch diagnosis:
     maxspkval = np.max(speaker[disj_state_index])
     is_max = np.round(speaker_val, 10) == np.round(maxspkval, 10)
     # Return values:
     return (listener_val, speaker_val, is_max)
Esempio n. 9
0
 def Q_implicature_simulation_datapoint(specific_cost,
                                        dcost=1.0,
                                        alpha=2.0):
     # Values to obtain:
     is_max = False
     listener_val = None
     speaker_val = None
     # Set-up:
     lexica = Lexica(baselexicon=BASELEXICON,
                     costs={
                         GENERAL_MSG: 0.0,
                         SPECIFIC_MSG: specific_cost
                     },
                     join_closure=True,
                     nullsem=True,
                     nullcost=5.0,
                     disjunction_cost=dcost)
     ref_probs = np.repeat(1.0 / len(lexica.states), len(lexica.states))
     lexprior = np.repeat(1.0 / len(lexica.lexica2matrices()),
                          len(lexica.lexica2matrices()))
     # Run the model:
     mod = Pragmod(lexica=lexica.lexica2matrices(),
                   messages=lexica.messages,
                   states=lexica.states,
                   costs=lexica.cost_vector(),
                   lexprior=lexprior,
                   prior=ref_probs,
                   alpha=alpha)
     langs = mod.run_expertise_model(n=3, display=False, digits=2)
     # Get the values we need:
     speaker = mod.speaker_lexical_marginalization(langs[-2])
     listener = mod.listener_lexical_marginalization(langs[-3])
     general_msg_index = lexica.messages.index(GENERAL_MSG)
     general_only_state = lexica.states.index(GENERAL_ONLY_REF)
     disj_state_index = lexica.states.index(DISJ_REF)
     disj_msg_index = lexica.messages.index(DISJ_MSG)
     speaker_val = speaker[disj_state_index, disj_msg_index]
     listener_val = listener[general_msg_index, general_only_state]
     # Determine whether max, with a bit of rounding to avoid spurious
     # mismatch diagnosis:
     maxspkval = np.max(speaker[disj_state_index])
     is_max = np.round(speaker_val, 10) == np.round(maxspkval, 10)
     # Return values:
     return (listener_val, speaker_val, is_max)
Esempio n. 10
0
 def I_implicature_simulation_datapoint(
         common_ref_prob, dcost=1.0, alpha=2.0):
     # Values to obtain:
     is_max = False
     listener_val = None
     speaker_val = None
     # Set-up:
     lexica = Lexica(
         baselexicon=BASELEXICON,
         costs=LEXICAL_COSTS,
         join_closure=True,
         nullsem=True,
         nullcost=5.0,
         disjunction_cost=dcost)
     ref_probs = np.array([common_ref_prob, (1.0-common_ref_prob)/2.0,
                           (1.0-common_ref_prob)/2.0])
     lexprior = np.repeat(1.0/len(lexica.lexica2matrices()),
                          len(lexica.lexica2matrices()))
     # Run the model:
     mod = Pragmod(
         lexica=lexica.lexica2matrices(),
         messages=lexica.messages,
         states=lexica.states,
         costs=lexica.cost_vector(),
         lexprior=lexprior,
         prior=ref_probs,
         alpha=alpha)
     langs = mod.run_expertise_model(n=3, display=False, digits=2)
     # Get the values we need:
     speaker = mod.speaker_lexical_marginalization(langs[-2])
     listener = mod.listener_lexical_marginalization(langs[-3])
     superkind_term_index = mod.messages.index(SUPERKIND_MSG)
     common_state_index = mod.states.index(COMMON_REF)
     disj_term_index = mod.messages.index(DISJ_MSG)
     disj_state_index = mod.states.index(DISJ_REF)
     # Fill in listener_val and speaker_val:
     listener_val = listener[superkind_term_index, common_state_index]
     speaker_val = speaker[disj_state_index, disj_term_index]
     # Determine whether max, with a bit of rounding to avoid
     # spurious mismatch diagnosis:
     maxspkval = np.max(speaker[disj_state_index])
     is_max = np.round(speaker_val, 10) == np.round(maxspkval, 10)
     # Return values:
     return (listener_val, speaker_val, is_max)
Esempio n. 11
0
 def Q_implicature_simulation_datapoint(
         specific_cost, dcost=1.0, alpha=2.0):
     # Values to obtain:
     is_max = False
     listener_val = None
     speaker_val = None
     # Set-up:
     lexica = Lexica(
         baselexicon=BASELEXICON,
         costs={GENERAL_MSG: 0.0, SPECIFIC_MSG: specific_cost},
         join_closure=True,
         nullsem=True,
         nullcost=5.0,
         disjunction_cost=dcost)
     ref_probs = np.repeat(1.0/len(lexica.states), len(lexica.states))
     lexprior = np.repeat(1.0/len(lexica.lexica2matrices()),
                          len(lexica.lexica2matrices()))
     # Run the model:
     mod = Pragmod(
         lexica=lexica.lexica2matrices(),
         messages=lexica.messages,
         states=lexica.states,
         costs=lexica.cost_vector(),
         lexprior=lexprior,
         prior=ref_probs,
         alpha=alpha)
     langs = mod.run_expertise_model(n=3, display=False, digits=2)
     # Get the values we need:
     speaker = mod.speaker_lexical_marginalization(langs[-2])
     listener = mod.listener_lexical_marginalization(langs[-3])
     general_msg_index = lexica.messages.index(GENERAL_MSG)
     general_only_state = lexica.states.index(GENERAL_ONLY_REF)
     disj_state_index = lexica.states.index(DISJ_REF)
     disj_msg_index = lexica.messages.index(DISJ_MSG)
     speaker_val = speaker[disj_state_index, disj_msg_index]
     listener_val = listener[general_msg_index, general_only_state]
     # Determine whether max, with a bit of rounding to avoid spurious
     # mismatch diagnosis:
     maxspkval = np.max(speaker[disj_state_index])
     is_max = np.round(speaker_val, 10) == np.round(maxspkval, 10)
     # Return values:
     return (listener_val, speaker_val, is_max)
Esempio n. 12
0
def compositional_disjunction():
    lexica = Lexica(
        baselexicon={p: [w1, w2], q: [w1, w3], pandq:[w1]},
        costs={p:0.0, q:0.0, pandq:1.0},
        join_closure=True,
        nullsem=True,
        nullcost=5.0,
        disjunction_cost=1.0)
    lexica.display()
    mod = Pragmod(
        lexica=lexica.lexica2matrices(),
        messages=lexica.messages,
        states=lexica.states,
        costs=lexica.cost_vector(),
        prior=np.repeat(1.0/len(lexica.states), len(lexica.states)),
        lexprior=np.repeat(1.0/len(lexica), len(lexica)),
        temperature=1.0,
        alpha=1.0,
        beta=1.0)
    mod.run_expertise_model(n=2, display=True, digits=2)
Esempio n. 13
0
def embedded_disjunction_example(refinable={}):
    players = [a,b]
    shots = [s1,s2]
    worlds = get_worlds(basic_states=(0,1,2,3), length=2, increasing=False)
    baselexicon = define_lexicon(player=players, shot=shots, worlds=worlds)
    subjs = ('every_player', )
    preds = ('hit_shot1', 'hit_shot2', 'hit_shot1_or_shot2', 'hit_shot1_and_shot2')
    temperature = 1.0
    nullmsg = True
    nullcost = 5.0
    # N: no shots; 1: made just shot1; 2: made just shot2; B: made both shot1 and shot2.
    new_worldnames = (lambda w : "".join(["N12B"[i] for i in w]))
    worldnames = [new_worldnames(w) for w in worlds]
    messages = []
    for subj, pred in product(subjs, preds):
        msg = "%s %s" % (subj.replace("_", " "), pred)        
        formula = "iv(%s, %s)" % (subj, pred)
        messages.append((msg, formula))
    # Refinement model:
    gram = UncertaintyGrammars(
        baselexicon=copy(baselexicon),
        messages=copy(messages),
        worlds=copy(worlds),        
        refinable=refinable,
        nullmsg=nullcost)
    mod = Pragmod(
        lexica=gram.lexicon_iterator,
        baselexicon=gram.baselexicon_mat,
        messages=gram.messages,
        states=worldnames,
        temperature=temperature,
        nullmsg=nullmsg,
        nullcost=nullcost)
    mod.stream_lexical_uncertainty(n=0)
    display_matrix(
        mod.final_listener,
        rnames=mod.messages,
        cnames=mod.states,
        digits=2,
        latex=True)
Esempio n. 14
0
def generic_disjunction_example(
        alpha=1.0,
        beta=1.0,
        disjunction_cost=1.0,
        n=2,
        fulldisplay=False,
        unknown_word=None):
    """Common code for our two illustrative examples, which
    differ only in the above keyword parameters. Increase n to see
    greater depths of recursion. use fulldisplay=True to see more
    details."""    
    # Use the lexicon generation convenience function to
    # generate all the join-closure lexica and calculate
    # the necessary message costs:
    lexica = Lexica(
        baselexicon={'A': ['1'], 'B': ['2'], 'X':['1', '2']}, 
        costs={'A':0.0, 'B':0.0, 'X':0.0},
        join_closure=True,
        nullsem=True,
        nullcost=5.0,
        disjunction_cost=disjunction_cost,
        unknown_word=unknown_word)
    # Lexical matrices:
    lexmats = lexica.lexica2matrices()         
    # Pragmatic models for the above lexical space.
    mod = Pragmod(
        lexica=lexmats,
        messages=lexica.messages,
        states=lexica.states,
        costs=lexica.cost_vector(),
        prior=np.repeat(1.0/len(lexica.states), len(lexica.states)),
        lexprior=np.repeat(1.0/len(lexmats), len(lexmats)),
        temperature=1.0,
        alpha=alpha,
        beta=beta)
    if fulldisplay:
        lexica.display()
        # Run the base model on the individual lexica so we can show
        # those lower steps:
        for lex in lexmats:
            print("=" * 70)
            print(mod.lex2str(lex))
            mod.run_base_model(lex, n=2, display=True, digits=2)         
    ## Run the anxious experts model - fulldisplay=True for a fuller picture:
    langs = mod.run_expertise_model(n=n, display=fulldisplay, digits=2)
    # Look at the specific table we care about:
    msg_index = mod.messages.index('A v X')
    final_listener = langs[-1]
    mod.display_joint_listener(final_listener[msg_index], digits=2)
    return langs
Esempio n. 15
0
def basic_scalar():
    lexica = [
        np.array([[1.0, 1.0], [1.0, 0.0], [1.0, 1.0]]),
        np.array([[1.0, 0.0], [1.0, 0.0], [1.0, 1.0]]),
        np.array([[0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
    ]
    mod = Pragmod(lexica=lexica,
                  messages=['cheap', 'free', NULL_MSG],
                  states=[w1, w2],
                  costs=np.array([0.0, 0.0, 5.0]),
                  prior=np.repeat(1.0 / 2.0, 2),
                  lexprior=np.repeat(1.0 / 3.0, 3),
                  temperature=1.0,
                  alpha=1.0,
                  beta=2.0)
    for lex in lexica:
        print("=" * 70)
        print(mod.lex2str(lex))
        mod.run_base_model(lex, n=2, display=True, digits=2)
    mod.run_expertise_model(n=3, display=True, digits=2)
Esempio n. 16
0
def basic_scalar():
    lexica = [np.array([[1.0,1.0], [1.0, 0.0], [1.0, 1.0]]),
              np.array([[1.0,0.0], [1.0, 0.0], [1.0, 1.0]]),
              np.array([[0.0,1.0], [1.0, 0.0], [1.0, 1.0]])]    
    mod = Pragmod(lexica=lexica,
                  messages=['cheap', 'free', NULL_MSG],
                  states=[w1, w2],
                  costs=np.array([0.0, 0.0, 5.0]),
                  prior=np.repeat(1.0/2.0, 2),
                  lexprior=np.repeat(1.0/3.0, 3),
                  temperature=1.0,
                  alpha=1.0,
                  beta=2.0)
    for lex in lexica:
        print("=" * 70)
        print(mod.lex2str(lex))
        mod.run_base_model(lex, n=2, display=True, digits=2)      
    mod.run_expertise_model(n=3, display=True, digits=2)
Esempio n. 17
0
def scalar_disjunction_example(refinable={}):
    players = [a]
    shots = ['1', '2', 'f']
    regular_shots = ['1', '2']    
    worlds = [("-",), ('f',), ('1',), ('2',), ('f1',), ('f2',), ('12',), ('f12',)]    
    lexicon = {
        "player": players,
        "PlayerA":  [[a]],
        "shot1": [['1']],
        "shot2": [['2']],
        "the_freethrow": [X for X in powerset(shots) if 'f' in X],
        "hit":    [[w, x, y] for w, x, y in product(worlds, players, shots)
                   if y in set(w[players.index(x)])],
        "some_shot": [Y for Y in powerset(shots) if len(set(regular_shots) & set(Y)) > 0],
        "only_some_shot": [Y for Y in powerset(shots) if len(set(regular_shots) & set(Y)) == 1],
        "every_shot": [Y for Y in powerset(shots) if set(regular_shots) <= set(Y)],
        "not_every_shot": [Y for Y in powerset(set(shots))
                           if not(set(regular_shots) <= set(Y))],
        "AND": [[X, Y, [z for z in X if z in Y]]
                for X, Y in product(list(powerset(powerset(shots))), repeat=2)],
        "OR": [[X, Y, [z for z in X]+[z for z in Y if z not in X]]
               for X, Y in product(list(powerset(powerset(shots))), repeat=2)],
        "XOR": [[X, Y, [z for z in X if z not in Y]+[z for z in Y if z not in X]]
                for X, Y in product(list(powerset(powerset(shots))), repeat=2)]}

    # Import the new lexicon into the namespace:
    for word, sem in list(lexicon.items()):
        setattr(sys.modules[__name__], word, sem)
    new_worldnames = (lambda x : "".join(map(str,x)))
    worldnames = [new_worldnames(w) for w in worlds]

    # Messages:
    messages = [
        ("PlayerA hit the freethrow", "iv(PlayerA, tv(hit, the_freethrow, self.worlds, player))"),
        ("PlayerA hit every shot", "iv(PlayerA, tv(hit, every_shot, self.worlds, player))"),
        ("PlayerA hit some shot", "iv(PlayerA, tv(hit, some_shot, self.worlds, player))"),
        ("PlayerA hit some shot or the freethrow", "iv(PlayerA, tv(hit, coord(OR, some_shot, the_freethrow), self.worlds, player))"),
        ("PlayerA hit some shot and the freethrow", "iv(PlayerA, tv(hit, coord(AND, some_shot, the_freethrow), self.worlds, player))"),
        ("PlayerA hit every shot or the freethrow", "iv(PlayerA, tv(hit, coord(OR, every_shot, the_freethrow), self.worlds, player))"),
        ("PlayerA hit every shot and freethrow", "iv(PlayerA, tv(hit, coord(AND, every_shot, the_freethrow), self.worlds, player))")]
    temperature = 1.0
    nullmsg = True
    nullcost = 5.0

    # Refinement model:
    gram = UncertaintyGrammars(
        baselexicon=copy(lexicon),
        messages=copy(messages),
        worlds=copy(worlds),        
        refinable=refinable,
        nullmsg=nullcost)
    mod = Pragmod(
        lexica=gram.lexicon_iterator,
        baselexicon=gram.baselexicon_mat,
        messages=gram.messages,
        states=worldnames,
        temperature=temperature,
        nullmsg=nullmsg,
        nullcost=nullcost)

    # Model run:
    mod.stream_lexical_uncertainty(n=0)
    display_matrix(mod.final_listener,  rnames=mod.messages, cnames=mod.states, digits=2, latex=True)
Esempio n. 18
0
def parameter_exploration(
        experiment_src=None,
        rescaler=None,
        output_filename=None,
        response_transformation=None):    
    # Parameter space:
    temps = np.concatenate((np.arange(0.1, 2.1, 0.1), np.arange(2.0, 6.0, 1)))
    depths = [0, 1, 2, 3, 4, 5]
    nullcosts = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]    
    # General settings:
    subjs= ('every_player', 'exactly_one_player', 'no_player')
    objs = ('every_shot', 'no_shot', 'some_shot')
    players = [a,b,c]
    shots = [s1,s2]
    basic_states = (0,1,2)
    worlds = get_worlds(basic_states=(0,1,2), length=3, increasing=True)
    baselexicon = define_lexicon(player=players, shot=shots, worlds=worlds)
    nullmsg = True
    worldnames = [worldname(w) for w in worlds]
    messages = []
    for d1, d2 in product(subjs, objs):
        subj = d1.replace("_player", "(player)")
        obj = d2.replace("_shot", "(shot)")
        msg = "%s(hit(%s))" % (subj, obj)
        formula = "iv(%s, tv(hit, %s, self.worlds, player))" % (d1,  d2)       
        messages.append((msg, formula))

    with open(output_filename, 'w') as output_file:
        writer = csv.DictWriter(output_file, 
            fieldnames=['Experiment', 'Listener', 'lambda', 'depth', 'nullcost'] + \
            ['Pearson', 'Pearson p', 'Spearman', 'Spearman p', 'MSE'])
        writer.writeheader()            
        # Runs:
        for temperature, n, nullcost in product(temps, depths, nullcosts):
            print(("{} {} {}".format(temperature, n, nullcost)))
            neogram = UncertaintyGrammars(
                baselexicon=copy(baselexicon),
                messages=copy(messages),
                worlds=copy(worlds),        
                refinable={'some_player': ['exactly_one_player'],
                           'some_shot': ['exactly_one_shot']},
                nullmsg=nullcost)    
            neomod = Pragmod(
                name="NeoGricean",
                lexica=neogram.lexicon_iterator,
                baselexicon=neogram.baselexicon_mat,
                messages=neogram.messages,
                states=worldnames,
                temperature=temperature,
                nullmsg=nullmsg,
                nullcost=nullcost)
            ucgram = UncertaintyGrammars(            
                baselexicon=copy(baselexicon),
                messages=copy(messages),
                worlds=copy(worlds),        
                refinable={'some_player': [], 'some_shot': []},
                nullmsg=nullcost)    
            ucmod = Pragmod(
                name="Unconstrained",
                lexica=ucgram.lexicon_iterator,
                baselexicon=ucgram.baselexicon_mat,
                messages=ucgram.messages,
                states=worldnames,
                temperature=temperature,
                nullmsg=nullmsg,
                nullcost=nullcost)        
            ucmod.stream_lexical_uncertainty(n=n)
            neomod.stream_lexical_uncertainty(n=n)        
            analysis = Analysis(
                experiment=Experiment(
                    src_filename=experiment_src,
                    response_transformation=response_transformation),
                models=[ucmod, neomod],
                rescaler=rescaler)
            results = analysis.numeric_analysis()
            for key, vals in list(results.items()):
                vals['Listener'] = key
                vals['Experiment'] = experiment_src
                vals['lambda'] = temperature
                vals['depth'] = n
                vals['nullcost'] = nullcost        
                writer.writerow(vals)
Esempio n. 19
0
def experimental_assessment(
        experiment_src=None,
        plot_output_filename=None,
        response_transformation=None,
        rescaler=None,
        uctemp=1.0,
        ucnullcost=5.0,
        ngtemp=1.0,
        ngnullcost=5.0,
        nrows=None):                            
    # General settings:
    subjs= ('every_player', 'exactly_one_player', 'no_player')
    objs = ('every_shot', 'no_shot', 'some_shot')
    players = [a,b,c]
    shots = [s1,s2]
    basic_states = (0,1,2)
    worlds = get_worlds(basic_states=(0,1,2), length=3, increasing=True)
    baselexicon = define_lexicon(player=players, shot=shots, worlds=worlds)
    temperature = 1.0
    nullmsg = True
    nullcost = 5.0       
    worldnames = [worldname(w) for w in worlds]
    messages = []
    for d1, d2 in product(subjs, objs):
        subj = d1.replace("_player", "(player)")
        obj = d2.replace("_shot", "(shot)")
        msg = "%s(hit(%s))" % (subj, obj)
        formula = "iv(%s, tv(hit, %s, self.worlds, player))" % (d1,  d2)       
        messages.append((msg, formula))    
     
    # Unconstrained refinement:
    ucgram = UncertaintyGrammars(
        baselexicon=copy(baselexicon),
        messages=copy(messages),
        worlds=copy(worlds),        
        refinable={'some_player': [], 'some_shot': []},
        nullmsg=nullmsg)    
    ucmod = Pragmod(
        name="Unconstrained",
        lexica=ucgram.lexicon_iterator,
        baselexicon=ucgram.baselexicon_mat,
        messages=ucgram.messages,
        states=worldnames,
        temperature=uctemp,
        nullmsg=nullmsg,
        nullcost=ucnullcost)

    # Neo-Gricean refinement:
    neogram = UncertaintyGrammars(
        baselexicon=copy(baselexicon),
        messages=copy(messages),
        worlds=copy(worlds),        
        refinable={'some_player': ['exactly_one_player'],
                   'some_shot': ['exactly_one_shot']},
        nullmsg=nullmsg)    
    neomod = Pragmod(
        name="Neo-Gricean",
        lexica=neogram.lexicon_iterator,
        baselexicon=neogram.baselexicon_mat,
        messages=neogram.messages,
        states=worldnames,
        temperature=ngtemp,
        nullmsg=nullmsg,
        nullcost=ngnullcost)

    # Run the models, going only to the first uncertainty listener (n=0):
    ucmod.stream_lexical_uncertainty(n=0)
    neomod.stream_lexical_uncertainty(n=0)
            
    # The analysis:
    analysis = Analysis(
        experiment=Experiment(
            src_filename=experiment_src,
            response_transformation=response_transformation),
        models=[ucmod, neomod],
        rescaler=rescaler)
    analysis.overall_analysis()
    analysis.analysis_by_message()
    analysis.comparison_plot(output_filename=plot_output_filename, nrows=nrows)
Esempio n. 20
0
def complex_example():
    # General settings:
    players = [a,b]
    shots = [s1,s2]
    worlds = get_worlds(basic_states=(0,1,2), length=2, increasing=False)
    baselexicon = define_lexicon(player=players, shot=shots, worlds=worlds)   
    subjs = ('PlayerA', 'PlayerB', 'some_player', 'every_player', 'no_player')
    preds = ('scored', 'aced')
    temperature = 1.0
    nullmsg = True
    nullcost = 5.0 
    worldnames = [worldname(w) for w in worlds]
    messages = []
    for subj, pred in product(subjs, preds):
        msg = "%s %s" % (subj.replace("_", " "), pred)        
        formula = "iv(%s, %s)" % (subj, pred)
        messages.append((msg, formula))

    # Neo-Gricean refinement:
    neogram = UncertaintyGrammars(
        baselexicon=copy(baselexicon),
        messages=copy(messages),
        worlds=worlds,
        refinable={'some_player': ['exactly_one_player'],
                   'PlayerA': ['only_PlayerA'],
                   'PlayerB': ['only_PlayerB'],
                   'scored': ['scored_not_aced'],
                   'aced': []},
        nullmsg=nullmsg)
    neomod = Pragmod(
        lexica=neogram.lexicon_iterator,
        baselexicon=neogram.baselexicon_mat,
        messages=neogram.messages,
        states=worldnames,
        temperature=temperature,
        nullmsg=nullmsg,
        nullcost=nullcost)
    
    # Run and report:
    neomod.stream_lexical_uncertainty(n=0)
    neomod.listener_report(digits=2)

    # Unconstrained refinement:
    ucgram = UncertaintyGrammars(
        baselexicon=copy(baselexicon),
        messages=copy(messages),
        worlds=worlds,
        refinable={'some_player': [],
                   'PlayerA': [],
                   'PlayerB': [],
                   'scored': [],
                   'aced': []},
        nullmsg=nullmsg)
    ucmod = Pragmod(
        lexica=ucgram.lexicon_iterator,
        baselexicon=ucgram.baselexicon_mat,
        messages=ucgram.messages,
        states=worldnames,
        temperature=temperature,
        nullmsg=nullmsg,
        nullcost=nullcost)

    # Run and report:
    ucmod.stream_lexical_uncertainty(n=0)
    ucmod.listener_report(digits=2)