def test_tartu_estimator(): # Test Tartu estimator on logical and pid_tartu = TartuPID(SETTINGS) Z = np.logical_and(X, Y).astype(int) est_tartu = pid_tartu.estimate(X, Y, Z) assert np.isclose(0.5, est_tartu['syn_s1_s2']), ( 'Synergy is not 0.5. for Tartu estimator.')
def _estimate(Z): """Estimate PID for a given target.""" # Sydney estimator pid_sydney = SydneyPID(SETTINGS) pid_tartu = TartuPID(SETTINGS) tic = tm.time() est_sydney = pid_sydney.estimate(X, Y, Z) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(X, Y, Z) t_tartu = tm.time() - tic print('\nCopied source') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format( t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) return est_sydney, est_tartu
def _estimate(Z): """Estimate PID for a given target.""" # Sydney estimator pid_sydney = SydneyPID(SETTINGS) pid_tartu = TartuPID(SETTINGS) tic = tm.time() est_sydney = pid_sydney.estimate(X, Y, Z) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(X, Y, Z) t_tartu = tm.time() - tic print('\nCopied source') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format(t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) return est_sydney, est_tartu
def test_tartu_estimator(): # Test Tartu estimator on logical and pid_tartu = TartuPID(SETTINGS) Z = np.logical_and(X, Y).astype(int) est_tartu = pid_tartu.estimate(X, Y, Z) assert np.isclose( 0.5, est_tartu['syn_s1_s2']), ('Synergy is not 0.5. for Tartu estimator.')
def test_non_binary_alphabet(): """Test PID estimators on larger alphabets.""" n = 1000 alph_s1 = 5 alph_s2 = 3 s1 = np.random.randint(0, alph_s1, n) s2 = np.random.randint(0, alph_s2, n) target = s1 + s2 settings = { 'alph_s1': alph_s1, 'alph_s2': alph_s2, 'alph_t': np.unique(target).shape[0], 'max_unsuc_swaps_row_parm': 60, 'num_reps': 63, 'max_iters': 1000 } pid_sydney = SydneyPID(settings) pid_tartu = TartuPID(settings) # Sydney estimator tic = tm.time() est_sydney = pid_sydney.estimate(s1, s2, target) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(s1, s2, target) t_tartu = tm.time() - tic print('\nInt addition - N = 1000') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format(t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) assert np.isclose(est_tartu['syn_s1_s2'], est_sydney['syn_s1_s2'], atol=1e-03), 'Synergies are not equal.' assert np.isclose(est_tartu['shd_s1_s2'], est_sydney['shd_s1_s2'], atol=1e-03), 'Shareds are not equal.' assert np.isclose(est_tartu['unq_s1'], est_sydney['unq_s1'], atol=1e-03), 'Unique1 is not equal.' assert np.isclose(est_tartu['unq_s2'], est_sydney['unq_s2'], atol=1e-03), 'Unique2 is not equal.'
def test_xor_long(): """Test PID estimation with Sydney estimator on XOR with higher N.""" # logical AND n = 1000 alph = 2 s1 = np.random.randint(0, alph, n) s2 = np.random.randint(0, alph, n) target = np.logical_xor(s1, s2).astype(int) settings = { 'alph_s1': alph, 'alph_s2': alph, 'alph_t': alph, 'max_unsuc_swaps_row_parm': 3, 'num_reps': 63, 'max_iters': 10000 } print('\n\nTesting PID estimator on binary XOR, pointset size: {0}, ' 'iterations: {1}'.format(n, settings['max_iters'])) # Sydney estimator pid_sydney = SydneyPID(settings) pid_tartu = TartuPID(settings) tic = tm.time() est_sydney = pid_sydney.estimate(s1, s2, target) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(s1, s2, target) t_tartu = tm.time() - tic print('\nLogical XOR - N = 1000') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format(t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) assert 0.9 < est_sydney['syn_s1_s2'] <= 1.1, ( 'Sydney estimator incorrect synergy: {0}, expected was {1}'.format( est_sydney['syn_s1s2'], 0.98)) assert 0.9 < est_tartu['syn_s1_s2'] <= 1.1, ( 'Tartu estimator incorrect synergy: {0}, expected was {1}'.format( est_sydney['syn_s1s2'], 0.98))
def test_non_binary_alphabet(): """Test PID estimators on larger alphabets.""" n = 1000 alph_s1 = 5 alph_s2 = 3 s1 = np.random.randint(0, alph_s1, n) s2 = np.random.randint(0, alph_s2, n) target = s1 + s2 settings = { 'alph_s1': alph_s1, 'alph_s2': alph_s2, 'alph_t': np.unique(target).shape[0], 'max_unsuc_swaps_row_parm': 60, 'num_reps': 63, 'max_iters': 1000 } pid_sydney = SydneyPID(settings) pid_tartu = TartuPID(settings) # Sydney estimator tic = tm.time() est_sydney = pid_sydney.estimate(s1, s2, target) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(s1, s2, target) t_tartu = tm.time() - tic print('\nInt addition - N = 1000') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format( t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) assert np.isclose(est_tartu['syn_s1_s2'], est_sydney['syn_s1_s2'], atol=1e-03), 'Synergies are not equal.' assert np.isclose(est_tartu['shd_s1_s2'], est_sydney['shd_s1_s2'], atol=1e-03), 'Shareds are not equal.' assert np.isclose(est_tartu['unq_s1'], est_sydney['unq_s1'], atol=1e-03), 'Unique1 is not equal.' assert np.isclose(est_tartu['unq_s2'], est_sydney['unq_s2'], atol=1e-03), 'Unique2 is not equal.'
def test_xor_long(): """Test PID estimation with Sydney estimator on XOR with higher N.""" # logical AND n = 1000 alph = 2 s1 = np.random.randint(0, alph, n) s2 = np.random.randint(0, alph, n) target = np.logical_xor(s1, s2).astype(int) settings = { 'alph_s1': alph, 'alph_s2': alph, 'alph_t': alph, 'max_unsuc_swaps_row_parm': 3, 'num_reps': 63, 'max_iters': 10000 } print('\n\nTesting PID estimator on binary XOR, pointset size: {0}, ' 'iterations: {1}'.format(n, settings['max_iters'])) # Sydney estimator pid_sydney = SydneyPID(settings) pid_tartu = TartuPID(settings) tic = tm.time() est_sydney = pid_sydney.estimate(s1, s2, target) t_sydney = tm.time() - tic # Tartu estimator tic = tm.time() est_tartu = pid_tartu.estimate(s1, s2, target) t_tartu = tm.time() - tic print('\nLogical XOR - N = 1000') print('Estimator Sydney\t\tTartu\n') print('PID evaluation {:.3f} s\t\t{:.3f} s\n'.format( t_sydney, t_tartu)) print('Uni s1 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s1'], est_tartu['unq_s1'])) print('Uni s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['unq_s2'], est_tartu['unq_s2'])) print('Shared s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['shd_s1_s2'], est_tartu['shd_s1_s2'])) print('Synergy s1_s2 {0:.8f}\t\t{1:.8f}'.format( est_sydney['syn_s1_s2'], est_tartu['syn_s1_s2'])) assert 0.9 < est_sydney['syn_s1_s2'] <= 1.1, ( 'Sydney estimator incorrect synergy: {0}, expected was {1}'.format( est_sydney['syn_s1s2'], 0.98)) assert 0.9 < est_tartu['syn_s1_s2'] <= 1.1, ( 'Tartu estimator incorrect synergy: {0}, expected was {1}'.format( est_sydney['syn_s1s2'], 0.98))
cmi = est.estimate(source_cor, target, source_uncor) print('Estimated CMI: {0:.5f}, expected CMI: {1:.5f}'.format(cmi[0], expected_mi)) est = OpenCLKraskovMI(settings) mi = est.estimate(source_cor, target) print('Estimated MI: {0:.5f}, expected MI: {1:.5f}'.format(mi[0], expected_mi)) # Generate binary test data alph_x = 2 alph_y = 2 alph_z = 2 x = np.random.randint(0, alph_x, n) y = np.random.randint(0, alph_y, n) z = np.logical_xor(x, y).astype(int) # PID estimators settings = { 'alph_s1': alph_x, 'alph_s2': alph_y, 'alph_t': alph_z, 'max_unsuc_swaps_row_parm': 60, 'num_reps': 63, 'max_iters': 100} est_sydney = SydneyPID(settings) est_tartu = TartuPID(settings) pid_sydney = est_sydney.estimate(x, y, z) print('Estimated synergy (Sydney): {0:.5f}, expected synergy: ~1'.format( pid_sydney['syn_s1_s2'])) pid_tartu = est_tartu.estimate(x, y, z) print('Estimated synergy (Tartu): {0:.5f}, expected synergy: ~1'.format( pid_tartu['syn_s1_s2']))
cmi[0], expected_mi)) est = OpenCLKraskovMI(settings) mi = est.estimate(source_cor, target) print('Estimated MI: {0:.5f}, expected MI: {1:.5f}'.format(mi[0], expected_mi)) # Generate binary test data alph_x = 2 alph_y = 2 alph_z = 2 x = np.random.randint(0, alph_x, n) y = np.random.randint(0, alph_y, n) z = np.logical_xor(x, y).astype(int) # PID estimators settings = { 'alph_s1': alph_x, 'alph_s2': alph_y, 'alph_t': alph_z, 'max_unsuc_swaps_row_parm': 60, 'num_reps': 63, 'max_iters': 100 } est_sydney = SydneyPID(settings) est_tartu = TartuPID(settings) pid_sydney = est_sydney.estimate(x, y, z) print('Estimated synergy (Sydney): {0:.5f}, expected synergy: ~1'.format( pid_sydney['syn_s1_s2'])) pid_tartu = est_tartu.estimate(x, y, z) print('Estimated synergy (Tartu): {0:.5f}, expected synergy: ~1'.format( pid_tartu['syn_s1_s2']))