From 5c5a999aa023fe6e11af2afb8039482a901c4903 Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:46:30 +0100 Subject: [PATCH 1/6] add: variance function --- nada_numpy/array.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/nada_numpy/array.py b/nada_numpy/array.py index 5334f8f..8c963a4 100644 --- a/nada_numpy/array.py +++ b/nada_numpy/array.py @@ -948,6 +948,31 @@ def test_shuffle_randomness(vector_size, num_shuffles): return arr + def var(self) -> "NadaArray": + """ + Compute the variable of array elements. + + Returns: + nadaarray: the variance of array elements + """ + + # Compute the mean + m = self.mean() + + # Initialise the sum to 0 + sum = Integer(0) + + # compute (x_i - mean)^2 + + for v in self.inner: + diff = v - m + sum += (diff * diff) + + # compute variance + var = sum / Integer(self.size) + + return NadaArray(np.array([var])) + def __len__(self): """ Overrides the default behavior of returning the length of the object. From 9c8c17ea1384bb5ef7e411213132316dfabb4b42 Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:46:55 +0100 Subject: [PATCH 2/6] add: variance wrapper --- nada_numpy/funcs.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nada_numpy/funcs.py b/nada_numpy/funcs.py index dc06087..494b6c0 100644 --- a/nada_numpy/funcs.py +++ b/nada_numpy/funcs.py @@ -78,6 +78,7 @@ "gelu", "silu", "shuffle", + "var", ] @@ -1290,3 +1291,18 @@ def test_shuffle_randomness(vector_size, num_shuffles): ``` """ return arr.shuffle() + +def var(array: NadaArray) -> NadaArray: + """ + Computes the variance of the array elements + + args: + array (nadaarray): input array + + returns: + nadaarray: the variance of the array elements + """ + + return array.var() + + From 491020d3bb820e2adb39901dbeeacc66df09d9cd Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:48:14 +0100 Subject: [PATCH 3/6] add variance script to nada-project.toml --- tests/nada-tests/nada-project.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/nada-tests/nada-project.toml b/tests/nada-tests/nada-project.toml index 6292beb..8449801 100644 --- a/tests/nada-tests/nada-project.toml +++ b/tests/nada-tests/nada-project.toml @@ -200,4 +200,8 @@ prime_size = 128 [[programs]] path = "src/shuffle.py" -prime_size = 128 \ No newline at end of file +prime_size = 128 + +[[programs]] +path = "src/variance.py" +prime_size = 128 From d000a680ada9d3baa6c5f0fc17e22fb0ad84b864 Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:49:18 +0100 Subject: [PATCH 4/6] add variance test --- tests/nada-tests/src/variance.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/nada-tests/src/variance.py diff --git a/tests/nada-tests/src/variance.py b/tests/nada-tests/src/variance.py new file mode 100644 index 0000000..0bb1cac --- /dev/null +++ b/tests/nada-tests/src/variance.py @@ -0,0 +1,16 @@ +from nada_dsl import * +import nada_numpy as na + +def nada_main(): + parties = na.parties(4) + + a = na.array([4], parties[0], "A", SecretInteger) + + a[0] = SecretInteger(Input("A_0", parties[0])) + a[1] = SecretInteger(Input("A_1", parties[1])) + a[2] = SecretInteger(Input("A_2", parties[2])) + a[3] = SecretInteger(Input("A_3", parties[3])) + + var = a.var() + + return na.output(var, parties[1], "my_output") From df376529fbe9e311ba89fce6a7b78e56335121b2 Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:50:19 +0100 Subject: [PATCH 5/6] add: test results generated using `nada generate-test --test-name variance variance` --- tests/nada-tests/tests/variance.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/nada-tests/tests/variance.yaml diff --git a/tests/nada-tests/tests/variance.yaml b/tests/nada-tests/tests/variance.yaml new file mode 100644 index 0000000..4e76edd --- /dev/null +++ b/tests/nada-tests/tests/variance.yaml @@ -0,0 +1,9 @@ +--- +program: variance +inputs: + A_0: 3 + A_1: 3 + A_2: 3 + A_3: 3 +expected_outputs: + my_output_0: 0 From ddab90c67b41a6f23f27fda81f45d44768becb0c Mon Sep 17 00:00:00 2001 From: Muhammed Abiola <147069270+mx750@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:51:15 +0100 Subject: [PATCH 6/6] add var to all tests --- tests/test_all.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_all.py b/tests/test_all.py index 205de46..8edac4e 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -57,6 +57,7 @@ "shuffle", "array_comparison", "private_inverse", + "var", ] EXAMPLES = [