def test_sample_without_replacement():
    array = np.ones(100)
    node_single = DataNode()
    node_single.set_private_data(name="array", data=array)
    sample_size = 50

    def u(x, r):
        output = np.zeros(len(r))
        for i in range(len(r)):
            output[i] = r[i] * sum(np.greater_equal(x, r[i]))
        return output

    r = np.arange(0, 3.5, 0.001)
    delta_u = r.max()
    epsilon = 5
    exponential_mechanism = ExponentialMechanism(u,
                                                 r,
                                                 delta_u,
                                                 epsilon,
                                                 size=sample_size)

    access_modes = [LaplaceMechanism(1, 1)]
    access_modes.append(GaussianMechanism(1, (0.5, 0.5)))
    access_modes.append(RandomizedResponseBinary(0.5, 0.5, 1))
    access_modes.append(RandomizedResponseCoins())
    access_modes.append(exponential_mechanism)

    for a in access_modes:
        sampling_method = SampleWithoutReplacement(a, sample_size, array.shape)
        node_single.configure_data_access("array", sampling_method)
        result = node_single.query("array")
        assert result.shape[0] == sample_size
Exemplo n.º 2
0
def test_randomize_binary_mechanism_no_binary():
    array = np.random.rand(1000)
    federated_array = shfl.private.federated_operation.federate_array(
        array, 100)

    federated_array.configure_data_access(
        RandomizedResponseBinary(f0=0.5, f1=0.5, epsilon=1))

    with pytest.raises(ValueError):
        federated_array.query()
Exemplo n.º 3
0
def test_randomize_binary_mechanism_no_binary_scalar():
    scalar = 0.1
    node_single = DataNode()
    node_single.set_private_data(name="scalar", data=scalar)
    data_access_definition = RandomizedResponseBinary(f0=0.5,
                                                      f1=0.5,
                                                      epsilon=1)
    node_single.configure_data_access("scalar", data_access_definition)

    with pytest.raises(ValueError):
        node_single.query("scalar")
Exemplo n.º 4
0
def test_randomize_binary_random_scalar_0():
    scalar = 0
    node_single = DataNode()
    node_single.set_private_data(name="scalar", data=scalar)
    data_access_definition = RandomizedResponseBinary(f0=0.5,
                                                      f1=0.5,
                                                      epsilon=1)
    node_single.configure_data_access("scalar", data_access_definition)

    result = node_single.query(private_property="scalar")

    assert np.isscalar(result)
    assert result == 0 or result == 1
Exemplo n.º 5
0
def test_randomize_binary_mechanism_array_almost_always_false_values_zeros():
    array = np.zeros(1000)
    node_single = DataNode()
    node_single.set_private_data(name="array", data=array)

    # Prob of one given 1 very low, mean should be near 0
    data_access_definition = RandomizedResponseBinary(f0=0.01,
                                                      f1=0.5,
                                                      epsilon=1)
    node_single.configure_data_access("array", data_access_definition)

    result = node_single.query("array")

    assert np.abs(1 - np.mean(result)) < 0.05
Exemplo n.º 6
0
def test_randomize_binary_mechanism_array_almost_always_true_values_ones():
    array = np.ones(1000)
    node_single = DataNode()
    node_single.set_private_data(name="array", data=array)

    # Prob of one given 1 very high, mean should be near 1
    data_access_definition = RandomizedResponseBinary(f0=0.5,
                                                      f1=0.99,
                                                      epsilon=5)
    node_single.configure_data_access("array", data_access_definition)

    result = node_single.query("array")

    assert 1 - np.mean(result) < 0.05
Exemplo n.º 7
0
def test_randomize_binary_random():
    data_size = 100
    array = np.ones(data_size)
    node_single = DataNode()
    node_single.set_private_data(name="A", data=array)
    data_access_definition = RandomizedResponseBinary(f0=0.5,
                                                      f1=0.5,
                                                      epsilon=1)
    node_single.configure_data_access("A", data_access_definition)

    result = node_single.query(private_property="A")

    differences = 0
    for i in range(data_size):
        if result[i] != array[i]:
            differences = differences + 1

    assert 0 < differences < data_size
    assert np.mean(result) < 1
Exemplo n.º 8
0
def test_randomized_response_binary_epsilon_delta():
    randomized_response_binary = RandomizedResponseBinary(f0=0.1,
                                                          f1=0.9,
                                                          epsilon=1)

    assert randomized_response_binary.epsilon_delta is not None
Exemplo n.º 9
0
def test_randomized_response_correctness():
    with pytest.raises(ValueError):
        RandomizedResponseBinary(0.1, 2, epsilon=20)

    with pytest.raises(ValueError):
        RandomizedResponseBinary(0.8, 0.8, epsilon=0.1)
Exemplo n.º 10
0
def test_randomize_binary_deterministic():
    array = np.array([0, 1])
    node_single = DataNode()
    node_single.set_private_data(name="A", data=array)
    with pytest.raises(ValueError):
        RandomizedResponseBinary(f0=1, f1=1, epsilon=1)