def test_bias_influence_array(self): """ This method tests the influence of the bias array. The results should be different. """ n = 8 k = 4 mu = 1 sigma = 0.5 challenges = tools.all_inputs(n) weight_array = NoisyLTFArray.normal_weights( n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556)) bias_array = NoisyLTFArray.normal_weights( 1, k, mu=mu, sigma=sigma * 2, random_instance=RandomState(0xBADAFF1)) biased_ltf_array = NoisyLTFArray( weight_array=weight_array, transform=NoisyLTFArray.transform_id, combiner=NoisyLTFArray.combiner_xor, sigma_noise=sigma, bias=bias_array, ) ltf_array = NoisyLTFArray( weight_array=weight_array, transform=NoisyLTFArray.transform_id, combiner=NoisyLTFArray.combiner_xor, sigma_noise=sigma, bias=None, ) self.assertEqual(ltf_array.weight_array.shape, biased_ltf_array.weight_array.shape) bias_array = biased_ltf_array.weight_array[:, -1] bias_array_compared = [ bias == bias_array[0] for bias in bias_array[1:] ] # the bias values should be different for this test. It is possible that they are all equal but this chance is # low. self.assertFalse(array(bias_array_compared).all()) biased_responses = biased_ltf_array.eval(challenges) responses = ltf_array.eval(challenges) # The arithmetic mean of the res self.assertFalse(array_equal(biased_responses, responses))
def test_reliability(self): """This method tests the test_reliability calculation.""" n = 8 k = 8 N = 2**n transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instance = LTFArray( weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1)), transform=transformation, combiner=combiner, ) challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB1A)) reliabilities = [] for challenge in challenges: reliabilities.append( PropertyTest.reliability(instance, reshape(challenge, (1, n)))) # For noiseless simulations the responses are always the same hence the reliability is 0% assert_array_equal(reliabilities, repeat(0.0, N)) noisy_instance = NoisyLTFArray( weight_array=NoisyLTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1)), transform=transformation, combiner=combiner, sigma_noise=15.0, random_instance=RandomState(0x5015E), ) for challenge in challenges: reliability = PropertyTest.reliability(noisy_instance, reshape(challenge, (1, n))) # For noisy simulations the responses should vary self.assertNotEqual(reliability, 0.0)
def example_reliability(): """This method shows how to use the PropertyTest.reliability function.""" n = 8 k = 8 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instance = NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights(n, 0.5)) challenge = array([-1, 1, 1, 1, -1, 1, 1, 1]) reliability = PropertyTest.reliability(instance, reshape(challenge, (1, n))) print('The reliability is {}.'.format(reliability))
def test_reliability_set(self): """This method tests the reliability_statistic calculation.""" n = 8 k = 3 N = 2**n measurements = 10 transformation = LTFArray.transform_id combiner = LTFArray.combiner_xor instances = [] instance_count = 3 for i in range(instance_count): instance = LTFArray( weight_array=LTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner, ) instances.append(instance) challenges = sample_inputs(n, N, random_instance=RandomState(0xFAB0)) reliability_set = PropertyTest.reliability_set( instances, challenges, measurements=measurements) # The result is an array like with N * k entries. self.assertEqual(len(reliability_set), N * instance_count) # For noiseless simulations the all reliabilities must be 0% assert_array_equal(reliability_set, repeat(0.0, N * instance_count)) noisy_instances = [] for i in range(instance_count): noisy_instance = NoisyLTFArray( weight_array=NoisyLTFArray.normal_weights( n=n, k=k, random_instance=RandomState(0xA1A1 + i)), transform=transformation, combiner=combiner, sigma_noise=0.5, random_instance=RandomState(0x5015C + i), ) noisy_instances.append(noisy_instance) noisy_reliability_set = PropertyTest.reliability_set( noisy_instances, challenges, measurements=measurements) # For a noisy simulation the mean reliability must differ from zero self.assertNotEqual(mean(noisy_reliability_set), 0.0)
def test_bias_influence_value(self): """ This method tests the influence of the bias value. The results should be different. """ n = 8 k = 4 mu = 1 sigma = 0.5 challenges = array(list(tools.all_inputs(n))) weight_array = NoisyLTFArray.normal_weights(n, k, mu=mu, sigma=sigma, random_instance=RandomState(0xBADA556)) bias_value = 2.5 biased_ltf_array = NoisyLTFArray( weight_array=weight_array, transform=NoisyLTFArray.transform_id, combiner=NoisyLTFArray.combiner_xor, sigma_noise=sigma, bias=bias_value, ) ltf_array = NoisyLTFArray( weight_array=weight_array, transform=NoisyLTFArray.transform_id, combiner=NoisyLTFArray.combiner_xor, sigma_noise=sigma, bias=None, ) # the second dimension of the weight_array plus one must be the number of elements in biased weight_array self.assertEqual(shape(ltf_array.weight_array)[1]+1, shape(biased_ltf_array.weight_array)[1]) bias_array = biased_ltf_array.weight_array[:, -1] bias_array_compared = [bias == bias_array[0] for bias in bias_array] # the bias values should be equal for this test. self.assertTrue(array(list(bias_array_compared)).all()) biased_responses = biased_ltf_array.eval(challenges) responses = ltf_array.eval(challenges) # The arithmetic mean of the res self.assertFalse(array_equal(biased_responses, responses))
def example_uniqueness(): """ This method shows the function which can be used to calculate the uniqueness of a set of simulation instances. """ n = 8 k = 1 instance_count = 3 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instances = [ NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights( n, weights)) for _ in range(instance_count) ] challenge = array([-1, 1, 1, 1, -1, 1, 1, 1]) uniqueness = PropertyTest.uniqueness(instances, reshape(challenge, (1, n))) print('The uniqueness is {}.'.format(uniqueness))
def example_reliability_statistic(): """This method shows hot to use the PropertyTest.reliability_statistic.""" n = 8 k = 1 N = 2**n instance_count = 3 measurements = 100 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instances = [ NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights(n, 0.5)) for _ in range(instance_count) ] challenges = array(list(sample_inputs(n, N))) property_test = PropertyTest(instances) reliability_statistic = property_test.reliability_statistic( challenges, measurements=measurements) print('The reliability statistic is {}.'.format(reliability_statistic))
def example_uniqueness_statistic(): """This method shows the uniqueness statistic function.""" n = 8 k = 1 N = 2**n instance_count = 11 measurements = 1 transformation = NoisyLTFArray.transform_id combiner = NoisyLTFArray.combiner_xor weights = NoisyLTFArray.normal_weights(n=n, k=k) instances = [ NoisyLTFArray( weight_array=weights, transform=transformation, combiner=combiner, sigma_noise=NoisyLTFArray.sigma_noise_from_random_weights( n, weights)) for _ in range(instance_count) ] challenges = array(list(sample_inputs(n, N))) property_test = PropertyTest(instances) uniqueness_statistic = property_test.uniqueness_statistic( challenges, measurements=measurements) print('The uniqueness statistic is {}.'.format(uniqueness_statistic))