def test_deep_kl_lag(m, p=.99, trials=100, eps=1e-3, n_samples=300, pre_burnin=100): net = m_deep_bistable(m+1, p) # using m+1 since leaf node is used for evidence # compute steady state model_nodes = net._nodes[:-1] evidence_node = net._nodes[-1] ss_distributions = steady_state(net, {evidence_node : 1}, model_nodes, eps) # accumulate data (KL divergence) in nodes x time_steps x trials data = np.zeros((m, n_samples, trials)) for t in range(trials): counts = {node: eps * np.ones(node.size()) for node in model_nodes} def do_divergence(i, net): for j,n in enumerate(model_nodes): counts[n][n.state_index()] += 1 # compute divergence between current estimate and ss_distribution data[j][i][t] = KL(ss_distributions[n], counts[n] / counts[n].sum()) # prepare net by burning-in to 0-evidence gibbs_sample(net, {evidence_node : 0}, None, 0, pre_burnin) # get data on 1-evidence (no burnin this time; demonstrating lag) gibbs_sample(net, {evidence_node : 1}, do_divergence, n_samples, 0) return data
def test_deep_likelihood_lag(m, p=.99, trials=100, eps=1e-3, n_samples=300, pre_burnin=100): net = m_deep_bistable(m+1, p) # compute steady state model_nodes = net._nodes[:-1] evidence_node = net._nodes[-1] ss_distributions = steady_state(net, {evidence_node : 1}, model_nodes, eps) # accumulate data (sample likelihood) in nodes x time_steps x trials data = np.zeros((m-1, n_samples, trials)) for t in range(trials): def do_likelihood(i, net): for j,n in enumerate(model_nodes): data[j][i][t] = ss_distributions[n][n.state_index()] # prepare net by burning-in to 0-evidence gibbs_sample(net, {evidence_node : 0}, None, 0, pre_burnin) # get data on 1-evidence (no burnin this time; demonstrating lag) gibbs_sample(net, {evidence_node : 1}, do_likelihood, n_samples, 0) return data
Ms = range(args.m_min, args.m_max+1) Ts = range(args.t_max + 1) alphas = np.linspace(1.0, args.boost_max, args.boost_steps) def eig_steadystate(A): # steady state distribution of A_ff transition matrix is largest eigenvector (eigenvalue=1) w,v = np.linalg.eig(A) inds = np.argsort(w) S_steady_state = np.abs(v[:,inds[-1]]) S_steady_state /= S_steady_state.sum() return S_steady_state mixing_times = np.zeros((args.boost_steps, args.t_max+1, len(Ms))) for mi,m in enumerate(Ms): net = m_deep_bistable(m, marg=args.marg) ev = net.get_node_by_name('X1') p = ev.get_table()[0,0] A = load_or_run('transition_matrix_M%d_p%.3f_ev1' % (m, p), lambda: construct_markov_transition_matrix(net, conditioned_on={ev: 1}), force_recompute=args.recompute) S_start = analytic_marginal_states(net, conditioned_on={ev: 0}) S_target = analytic_marginal_states(net, conditioned_on={ev: 1}) for ai, a in enumerate(alphas): A_ff = load_or_run('transition_matrix_transient_ff_M%d_p%.3f_b%.3f_ev1' % (m, p, a), lambda: construct_markov_transition_matrix(net, feedforward_boost=a, conditioned_on={ev: 1}), force_recompute=args.recompute) for transient_steps in Ts: # avoid possibility that S-->S_ff_steadystate 'passes through' S_target, apparently # looking like mixing time is very small ff_steady_state_in_range = variational_distance(eig_steadystate(A_ff), S_target) < args.eps
parser.add_argument('--tau-min', dest='tau_min', type=float, default=0.1) parser.add_argument('--no-plot', dest='plot', action='store_false', default=True) args = parser.parse_args() # for each M [2..m-max], get baseline mixing time, then try the # 'adaptation' model for <tau_steps> values of tau=0.5...1.0 Ms = range(2,args.m+1) taus = np.linspace(args.tau_min, 0.5, args.tau_steps) # _true and _self are MT with respect to unmodified transition matrix and modified one, respectively. # When TVD(S_true_ss, S_self_ss)>eps, mixing_time_true is infinite. mixing_times_true = np.zeros((args.tau_steps, len(Ms))) # one data point per model per tau mixing_times_self = np.zeros((args.tau_steps, len(Ms))) for mi,m in enumerate(Ms): net = m_deep_bistable(m, marg=args.marg) ev = net.get_node_by_name('X1') p = ev.get_table()[0,0] S_target = analytic_marginal_states(net, conditioned_on={ev: 1}) for ti, tau in enumerate(taus): A_adapt = load_or_run('transition_matrix_adapt_M%d_p%.3f_tau%.3f_ev1' % (m, p, tau), lambda: construct_markov_transition_matrix(net, fatigue_tau=tau, conditioned_on={ev: 1}), force_recompute=args.recompute) converging_to = eig_steadystate(A_adapt) # steadystate with {ev:0} is our starting point. It is computed by simply 'flipping' # the distribution from {ev:1} (symmetry is convenient) S_steadystate_ev0 = flip_distribution_binary_nodes(net, converging_to)
parser.add_argument('--compare-p', dest='cmp_p', action='store_true', default=False) parser.add_argument('--marg', dest='marg', type=float, default=0.9) parser.add_argument('--eps', dest='eps', type=float, default=0.05) parser.add_argument('--no-plot', dest='plot', action='store_false', default=True) parser.add_argument('--m-max', dest='m_max', type=int, default=7) args = parser.parse_args() max_t = 1000 m_min = 2 n_layers = args.m_max - m_min + 1 layers = range(m_min, args.m_max+1) mixing_times = np.zeros(n_layers) for M in layers: net = m_deep_bistable(M, marg=args.marg) ev = net.get_node_by_name('X1') p = ev.get_table()[0,0] A = load_or_run('transition_matrix_M%d_p%.3f_ev1' % (M, p), lambda: construct_markov_transition_matrix(net, conditioned_on={ev:1}), force_recompute=args.recompute) # S_start and S_target are marginal distributions conditioned on {ev:0} and {ev:1} respectively. S_start = analytic_marginal_states(net, conditioned_on={ev: 0}) S_target = analytic_marginal_states(net, conditioned_on={ev: 1}) mixing_times[M-m_min], _ = mixing_time(S_start, S_target, A, eps=args.eps) if args.cmp_p: p = 0.96 mixing_times_rho_const = np.zeros(n_layers)
current_state = plurality_state(net) if self.last_state is not None and self.last_state != current_state: self.switching_times[i - self.t0] += 1 self.t0 = i self.last_state = current_state def distribution(self): d = np.zeros(max(self.switching_times.keys()) + 1) for m, v in self.switching_times.iteritems(): d[m] = v return d for M in range(2, args.m_max + 1): print M net = m_deep_bistable(M, args.p) N = count_states(net) S = analytic_marginal_states(net) p_sum_state = np.zeros(len(net._nodes) + 1) for i in range(N): sum_state = sum(id_to_state(net, i)) p_sum_state[sum_state] += S[i] if args.plot: # plot distribution over sum of states (show it's bimodal) plt.figure() plt.plot(p_sum_state) plt.title('P(sum of states) for M = %d' % M) plt.xlabel('sum of states')
parser.add_argument('--burnin', dest='burnin', type=int, default='20') parser.add_argument('--frames', dest='frames', type=int, default=60) parser.add_argument('--no-movie', dest='movie', action='store_false', default=True) parser.add_argument('--res', type=int, default=240) args = parser.parse_args() FFMpegWriter = manimation.writers['ffmpeg'] metadata = dict(title='Mixing Time Animation', artist='Matplotlib', comment='') writer = FFMpegWriter(fps=15, metadata=metadata) m_min = 2 layers = range(m_min, args.m_max+1) for M in layers: print M net = m_deep_bistable(M, args.p) ev_node = net.get_node_by_name('X1') def sample_net_response(): # keep track of state of every node at every sample states = np.zeros((args.samples, M)) def record_sample(i, net): states[i,:] = net.state_vector() gibbs_sample_dynamic_evidence( net, alternator([{ev_node: 0}, {ev_node: 1}], period=args.T), record_sample, args.samples, args.burnin)
parser.add_argument('--no-plot', dest='plot', action='store_false', default=True) parser.add_argument('--m-max', dest='m_max', type=int, default=7) args = parser.parse_args() max_t = 1000 m_min = 2 n_layers = args.m_max - m_min + 1 layers = range(m_min, args.m_max + 1) mixing_times = np.zeros(n_layers) for M in layers: net = m_deep_bistable(M, marg=args.marg) ev = net.get_node_by_name('X1') p = ev.get_table()[0, 0] A = load_or_run('transition_matrix_M%d_p%.3f_ev1' % (M, p), lambda: construct_markov_transition_matrix( net, conditioned_on={ev: 1}), force_recompute=args.recompute) # S_start and S_target are marginal distributions conditioned on {ev:0} and {ev:1} respectively. S_start = analytic_marginal_states(net, conditioned_on={ev: 0}) S_target = analytic_marginal_states(net, conditioned_on={ev: 1}) mixing_times[M - m_min], _ = mixing_time(S_start, S_target, A, eps=args.eps)
import argparse parser = argparse.ArgumentParser() parser.add_argument('--recompute', action='store_true', default=False) parser.add_argument('--plot-self', dest='plot_self', action='store_true', default=False) parser.add_argument('--steps', dest='steps', type=int, default=12) parser.add_argument('--q-min', dest='q_min', type=float, default=0.5) parser.add_argument('--q-max', dest='q_max', type=float, default=1.0) parser.add_argument('--eps', dest='eps', type=float, default=.05) parser.add_argument('--depth', dest='m', type=int, default=6) parser.add_argument('--marg', dest='marg', type=float, default=0.9) parser.add_argument('--no-plot', dest='plot', action='store_false', default=True) args = parser.parse_args() # first data point: baseline mixing time (no shortcut) net_baseline = m_deep_bistable(args.m, marg=args.marg) ev = net_baseline.get_node_by_name('X1') p = ev.get_table()[0,0] A = load_or_run('transition_matrix_M%d_p%.3f_ev1' % (args.m, p), lambda: construct_markov_transition_matrix(net_baseline, conditioned_on={ev: 1}), force_recompute=args.recompute) S_start_baseline = analytic_marginal_states(net_baseline, conditioned_on={ev: 0}) S_target_baseline = analytic_marginal_states(net_baseline, conditioned_on={ev: 1}) mixing_time_baseline, _ = mixing_time(S_start_baseline, S_target_baseline, A, eps=args.eps) print 'baseline', mixing_time_baseline def get_mixing_time_base_self(net, fro, to, identifier): ev = net.get_node_by_name('X1') A_sc = load_or_run('transition_matrix_shortcuts_m%d_f%d_t%d_%s_ev1' % (args.m, fro, to, identifier), lambda: construct_markov_transition_matrix(net, conditioned_on={ev: 1}), force_recompute=args.recompute)
action='store_true', default=False) parser.add_argument('--steps', dest='steps', type=int, default=12) parser.add_argument('--q-min', dest='q_min', type=float, default=0.5) parser.add_argument('--q-max', dest='q_max', type=float, default=1.0) parser.add_argument('--eps', dest='eps', type=float, default=.05) parser.add_argument('--depth', dest='m', type=int, default=6) parser.add_argument('--marg', dest='marg', type=float, default=0.9) parser.add_argument('--no-plot', dest='plot', action='store_false', default=True) args = parser.parse_args() # first data point: baseline mixing time (no shortcut) net_baseline = m_deep_bistable(args.m, marg=args.marg) ev = net_baseline.get_node_by_name('X1') p = ev.get_table()[0, 0] A = load_or_run('transition_matrix_M%d_p%.3f_ev1' % (args.m, p), lambda: construct_markov_transition_matrix( net_baseline, conditioned_on={ev: 1}), force_recompute=args.recompute) S_start_baseline = analytic_marginal_states(net_baseline, conditioned_on={ev: 0}) S_target_baseline = analytic_marginal_states(net_baseline, conditioned_on={ev: 1}) mixing_time_baseline, _ = mixing_time(S_start_baseline, S_target_baseline, A, eps=args.eps) print 'baseline', mixing_time_baseline
from counting import * from graphical_models import * def net_to_digraph(net): g = nx.DiGraph() g.add_nodes_from(net.iter_nodes()) g.add_edges_from(net.iter_edges()) return g if __name__ == '__main__': from models import erdos_renyi import matplotlib.pyplot as plt G = erdos_renyi(100,0.01) DG = net_to_digraph(G) fig = plt.figure() fig.add_subplot(121) nx.draw_random(DG) fig.add_subplot(122) nx.draw_circular(DG) plt.show() plt.close() from models import m_deep_bistable net = m_deep_bistable(7, 0.96) g = net_to_digraph(net) nx.draw(g) plt.show() plt.close()