From 0c97d6afb1c268a39b1785b89d60253862d44be0 Mon Sep 17 00:00:00 2001 From: tpsatish95 Date: Sun, 24 Mar 2019 20:04:05 -0400 Subject: [PATCH] Add Manova unit tests and fix other unit tests --- CHANGELOG.md | 1 + mgcpy/benchmarks/unit_tests/power_test.py | 9 +------- mgcpy/hypothesis_tests/unit_test.py | 4 ++-- .../unit_tests/manova_test.py | 22 +++++++++++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 mgcpy/independence_tests/unit_tests/manova_test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8150bd7..e8aeb41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [0.3.0] - 2019-02-07 - Refactor the mgcpy package structure +- Add Manova - Fix two sample tests - Add scripts for k sample test power curves - Update sphinx docs diff --git a/mgcpy/benchmarks/unit_tests/power_test.py b/mgcpy/benchmarks/unit_tests/power_test.py index 3db275e..36cde70 100644 --- a/mgcpy/benchmarks/unit_tests/power_test.py +++ b/mgcpy/benchmarks/unit_tests/power_test.py @@ -1,16 +1,9 @@ -import pickle - import h5py import numpy as np import pytest from mgcpy.benchmarks.power import power, power_given_data -from mgcpy.benchmarks.simulations import (circle_sim, joint_sim, - multi_indep_sim, multi_noise_sim, - sin_sim, square_sim, ubern_sim, - w_sim) +from mgcpy.benchmarks.simulations import joint_sim from mgcpy.independence_tests.dcorr import DCorr -from mgcpy.independence_tests.rv_corr import RVCorr -from scipy.spatial.distance import pdist, squareform def test_power(): diff --git a/mgcpy/hypothesis_tests/unit_test.py b/mgcpy/hypothesis_tests/unit_test.py index 27d665d..410c510 100644 --- a/mgcpy/hypothesis_tests/unit_test.py +++ b/mgcpy/hypothesis_tests/unit_test.py @@ -23,7 +23,7 @@ def test_k_sample(): # k sample case salaries = salary_data["Current Annual Salary"].values - department_labels = salary_data["Department"].values + department_labels = salary_data["Department"].values.reshape(-1, 1) u, v = k_sample_transform(salaries[:100], department_labels[:100], is_y_categorical=True) mgc = MGC() p_value, p_value_metadata = mgc.p_value(u, v) @@ -43,7 +43,7 @@ def test_k_sample(): 0.22417797, 0.45241074, -1.03024521, 0.6615743, 0.27216365, 2.4188678, 0.20561134, 0.71095061, -1.02478312, 0.54512964, 0.16582386, -0.39648338, -0.77905918, -0.33196771, 0.69407125, -0.81484451, 3.01568098, -0.49053868, -0.60987204, 1.72967348]) # assign half of them as samples from 1 and the other half as samples from 2 - y = np.concatenate([np.repeat(1, 50), np.repeat(2, 50)], axis=0) + y = np.concatenate([np.repeat(1, 50), np.repeat(2, 50)], axis=0).reshape(-1, 1) u, v = k_sample_transform(x, y, is_y_categorical=True) mgc = MGC() diff --git a/mgcpy/independence_tests/unit_tests/manova_test.py b/mgcpy/independence_tests/unit_tests/manova_test.py new file mode 100644 index 0000000..f8526a2 --- /dev/null +++ b/mgcpy/independence_tests/unit_tests/manova_test.py @@ -0,0 +1,22 @@ +import numpy as np +import pytest +from mgcpy.independence_tests.manova import Manova +from mgcpy.benchmarks.hypothesis_tests.three_sample_test.power import generate_three_two_d_gaussians +from mgcpy.hypothesis_tests.transforms import k_sample_transform + + +def test_local_corr(): + np.random.seed(0) + matrix_X, matrix_Y, matrix_Z = generate_three_two_d_gaussians(2, 100, 3) + + data = np.concatenate([matrix_X, matrix_Y, matrix_Z], axis=0) + labels = np.concatenate([np.repeat(1, matrix_X.shape[0]), np.repeat(2, matrix_Y.shape[0]), np.repeat(3, matrix_Z.shape[0])], axis=0).reshape(-1, 1) + + matrix_U, matrix_V = k_sample_transform(data, labels, is_y_categorical=True) + + # Against linear simulations + manova = Manova() + test_stat = manova.test_statistic(matrix_U, matrix_V)[0] + + assert manova.get_name() == 'manova' + assert np.allclose(test_stat, 0.06, atol=1.e-2)