In [1]:
%pylab qt
from LOTlib.Hypotheses.GrammarHypothesis import GrammarHypothesis
from LOTlib.Inference.MetropolisHastings import MHSampler
from LOTlib.Inference.PriorSample import prior_sample
from LOTlib.Examples.NumberGame.NewVersion.Model import *
from Model import *
from LOTlib.Visualization.SampleCollector import *
from matplotlib.widgets import Slider


Populating the interactive namespace from numpy and matplotlib

In [11]:
# Parameters for number game inference
alpha = 0.99
n = 1000
domain = 20

# Parameters for GrammarHypothesis inference
grammar_n = 100000
data = toy_2n

# Variables for NumberGameHypothesis inference
h0 = make_h0(grammar=simple_test_grammar, domain=domain, alpha=alpha)
mh_sampler = MHSampler(h0, data[0].input, n)

In [3]:
'''Generate number game hypotheses'''
hypotheses = set()
for h in lot_iter(mh_sampler):
    hypotheses.add(h)

for h in hypotheses:
    print h, h(), h.domain, h.alpha


lambda : mapset_(lambda y1: times_(y1, 3), range_set_(1, 20, bound=20)) [3, 6, 9, 12, 15, 18] 20 0.99
lambda : mapset_(lambda y1: times_(y1, 1), range_set_(1, 20, bound=20)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 20 0.99
lambda : mapset_(lambda y1: times_(y1, 2), range_set_(1, 20, bound=20)) [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 20 0.99
lambda : mapset_(lambda y1: times_(y1, 7), range_set_(1, 20, bound=20)) [7, 14] 20 0.99

In [4]:
'''What grammar probabilities will best model our human data?'''
grammar_h0 = GrammarHypothesis(simple_test_grammar, hypotheses, proposal_step=.1, proposal_n=1)

for r in grammar_h0.rules:
    print r


SET -> mapset_['FUNC', 'RANGE'] w/ p=1.0, resample_p=1.0
EXPR -> times_['X', '1'] w/ p=1.0, resample_p=1.0
EXPR -> times_['X', '2'] w/ p=1.0, resample_p=1.0
EXPR -> times_['X', '3'] w/ p=1.0, resample_p=1.0
EXPR -> times_['X', '7'] w/ p=1.0, resample_p=1.0
START -> ['SET'] w/ p=1.0, resample_p=1.0
RANGE -> range_set_['1', '20', 'bound=20'] w/ p=1.0, resample_p=1.0
FUNC -> lambda['EXPR'] w/ p=1.0, resample_p=1.0BV:X;None;y

In [13]:
'''GrammarHypothesis inference'''
mh_grammar_sampler = MHSampler(grammar_h0, data, grammar_n, trace=False)

grammar_hypotheses = []
i = 0
samplec = VectorSampleCollector(rate=100)
for grammar_h in lot_iter(mh_grammar_sampler):
    i += 1
    samplec.add_sample(grammar_h)
    grammar_hypotheses.append(grammar_h)
    if i % (mh_grammar_sampler.steps/20) == 0:
        print ['%.3f' % v for v in grammar_h.value]
        print i, '='*70
        print grammar_h.prior, grammar_h.likelihood, grammar_h.posterior_score


['1.000', '0.089', '1.889', '2.402', '0.528', '1.000', '1.000', '1.000']
5000 ======================================================================
-10.4513716809 -2.22753167366 -12.6789033545
['1.000', '0.103', '3.245', '3.494', '2.012', '1.000', '1.000', '1.000']
10000 ======================================================================
-11.99782844 -1.73650323442 -13.7343316744
['1.000', '0.134', '2.638', '0.508', '1.874', '1.000', '1.000', '1.000']
15000 ======================================================================
-10.2445023839 -2.12239339782 -12.3668957817
['1.000', '0.702', '7.586', '2.967', '0.745', '1.000', '1.000', '1.000']
20000 ======================================================================
-13.5337644044 -3.43908043623 -16.9728448406
['1.000', '0.536', '4.773', '0.239', '2.101', '1.000', '1.000', '1.000']
25000 ======================================================================
-11.3987401957 -4.11077792023 -15.509518116
['1.000', '0.161', '6.612', '1.214', '3.571', '1.000', '1.000', '1.000']
30000 ======================================================================
-14.0312195169 -1.1287539928 -15.1599735097
['1.000', '0.394', '4.991', '1.236', '1.364', '1.000', '1.000', '1.000']
35000 ======================================================================
-10.7866628554 -2.97620000944 -13.7628628648
['1.000', '0.144', '2.527', '2.445', '0.717', '1.000', '1.000', '1.000']
40000 ======================================================================
-10.2822392535 -2.46353128171 -12.7457705352
['1.000', '0.099', '2.161', '0.420', '2.002', '1.000', '1.000', '1.000']
45000 ======================================================================
-10.3979905862 -2.02516895885 -12.4231595451
['1.000', '0.729', '5.269', '1.242', '1.656', '1.000', '1.000', '1.000']
50000 ======================================================================
-10.8288098831 -5.01848675208 -15.8472966352
['1.000', '0.345', '6.821', '2.694', '0.355', '1.000', '1.000', '1.000']
55000 ======================================================================
-13.4031712771 -1.96454930391 -15.367720581
['1.000', '0.547', '6.062', '3.255', '5.162', '1.000', '1.000', '1.000']
60000 ======================================================================
-15.005425877 -3.6535389323 -18.6589648093
['1.000', '0.104', '4.094', '1.142', '0.849', '1.000', '1.000', '1.000']
65000 ======================================================================
-11.0706855056 -1.08807262822 -12.1587581338
['1.000', '0.188', '5.708', '1.191', '0.473', '1.000', '1.000', '1.000']
70000 ======================================================================
-12.065401575 -1.28148715986 -13.3468887348
['1.000', '0.217', '3.539', '0.981', '0.386', '1.000', '1.000', '1.000']
75000 ======================================================================
-10.3584797725 -2.31877881788 -12.6772585904
['1.000', '0.204', '2.694', '0.966', '3.281', '1.000', '1.000', '1.000']
80000 ======================================================================
-10.5909782 -3.21458068385 -13.8055588839
['1.000', '0.278', '10.356', '0.272', '3.162', '1.000', '1.000', '1.000']
85000 ======================================================================
-17.1630917661 -1.08119125601 -18.2442830221
['1.000', '0.546', '10.520', '1.413', '0.745', '1.000', '1.000', '1.000']
90000 ======================================================================
-15.4244520777 -1.92503549725 -17.349487575
['1.000', '0.197', '10.628', '1.849', '3.138', '1.000', '1.000', '1.000']
95000 ======================================================================
-17.3167648492 -0.832807234747 -18.149572084
['1.000', '0.414', '4.060', '1.084', '3.585', '1.000', '1.000', '1.000']
100000 ======================================================================
-11.2664459216 -3.97324194367 -15.2396878652

In [25]:
# Numpy array of sampled values for each vector element altered in proposals
vector_data = zip(*[[s.value[i] for i in s.propose_idxs] for s in samplec.samples])
vector_data = [np.array(l) for l in vector_data]

# Set up initial violinplot
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2, left=0.1)
ax.set_title('Distribution of values over GrammarRules generated by MH')
ax.violinplot(vector_data, points=100, vert=False, widths=0.7,
              showmeans=True, showextrema=True, showmedians=True)
max_interval = ax.xaxis.get_view_interval()

def update_violinplot(value):
    """Update the violinplot when you move the slider"""
    ax.clear()
    data = [vector[0:value] for vector in vector_data]
    ax.violinplot(data, points=100, vert=False, widths=0.7,
                  showmeans=True, showextrema=True, showmedians=True)
    ax.xaxis.set_view_interval(max_interval)
    fig.canvas.draw_idle()


# Add slider to plot; slider updates violinplot as a function of how many samples have been generated
slider_ax = plt.axes([0.1, 0.1, 0.8, 0.02])    
slider = Slider(slider_ax, "after N samples", valmin=1., valmax=samplec.sample_count, valinit=1.)
slider.on_changed(update_violinplot)


Out[25]:
0

scrap


In [5]:
'''Print distribution over power rule:  [prior, likelihood, posterior]'''
# vals, posteriors = grammar_h0.rule_distribution(data, 'ipowf_', np.arange(0.1, 5., 0.1))
# print_dist(vals, posteriors)
#visualize_dist(vals, posteriors, 'union_')


Out[5]:
'print distribution over power rule:  [prior, likelihood, posterior]'

In [20]:
%qtconsole