0

I want to create a web-based demonstration of differential privacy. To do this, I need a JavaScript implementation of the Laplace noise distribution.

I've been unable to find basic noise distributions in JavaScript, such as Gaussian. This is surprising, because I would like that there would be lots of cool JavaScript demos showing how the distribution builds up, draw by draw, to produce a pretty bell-shaped curve.

How do I generate a Laplace noise distribution in Javascript?

vy32
  • 28,461
  • 37
  • 122
  • 246
  • One of the first links when searching your title – mplungjan Sep 17 '17 at 11:34
  • Normal distribution is not the same as Laplace distribution. – vy32 Sep 17 '17 at 20:04
  • Did you look? 3 or 4 different distribution versions - 2 or more Gaussian – mplungjan Sep 17 '17 at 20:11
  • Hi. I did look. Laplace distribution is not a normal or Gaussian distribution. I want a Laplace distribution. – vy32 Sep 17 '17 at 22:04
  • Likelly a duplicate of https://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve but OP says not. – mplungjan Sep 18 '17 at 04:11
  • Not a duplicate, because https://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve is a normal distribution, and this is a asking about a laplace distribution, which are different distributions. – vy32 Nov 11 '17 at 19:03

1 Answers1

1
       function sgn(x) {
            return x < 0 ? -1 : 1;
        }

        // From wikipedia:
        // Lap(X) = mu - b sgn(U) ln (1-2|U|) where U is a random variable between -0.5 and 0.5
        function laplace(mu, b) {
            var U = Math.random() - 0.5;
            return mu - (b * sgn(U) * Math.log(1 - 2* Math.abs(U)));
        }

        function privatize(F,deltaF,epsilon) {
            return F + laplace(0.0, deltaF/epsilon);
        }
vy32
  • 28,461
  • 37
  • 122
  • 246