def _get_simple_model_expectations_summary(nnodes, r0, r1, e0, e1): nedges = nnodes - 1 nodes = range(nnodes) j_in = dict(node_count=nnodes, process_count=1, state_space_shape=[3], prior_feasible_states=[[0]], prior_distribution=[1.0], tree=dict( row=nodes[:-1], col=nodes[1:], process=[0] * nedges, rate=[1] * nedges, ), processes=[ dict(row=[[0], [0]], col=[[1], [2]], rate=[r0, r1], expect=[e0, e1]) ], observable_nodes=[nnodes - 1], observable_axes=[0], iid_observations=[[0], [1], [0], [2]]) return process_json_in(j_in)
def _get_tiny_model_expectations_summary(nnodes, r): """ Models evolution of a binary state along a path. """ nedges = nnodes - 1 nodes = range(nnodes) nstates = 2 j_in = dict(node_count=nnodes, process_count=1, state_space_shape=[2], prior_feasible_states=[[0]], prior_distribution=[1.0], dwell_states=[[1]], dwell_expect=[1], tree=dict( row=nodes[:-1], col=nodes[1:], process=[0] * nedges, rate=[1] * nedges, ), processes=[dict( row=[[0]], col=[[1]], rate=[r], expect=[1], )], observable_nodes=nodes[-1:], observable_axes=[0], iid_observations=[[1]]) return process_json_in(j_in)
def _get_tiny_model_expectations_summary(nnodes, r): """ Models evolution of a binary state along a path. """ nedges = nnodes - 1 nodes = range(nnodes) nstates = 2 j_in = dict( node_count = nnodes, process_count = 1, state_space_shape = [2], prior_feasible_states = [[0]], prior_distribution = [1.0], dwell_states = [[1]], dwell_expect = [1], tree = dict( row = nodes[:-1], col = nodes[1:], process = [0]*nedges, rate = [1]*nedges, ), processes = [dict( row = [[0]], col = [[1]], rate = [r], expect = [1], )], observable_nodes = nodes[-1:], observable_axes = [0], iid_observations = [[1]]) return process_json_in(j_in)
def _get_simple_model_expectations_summary(nnodes, r0, r1, e0, e1): nedges = nnodes - 1 nodes = range(nnodes) j_in = dict( node_count = nnodes, process_count = 1, state_space_shape = [3], prior_feasible_states = [[0]], prior_distribution = [1.0], tree = dict( row = nodes[:-1], col = nodes[1:], process = [0]*nedges, rate = [1]*nedges, ), processes = [dict( row = [[0], [0]], col = [[1], [2]], rate = [r0, r1], expect = [e0, e1])], observable_nodes = [nnodes-1], observable_axes = [0], iid_observations = [ [0], [1], [0], [2]]) return process_json_in(j_in)
def _compute_expectations(Q, d, dwell_expect, root_expect): nnodes = 5 nedges = nnodes - 1 nstates = d.shape[0] states = range(nstates) edge_rates = sample_distn(nedges) state_pairs = list(permutations(range(nstates), 2)) ntrans = len(state_pairs) transition_rates = [Q[i, j] for i, j in state_pairs] root_posterior_states = None root_posterior_expect = None if root_expect is not None: root_posterior_states = [[s] for s in states] root_posterior_expect = root_expect dwell_states = None if dwell_expect is not None: dwell_states = [[s] for s in states] j_in = dict(node_count=nnodes, process_count=1, state_space_shape=[nstates], prior_feasible_states=[[s] for s in states], prior_distribution=d.tolist(), dwell_states=dwell_states, dwell_expect=dwell_expect, root_posterior_states=root_posterior_states, root_posterior_expect=root_posterior_expect, tree=dict( row=[0, 0, 2, 2], col=[2, 1, 4, 3], process=[0] * nedges, rate=edge_rates.tolist(), ), processes=[ dict( row=[[i] for i, j in state_pairs], col=[[j] for i, j in state_pairs], rate=transition_rates, expect=[1] * ntrans, ) ], observable_nodes=[], observable_axes=[], iid_observations=[ [], [], [], ]) return process_json_in(j_in)
def _compute_expectations(Q, d, dwell_expect, root_expect): nnodes = 5 nedges = nnodes - 1 nstates = d.shape[0] states = range(nstates) edge_rates = sample_distn(nedges) state_pairs = list(permutations(range(nstates), 2)) ntrans = len(state_pairs) transition_rates = [Q[i, j] for i, j in state_pairs] root_posterior_states = None root_posterior_expect = None if root_expect is not None: root_posterior_states = [[s] for s in states] root_posterior_expect = root_expect dwell_states = None if dwell_expect is not None: dwell_states = [[s] for s in states] j_in = dict( node_count = nnodes, process_count = 1, state_space_shape = [nstates], prior_feasible_states = [[s] for s in states], prior_distribution = d.tolist(), dwell_states = dwell_states, dwell_expect = dwell_expect, root_posterior_states = root_posterior_states, root_posterior_expect = root_posterior_expect, tree = dict( row = [0, 0, 2, 2], col = [2, 1, 4, 3], process = [0]*nedges, rate = edge_rates.tolist(), ), processes = [dict( row = [[i] for i, j in state_pairs], col = [[j] for i, j in state_pairs], rate = transition_rates, expect = [1]*ntrans, )], observable_nodes = [], observable_axes = [], iid_observations = [ [], [], [], ]) return process_json_in(j_in)
def _process(Q, d, observable_node, debug=False): """ Use the obsolete interface. """ state_space_shape = (2, 3) nstates = np.prod(state_space_shape) nnodes = 4 nedges = nnodes - 1 nodes = range(nnodes) edges = range(nedges) states = list(product( range(state_space_shape[0]), range(state_space_shape[1]), )) state_pairs = list(permutations(states, 2)) ntrans = len(state_pairs) assert_equal(ntrans, nstates * (nstates - 1)) row, col = zip(*state_pairs) idx_row = np.ravel_multi_index(np.transpose(row), state_space_shape) idx_col = np.ravel_multi_index(np.transpose(col), state_space_shape) transition_rates = [Q[i, j] for i, j in zip(idx_row, idx_col)] root_posterior_states = [[0, 0]] root_posterior_expect = [1] dwell_states = [[0, 0], [0, 1], [0, 2]] dwell_expect = [1, 1, 1] j_in = dict( node_count = nnodes, process_count = 1, state_space_shape = state_space_shape, prior_feasible_states = states, prior_distribution = d.tolist(), dwell_states = dwell_states, dwell_expect = dwell_expect, root_posterior_states = root_posterior_states, root_posterior_expect = root_posterior_expect, tree = dict( row = nodes[:-1], col = nodes[1:], process = [0]*nedges, rate = [0.2]*nedges, ), processes = [dict( row = [i for i, j in state_pairs], col = [j for i, j in state_pairs], rate = transition_rates, expect = [1]*ntrans, )], observable_nodes = [observable_node], observable_axes = [1], iid_observations = [ [0], [2], [1], [0], [1], ]) return expect.process_json_in(j_in, debug=debug)
def test_latent_variable(): # This model is bivariate. # Both variables evolve deterministically along a path, # but each variable may also convert to its neighbor state. n = 3 norm_rate = 0.1 conv_rate = 0.01 state_space_shape = [n, n] nstates = np.prod(state_space_shape) states = list(product(range(n), repeat=2)) rate_info = list(_gen_info(states, norm_rate, conv_rate)) #_show_jordan_blocks(np.array([ #[-1, 1, 0], #[0, -1, 1], #[1, 0, -1]], dtype=int)) # Check the jordan form using sympy. """ mstates = list(product(range(3), repeat=2)) mnstates = len(mstates) rmap = dict((s, i) for i, s in enumerate(mstates)) R = np.zeros((mnstates, mnstates), dtype=int) for sa, sb, r, x in _gen_info(mstates, 1, 1): print(sa, sb, r) R[rmap[sa], rmap[sb]] = r R -= np.diag(R.sum(axis=1)) _show_jordan_blocks(R) """ nnodes = 5 nedges = nnodes - 1 nodes = range(nnodes) tree = dict( row=nodes[:-1], col=nodes[1:], rate=[1] * nedges, process=[0] * nedges, ) row, col, rate, expect = zip(*rate_info) proc = dict( row=row, col=col, rate=rate, expect=expect, ) j_in = dict(node_count=nnodes, process_count=1, state_space_shape=state_space_shape, prior_feasible_states=[[0, 0]], prior_distribution=[1.0], tree=tree, processes=[proc], observable_nodes=nodes, observable_axes=[0] * nnodes, iid_observations=[ [0, 1, 0, 1, 2], ]) #print(json.dumps(j_in, indent=4)) j_out = process_json_in(j_in) site = 0 expectations = j_out['edge_expectations'][site] assert_equal(len(expectations), nedges) assert_array_less(0, expectations) assert_array_less(1, expectations[1])
def test_latent_variable(): # This model is bivariate. # Both variables evolve deterministically along a path, # but each variable may also convert to its neighbor state. n = 3 norm_rate = 0.1 conv_rate = 0.01 state_space_shape = [n, n] nstates = np.prod(state_space_shape) states = list(product(range(n), repeat=2)) rate_info = list(_gen_info(states, norm_rate, conv_rate)) #_show_jordan_blocks(np.array([ #[-1, 1, 0], #[0, -1, 1], #[1, 0, -1]], dtype=int)) # Check the jordan form using sympy. """ mstates = list(product(range(3), repeat=2)) mnstates = len(mstates) rmap = dict((s, i) for i, s in enumerate(mstates)) R = np.zeros((mnstates, mnstates), dtype=int) for sa, sb, r, x in _gen_info(mstates, 1, 1): print(sa, sb, r) R[rmap[sa], rmap[sb]] = r R -= np.diag(R.sum(axis=1)) _show_jordan_blocks(R) """ nnodes = 5 nedges = nnodes - 1 nodes = range(nnodes) tree = dict( row = nodes[:-1], col = nodes[1:], rate = [1]*nedges, process = [0]*nedges, ) row, col, rate, expect = zip(*rate_info) proc = dict( row=row, col=col, rate=rate, expect=expect, ) j_in = dict( node_count = nnodes, process_count = 1, state_space_shape = state_space_shape, prior_feasible_states = [[0, 0]], prior_distribution = [1.0], tree=tree, processes=[proc], observable_nodes = nodes, observable_axes = [0]*nnodes, iid_observations = [ [0, 1, 0, 1, 2], ] ) #print(json.dumps(j_in, indent=4)) j_out = process_json_in(j_in) site = 0 expectations = j_out['edge_expectations'][site] assert_equal(len(expectations), nedges) assert_array_less(0, expectations) assert_array_less(1, expectations[1])