LAPLACE DISTRIBUTION
Interactive Statistical Explorer
Key Metrics
5
INTERACTIVE TABS
~1,760
LINES OF CODE
10–10K
SAMPLE RANGE
3
CASE STUDIES
3
EXCESS KURTOSIS
40ms
SLIDER THROTTLE
OVERVIEW
This project is a fully interactive web application built for STA 532 — Applied Statistics at Grand Valley State University. It provides an educational and computational exploration of the Laplace distribution (also known as the Double Exponential distribution) — a probability distribution first introduced in 1774 by Pierre-Simon Laplace as his "First Law of Errors."
The Laplace distribution is the Normal distribution's more rugged cousin. Where the bell curve models errors based on their squared magnitude, the Laplace models errors based on their absolute magnitude, producing two defining characteristics: a sharp cusp at the center (ideal for data that clusters tightly around a specific value) and heavy tails (assigning much higher probability to extreme events than the Normal). These properties make it a cornerstone of robust statistical modeling in fields ranging from finance to data privacy.
The application spans five tabs — About, Applications, PDF, CDF, and Simulation — providing both theoretical grounding and hands-on computational tools. Users can manipulate distribution parameters in real time, compute exact probabilities and quantiles, run Monte Carlo simulations with configurable sample sizes, and compare the Laplace's behavior against the Normal distribution across every visualization. The app is deployed to shinyapps.io and built with approximately 1,760 lines of R, JavaScript, and CSS.
INTERACTIVE TOOLS
The core of the application is a suite of interactive visualization and computation tools spread across the PDF and CDF tabs.
PDF Explorer — The PDF tab renders the Laplace probability density function in real time as users adjust the location parameter (µ, range −10 to 10) and scale parameter (b, range 0.1 to 5). A toggleable Normal distribution overlay plots a Normal curve with identical mean and variance (σ² = 2b²), making the Laplace's sharp peak and heavy tails immediately visible by direct comparison. The x-axis range is independently adjustable (−30 to 30), and four stat cards display the theoretical mean, variance (2b²), excess kurtosis (always 3), and skewness (always 0) — all updating reactively as parameters change.
CDF with Probability Calculator — The CDF tab visualizes the cumulative distribution function with the same parameter controls. An integrated probability calculator evaluates P(X ≤ x) at any user-specified x value, displaying the result to six decimal places with corresponding dashed crosshair annotations on the CDF plot. A quantile calculator (inverse CDF) takes a probability p and returns the x value satisfying P(X ≤ x) = p, using nimble's qdexp function.
Synchronized Shaded-Area Plot — Below the CDF curve, a second PDF plot renders in sync, shading the area under the density curve up to the user-specified x value. This dual visualization connects the abstract CDF value to its geometric interpretation on the density — users see both the cumulative probability and the corresponding shaded region simultaneously.
All plots are rendered as Plotly interactive charts via ggplotly, supporting hover tooltips with unified hover mode, pan/zoom, and PNG export. Slider inputs use a custom 40ms throttle (overriding Shiny's default release-only behavior) via a JavaScript binding override, enabling smooth real-time parameter exploration without waiting for slider release.
SIMULATION ENGINE
The Simulation tab provides a full Monte Carlo sampling engine for empirical exploration of the Laplace distribution.
Configurable Sampling — Users set the sample size (n = 10 to 10,000, step 10), location and scale parameters, and an optional random seed for reproducibility. Clicking "Generate Sample" draws n observations from the Laplace distribution using nimble's rdexp function and renders a histogram overlaid with the theoretical density curve. As n increases, the histogram visibly converges to the theoretical Laplace PDF — a direct demonstration of the Law of Large Numbers.
Normal Overlay Comparison — A toggleable Normal distribution overlay (with matched mean and variance) can be superimposed on the simulation histogram, allowing users to compare the empirical Laplace sample against both the theoretical Laplace curve and the equivalent Normal density simultaneously.
Summary Statistics Panel — Seven real-time summary statistics are computed from each generated sample: n, mean, median, standard deviation, kurtosis (via the moments package), minimum, and maximum. These values update with every new sample, letting users observe sampling variability and track convergence of empirical moments toward their theoretical values.
Heavy-Tail Comparison — The most distinctive feature of the simulation tab is a four-panel comparison grid that quantifies the Laplace distribution's fat-tail property with real data. For each sample, the application computes the empirical percentage of observations falling beyond 2 standard deviations and 3 standard deviations from the mean, displayed alongside the Normal distribution's theoretical expectations (4.55% and 0.27%, respectively). With typical Laplace samples, users observe empirical values roughly double the Normal expectation at 2σ and significantly higher at 3σ — providing visceral, data-driven evidence of heavy-tail behavior.
REAL-WORLD APPLICATIONS
The Applications tab presents three curated case studies demonstrating where the Laplace distribution outperforms Normal-based models in practice. Each case study follows a consistent structure — The Problem, The Laplace Solution, and a concrete Case Study — rendered in a responsive card grid with hover effects.
Finance & Black Swans — Traditional financial models use the Normal distribution, which treats extreme market moves as nearly impossible events. In reality, stock returns exhibit heavy tails — crashes and spikes occur far more frequently than a bell curve predicts. Analysts use the Laplace distribution to model asset returns and "volatility smiles," enabling banks to calculate more realistic Value-at-Risk (VaR) estimates that provide adequate capital buffers for Black Swan events.
Machine Learning & Sparsity (LASSO) — When building models with thousands of variables, identifying which features actually matter is critical. LASSO regression uses a Laplace prior (L1 penalty), exploiting the distribution's sharp, needle-like peak at zero to mathematically force unimportant variable coefficients to exactly zero. This produces sparse models that perform automatic feature selection — enabling researchers to identify the handful of critical genes in a genomic study or the most important indicators in an economic forecast.
Data Privacy & The Laplace Mechanism — The cornerstone of differential privacy is the Laplace Mechanism: adding carefully calibrated Laplace noise to database query results. Because of the Laplace distribution's unique mathematical properties, this provides a formal privacy guarantee — the result of any query looks essentially the same whether a specific individual's data is included or not. This technique protects individual privacy in large-scale census data, tech-company analytics, and healthcare trend reporting while preserving aggregate statistical accuracy.
The tab also features a summary infographic synthesizing the distribution's key properties and applications into a single visual reference.
DESIGN & ENGINEERING
Significant engineering effort went into making the application feel polished and responsive rather than a typical academic Shiny prototype.
Viewport-Locked Layout — All interactive tabs (PDF, CDF, Simulation) are locked to the browser viewport with no page scrolling. A custom JavaScript function (resizePlots) dynamically calculates available height by subtracting the navbar, formula callouts, stat cards, and padding from the viewport height, then resizes each Plotly container to fill the remaining space. This function fires on window resize, tab switch (via Bootstrap's shown.bs.tab event), and after every Shiny render cycle — ensuring plots always fill the screen regardless of browser dimensions.
Animated Splash Screen — The app opens with a full-screen splash overlay featuring a CSS keyframe entrance animation (fade + scale from 88% with a 15px vertical offset over 0.9s using a custom cubic-bezier easing) followed by a 5-second display period, then a cinematic exit animation (subtle brightness flash at 8%, progressive zoom-blur dissolve over 1.3s with the blur reaching 20px at full opacity fadeout). The overlay is removed from the DOM after 6.5 seconds via setTimeout.
Bootstrap 5 Theming — The UI is built with bslib (not shinydashboard), using Bootstrap 5 with a warm color palette (primary: #E8713A, background: #FDF6EE, foreground: #1a1a2e) and Google Fonts (Inter for body text, Fira Code for monospaced elements). Custom CSS extends the theme with card hover effects (translateY lift + shadow intensification), scrollable sidebars with styled scrollbars, responsive grid breakpoints, and consistent stat card styling.
Throttled Slider Input — Shiny's default slider behavior only sends values on mouse release. A custom JavaScript snippet overrides the shiny.sliderInput binding's getRatePolicy method, replacing it with a 40ms throttle policy. This enables smooth, real-time parameter exploration — users see the plot update continuously as they drag, not just when they release.
Pre-Rendered Hidden Tabs — By default, Shiny suspends outputs on hidden tabs to save computation. This app sets suspendWhenHidden = FALSE for all four plot outputs and overrides Bootstrap's tab hiding with CSS (display: block with opacity/pointer-events toggling instead of display: none), ensuring all Plotly charts are pre-rendered with correct dimensions when the user first switches tabs.
Responsive Design — The CSS includes breakpoints at 992px, 768px, and 480px that reflow card grids, adjust stat card sizing, and collapse multi-column layouts to single columns on mobile devices.
MATHEMATICAL FOUNDATION
The application renders all mathematical content through MathJax, providing publication-quality formula display inline with the interactive tools.
PDF Formula — The probability density function is displayed as: f(x | µ, b) = (1/2b) · exp(−|x − µ|/b), defined for all real x. The formula is rendered in a callout box directly above the interactive plot, providing immediate reference as users manipulate parameters.
CDF Formula — The piecewise cumulative distribution function is rendered with cases notation: F(x) = (1/2) · exp((x − µ)/b) for x < µ, and F(x) = 1 − (1/2) · exp(−(x − µ)/b) for x ≥ µ. This piecewise structure reflects the Laplace's construction as two back-to-back exponential distributions.
Key Properties — The stat cards and educational content highlight the Laplace distribution's defining characteristics:
- Mean = µ (the location parameter directly controls the center)
- Variance = 2b² (grows quadratically with the scale parameter)
- Excess Kurtosis = 3 (constant, independent of parameters — compared to 0 for the Normal, indicating much heavier tails)
- Skewness = 0 (perfectly symmetric about µ, like the Normal)
nimble Package Integration — The Laplace distribution functions (ddexp, pdexp, qdexp, rdexp — density, CDF, quantile, and random generation) are not available in base R. The application uses the nimble package, which provides these as part of its extended distribution library. This was a deliberate technical choice that enabled clean, consistent computation across all tabs without reimplementing the distribution functions from scratch.
Normal Comparison — When the Normal overlay is enabled, the application constructs a Normal distribution with identical mean (µ) and variance (2b²), yielding σ = b√2. This matched-moment comparison isolates the distributional shape difference — the Laplace's sharper peak and heavier tails become immediately apparent, even though both distributions share the same first two moments.
Tech Stack
Details
Team
Steve Meadows
Course
STA 532 — Applied Statistics
Timeline
Fall 2024