Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/bindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pybind_extension(
"@google_dp//differential_privacy/algorithms:algorithm",
"@google_dp//differential_privacy/algorithms:bounded-mean",
"@google_dp//differential_privacy/algorithms:bounded-sum",
"@google_dp//differential_privacy/algorithms:bounded-standard-deviation",
"@google_dp//differential_privacy/algorithms:count",
"@google_dp//differential_privacy/algorithms:order-statistics",
"@google_dp//differential_privacy/proto:data_cc_proto"
Expand Down
19 changes: 19 additions & 0 deletions src/bindings/PyDP/algorithms/bounded_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class BoundedSumDummy : public Dummy {
}
};

class BoundedStandardDeviationDummy : public Dummy {
public:
using Dummy::Dummy;
double Result(py::list l) override {
return Result_BoundedStandardDeviation(obj, l);
}
};

void declareBoundedMean(py::module& m) {
py::class_<BoundedMeanDummy> bld(m, "BoundedMean");

Expand All @@ -50,7 +58,18 @@ void declareBoundedSum(py::module& m) {
cls.def("result", &BoundedSumDummy::Result);
}

void declareBoundedStandardDeviation(py::module& m) {
py::class_<BoundedStandardDeviationDummy> cls(m, "BoundedStandardDeviation");

cls.def(py::init<double, int, int>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
cls.def(py::init<double>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
cls.def("result", &BoundedStandardDeviationDummy::Result);
}

void init_algorithms_bounded_functions(py::module& m) {
declareBoundedMean(m);
declareBoundedSum(m);
declareBoundedStandardDeviation(m);
}
27 changes: 27 additions & 0 deletions src/bindings/c/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "differential_privacy/algorithms/bounded-mean.h"
#include "differential_privacy/algorithms/bounded-sum.h"
#include "differential_privacy/algorithms/bounded-standard-deviation.h"

#include "absl/random/distributions.h"
#include "differential_privacy/algorithms/order-statistics.h"
Expand Down Expand Up @@ -67,6 +68,32 @@ double Result_BoundedSum(BoundedFunctionHelperObject* config, pybind11::list l)
return GetValue<double>(result);
}

double Result_BoundedStandardDeviation(BoundedFunctionHelperObject* config, pybind11::list l) {
std::vector<double> a;

for (auto i : l) {
a.push_back(i.cast<double>());
}
std::unique_ptr<BoundedStandardDeviation<double>> standard_deviation;

if (has_bounds) {
standard_deviation = BoundedStandardDeviation<double>::Builder()
.SetEpsilon(config->epsilon)
.SetLower(config->lower)
.SetUpper(config->upper)
.Build()
.ValueOrDie();
} else {
standard_deviation = BoundedStandardDeviation<double>::Builder()
.SetEpsilon(config->epsilon)
.Build()
.ValueOrDie();
}
Output result = standard_deviation->Result(a.begin(), a.end()).ValueOrDie();

return GetValue<double>(result);
}

// Order Statistics

// Max
Expand Down
2 changes: 2 additions & 0 deletions src/bindings/c/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern double Result_BoundedMean(BoundedFunctionHelperObject* config, pybind11::

extern double Result_BoundedSum(BoundedFunctionHelperObject* config, pybind11::list a);

extern double Result_BoundedStandardDeviation(BoundedFunctionHelperObject* config, pybind11::list a);

// Order statistics
extern int64_t Result_Max(BoundedFunctionHelperObject* config, pybind11::list a,
double privacy_budget);
Expand Down
12 changes: 12 additions & 0 deletions tests/algorithms/test_bounded_standard_deviation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pydp as dp


class TestBoundedStandardDeviation:
def test_c_api(self):
Comment thread
chinmayshah99 marked this conversation as resolved.
example_data = [1, 5, 7, 9, 13]
epsilon = 1.0
lower_bound, upper_bound = 0, 15
bsd = dp.BoundedStandardDeviation(epsilon, lower_bound, upper_bound)
result = bsd.result(example_data)
assert type(result) is float
assert lower_bound <= result <= upper_bound