Tests if the random sorting obeys a gaussian distribution. Could possibly fail even though everything is OK. Measures how many times the two rows with biggest norm exchange place when random noise is added. This should correspond to the probability P(X > Y), where X = N(\\mu_1, \\si
(descriptor_func)
| 693 | |
| 694 | |
| 695 | def assert_matrix_descriptor_random(descriptor_func): |
| 696 | """Tests if the random sorting obeys a gaussian distribution. Could |
| 697 | possibly fail even though everything is OK. |
| 698 | |
| 699 | Measures how many times the two rows with biggest norm exchange place when |
| 700 | random noise is added. This should correspond to the probability P(X > Y), |
| 701 | where X = N(\\mu_1, \\sigma^2), Y = N(\\mu_2, \\sigma^2). This probability can |
| 702 | be reduced to P(X > Y) = P(X-Y > 0) = P(N(\\mu_1 - \\mu_2, \\sigma^2 + |
| 703 | \\sigma^2) > 0). See e.g. |
| 704 | https://en.wikipedia.org/wiki/Sum_of_normally_distributed_random_variables |
| 705 | """ |
| 706 | HHe = Atoms( |
| 707 | cell=[[5.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 5.0]], |
| 708 | positions=[ |
| 709 | [0, 0, 0], |
| 710 | [0.71, 0, 0], |
| 711 | ], |
| 712 | symbols=["H", "He"], |
| 713 | ) |
| 714 | |
| 715 | # Get the mean value to compare to |
| 716 | sigma = 5 |
| 717 | desc = descriptor_func(permutation="sorted_l2")([HHe]) |
| 718 | features = desc.create(HHe) |
| 719 | features = desc.unflatten(features) |
| 720 | means = np.linalg.norm(features, axis=1) |
| 721 | mu2 = means[0] |
| 722 | mu1 = means[1] |
| 723 | |
| 724 | desc = descriptor_func( |
| 725 | permutation="random", |
| 726 | sigma=sigma, |
| 727 | seed=42, |
| 728 | )([HHe]) |
| 729 | count = 0 |
| 730 | rand_instances = 20000 |
| 731 | for i in range(0, rand_instances): |
| 732 | features = desc.create(HHe) |
| 733 | features = desc.unflatten(features) |
| 734 | i_means = np.linalg.norm(features, axis=1) |
| 735 | if i_means[0] < i_means[1]: |
| 736 | count += 1 |
| 737 | |
| 738 | # The expected probability is calculated from the cumulative |
| 739 | # distribution function. |
| 740 | expected = 1 - scipy.stats.norm.cdf(0, mu1 - mu2, np.sqrt(sigma**2 + sigma**2)) |
| 741 | observed = count / rand_instances |
| 742 | |
| 743 | assert abs(expected - observed) <= 1e-2 |
| 744 | |
| 745 | |
| 746 | def assert_mbtr_location(descriptor_func, k): |
no test coverage detected