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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

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

  • Understanding k-NN Search in Elasticsearch
  • Advanced gRPC in Microservices: Hard-Won Insights and Best Practices
  • Stop Building Monolithic AI Brains, Build a Specialist Team Instead
  • Designing Microservices Architecture With a Custom Spring Boot Starter and Auto-Configuration Framework
  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.5K 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, DZone MVB. 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: