DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Parent Document Retrieval (PDR): Useful Technique in RAG
  • Best Python Libraries for Web Scraping
  • What Is API-First?
  • Best Ways to Learn Python From Scratch

Trending

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  1. DZone
  2. Coding
  3. Languages
  4. Generating Laplace Distributed Random Values

Generating Laplace Distributed Random Values

Although it's simple to generate Laplacian random values, the Laplace distribution is not always one of the built-in options for random number generation libraries.

By 
John Cook user avatar
John Cook
·
Mar. 14, 18 · Analysis
Likes (3)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

Differential privacy adds Laplace-distributed random noise to data to protect individual privacy. (More on that here.) Although it's simple to generate Laplacian random values, the Laplace distribution is not always one of the built-in options for random number generation libraries.

The Laplace distribution with scale β has density:

The Laplace distribution is also called the double exponential because it looks like two mirror-image exponential distributions glued together.

Note that the scale β is not the standard deviation. The standard deviation is √2 β.

To generate samples from a Laplace distribution with scale β, generate two independent exponential samples with mean β and return their difference.

If you don't have an API for generating exponential random values, generate uniform random values and return the negative of the log. That will produce exponential values with mean 1. To make random values with mean β, just multiply the results by β.

If you want to generate Laplace values in Python, you could simply use the laplace function in scipy.stats. But I'll write a generator from scratch just to show what you might do in another environment where you didn't have exponential or Laplace generators.

    from math import log
    from random import random

    def exp_sample(mean): 
        return -mean*log(random())

    def laplace(scale):
        e1 = exp_sample(scale)
        e2 = exp_sample(scale)
        return e1 - e2

Related: Stand-alone numerical code is useful when you need a few common mathematical functions but are in an environment that doesn't provide them or when you want to avoid adding a library to your project.

Distribution (differential geometry) Random number generation Differential privacy Deviation (statistics) Library Python (language) Data (computing) API Scratch (programming language)

Published at DZone with permission of John Cook. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Parent Document Retrieval (PDR): Useful Technique in RAG
  • Best Python Libraries for Web Scraping
  • What Is API-First?
  • Best Ways to Learn Python From Scratch

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook