diff options
author | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-24 14:58:54 +0100 |
---|---|---|
committer | Daniel Langesten <daniel.langest@gmail.com> | 2015-02-24 14:58:54 +0100 |
commit | e9d77b864c75d81ea7b1796fda0abe024756e8d1 (patch) | |
tree | 037da5f1c9421df604cb7607d885f21d367ecd65 | |
parent | 99776acca9b9315fb71e2170629285df1a15ca08 (diff) |
wrote function for generation of values in a laplace distribution
-rw-r--r-- | diffpriv.go | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/diffpriv.go b/diffpriv.go index 22e844f..8b945d3 100644 --- a/diffpriv.go +++ b/diffpriv.go @@ -1,5 +1,24 @@ package main -func laplaceDist(u, b int) int { - return 4 //chosen by fair laplance weighted diceroll +import ( + "math/rand" +) + +// Returns a random value from a laplace +// distribution with parameters u and b. +func laplaceDist(u, b float64) float64 { + uniform := rand.Float64() + uniform -= 0.5 + return u - b*sgn(uniform)*math.Log(1-2*math.Abs(uniform)) +} + +// The signum function +func sgn(x float64) flot64 { + if x < 0 { + return -1 + } + if x > 0 { + return 1 + } + return 0 } |