This gallery example demonstrates visualization of multiple statistical distributions using Plotly.cpp. It combines theoretical probability density functions with empirical histogram data to showcase both continuous distributions and sample-based analysis.
#include <cmath>
#include <random>
#include <vector>
auto normalPDF(
double x,
double mean,
double stddev) ->
double {
double variance = stddev * stddev;
return (1.0 / std::sqrt(2 * M_PI * variance)) *
std::exp(-0.5 * std::pow(x - mean, 2) / variance);
}
return (x >= 0) ? lambda * std::exp(-lambda * x) : 0.0;
}
auto gammaPDF(
double x,
double shape,
double scale) ->
double {
if (x <= 0)
return 0.0;
return std::pow(x, shape - 1) * std::exp(-x / scale) /
(std::tgamma(shape) * std::pow(scale, shape));
}
auto main(
int argc,
char *argv[]) ->
int {
std::vector<double> normalY, exponentialY, gammaY;
normalY.reserve(x.size());
exponentialY.reserve(x.size());
gammaY.reserve(x.size());
for (const auto &xi : x) {
gammaY.push_back(
gammaPDF(xi, 2.0, 1.5));
}
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> normalDist(2.0, 1.0);
const int numSamples = 1000;
std::vector<double> normalSamples;
normalSamples.reserve(numSamples);
for (int i = 0; i < numSamples; i++) {
normalSamples.push_back(normalDist(gen));
}
{"x", x},
{"y", normalY},
{"mode", "lines"},
{"name", "Normal(μ=2, σ=1)"},
{"line", {{"color", "blue"}, {"width", 3}}}};
{"type", "scatter"},
{"x", x},
{"y", exponentialY},
{"mode", "lines"},
{"name", "Exponential(λ=0.5)"},
{"line", {{"color", "red"}, {"width", 3}}}};
{"x", x},
{"y", gammaY},
{"mode", "lines"},
{"name", "Gamma(k=2, θ=1.5)"},
{"line", {{"color", "green"}, {"width", 3}}}};
{"x", normalSamples},
{"name", "Normal Samples (n=1000)"},
{"opacity", 0.4},
{"marker", {{"color", "lightblue"}}},
{"yaxis", "y2"},
{"histnorm", "probability density"}};
{{"text", "Statistical Distributions Comparison"},
{"font", {{"size", 18}}}}},
{"xaxis", {{"title", "x"}, {"showgrid", true}}},
{"yaxis",
{{"title", "Probability Density Function"},
{"showgrid", true},
{"domain", {0.0, 0.7}}}},
{"yaxis2",
{{"title", "Sample Frequency"},
{"domain", {0.75, 1.0}},
{"side", "right"}}},
{"width", 900},
{"height", 700},
{"showlegend", true},
{"legend", {{"x", 0.7}, {"y", 0.9}}}};
std::vector<plotly::Object> data = {normalTrace, exponentialTrace, gammaTrace,
histogramTrace};
if (!args.headless) {
} else {
{"width", 900},
{"height", 700},
{"filename", "statistical_distributions"}};
}
return 0;
}
auto parseGalleryArgs(int argc, char *argv[]) -> GalleryArgs
Parse command line arguments for gallery examples.
Definition arg_parser.cpp:4
auto main() -> int
Definition gallery_animate_sin_wave.cpp:48
auto gammaPDF(double x, double shape, double scale) -> double
Definition gallery_statistical_distributions.cpp:77
auto normalPDF(double x, double mean, double stddev) -> double
Definition gallery_statistical_distributions.cpp:67
auto exponentialPDF(double x, double lambda) -> double
Definition gallery_statistical_distributions.cpp:73
auto linspace(double a, double b, int n) -> std::vector< double >
Create a linearly spaced vector of values between two endpoints.
Definition linspace.cpp:4
nlohmann::json Object
Definition plotly.hpp:26
Public Plotly C++ API header.