Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variance in nada-numpy #75

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions nada_numpy/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nada Numpy also supports SecretRational types. The function should also work for Rational types. You can probably look into the implementation of other functions to check for this.


# compute (x_i - mean)^2

for v in self.inner:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should profit from broadcasting here. The function as it is, may not be working as you expect.

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.
Expand Down
16 changes: 16 additions & 0 deletions nada_numpy/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"gelu",
"silu",
"shuffle",
"var",
]


Expand Down Expand Up @@ -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()


6 changes: 5 additions & 1 deletion tests/nada-tests/nada-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,8 @@ prime_size = 128

[[programs]]
path = "src/shuffle.py"
prime_size = 128
prime_size = 128

[[programs]]
path = "src/variance.py"
prime_size = 128
16 changes: 16 additions & 0 deletions tests/nada-tests/src/variance.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nada arrays do not require re-initialization of each of the entries. na.array already does the initialization of the SecretIntegers within it.


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")
9 changes: 9 additions & 0 deletions tests/nada-tests/tests/variance.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is correct but doesn't check for other behaviors of the function and other datatypes. You should add the check for other variance values (!= 0) and for other datatypes (Rational, SecretRational).

Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"shuffle",
"array_comparison",
"private_inverse",
"var",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should match the name of the Python executable, thus variance

]

EXAMPLES = [
Expand Down
Loading