def thomas_tests(): graph = thomas() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'Cross') print("P(Cross | Diesel=true, Diesel10=true) = <", mean, ", ", f_mean, ">") graph = thomas() graph.node_dict['Diesel'].observed = False graph.node_dict['Diesel10'].observed = False graph.node_dict['Thomas'].observed = True mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'Cross') print("P(Cross | Thomas=true) = <", mean, ", ", f_mean, ">") graph = thomas() graph.node_dict['Diesel'].observed = False graph.node_dict['Diesel10'].observed = False graph.node_dict['Cross'].observed = True mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'Diesel') print("P(Diesel | Cross=true) = <", mean, ", ", f_mean, ">") print()
def alarm_tests(): mcmc = MCMC() samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'B') print("P(Burglary | JohnCalls=true, MaryCalls=true) = <", mean, ", ", f_mean, ">") mean, f_mean = Tests.sample_dim(samples, 'A') print("P(Alarm | JohnCalls=true, MaryCalls=true) = <", mean, ", ", f_mean, ">") mean, f_mean = Tests.sample_dim(samples, 'E') print("P(Earthquake | JohnCalls=true, MaryCalls=true) = <", mean, ", ", f_mean, ">") mcmc = MCMC({'J': 1., 'M': 0.}) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'B') print("P(Burglary | JohnCalls=true, MaryCalls=false) = <", mean, ", ", f_mean, ">") mcmc = MCMC({'J': 1.}) mcmc.graph.node_dict['M'].observed = False samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'B') print("P(Burglary | JohnCalls=true) = <", mean, ", ", f_mean, ">") mcmc = MCMC({'M': 1.}) mcmc.graph.node_dict['J'].observed = False samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'B') print("P(Burglary | MaryCalls=true) = <", mean, ", ", f_mean, ">") print()
def beta_bernoulli_tests(): graph = beta_bernoulli() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(5000, 100000) Tests.plotposterior([s['A'] for s in samples], beta_expected_t, 'beta-bernoulli', 0, 1) graph = beta_bernoulli(b=0) mcmc = MCMC(graph=graph) samples = mcmc.gibbs(13000, 100000) Tests.plotposterior([s['A'] for s in samples], beta_expected_f, 'beta-bernoulli', 0, 1)
def home_or_school_tests(): graph = home_or_school() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'AS') print("P(AS | IA=false) = <", mean, ", ", f_mean, ">") graph = home_or_school() mcmc = MCMC({'IA': 1}, graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'AS') print("P(AS | IA=false) = <", mean, ", ", f_mean, ">") print()
def dirty_roommates_tests(): graph = dirty_roommates() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'CK') print("P(CK | DR=false) = <", mean, ", ", f_mean, ">") graph = dirty_roommates() graph.node_dict['DR'].observed = False graph.node_dict['CK'].observed = True mcmc = MCMC({'CK': 1}, graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'IT') print("P(IT | CK=true) = <", mean, ", ", f_mean, ">")
def burn_tests(): graph = burn() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 10000) mean, f_mean = Tests.sample_dim(samples, 'M') print("P(MapoDoufu | Burn=false) = <", mean, ", ", f_mean, ">") print()
def progress_tests(): graph = progress() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(2000, 50000) Tests.plot_distribution(samples, 'Encounters') encounters = [s['Encounters'] for s in samples] median = np.median(encounters) print('median', median)
def tanks_tests(): graph = tanks() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(50, 100000) Tests.mixing_plot(samples, 'num_tanks') Tests.plot_distribution(samples, 'num_tanks') Tests.mixing_plot(samples, 'A') Tests.plot_distribution(samples, 'A')
def faculty_evaluation_tests(): graph = faculty_evals() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(500, 10000) Tests.mixing_plot(samples, 'mu') Tests.mixing_plot(samples, 'sigma2') Tests.plotposterior([s['mu'] for s in samples], faculty_mean_prior, 'mean', 5.0, 6.5) Tests.plotposterior([s['sigma2'] for s in samples], faculty_var_prior, 'var', 0.0001, 1.0)
def golfer_network_tests(): graph = golf() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(1000, 100000) print('woohoo! the golfers finished!') with open('golf_samples.pickle', 'wb') as gs: pickle.dump(samples, gs) with open('golf_graph.pickle', 'wb') as gg: pickle.dump(graph, gg)
def hyper_alarm_inference(): legs = [10, 25, 50, 100, 1000] model = 'lab' for n in legs: name = model + '-inf_' + str(n) graph = hyper_alarm(val_dict=name, inference=True) mcmc = MCMC(graph=graph) samples = mcmc.gibbs(2000, 100000) mean, f_mean = Tests.sample_dim(samples, 'B') print(mean)
def pareto_poisson_tests(): graph = pareto() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(2000, 100000) for node in graph.hidden_nodes: #Tests.mixing_plot(samples, node.name) Tests.plot_distribution(samples, node.name) encounters = [s[node.name] for s in samples] median = np.median(encounters) print('median', median)
def wacky_network_tests(): graph = wacky() # graph.node_dict['G'].observed = True mcmc = MCMC(graph=graph) samples = mcmc.gibbs(20000, 2000000) with open('wacky_G_samples.pickle', 'wb') as ws: pickle.dump(samples, ws) with open('wacky_G_graph.pickle', 'wb') as wg: pickle.dump(graph, wg) for node in graph.nodes: Tests.mixing_plot(samples, node.name) Tests.plot_distribution(samples, node.name)
def hyper_faculty_tests(): meta_samples = [] for n in range(5): graph = faculty_evals_1hyper(n) mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 100000) print(n) Tests.plot_multi(samples, ['mu', 'sigma2']) print(graph.nodes) for node in graph.hidden_nodes: if node.name != 'mu' and node.name != 'sigma2': Tests.mixing_plot(samples, node.name) Tests.plot_distribution(samples, node.name)
def hyper_alarm_learning_tests(): legs = [1, 10, 25, 50, 75, 100, 250, 500, 750, 1000] prior = 'lab' model = '01_50' for n in legs: graph = hyper_alarm_learn('alarm-gen-' + model + '.json', n=n, val_dict=prior) mcmc = MCMC(graph=graph) samples = mcmc.gibbs(1000, 1000) mean, f_mean = Tests.sample_dim(samples, 'b_B') print(mean, f_mean) #mean, f_mean = Tests.sample_dim(samples, 'b_E') #print(mean, f_mean) name = 'alarm-' + model + '_' + str(n) + '_samples.pickle' with open(name, 'wb') as f: pickle.dump(samples, f)
def hyper_alarm_generate(): val_dict = 'orig' graph = hyper_alarm(val_dict) mcmc = MCMC(graph=graph) samples = mcmc.gibbs(10000, 30000) mean, f_mean = Tests.sample_dim(samples, 'B') print(mean, f_mean) mean, f_mean = Tests.sample_dim(samples, 'E') print(mean, f_mean) saved_samples = [] for i, sample in enumerate(samples): if i < 3001 and i % 3 == 0: #print(sample) saved_samples.append(sample) with open('alarm-gen-' + val_dict + '.json', 'w') as f: json.dump(saved_samples,f)
def normal_normal_tests(): graph = normal_normal() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(500, 10000) Tests.plotposterior([s['A'] for s in samples], normal_expected, 'normal-normal', -3, 3)
def gamma_poisson_tests(): graph = gamma_poisson() mcmc = MCMC(graph=graph) samples = mcmc.gibbs(50000, 10000) Tests.plotposterior([s['L'] for s in samples], gamma_expected, 'gamma-poisson', 0, 12)