def mymarginal(d): """ Provides a dict -> pandas.DataFrame wrapper of the JSON arbplf_marginal. """ s = arbplf_marginal(json.dumps(d)) df = pd.read_json(StringIO(s), orient='split', precise_float=True) return df
def test_heterogeneous_edge_rates(): # try changing one of the edge rate coefficients d = { "model_and_data": { "edges": [[0, 1], [1, 2]], "edge_rate_coefficients": [1, 2], "rate_matrix": [[0, 1], [0, 0]], "probability_array": [[[1, 0], [1, 1], [1, 0]]] }, "site_reduction": { "aggregation": "only" } } actual_marginal = json.loads(arbplf_marginal(json.dumps(d))) assert_equal(actual_marginal, desired_marginal) g = copy.deepcopy(d) g['trans_reduction'] = dict(selection=[[0, 1], [1, 0]]) actual_trans = json.loads(arbplf_trans(json.dumps(g))) assert_equal(actual_trans, desired_trans) actual_ll = json.loads(arbplf_ll(json.dumps(d))) desired_ll = {"columns": ["value"], "data": [[-3.0]]} assert_equal(actual_ll, desired_ll) actual_em_update = json.loads(arbplf_em_update(json.dumps(d))) assert_equal(actual_em_update, desired_em_update) actual_dwell = json.loads(arbplf_dwell(json.dumps(d))) assert_equal(actual_dwell, desired_dwell)
def test_edges_are_not_preordered(): # Try switching the order of the edges in the input # and increasing the birth rate in the rate matrix. d = { "model_and_data": { "edges": [[1, 2], [0, 1]], "edge_rate_coefficients": [1, 2], "rate_matrix": [[0, 2], [0, 0]], "probability_array": [[[1, 0], [1, 1], [1, 0]]] }, "site_reduction": { "aggregation": "only" } } actual_marginal = json.loads(arbplf_marginal(json.dumps(d))) assert_equal(actual_marginal, desired_marginal) g = copy.deepcopy(d) g['trans_reduction'] = dict(selection=[[0, 1], [1, 0]]) actual_trans = json.loads(arbplf_trans(json.dumps(g))) assert_equal(actual_trans, desired_trans) actual_ll = json.loads(arbplf_ll(json.dumps(d))) desired_ll = {"columns": ["value"], "data": [[-6.0]]} assert_equal(actual_ll, desired_ll) actual_em_update = json.loads(arbplf_em_update(json.dumps(d))) assert_equal(actual_em_update, desired_em_update) actual_dwell = json.loads(arbplf_dwell(json.dumps(d))) assert_equal(actual_dwell, desired_dwell)
def test_marginal(): d = copy.deepcopy(D) s = arbplf_marginal(json.dumps(d)) df = pd.read_json(StringIO(s), orient='split', precise_float=True) actual = df.pivot('node', 'state', 'value').values # compute the desired closed form solution u = np.cumsum([0] + rates) desired = np.vstack([np.exp(-u), -np.expm1(-u)]).T # compare actual and desired result assert_allclose(actual, desired)
def test_truncated_marginal(): d = copy.deepcopy(D) d['model_and_data']['probability_array'][0][-1] = [0, 1] s = arbplf_marginal(json.dumps(d)) df = pd.read_json(StringIO(s), orient='split', precise_float=True) actual = df.pivot('node', 'state', 'value').values # compute the desired closed form solution u = np.cumsum([0] + rates) T = u[-1] v = (exp(-T) - exp(-u)) / expm1(-T) desired = np.vstack([v, 1-v]).T # compare actual and desired result assert_allclose(actual, desired)