Fulgurance is my C++ statistics library where I reimplemented several probability distributions from scratch, including the Cauchy distribution. The goal of Fulgurance is to provide flexible statistical tools in modern C++, without relying heavily on external libraries. It’s also a playground to explore how distributions work internally, both mathematically and computationally.
The Cauchy distribution is a well-known heavy-tailed distribution, often used in physics, Bayesian inference, and robustness testing. Unlike the normal distribution, its mean and variance are undefined — which makes it a good candidate to test statistical implementations. Rebuilding it from scratch in C++ was a great exercise in both numerical computation and random number generation.
dcauchy
— Density FunctionThe probability density function (PDF) of the Cauchy distribution is:
f(x) = 1 / (scale · π · (1 + ((x - location)/scale)²))
In Fulgurance:
std::vector<double> dcauchy(std::vector<double> &x,
double location = 0,
double scale = 1);
This returns the probability density values for each element in the vector x
.
pcauchy
— Cumulative Distribution FunctionThe cumulative distribution function (CDF) of the Cauchy distribution can be written in closed form using the arctangent. I implemented it as:
std::vector<double> pcauchy(std::vector<double> &x,
double location = 0,
double scale = 1,
double step = 0.01);
The formula used is:
F(x) = (1/π) · atan((x − location)/scale) + 0.5
I also normalize relative to the first value of the input vector, so results can be interpreted as probabilities over the input set.
qcauchy
— Quantile FunctionThe quantile (inverse CDF) is useful for generating samples or computing thresholds. It is defined as:
Q(p) = location + scale · tan(π(p − 0.5))
In Fulgurance:
std::vector<double> qcauchy(std::vector<double> &p,
double location = 0,
double scale = 1);
It takes a vector of probabilities p
and returns the corresponding quantile values.
rcauchy
— Random Number GenerationThis one was more fun (and a little hacky 😅). I wanted to generate pseudo-random numbers following a Cauchy distribution without relying directly on the standard library’s random engines. So I improvised by using:
std::chrono
)std::this_thread::sleep_for
calls to add entropytan()
transform to map uniform-like values into a Cauchy distributionThe implementation is:
std::vector<double> rcauchy(unsigned int n,
double location = 0,
double scale = 1);
It generates n
values distributed approximately like Cauchy(location
, scale
).
rcauchy
is not a cryptographically secure or statistically rigorous RNG — it’s a custom experiment to simulate randomness.std::cauchy_distribution
from <random>
.Reimplementing the Cauchy distribution in Fulgurance was both a math refresher and a programming challenge. I now have my own versions of:
dcauchy
— density functionpcauchy
— cumulative distribution functionqcauchy
— quantile functionrcauchy
— random number generatorAnd this is only one example: I’ve implemented dozens of other statistical laws in Fulgurance, all available here:
👉 GitHub Repository: Fulgurance
Even though C++ already provides some distributions in <random>
, reimplementing them from scratch gave me a much deeper understanding of probability distributions, and was a fun addition to Fulgurance.