示例#1
0
class Finder(object):
    def __init__(self):
        self.lh = Lighthouse(.25, .75)
        x = np.linspace(0, 1, 99)
        y = np.linspace(0, 1, 99)
        pdf = {(xi, yi): 1 for xi in x for yi in y}
        likelihood = lambda x, (d, m): d / (d**2 + (x - m)**2)
        self.bayes = Bayes(pdf, likelihood)

    def solve_stuff(self):
        for datum in self.lh.dataset:
            self.bayes.update(datum)

    def print_distribution(self):
        self.bayes.print_distribution()
示例#2
0
class Finder(object):

    def __init__(self):
        self.lh = Lighthouse(.25,.75)
        x = np.linspace(0, 1, 99)
        y = np.linspace(0, 1, 99)
        pdf = {(xi,yi):1 for xi in x for yi in y}
        likelihood = lambda x, (d, m): d / (d**2 + (x-m)**2 )
        self.bayes = Bayes(pdf, likelihood)

    def solve_stuff(self):
        for datum in self.lh.dataset:
            self.bayes.update(datum)

    def print_distribution(self):
        self.bayes.print_distribution()
示例#3
0
    elif val == 'T':
        return 1-p

'''Make a graph with 8 subplots that has the posterior for each of the following scenarios.
   Make sure to give each graph a title!
        * You get the data: H
        * You get the data: T
        * You get the data: H, H
        * You get the data: T, H
        * You get the data: H, H, H
        * You get the data: T, H, T
        * You get the data: H, H, H, H
        * You get the data: T, H, T, H'''

bayes_uniform = Bayes(prior_dict.copy(),likelihood_func=likelihood)
bayes_uniform.update('H')

fig, axs = plt.subplots(4,2, figsize=(14,8))
scenarios = ['H','T',['H','H'],['T','H'],['H','H','H'],['T','H','T'],['H','H','H','H'],['T','H','T','H']]
i = 1
for scenario, ax in zip(scenarios, axs.flatten()):
    bayes = Bayes(prior_dict.copy(),likelihood_func=likelihood)
    for flip in scenario:
        bayes.update(flip)
    bayes.plot(ax,title='Scenario #'+str(i)+': '+ ', '.join(scenario))
    i+=1
plt.tight_layout()
plt.show()

'''On a single graph, Use the coin.py random coin generator and overlay the initial uniform prior with the prior after 1, 2, 10, 50 and 250 flips..
Use the color parameter to give a different color to each layer. Use the label parameter to label each label.
示例#4
0
    Returns:
        likelihood (float): the probability of the roll given the die.
    """
    if roll in range(1, die + 1):
        return 1 / die
    else:
        return 0


if __name__ == '__main__':
    uniform_prior = {4: .08, 6: .12, 8: .16, 12: .24, 20: .40}
    unbalanced_prior = {}

    die_bayes_1 = Bayes(uniform_prior.copy(), die_likelihood)
    experiment = [
        8, 2, 1, 2, 5, 8, 2, 4, 3, 7, 6, 5, 1, 6, 2, 5, 8, 8, 5, 3, 4, 2, 4, 3,
        8, 8, 7, 8, 8, 8, 5, 5, 1, 3, 8, 7, 8, 5, 2, 5, 1, 4, 1, 2, 1, 3, 1, 3,
        1, 5
    ]

    experiment2 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    experiment3 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20]

    for idx, roll in enumerate(experiment3):
        print('roll#={} dic rolled={}'.format(idx + 1, roll))
        die_bayes_1.update(roll)
        die_bayes_1.print_distribution()
        #unbalanced_prior = die_bayes_1.prior
        print('*' * 32)
示例#5
0

uniform_prior = {4: 0.2, 6: 0.2, 8: 0.2, 12: 0.2, 20: 0.2}
unbalanced_prior = {4: 0.08, 6: 0.12, 8: 0.16, 12: 0.24, 20: 0.4}

d = [
    8, 2, 1, 2, 5, 8, 2, 4, 3, 7, 6, 5, 1, 6, 2, 5, 8, 8, 5, 3, 4, 2, 4, 3, 8,
    8, 7, 8, 8, 8, 5, 5, 1, 3, 8, 7, 8, 5, 2, 5, 1, 4, 1, 2, 1, 3, 1, 3, 1, 5
]

set1 = [1, 1, 1, 3, 1, 2]
set2 = [10, 10, 10, 10, 8, 8]

print('What are the posteriors if we started with the uniform prior?')
bayes_uniform = Bayes(uniform_prior.copy(), likelihood_func=likelihood_func)
bayes_uniform.update(8)
bayes_uniform.print_distribution()

print('What are the posteriors if we started with the unbalanced prior?')
bayes_unbalanced = Bayes(unbalanced_prior.copy(),
                         likelihood_func=likelihood_func)
bayes_unbalanced.update(8)
bayes_unbalanced.print_distribution()

print(
    'How different were these two posteriors (the uniform from the unbalanced)?'
)
for k, v in bayes_unbalanced.posterior.items():
    print("{} : {}".format(
        k, bayes_uniform.posterior[k] - bayes_unbalanced.posterior[k]))