Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mh stephen boyd et al #15

Merged
merged 3 commits into from
Oct 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 81 additions & 4 deletions hive/app/tests/metropolis_hastings_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,83 @@ def construct_rejection_matrix_div_by_zero_error_exist_test():
return print_test("construct_rejection_matrix_div_by_zero_error_exist_test", "no excpetion", "ZeroDivisionError", False)


def arbitrary_matrix_converges_to_ddv():
target = [0.2, 0.3, 0.5, 0.0]
def arbitrary_matrix_converges_to_ddv_1():
target = [0.2, 0.3, 0.5, 0]
adj = np.asarray([[1, 1, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1], [0, 1, 1, 1]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
return print_test("metropols_algorithm_test", target, mh_pow[:, 0], np.allclose(target, mh_pow[:, 0]))
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_1", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_1", target, mh_pow[:, 0], True)


def arbitrary_matrix_converges_to_ddv_2():
target = [0.2, 0.3, 0.2, 0.3]
adj = np.asarray([[1, 1, 0, 0], [1, 1, 1, 1], [0, 1, 1, 1], [0, 1, 1, 1]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_2", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_2", target, mh_pow[:, 0], True)


def arbitrary_matrix_converges_to_ddv_3():
target = [0.2, 0.3, 0.5, 0]
adj = np.asarray([[1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_3", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_3", target, mh_pow[:, 0], True)


def arbitrary_matrix_converges_to_ddv_4():
target = [0.0, 0.1, 0.1, 0.8]
adj = np.asarray([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [0, 1, 1, 1]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_4", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_4", target, mh_pow[:, 0], True)


def arbitrary_matrix_converges_to_ddv_5():
target = [0.2, 0.3, 0.5, 0.0]
adj = np.asarray([[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_5", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_5", target, mh_pow[:, 0], True)


def arbitrary_matrix_converges_to_ddv_6():
target = [1, 0, 0, 0]
adj = np.asarray([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_converges_to_ddv_6", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_converges_to_ddv_6", target, mh_pow[:, 0], True)


def arbitrary_matrix_does_not_converges_to_ddv_1():
target = [1, 0, 0, 0]
adj = np.asarray([[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0]])
mh = mH.metropolis_algorithm(adj, ddv=target, column_major_in=False, column_major_out=True)
mh_pow = np.linalg.matrix_power(mh, 1000)
for j in range(mh_pow.shape[1]):
if j == 1 and np.allclose(target, mh_pow[:, 1]):
return print_test("arbitrary_matrix_does_not_converges_to_ddv_1", [0, 1, 0, 0], mh_pow[:, 1], False)
elif j != 1 and not np.allclose(target, mh_pow[:, j]):
return print_test("arbitrary_matrix_does_not_converges_to_ddv_1", target, mh_pow[:, j], False)
return print_test("arbitrary_matrix_does_not_converges_to_ddv_1", target, mh_pow[:, 0], True)


if __name__ == "__main__":
Expand All @@ -62,7 +133,13 @@ def arbitrary_matrix_converges_to_ddv():
matrix_converges_to_known_ddv_test,
construct_random_walk_test,
construct_rejection_matrix_div_by_zero_error_exist_test,
arbitrary_matrix_converges_to_ddv,
arbitrary_matrix_converges_to_ddv_1,
arbitrary_matrix_converges_to_ddv_2,
arbitrary_matrix_converges_to_ddv_3,
arbitrary_matrix_converges_to_ddv_4,
arbitrary_matrix_converges_to_ddv_5,
arbitrary_matrix_converges_to_ddv_6,
arbitrary_matrix_does_not_converges_to_ddv_1
]

passed = 0
Expand Down