diff options
| author | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-24 15:35:04 +0100 |
|---|---|---|
| committer | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-24 15:35:04 +0100 |
| commit | 2a9098403281156b2cca1085f5bff3417942dbda (patch) | |
| tree | b2f6d5d09daf0bbb876c0eee49f8a8b1087199f9 | |
| parent | aaeac2f403d5e96e483c672f9f063a6e0f665caf (diff) | |
now it can add noise of a laplace distribution
| -rw-r--r-- | diffpriv.go | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/diffpriv.go b/diffpriv.go index 8b945d3..f35dd9a 100644 --- a/diffpriv.go +++ b/diffpriv.go @@ -1,19 +1,36 @@ package main import ( + "fmt" + "math" "math/rand" + "time" ) +var ( + rnd = rand.New(rand.NewSource(time.Now().UnixNano())) +) + +func main() { + fmt.Println(diffpriv(0, 1, 2)) +} + +func diffpriv(value, sensitivity, epsilon float64) float64 { + noise := laplaceDist(0, sensitivity/epsilon) + fmt.Println("noise: ", noise) + return round(float64(value) + noise) +} + // Returns a random value from a laplace // distribution with parameters u and b. func laplaceDist(u, b float64) float64 { - uniform := rand.Float64() + uniform := rnd.Float64() uniform -= 0.5 return u - b*sgn(uniform)*math.Log(1-2*math.Abs(uniform)) } // The signum function -func sgn(x float64) flot64 { +func sgn(x float64) float64 { if x < 0 { return -1 } @@ -22,3 +39,10 @@ func sgn(x float64) flot64 { } return 0 } + +func round(n float64) float64 { + if n < 0 { + return math.Ceil(n - 0.5) + } + return math.Floor(n + 0.5) +} |
