Test the validity of the North-West gluing.
(gamma, J, w, pi_list)
| 684 | |
| 685 | |
| 686 | def verify_gluing_validity(gamma, J, w, pi_list): |
| 687 | """ |
| 688 | Test the validity of the North-West gluing. |
| 689 | """ |
| 690 | nx = get_backend(gamma) |
| 691 | K = len(pi_list) |
| 692 | n = pi_list[0].shape[0] |
| 693 | nk_list = [pi.shape[1] for pi in pi_list] |
| 694 | |
| 695 | # Check first marginal |
| 696 | a = nx.sum(gamma, axis=tuple(range(1, K + 1))) |
| 697 | assert nx.allclose(a, nx.sum(pi_list[0], axis=1)) |
| 698 | |
| 699 | # Check other marginals |
| 700 | for k in range(K): |
| 701 | b_k = nx.sum(gamma, axis=tuple(i for i in range(K + 1) if i != k + 1)) |
| 702 | assert nx.allclose(b_k, nx.sum(pi_list[k], axis=0)) |
| 703 | |
| 704 | # Check bi-marginals |
| 705 | for k in range(K): |
| 706 | gamma_0k = nx.sum(gamma, axis=tuple(i for i in range(1, K + 1) if i != k + 1)) |
| 707 | assert nx.allclose(gamma_0k, pi_list[k]) |
| 708 | |
| 709 | # Check that N <= n + sum_k n_k - K |
| 710 | N = J.shape[0] |
| 711 | n_k_sum = sum(nk_list) |
| 712 | assert N <= n + n_k_sum - K, f"N={N}, n={n}, sum(n_k)={n_k_sum}, K={K}" |
| 713 | |
| 714 | # Check that w is on the simplex |
| 715 | w_sum = nx.sum(w) |
| 716 | assert nx.allclose(w_sum, 1), f"Sum of weights w is not 1: {w_sum}" |
| 717 | |
| 718 | # Check that gamma_1...K and (J, w) are consistent |
| 719 | rho = nx.zeros(nk_list, type_as=gamma) |
| 720 | for i in range(N): |
| 721 | jj = J[i] |
| 722 | rho[tuple(jj)] += w[i] |
| 723 | |
| 724 | gamma_1toK = nx.sum(gamma, axis=0) |
| 725 | assert nx.allclose(rho, gamma_1toK), "rho and gamma_1...K are not consistent" |
| 726 | |
| 727 | |
| 728 | def test_north_west_mm_gluing(): |
no test coverage detected