The density distribution is a non-parametric distribution defined by the values of its probability density function at a set of points. This is useful for representing a distribution which is known only through its density, such as a kernel density estimate, a posterior evaluated over a grid, or a density obtained numerically.
The density is linearly interpolated between the given values, and is zero beyond them. All other properties of the distribution are computed exactly from that interpolation.
dist_density(x, density)We recommend reading this documentation on pkgdown which renders math nicely. https://pkg.mitchelloharawild.com/distributional/reference/dist_density.html
In the following, let \(X\) be a random variable with density \(f_1, f_2, \ldots, f_n\) given at the values \(x_1 < x_2 < \cdots < x_n\). Write \(w_i = x_{i+1} - x_i\) for the width of the \(i\)th interval, and \(s_i = (f_{i+1} - f_i) / w_i\) for the slope of the density over it.
Support: \([x_1, x_n]\)
Probability density function (p.d.f): Linear interpolation of the given values, standardised to integrate to one:
$$ f(t) = f_i + (t - x_i) s_i \quad \text{for } x_i \le t \le x_{i+1} $$
and \(f(t) = 0\) for \(t < x_1\) or \(t > x_n\).
Cumulative distribution function (c.d.f): The exact integral of the interpolated density, which is piecewise quadratic. With \(h = t - x_i\) and \(F_i = F(x_i)\),
$$ F(t) = F_i + f_i h + \frac{s_i h^2}{2} $$
Quantile function: The inverse of the above, obtained by solving the quadratic within the interval containing the requested probability.
Mean: Computed exactly from the interpolated density,
$$ E(X) = \sum_{i=1}^{n-1} \left[ x_i m_i + w_i^2 \left( \frac{f_i}{6} + \frac{f_{i+1}}{3} \right) \right] $$
where \(m_i = w_i (f_i + f_{i+1}) / 2\) is the probability of the \(i\)th interval.
Variance, skewness, and excess kurtosis: Computed from the central moments \(\mu_k = E[(X - E(X))^k]\). Writing \(c_i = x_i - E(X)\),
$$ \mu_2 = \sum_{i=1}^{n-1} \left[ c_i^2 m_i + \frac{c_i w_i^2 (f_i + 2 f_{i+1})}{3} + \frac{w_i^3 (f_i + 3 f_{i+1})}{12} \right] $$
$$ \mu_3 = \sum_{i=1}^{n-1} \left[ c_i^3 m_i + \frac{c_i^2 w_i^2 (f_i + 2 f_{i+1})}{2} + \frac{c_i w_i^3 (f_i + 3 f_{i+1})}{4} + \frac{w_i^4 (f_i + 4 f_{i+1})}{20} \right] $$
$$ \mu_4 = \sum_{i=1}^{n-1} \left[ c_i^4 m_i + \frac{2 c_i^3 w_i^2 (f_i + 2 f_{i+1})}{3} + \frac{c_i^2 w_i^3 (f_i + 3 f_{i+1})}{2} + \frac{c_i w_i^4 (f_i + 4 f_{i+1})}{5} + \frac{w_i^5 (f_i + 5 f_{i+1})}{30} \right] $$
Variance is \(\mu_2\), skewness is \(\mu_3 / \mu_2^{3/2}\), and excess kurtosis is \(\mu_4 / \mu_2^2 - 3\).
# A distribution given by its density over a grid
at <- seq(-4, 4, by = 0.01)
dist <- dist_density(list(at), list(dnorm(at)))
dist
#> <distribution[1]>
#> [1] density[801]
mean(dist)
#> [1] -1.820392e-16
variance(dist)
#> [1] 0.9989458
skewness(dist)
#> [1] -2.336246e-15
kurtosis(dist)
#> [1] -0.01395399
density(dist, 0)
#> [1] 0.3989676
cdf(dist, 1.96)
#> [1] 0.9750312
quantile(dist, 0.975)
#> [1] 1.959466
# The density needn't be standardised, it is scaled to integrate to one
dist_density(list(c(0, 1)), list(c(2, 2)))
#> Warning: The density integrates to 2, standardising it to integrate to 1.
#> <distribution[1]>
#> [1] density[2]
# A kernel density estimate
kd <- density(rnorm(100))
dist_density(list(kd$x), list(kd$y))
#> <distribution[1]>
#> [1] density[512]